fix: variable naming

This commit is contained in:
xiaodong zuo 2022-08-04 06:11:07 +08:00
parent 2a19006196
commit 2f03304961
11 changed files with 430 additions and 430 deletions

View File

@ -3,29 +3,29 @@ import { Array as YArray, Map as YMap } from 'yjs';
import { RemoteKvService } from '@toeverything/datasource/remote-kv'; import { RemoteKvService } from '@toeverything/datasource/remote-kv';
export class YjsRemoteBinaries { export class YjsRemoteBinaries {
readonly #binaries: YMap<YArray<ArrayBuffer>>; // binary instance readonly _binaries: YMap<YArray<ArrayBuffer>>; // binary instance
readonly #remote_storage?: RemoteKvService; readonly _remoteStorage?: RemoteKvService;
constructor(binaries: YMap<YArray<ArrayBuffer>>, remote_token?: string) { constructor(binaries: YMap<YArray<ArrayBuffer>>, remote_token?: string) {
this.#binaries = binaries; this._binaries = binaries;
if (remote_token) { if (remote_token) {
this.#remote_storage = new RemoteKvService(remote_token); this._remoteStorage = new RemoteKvService(remote_token);
} else { } else {
console.warn(`Remote storage is not ready`); console.warn(`Remote storage is not ready`);
} }
} }
has(name: string): boolean { has(name: string): boolean {
return this.#binaries.has(name); return this._binaries.has(name);
} }
async get(name: string): Promise<YArray<ArrayBuffer> | undefined> { async get(name: string): Promise<YArray<ArrayBuffer> | undefined> {
if (this.#binaries.has(name)) { if (this._binaries.has(name)) {
return this.#binaries.get(name); return this._binaries.get(name);
} else { } else {
// TODO: Remote Load // TODO: Remote Load
try { try {
const file = await this.#remote_storage?.instance.getBuffData( const file = await this._remoteStorage?.instance.getBuffData(
name name
); );
console.log(file); console.log(file);
@ -38,16 +38,16 @@ export class YjsRemoteBinaries {
} }
async set(name: string, binary: YArray<ArrayBuffer>) { async set(name: string, binary: YArray<ArrayBuffer>) {
if (!this.#binaries.has(name)) { if (!this._binaries.has(name)) {
console.log(name, 'name'); console.log(name, 'name');
if (binary.length === 1) { if (binary.length === 1) {
this.#binaries.set(name, binary); this._binaries.set(name, binary);
if (this.#remote_storage) { if (this._remoteStorage) {
// TODO: Remote Save, if there is an object with the same name remotely, the upload is skipped, because the file name is the hash of the file content // TODO: Remote Save, if there is an object with the same name remotely, the upload is skipped, because the file name is the hash of the file content
const has_file = this.#remote_storage.instance.exist(name); const has_file = this._remoteStorage.instance.exist(name);
if (!has_file) { if (!has_file) {
const upload_file = new File(binary.toArray(), name); const upload_file = new File(binary.toArray(), name);
await this.#remote_storage.instance await this._remoteStorage.instance
.upload(upload_file) .upload(upload_file)
.catch(err => { .catch(err => {
throw new Error(`${err} upload error`); throw new Error(`${err} upload error`);

View File

@ -32,49 +32,49 @@ type YjsBlockInstanceProps = {
}; };
export class YjsBlockInstance implements BlockInstance<YjsContentOperation> { export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
readonly #id: string; readonly _id: string;
readonly #block: YMap<unknown>; readonly _block: YMap<unknown>;
readonly #binary?: YArray<ArrayBuffer>; readonly _binary?: YArray<ArrayBuffer>;
readonly #children: YArray<string>; readonly _children: YArray<string>;
readonly #set_block: ( readonly _setBlock: (
id: string, id: string,
block: BlockItem<YjsContentOperation> block: BlockItem<YjsContentOperation>
) => Promise<void>; ) => Promise<void>;
readonly #get_updated: (id: string) => number | undefined; readonly _getUpdated: (id: string) => number | undefined;
readonly #get_creator: (id: string) => string | undefined; readonly _getCreator: (id: string) => string | undefined;
readonly #get_block_instance: (id: string) => YjsBlockInstance | undefined; readonly _getBlockInstance: (id: string) => YjsBlockInstance | undefined;
readonly #children_listeners: Map<string, BlockListener>; readonly _childrenListeners: Map<string, BlockListener>;
readonly #content_listeners: Map<string, BlockListener>; readonly _contentListeners: Map<string, BlockListener>;
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
#children_map: Map<string, number>; _childrenMap: Map<string, number>;
constructor(props: YjsBlockInstanceProps) { constructor(props: YjsBlockInstanceProps) {
this.#id = props.id; this._id = props.id;
this.#block = props.block; this._block = props.block;
this.#binary = props.binary; this._binary = props.binary;
this.#children = props.block.get('children') as YArray<string>; this._children = props.block.get('children') as YArray<string>;
this.#children_map = getMapFromYArray(this.#children); this._childrenMap = getMapFromYArray(this._children);
this.#set_block = props.setBlock; this._setBlock = props.setBlock;
this.#get_updated = props.getUpdated; this._getUpdated = props.getUpdated;
this.#get_creator = props.getCreator; this._getCreator = props.getCreator;
this.#get_block_instance = props.getBlockInstance; this._getBlockInstance = props.getBlockInstance;
this.#children_listeners = new Map(); this._childrenListeners = new Map();
this.#content_listeners = new Map(); this._contentListeners = new Map();
const content = this.#block.get('content') as YMap<unknown>; const content = this._block.get('content') as YMap<unknown>;
this.#children.observe(event => this._children.observe(event =>
ChildrenListenerHandler(this.#children_listeners, event) ChildrenListenerHandler(this._childrenListeners, event)
); );
content?.observeDeep(events => content?.observeDeep(events =>
ContentListenerHandler(this.#content_listeners, events) ContentListenerHandler(this._contentListeners, events)
); );
// TODO: flavor needs optimization // TODO: flavor needs optimization
this.#block.observeDeep(events => this._block.observeDeep(events =>
ContentListenerHandler(this.#content_listeners, events) ContentListenerHandler(this._contentListeners, events)
); );
} }
@ -99,85 +99,85 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
} }
addChildrenListener(name: string, listener: BlockListener): void { addChildrenListener(name: string, listener: BlockListener): void {
this.#children_listeners.set(name, listener); this._childrenListeners.set(name, listener);
} }
removeChildrenListener(name: string): void { removeChildrenListener(name: string): void {
this.#children_listeners.delete(name); this._childrenListeners.delete(name);
} }
addContentListener(name: string, listener: BlockListener): void { addContentListener(name: string, listener: BlockListener): void {
this.#content_listeners.set(name, listener); this._contentListeners.set(name, listener);
} }
removeContentListener(name: string): void { removeContentListener(name: string): void {
this.#content_listeners.delete(name); this._contentListeners.delete(name);
} }
get id() { get id() {
return this.#id; return this._id;
} }
get content(): YjsContentOperation { get content(): YjsContentOperation {
if (this.type === BlockTypes.block) { if (this.type === BlockTypes.block) {
const content = this.#block.get('content'); const content = this._block.get('content');
if (content instanceof YAbstractType) { if (content instanceof YAbstractType) {
return new YjsContentOperation(content); return new YjsContentOperation(content);
} else { } else {
throw new Error(`Invalid content type: ${typeof content}`); throw new Error(`Invalid content type: ${typeof content}`);
} }
} else if (this.type === BlockTypes.binary && this.#binary) { } else if (this.type === BlockTypes.binary && this._binary) {
return new YjsContentOperation(this.#binary); return new YjsContentOperation(this._binary);
} }
throw new Error( throw new Error(
`Invalid content type: ${this.type}, ${this.#block.get( `Invalid content type: ${this.type}, ${this._block.get(
'content' 'content'
)}, ${this.#binary}` )}, ${this._binary}`
); );
} }
get type(): BlockItem<YjsContentOperation>['type'] { get type(): BlockItem<YjsContentOperation>['type'] {
return this.#block.get( return this._block.get(
'type' 'type'
) as BlockItem<YjsContentOperation>['type']; ) as BlockItem<YjsContentOperation>['type'];
} }
get flavor(): BlockItem<YjsContentOperation>['flavor'] { get flavor(): BlockItem<YjsContentOperation>['flavor'] {
return this.#block.get( return this._block.get(
'flavor' 'flavor'
) as BlockItem<YjsContentOperation>['flavor']; ) as BlockItem<YjsContentOperation>['flavor'];
} }
// TODO: bad case. Need to optimize. // TODO: bad case. Need to optimize.
setFlavor(flavor: BlockItem<YjsContentOperation>['flavor']) { setFlavor(flavor: BlockItem<YjsContentOperation>['flavor']) {
this.#block.set('flavor', flavor); this._block.set('flavor', flavor);
} }
get created(): BlockItem<YjsContentOperation>['created'] { get created(): BlockItem<YjsContentOperation>['created'] {
return this.#block.get( return this._block.get(
'created' 'created'
) as BlockItem<YjsContentOperation>['created']; ) as BlockItem<YjsContentOperation>['created'];
} }
get updated(): number { get updated(): number {
return this.#get_updated(this.#id) || this.created; return this._getUpdated(this._id) || this.created;
} }
get creator(): string | undefined { get creator(): string | undefined {
return this.#get_creator(this.#id); return this._getCreator(this._id);
} }
get children(): string[] { get children(): string[] {
return this.#children.toArray(); return this._children.toArray();
} }
getChildren(ids?: (string | undefined)[]): YjsBlockInstance[] { getChildren(ids?: (string | undefined)[]): YjsBlockInstance[] {
const query_ids = ids?.filter((id): id is string => !!id) || []; const query_ids = ids?.filter((id): id is string => !!id) || [];
const exists_ids = this.#children.map(id => id); const exists_ids = this._children.map(id => id);
const filter_ids = query_ids.length ? query_ids : exists_ids; const filter_ids = query_ids.length ? query_ids : exists_ids;
return exists_ids return exists_ids
.filter(id => filter_ids.includes(id)) .filter(id => filter_ids.includes(id))
.map(id => this.#get_block_instance(id)) .map(id => this._getBlockInstance(id))
.filter((v): v is YjsBlockInstance => !!v); .filter((v): v is YjsBlockInstance => !!v);
} }
@ -196,7 +196,7 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
return pos; return pos;
} }
} else if (before) { } else if (before) {
const current_pos = this.#children_map.get(before || ''); const current_pos = this._childrenMap.get(before || '');
if ( if (
typeof current_pos === 'number' && typeof current_pos === 'number' &&
Number.isInteger(current_pos) Number.isInteger(current_pos)
@ -207,7 +207,7 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
} }
} }
} else if (after) { } else if (after) {
const current_pos = this.#children_map.get(after || ''); const current_pos = this._childrenMap.get(after || '');
if ( if (
typeof current_pos === 'number' && typeof current_pos === 'number' &&
Number.isInteger(current_pos) Number.isInteger(current_pos)
@ -227,44 +227,44 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
): Promise<void> { ): Promise<void> {
const content = block[GET_BLOCK_ITEM](); const content = block[GET_BLOCK_ITEM]();
if (content) { if (content) {
const lastIndex = this.#children_map.get(block.id); const lastIndex = this._childrenMap.get(block.id);
if (typeof lastIndex === 'number') { if (typeof lastIndex === 'number') {
this.#children.delete(lastIndex); this._children.delete(lastIndex);
this.#children_map = getMapFromYArray(this.#children); this._childrenMap = getMapFromYArray(this._children);
} }
const position = this.position_calculator( const position = this.position_calculator(
this.#children_map.size, this._childrenMap.size,
pos pos
); );
if (typeof position === 'number') { if (typeof position === 'number') {
this.#children.insert(position, [block.id]); this._children.insert(position, [block.id]);
} else { } else {
this.#children.push([block.id]); this._children.push([block.id]);
} }
await this.#set_block(block.id, content); await this._setBlock(block.id, content);
this.#children_map = getMapFromYArray(this.#children); this._childrenMap = getMapFromYArray(this._children);
} }
} }
removeChildren(ids: (string | undefined)[]): Promise<string[]> { removeChildren(ids: (string | undefined)[]): Promise<string[]> {
return new Promise(resolve => { return new Promise(resolve => {
if (this.#children.doc) { if (this._children.doc) {
transact(this.#children.doc, () => { transact(this._children.doc, () => {
const failed = []; const failed = [];
for (const id of ids) { for (const id of ids) {
let idx = -1; let idx = -1;
for (const block_id of this.#children) { for (const block_id of this._children) {
idx += 1; idx += 1;
if (block_id === id) { if (block_id === id) {
this.#children.delete(idx); this._children.delete(idx);
break; break;
} }
} }
if (id) failed.push(id); if (id) failed.push(id);
} }
this.#children_map = getMapFromYArray(this.#children); this._childrenMap = getMapFromYArray(this._children);
resolve(failed); resolve(failed);
}); });
} else { } else {
@ -274,7 +274,7 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
} }
public scopedHistory(scope: any[]): HistoryManager { public scopedHistory(scope: any[]): HistoryManager {
return new YjsHistoryManager(this.#block, scope); return new YjsHistoryManager(this._block, scope);
} }
[GET_BLOCK_ITEM]() { [GET_BLOCK_ITEM]() {
@ -283,7 +283,7 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
return { return {
type: this.type, type: this.type,
flavor: this.flavor, flavor: this.flavor,
children: this.#children.slice(), children: this._children.slice(),
created: this.created, created: this.created,
content: this.content, content: this.content,
}; };

View File

@ -2,35 +2,35 @@ import { Map as YMap } from 'yjs';
export class GateKeeper { export class GateKeeper {
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
#user_id: string; _userId: string;
#creators: YMap<string>; _creators: YMap<string>;
#common: YMap<string>; _common: YMap<string>;
constructor(userId: string, creators: YMap<string>, common: YMap<string>) { constructor(userId: string, creators: YMap<string>, common: YMap<string>) {
this.#user_id = userId; this._userId = userId;
this.#creators = creators; this._creators = creators;
this.#common = common; this._common = common;
} }
getCreator(block_id: string): string | undefined { getCreator(block_id: string): string | undefined {
return this.#creators.get(block_id) || this.#common.get(block_id); return this._creators.get(block_id) || this._common.get(block_id);
} }
setCreator(block_id: string) { setCreator(block_id: string) {
if (!this.#creators.get(block_id)) { if (!this._creators.get(block_id)) {
this.#creators.set(block_id, this.#user_id); this._creators.set(block_id, this._userId);
} }
} }
setCommon(block_id: string) { setCommon(block_id: string) {
if (!this.#creators.get(block_id) && !this.#common.get(block_id)) { if (!this._creators.get(block_id) && !this._common.get(block_id)) {
this.#common.set(block_id, this.#user_id); this._common.set(block_id, this._userId);
} }
} }
private check_delete(block_id: string): boolean { private check_delete(block_id: string): boolean {
const creator = this.#creators.get(block_id); const creator = this._creators.get(block_id);
return creator === this.#user_id || !!this.#common.get(block_id); return creator === this._userId || !!this._common.get(block_id);
} }
checkDeleteLists(block_ids: string[]) { checkDeleteLists(block_ids: string[]) {
@ -47,7 +47,7 @@ export class GateKeeper {
} }
clear() { clear() {
this.#creators.clear(); this._creators.clear();
this.#common.clear(); this._common.clear();
} }
} }

View File

@ -5,34 +5,34 @@ import { HistoryCallback, HistoryManager } from '../../adapter';
type StackItem = UndoManager['undoStack'][0]; type StackItem = UndoManager['undoStack'][0];
export class YjsHistoryManager implements HistoryManager { export class YjsHistoryManager implements HistoryManager {
readonly #blocks: YMap<any>; readonly _blocks: YMap<any>;
readonly #history_manager: UndoManager; readonly _historyManager: UndoManager;
readonly #push_listeners: Map<string, HistoryCallback<any>>; readonly _pushListeners: Map<string, HistoryCallback<any>>;
readonly #pop_listeners: Map<string, HistoryCallback<any>>; readonly _popListeners: Map<string, HistoryCallback<any>>;
constructor(scope: YMap<any>, tracker?: any[]) { constructor(scope: YMap<any>, tracker?: any[]) {
this.#blocks = scope; this._blocks = scope;
this.#history_manager = new UndoManager(scope, { this._historyManager = new UndoManager(scope, {
trackedOrigins: tracker ? new Set(tracker) : undefined, trackedOrigins: tracker ? new Set(tracker) : undefined,
}); });
this.#push_listeners = new Map(); this._pushListeners = new Map();
this.#history_manager.on( this._historyManager.on(
'stack-item-added', 'stack-item-added',
(event: { stackItem: StackItem }) => { (event: { stackItem: StackItem }) => {
const meta = event.stackItem.meta; const meta = event.stackItem.meta;
for (const listener of this.#push_listeners.values()) { for (const listener of this._pushListeners.values()) {
listener(meta); listener(meta);
} }
} }
); );
this.#pop_listeners = new Map(); this._popListeners = new Map();
this.#history_manager.on( this._historyManager.on(
'stack-item-popped', 'stack-item-popped',
(event: { stackItem: StackItem }) => { (event: { stackItem: StackItem }) => {
const meta = event.stackItem.meta; const meta = event.stackItem.meta;
for (const listener of this.#pop_listeners.values()) { for (const listener of this._popListeners.values()) {
listener(new Map(meta)); listener(new Map(meta));
} }
} }
@ -40,19 +40,19 @@ export class YjsHistoryManager implements HistoryManager {
} }
onPush<T = unknown>(name: string, callback: HistoryCallback<T>): void { onPush<T = unknown>(name: string, callback: HistoryCallback<T>): void {
this.#push_listeners.set(name, callback); this._pushListeners.set(name, callback);
} }
offPush(name: string): boolean { offPush(name: string): boolean {
return this.#push_listeners.delete(name); return this._pushListeners.delete(name);
} }
onPop<T = unknown>(name: string, callback: HistoryCallback<T>): void { onPop<T = unknown>(name: string, callback: HistoryCallback<T>): void {
this.#pop_listeners.set(name, callback); this._popListeners.set(name, callback);
} }
offPop(name: string): boolean { offPop(name: string): boolean {
return this.#pop_listeners.delete(name); return this._popListeners.delete(name);
} }
break(): void { break(): void {
@ -60,14 +60,14 @@ export class YjsHistoryManager implements HistoryManager {
} }
undo<T = unknown>(): Map<string, T> | undefined { undo<T = unknown>(): Map<string, T> | undefined {
return this.#history_manager.undo()?.meta; return this._historyManager.undo()?.meta;
} }
redo<T = unknown>(): Map<string, T> | undefined { redo<T = unknown>(): Map<string, T> | undefined {
return this.#history_manager.redo()?.meta; return this._historyManager.redo()?.meta;
} }
clear(): void { clear(): void {
return this.#history_manager.clear(); return this._historyManager.clear();
} }
} }

View File

@ -178,22 +178,22 @@ export type YjsInitOptions = {
}; };
export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> { export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
readonly #provider: YjsProviders; readonly _provider: YjsProviders;
readonly #doc: Doc; // doc instance readonly _doc: Doc; // doc instance
readonly #awareness: Awareness; // lightweight state synchronization readonly _awareness: Awareness; // lightweight state synchronization
readonly #gatekeeper: GateKeeper; // Simple access control readonly _gatekeeper: GateKeeper; // Simple access control
readonly #history: YjsHistoryManager; readonly _history: YjsHistoryManager;
// Block Collection // Block Collection
// key is a randomly generated global id // key is a randomly generated global id
readonly #blocks: YMap<YMap<unknown>>; readonly _blocks: YMap<YMap<unknown>>;
readonly #block_updated: YMap<number>; readonly _blockUpdated: YMap<number>;
// Maximum cache Block 1024, ttl 10 minutes // Maximum cache Block 1024, ttl 10 minutes
readonly #block_caches: LRUCache<string, YjsBlockInstance>; readonly _blockCaches: LRUCache<string, YjsBlockInstance>;
readonly #binaries: YjsRemoteBinaries; readonly _binaries: YjsRemoteBinaries;
readonly #listener: Map<string, BlockListener<any>>; readonly _listener: Map<string, BlockListener<any>>;
static async init( static async init(
workspace: string, workspace: string,
@ -209,30 +209,30 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
} }
private constructor(providers: YjsProviders) { private constructor(providers: YjsProviders) {
this.#provider = providers; this._provider = providers;
this.#doc = providers.idb.doc; this._doc = providers.idb.doc;
this.#awareness = providers.awareness; this._awareness = providers.awareness;
this.#gatekeeper = providers.gatekeeper; this._gatekeeper = providers.gatekeeper;
const blocks = this.#doc.getMap<YMap<any>>('blocks'); const blocks = this._doc.getMap<YMap<any>>('blocks');
this.#blocks = this._blocks =
blocks.get('content') || blocks.set('content', new YMap()); blocks.get('content') || blocks.set('content', new YMap());
this.#block_updated = this._blockUpdated =
blocks.get('updated') || blocks.set('updated', new YMap()); blocks.get('updated') || blocks.set('updated', new YMap());
this.#block_caches = new LRUCache({ max: 1024, ttl: 1000 * 60 * 10 }); this._blockCaches = new LRUCache({ max: 1024, ttl: 1000 * 60 * 10 });
this.#binaries = new YjsRemoteBinaries( this._binaries = new YjsRemoteBinaries(
providers.binariesIdb.doc.getMap(), providers.binariesIdb.doc.getMap(),
providers.remoteToken providers.remoteToken
); );
this.#history = new YjsHistoryManager(this.#blocks); this._history = new YjsHistoryManager(this._blocks);
this.#listener = new Map(); this._listener = new Map();
const ws = providers.ws as any; const ws = providers.ws as any;
if (ws) { if (ws) {
const workspace = providers.idb.name; const workspace = providers.idb.name;
const emitState = (connectivity: Connectivity) => { const emitState = (connectivity: Connectivity) => {
this.#listener.get('connectivity')?.( this._listener.get('connectivity')?.(
new Map([[workspace, connectivity]]) new Map([[workspace, connectivity]])
); );
}; };
@ -244,9 +244,9 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
const debounced_editing_notifier = debounce( const debounced_editing_notifier = debounce(
() => { () => {
const listener: BlockListener<Set<string>> | undefined = const listener: BlockListener<Set<string>> | undefined =
this.#listener.get('editing'); this._listener.get('editing');
if (listener) { if (listener) {
const mapping = this.#awareness.getStates(); const mapping = this._awareness.getStates();
const editing_mapping: Record<string, string[]> = {}; const editing_mapping: Record<string, string[]> = {};
for (const { for (const {
userId, userId,
@ -280,11 +280,11 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
{ maxWait: 1000 } { maxWait: 1000 }
); );
this.#awareness.setLocalStateField('userId', providers.userId); this._awareness.setLocalStateField('userId', providers.userId);
this.#awareness.on('update', debounced_editing_notifier); this._awareness.on('update', debounced_editing_notifier);
this.#blocks.observeDeep(events => { this._blocks.observeDeep(events => {
const now = Date.now(); const now = Date.now();
const keys = events.flatMap(e => { const keys = events.flatMap(e => {
@ -300,14 +300,14 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
} }
}); });
EmitEvents(keys, this.#listener.get('updated')); EmitEvents(keys, this._listener.get('updated'));
transact(this.#doc, () => { transact(this._doc, () => {
for (const [key, action] of keys) { for (const [key, action] of keys) {
if (action === 'delete') { if (action === 'delete') {
this.#block_updated.delete(key); this._blockUpdated.delete(key);
} else { } else {
this.#block_updated.set(key, now); this._blockUpdated.set(key, now);
} }
} }
}); });
@ -315,7 +315,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
} }
getUserId(): string { getUserId(): string {
return this.#provider.userId; return this._provider.userId;
} }
inspector() { inspector() {
@ -333,7 +333,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
return { return {
save: () => { save: () => {
const binary = encodeStateAsUpdate(this.#doc); const binary = encodeStateAsUpdate(this._doc);
saveAs( saveAs(
new Blob([binary]), new Blob([binary]),
`affine_workspace_${new Date().toDateString()}.apk` `affine_workspace_${new Date().toDateString()}.apk`
@ -353,7 +353,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
}); });
const [file] = (await fromEvent(handles)) as File[]; const [file] = (await fromEvent(handles)) as File[];
const binary = await file.arrayBuffer(); const binary = await file.arrayBuffer();
await this.#provider.idb.clearData(); await this._provider.idb.clearData();
const doc = new Doc({ autoLoad: true, shouldLoad: true }); const doc = new Doc({ autoLoad: true, shouldLoad: true });
let updated = 0; let updated = 0;
let isUpdated = false; let isUpdated = false;
@ -374,21 +374,21 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
}; };
check(); check();
}); });
await new IndexeddbPersistence(this.#provider.idb.name, doc) await new IndexeddbPersistence(this._provider.idb.name, doc)
.whenSynced; .whenSynced;
applyUpdate(doc, new Uint8Array(binary)); applyUpdate(doc, new Uint8Array(binary));
await update_check; await update_check;
console.log('load success'); console.log('load success');
}, },
parse: () => this.#doc.toJSON(), parse: () => this._doc.toJSON(),
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
parse_page: (page_id: string) => { parse_page: (page_id: string) => {
const blocks = this.#blocks.toJSON(); const blocks = this._blocks.toJSON();
return resolve_block(blocks, page_id); return resolve_block(blocks, page_id);
}, },
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
parse_pages: (resolve = false) => { parse_pages: (resolve = false) => {
const blocks = this.#blocks.toJSON(); const blocks = this._blocks.toJSON();
return Object.fromEntries( return Object.fromEntries(
Object.entries(blocks) Object.entries(blocks)
.filter(([, block]) => block.flavor === 'page') .filter(([, block]) => block.flavor === 'page')
@ -402,21 +402,21 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
); );
}, },
clear: () => { clear: () => {
this.#blocks.clear(); this._blocks.clear();
this.#block_updated.clear(); this._blockUpdated.clear();
this.#gatekeeper.clear(); this._gatekeeper.clear();
this.#doc.getMap('blocks').clear(); this._doc.getMap('blocks').clear();
this.#doc.getMap('gatekeeper').clear(); this._doc.getMap('gatekeeper').clear();
}, },
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
clear_old: () => { clear_old: () => {
this.#doc.getMap('block_updated').clear(); this._doc.getMap('block_updated').clear();
this.#doc.getMap('blocks').clear(); this._doc.getMap('blocks').clear();
this.#doc.getMap('common').clear(); this._doc.getMap('common').clear();
this.#doc.getMap('creators').clear(); this._doc.getMap('creators').clear();
}, },
snapshot: () => { snapshot: () => {
return snapshot(this.#doc); return snapshot(this._doc);
}, },
}; };
} }
@ -459,15 +459,15 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
} }
private get_updated(id: string) { private get_updated(id: string) {
return this.#block_updated.get(id); return this._blockUpdated.get(id);
} }
private get_creator(id: string) { private get_creator(id: string) {
return this.#gatekeeper.getCreator(id); return this._gatekeeper.getCreator(id);
} }
private get_block_sync(id: string): YjsBlockInstance | undefined { private get_block_sync(id: string): YjsBlockInstance | undefined {
const cached = this.#block_caches.get(id); const cached = this._blockCaches.get(id);
if (cached) { if (cached) {
// Synchronous read cannot read binary // Synchronous read cannot read binary
if (cached.type === BlockTypes.block) { if (cached.type === BlockTypes.block) {
@ -476,7 +476,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
return undefined; return undefined;
} }
const block = this.#blocks.get(id); const block = this._blocks.get(id);
// Synchronous read cannot read binary // Synchronous read cannot read binary
if (block && block.get('type') === BlockTypes.block) { if (block && block.get('type') === BlockTypes.block) {
@ -496,9 +496,9 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
async getBlock(id: string): Promise<YjsBlockInstance | undefined> { async getBlock(id: string): Promise<YjsBlockInstance | undefined> {
const block_instance = this.get_block_sync(id); const block_instance = this.get_block_sync(id);
if (block_instance) return block_instance; if (block_instance) return block_instance;
const block = this.#blocks.get(id); const block = this._blocks.get(id);
if (block && block.get('type') === BlockTypes.binary) { if (block && block.get('type') === BlockTypes.binary) {
const binary = await this.#binaries.get( const binary = await this._binaries.get(
block.get('hash') as string block.get('hash') as string
); );
if (binary) { if (binary) {
@ -520,7 +520,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
flavor: BlockItem<YjsContentOperation>['flavor'] flavor: BlockItem<YjsContentOperation>['flavor']
): Promise<string[]> { ): Promise<string[]> {
const keys: string[] = []; const keys: string[] = [];
this.#blocks.forEach((doc, key) => { this._blocks.forEach((doc, key) => {
if (doc.get('flavor') === flavor) { if (doc.get('flavor') === flavor) {
keys.push(key); keys.push(key);
} }
@ -533,7 +533,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
type: BlockItem<YjsContentOperation>['type'] type: BlockItem<YjsContentOperation>['type']
): Promise<string[]> { ): Promise<string[]> {
const keys: string[] = []; const keys: string[] = [];
this.#blocks.forEach((doc, key) => { this._blocks.forEach((doc, key) => {
if (doc.get('type') === type) { if (doc.get('type') === type) {
keys.push(key); keys.push(key);
} }
@ -547,8 +547,8 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
item: BlockItem<YjsContentOperation> & { hash?: string } item: BlockItem<YjsContentOperation> & { hash?: string }
): Promise<void> { ): Promise<void> {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
const block = this.#blocks.get(key) || new YMap(); const block = this._blocks.get(key) || new YMap();
transact(this.#doc, () => { transact(this._doc, () => {
// Insert only if the block doesn't exist yet // Insert only if the block doesn't exist yet
// Other modification operations are done in the block instance // Other modification operations are done in the block instance
let uploaded: Promise<void> | undefined; let uploaded: Promise<void> | undefined;
@ -568,8 +568,8 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
} else if (item.type === BlockTypes.binary && item.hash) { } else if (item.type === BlockTypes.binary && item.hash) {
if (content instanceof YArray) { if (content instanceof YArray) {
block.set('hash', item.hash); block.set('hash', item.hash);
if (!this.#binaries.has(item.hash)) { if (!this._binaries.has(item.hash)) {
uploaded = this.#binaries.set( uploaded = this._binaries.set(
item.hash, item.hash,
content content
); );
@ -583,18 +583,18 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
throw new Error('invalid block type: ' + item.type); throw new Error('invalid block type: ' + item.type);
} }
this.#blocks.set(key, block); this._blocks.set(key, block);
} }
if (item.flavor === 'page') { if (item.flavor === 'page') {
this.#awareness.setLocalStateField('editing', key); this._awareness.setLocalStateField('editing', key);
this.#awareness.setLocalStateField('updated', Date.now()); this._awareness.setLocalStateField('updated', Date.now());
} }
// References do not add delete restrictions // References do not add delete restrictions
if (item.flavor === 'reference') { if (item.flavor === 'reference') {
this.#gatekeeper.setCommon(key); this._gatekeeper.setCommon(key);
} else { } else {
this.#gatekeeper.setCreator(key); this._gatekeeper.setCreator(key);
} }
if (uploaded) { if (uploaded) {
@ -613,15 +613,15 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
async checkBlocks(keys: string[]): Promise<boolean> { async checkBlocks(keys: string[]): Promise<boolean> {
return ( return (
keys.filter(key => !!this.#blocks.get(key)).length === keys.length keys.filter(key => !!this._blocks.get(key)).length === keys.length
); );
} }
async deleteBlocks(keys: string[]): Promise<string[]> { async deleteBlocks(keys: string[]): Promise<string[]> {
const [success, fail] = this.#gatekeeper.checkDeleteLists(keys); const [success, fail] = this._gatekeeper.checkDeleteLists(keys);
transact(this.#doc, () => { transact(this._doc, () => {
for (const key of success) { for (const key of success) {
this.#blocks.delete(key); this._blocks.delete(key);
} }
}); });
return fail; return fail;
@ -631,7 +631,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
key: 'editing' | 'updated' | 'connectivity', key: 'editing' | 'updated' | 'connectivity',
listener: BlockListener<S, R> listener: BlockListener<S, R>
): void { ): void {
this.#listener.set(key, listener); this._listener.set(key, listener);
} }
suspend(suspend: boolean) { suspend(suspend: boolean) {
@ -639,6 +639,6 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
} }
public history(): HistoryManager { public history(): HistoryManager {
return this.#history; return this._history;
} }
} }

View File

@ -52,18 +52,18 @@ function auto_set(root: ContentOperation, key: string, data: BaseTypes): void {
} }
export class YjsContentOperation implements ContentOperation { export class YjsContentOperation implements ContentOperation {
readonly #content: YAbstractType<unknown>; readonly _content: YAbstractType<unknown>;
constructor(content: YAbstractType<any>) { constructor(content: YAbstractType<any>) {
this.#content = content; this._content = content;
} }
get length(): number { get length(): number {
if (this.#content instanceof YMap) { if (this._content instanceof YMap) {
return this.#content.size; return this._content.size;
} }
if (this.#content instanceof YArray || this.#content instanceof YText) { if (this._content instanceof YArray || this._content instanceof YText) {
return this.#content.length; return this._content.length;
} }
return 0; return 0;
} }
@ -83,8 +83,8 @@ export class YjsContentOperation implements ContentOperation {
} }
asText(): YjsTextOperation | undefined { asText(): YjsTextOperation | undefined {
if (this.#content instanceof YText) { if (this._content instanceof YText) {
return new YjsTextOperation(this.#content); return new YjsTextOperation(this._content);
} }
return undefined; return undefined;
} }
@ -92,8 +92,8 @@ export class YjsContentOperation implements ContentOperation {
asArray<T extends ContentTypes = ContentOperation>(): asArray<T extends ContentTypes = ContentOperation>():
| YjsArrayOperation<T> | YjsArrayOperation<T>
| undefined { | undefined {
if (this.#content instanceof YArray) { if (this._content instanceof YArray) {
return new YjsArrayOperation(this.#content); return new YjsArrayOperation(this._content);
} }
return undefined; return undefined;
} }
@ -101,8 +101,8 @@ export class YjsContentOperation implements ContentOperation {
asMap<T extends ContentTypes = ContentOperation>(): asMap<T extends ContentTypes = ContentOperation>():
| YjsMapOperation<T> | YjsMapOperation<T>
| undefined { | undefined {
if (this.#content instanceof YMap) { if (this._content instanceof YMap) {
return new YjsMapOperation(this.#content); return new YjsMapOperation(this._content);
} }
return undefined; return undefined;
} }
@ -184,24 +184,24 @@ export class YjsContentOperation implements ContentOperation {
} }
[INTO_INNER](): YAbstractType<unknown> | undefined { [INTO_INNER](): YAbstractType<unknown> | undefined {
if (this.#content instanceof YAbstractType) { if (this._content instanceof YAbstractType) {
return this.#content; return this._content;
} }
return undefined; return undefined;
} }
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
private toJSON() { private toJSON() {
return this.#content.toJSON(); return this._content.toJSON();
} }
} }
class YjsTextOperation extends YjsContentOperation implements TextOperation { class YjsTextOperation extends YjsContentOperation implements TextOperation {
readonly #content: YText; readonly _textContent: YText;
constructor(content: YText) { constructor(content: YText) {
super(content); super(content);
this.#content = content; this._textContent = content;
} }
insert( insert(
@ -209,7 +209,7 @@ class YjsTextOperation extends YjsContentOperation implements TextOperation {
content: string, content: string,
format?: Record<string, string> format?: Record<string, string>
): void { ): void {
this.#content.insert(index, content, format); this._textContent.insert(index, content, format);
} }
format( format(
@ -217,23 +217,23 @@ class YjsTextOperation extends YjsContentOperation implements TextOperation {
length: number, length: number,
format: Record<string, string> format: Record<string, string>
): void { ): void {
this.#content.format(index, length, format); this._textContent.format(index, length, format);
} }
delete(index: number, length: number): void { delete(index: number, length: number): void {
this.#content.delete(index, length); this._textContent.delete(index, length);
} }
setAttribute(name: string, value: BaseTypes) { setAttribute(name: string, value: BaseTypes) {
this.#content.setAttribute(name, value); this._textContent.setAttribute(name, value);
} }
getAttribute<T extends BaseTypes = string>(name: string): T | undefined { getAttribute<T extends BaseTypes = string>(name: string): T | undefined {
return this.#content.getAttribute(name); return this._textContent.getAttribute(name);
} }
override toString(): TextToken[] { override toString(): TextToken[] {
return this.#content.toDelta(); return this._textContent.toDelta();
} }
} }
@ -241,67 +241,69 @@ class YjsArrayOperation<T extends ContentTypes>
extends YjsContentOperation extends YjsContentOperation
implements ArrayOperation<T> implements ArrayOperation<T>
{ {
readonly #content: YArray<T>; readonly _arrayContent: YArray<T>;
readonly #listeners: Map<string, BlockListener>; readonly _listeners: Map<string, BlockListener>;
constructor(content: YArray<T>) { constructor(content: YArray<T>) {
super(content); super(content);
this.#content = content; this._arrayContent = content;
this.#listeners = new Map(); this._listeners = new Map();
this.#content.observe(event => this._arrayContent.observe(event =>
ChildrenListenerHandler(this.#listeners, event) ChildrenListenerHandler(this._listeners, event)
); );
} }
on(name: string, listener: BlockListener) { on(name: string, listener: BlockListener) {
this.#listeners.set(name, listener); this._listeners.set(name, listener);
} }
off(name: string) { off(name: string) {
this.#listeners.delete(name); this._listeners.delete(name);
} }
insert(index: number, content: Array<Operable<T>>): void { insert(index: number, content: Array<Operable<T>>): void {
this.#content.insert( this._arrayContent.insert(
index, index,
content.map(v => this.into_inner(v)) content.map(v => this.into_inner(v))
); );
} }
delete(index: number, length: number): void { delete(index: number, length: number): void {
this.#content.delete(index, length); this._arrayContent.delete(index, length);
} }
push(content: Array<Operable<T>>): void { push(content: Array<Operable<T>>): void {
this.#content.push(content.map(v => this.into_inner(v))); this._arrayContent.push(content.map(v => this.into_inner(v)));
} }
unshift(content: Array<Operable<T>>): void { unshift(content: Array<Operable<T>>): void {
this.#content.unshift(content.map(v => this.into_inner(v))); this._arrayContent.unshift(content.map(v => this.into_inner(v)));
} }
get(index: number): Operable<T> | undefined { get(index: number): Operable<T> | undefined {
const content = this.#content.get(index); const content = this._arrayContent.get(index);
if (content) return this.to_operable(content); if (content) return this.to_operable(content);
return undefined; return undefined;
} }
private get_internal(index: number): T { private get_internal(index: number): T {
return this.#content.get(index); return this._arrayContent.get(index);
} }
slice(start?: number, end?: number): Operable<T>[] { slice(start?: number, end?: number): Operable<T>[] {
return this.#content.slice(start, end).map(v => this.to_operable(v)); return this._arrayContent
.slice(start, end)
.map(v => this.to_operable(v));
} }
map<R = unknown>(callback: (value: T, index: number) => R): R[] { map<R = unknown>(callback: (value: T, index: number) => R): R[] {
return this.#content.map((value, index) => callback(value, index)); return this._arrayContent.map((value, index) => callback(value, index));
} }
// Traverse, if callback returns false, stop traversing // Traverse, if callback returns false, stop traversing
forEach(callback: (value: T, index: number) => boolean) { forEach(callback: (value: T, index: number) => boolean) {
for (let i = 0; i < this.#content.length; i++) { for (let i = 0; i < this._arrayContent.length; i++) {
const ret = callback(this.get_internal(i), i); const ret = callback(this.get_internal(i), i);
if (ret === false) { if (ret === false) {
break; break;
@ -342,47 +344,47 @@ class YjsMapOperation<T extends ContentTypes>
extends YjsContentOperation extends YjsContentOperation
implements MapOperation<T> implements MapOperation<T>
{ {
readonly #content: YMap<T>; readonly _mapContent: YMap<T>;
readonly #listeners: Map<string, BlockListener>; readonly _listeners: Map<string, BlockListener>;
constructor(content: YMap<T>) { constructor(content: YMap<T>) {
super(content); super(content);
this.#content = content; this._mapContent = content;
this.#listeners = new Map(); this._listeners = new Map();
content?.observeDeep(events => content?.observeDeep(events =>
ContentListenerHandler(this.#listeners, events) ContentListenerHandler(this._listeners, events)
); );
} }
on(name: string, listener: BlockListener) { on(name: string, listener: BlockListener) {
this.#listeners.set(name, listener); this._listeners.set(name, listener);
} }
off(name: string) { off(name: string) {
this.#listeners.delete(name); this._listeners.delete(name);
} }
set(key: string, value: Operable<T>): void { set(key: string, value: Operable<T>): void {
if (value instanceof YjsContentOperation) { if (value instanceof YjsContentOperation) {
const content = value[INTO_INNER](); const content = value[INTO_INNER]();
if (content) this.#content.set(key, content as unknown as T); if (content) this._mapContent.set(key, content as unknown as T);
} else { } else {
this.#content.set(key, value as T); this._mapContent.set(key, value as T);
} }
} }
get(key: string): Operable<T> | undefined { get(key: string): Operable<T> | undefined {
const content = this.#content.get(key); const content = this._mapContent.get(key);
if (content) return this.to_operable(content); if (content) return this.to_operable(content);
return undefined; return undefined;
} }
delete(key: string): void { delete(key: string): void {
this.#content.delete(key); this._mapContent.delete(key);
} }
has(key: string): boolean { has(key: string): boolean {
return this.#content.has(key); return this._mapContent.has(key);
} }
} }

View File

@ -26,47 +26,47 @@ export class AbstractBlock<
B extends BlockInstance<C>, B extends BlockInstance<C>,
C extends ContentOperation C extends ContentOperation
> { > {
readonly #id: string; readonly _id: string;
readonly #block: BlockInstance<C>; readonly #block: BlockInstance<C>;
readonly #history: HistoryManager; readonly _history: HistoryManager;
readonly #root?: AbstractBlock<B, C>; readonly _root?: AbstractBlock<B, C>;
readonly #parent_listener: Map<string, BlockListener>; readonly _parentListener: Map<string, BlockListener>;
#parent?: AbstractBlock<B, C>; _parent?: AbstractBlock<B, C>;
constructor( constructor(
block: B, block: B,
root?: AbstractBlock<B, C>, root?: AbstractBlock<B, C>,
parent?: AbstractBlock<B, C> parent?: AbstractBlock<B, C>
) { ) {
this.#id = block.id; this._id = block.id;
this.#block = block; this.#block = block;
this.#history = this.#block.scopedHistory([this.#id]); this._history = this.#block.scopedHistory([this._id]);
this.#root = root; this._root = root;
this.#parent_listener = new Map(); this._parentListener = new Map();
this.#parent = parent; this._parent = parent;
JWT_DEV && logger_debug(`init: exists ${this.#id}`); JWT_DEV && logger_debug(`init: exists ${this._id}`);
} }
public get root() { public get root() {
return this.#root; return this._root;
} }
protected get parent_node() { protected get parent_node() {
return this.#parent; return this._parent;
} }
protected _getParentPage(warning = true): string | undefined { protected _getParentPage(warning = true): string | undefined {
if (this.flavor === 'page') { if (this.flavor === 'page') {
return this.#block.id; return this.#block.id;
} else if (!this.#parent) { } else if (!this._parent) {
if (warning && this.flavor !== 'workspace') { if (warning && this.flavor !== 'workspace') {
console.warn('parent not found'); console.warn('parent not found');
} }
return undefined; return undefined;
} else { } else {
return this.#parent.parent_page; return this._parent.parent_page;
} }
} }
@ -80,7 +80,7 @@ export class AbstractBlock<
callback: BlockListener callback: BlockListener
) { ) {
if (event === 'parent') { if (event === 'parent') {
this.#parent_listener.set(name, callback); this._parentListener.set(name, callback);
} else { } else {
this.#block.on(event, name, callback); this.#block.on(event, name, callback);
} }
@ -88,7 +88,7 @@ export class AbstractBlock<
public off(event: 'content' | 'children' | 'parent', name: string) { public off(event: 'content' | 'children' | 'parent', name: string) {
if (event === 'parent') { if (event === 'parent') {
this.#parent_listener.delete(name); this._parentListener.delete(name);
} else { } else {
this.#block.off(event, name); this.#block.off(event, name);
} }
@ -117,7 +117,7 @@ export class AbstractBlock<
return this.#block.content.asMap() as MapOperation<T>; return this.#block.content.asMap() as MapOperation<T>;
} }
throw new Error( throw new Error(
`this block not a structured block: ${this.#id}, ${ `this block not a structured block: ${this._id}, ${
this.#block.type this.#block.type
}` }`
); );
@ -181,9 +181,9 @@ export class AbstractBlock<
} }
[SET_PARENT](parent: AbstractBlock<B, C>) { [SET_PARENT](parent: AbstractBlock<B, C>) {
this.#parent = parent; this._parent = parent;
const states: Map<string, 'update'> = new Map([[parent.id, 'update']]); const states: Map<string, 'update'> = new Map([[parent.id, 'update']]);
for (const listener of this.#parent_listener.values()) { for (const listener of this._parentListener.values()) {
listener(states); listener(states);
} }
} }
@ -196,7 +196,7 @@ export class AbstractBlock<
const updated = this.last_updated_date; const updated = this.last_updated_date;
return [ return [
`id:${this.#id}`, `id:${this._id}`,
`type:${this.type}`, `type:${this.type}`,
`type:${this.flavor}`, `type:${this.flavor}`,
this.flavor === BlockFlavors.page && `type:doc`, // normal documentation this.flavor === BlockFlavors.page && `type:doc`, // normal documentation
@ -211,7 +211,7 @@ export class AbstractBlock<
* current document instance id * current document instance id
*/ */
public get id(): string { public get id(): string {
return this.#id; return this._id;
} }
/** /**
@ -249,7 +249,7 @@ export class AbstractBlock<
) { ) {
JWT_DEV && logger(`insertChildren: start`); JWT_DEV && logger(`insertChildren: start`);
if (block.id === this.#id) return; // avoid self-reference if (block.id === this._id) return; // avoid self-reference
if ( if (
this.type !== BlockTypes.block || // binary cannot insert subblocks this.type !== BlockTypes.block || // binary cannot insert subblocks
(block.type !== BlockTypes.block && (block.type !== BlockTypes.block &&
@ -367,6 +367,6 @@ export class AbstractBlock<
* TODO: scoped history * TODO: scoped history
*/ */
public get history(): HistoryManager { public get history(): HistoryManager {
return this.#history; return this._history;
} }
} }

View File

@ -51,24 +51,24 @@ export class BaseBlock<
B extends BlockInstance<C>, B extends BlockInstance<C>,
C extends ContentOperation C extends ContentOperation
> extends AbstractBlock<B, C> { > extends AbstractBlock<B, C> {
readonly #exporters?: Exporters; readonly _exporters?: Exporters;
readonly #content_exporters_getter: () => Map< readonly _contentExportersGetter: () => Map<
string, string,
ReadableContentExporter<string, any> ReadableContentExporter<string, any>
>; >;
readonly #metadata_exporters_getter: () => Map< readonly _metadataExportersGetter: () => Map<
string, string,
ReadableContentExporter< ReadableContentExporter<
Array<[string, number | string | string[]]>, Array<[string, number | string | string[]]>,
any any
> >
>; >;
readonly #tag_exporters_getter: () => Map< readonly _tagExportersGetter: () => Map<
string, string,
ReadableContentExporter<string[], any> ReadableContentExporter<string[], any>
>; >;
#validators: Map<string, Validator> = new Map(); validators: Map<string, Validator> = new Map();
constructor( constructor(
block: B, block: B,
@ -78,12 +78,11 @@ export class BaseBlock<
) { ) {
super(block, root, parent); super(block, root, parent);
this.#exporters = exporters; this._exporters = exporters;
this.#content_exporters_getter = () => this._contentExportersGetter = () => new Map(exporters?.content(block));
new Map(exporters?.content(block)); this._metadataExportersGetter = () =>
this.#metadata_exporters_getter = () =>
new Map(exporters?.metadata(block)); new Map(exporters?.metadata(block));
this.#tag_exporters_getter = () => new Map(exporters?.tag(block)); this._tagExportersGetter = () => new Map(exporters?.tag(block));
} }
get parent() { get parent() {
@ -158,14 +157,14 @@ export class BaseBlock<
setValidator(key: string, validator?: Validator) { setValidator(key: string, validator?: Validator) {
if (validator) { if (validator) {
this.#validators.set(key, validator); this.validators.set(key, validator);
} else { } else {
this.#validators.delete(key); this.validators.delete(key);
} }
} }
private validate(key: string, value: unknown): boolean { private validate(key: string, value: unknown): boolean {
const validate = this.#validators.get(key); const validate = this.validators.get(key);
if (validate) { if (validate) {
return validate(value) === false ? false : true; return validate(value) === false ? false : true;
} }
@ -185,13 +184,13 @@ export class BaseBlock<
*/ */
private get_children_instance(blockId?: string): BaseBlock<B, C>[] { private get_children_instance(blockId?: string): BaseBlock<B, C>[] {
return this.get_children(blockId).map( return this.get_children(blockId).map(
block => new BaseBlock(block, this.root, this, this.#exporters) block => new BaseBlock(block, this.root, this, this._exporters)
); );
} }
private get_indexable_metadata() { private get_indexable_metadata() {
const metadata: Record<string, number | string | string[]> = {}; const metadata: Record<string, number | string | string[]> = {};
for (const [name, exporter] of this.#metadata_exporters_getter()) { for (const [name, exporter] of this._metadataExportersGetter()) {
try { try {
for (const [key, val] of exporter(this.getContent())) { for (const [key, val] of exporter(this.getContent())) {
metadata[key] = val; metadata[key] = val;
@ -226,7 +225,7 @@ export class BaseBlock<
private get_indexable_content(): string | undefined { private get_indexable_content(): string | undefined {
const contents = []; const contents = [];
for (const [name, exporter] of this.#content_exporters_getter()) { for (const [name, exporter] of this._contentExportersGetter()) {
try { try {
const content = exporter(this.getContent()); const content = exporter(this.getContent());
if (content) contents.push(content); if (content) contents.push(content);
@ -246,7 +245,7 @@ export class BaseBlock<
private get_indexable_tags(): string[] { private get_indexable_tags(): string[] {
const tags: string[] = []; const tags: string[] = [];
for (const [name, exporter] of this.#tag_exporters_getter()) { for (const [name, exporter] of this._tagExportersGetter()) {
try { try {
tags.push(...exporter(this.getContent())); tags.push(...exporter(this.getContent()));
} catch (err) { } catch (err) {

View File

@ -94,18 +94,18 @@ export class BlockIndexer<
B extends BlockInstance<C>, B extends BlockInstance<C>,
C extends ContentOperation C extends ContentOperation
> { > {
readonly #adapter: A; readonly _adapter: A;
readonly #idb: BlockIdbInstance; readonly _idb: BlockIdbInstance;
readonly #block_indexer: DocumentIndexer<IndexMetadata>; readonly _blockIndexer: DocumentIndexer<IndexMetadata>;
readonly #block_metadata: LRUCache<string, QueryMetadata>; readonly _blockMetadata: LRUCache<string, QueryMetadata>;
readonly #event_bus: BlockEventBus; readonly _eventBus: BlockEventBus;
readonly #block_builder: ( readonly _blockBuilder: (
block: BlockInstance<C> block: BlockInstance<C>
) => Promise<BaseBlock<B, C>>; ) => Promise<BaseBlock<B, C>>;
readonly #delay_index: { documents: Map<string, BaseBlock<B, C>> }; readonly _delayIndex: { documents: Map<string, BaseBlock<B, C>> };
constructor( constructor(
adapter: A, adapter: A,
@ -113,10 +113,10 @@ export class BlockIndexer<
block_builder: (block: BlockInstance<C>) => Promise<BaseBlock<B, C>>, block_builder: (block: BlockInstance<C>) => Promise<BaseBlock<B, C>>,
event_bus: BlockEventBus event_bus: BlockEventBus
) { ) {
this.#adapter = adapter; this._adapter = adapter;
this.#idb = initIndexIdb(workspace); this._idb = initIndexIdb(workspace);
this.#block_indexer = new DocumentIndexer({ this._blockIndexer = new DocumentIndexer({
document: { document: {
id: 'id', id: 'id',
index: ['content', 'reference'], index: ['content', 'reference'],
@ -126,23 +126,23 @@ export class BlockIndexer<
tokenize: 'forward', tokenize: 'forward',
context: true, context: true,
}); });
this.#block_metadata = new LRUCache({ this._blockMetadata = new LRUCache({
max: 10240, max: 10240,
ttl: 1000 * 60 * 30, ttl: 1000 * 60 * 30,
}); });
this.#block_builder = block_builder; this._blockBuilder = block_builder;
this.#event_bus = event_bus; this._eventBus = event_bus;
this.#delay_index = { documents: new Map() }; this._delayIndex = { documents: new Map() };
this.#event_bus this._eventBus
.topic('reindex') .topic('reindex')
.on('reindex', this.content_reindex.bind(this), { .on('reindex', this.content_reindex.bind(this), {
debounce: { wait: 1000, maxWait: 1000 * 10 }, debounce: { wait: 1000, maxWait: 1000 * 10 },
}); });
this.#event_bus this._eventBus
.topic('save_index') .topic('save_index')
.on('save_index', this.save_index.bind(this), { .on('save_index', this.save_index.bind(this), {
debounce: { wait: 1000 * 10, maxWait: 1000 * 20 }, debounce: { wait: 1000 * 10, maxWait: 1000 * 20 },
@ -152,8 +152,8 @@ export class BlockIndexer<
private async content_reindex() { private async content_reindex() {
const paddings: Record<string, BlockIndexedContent> = {}; const paddings: Record<string, BlockIndexedContent> = {};
this.#delay_index.documents = produce( this._delayIndex.documents = produce(
this.#delay_index.documents, this._delayIndex.documents,
draft => { draft => {
for (const [k, block] of draft) { for (const [k, block] of draft) {
paddings[k] = { paddings[k] = {
@ -166,11 +166,11 @@ export class BlockIndexer<
); );
for (const [key, { index, query }] of Object.entries(paddings)) { for (const [key, { index, query }] of Object.entries(paddings)) {
if (index.content) { if (index.content) {
await this.#block_indexer.addAsync(key, index); await this._blockIndexer.addAsync(key, index);
this.#block_metadata.set(key, query); this._blockMetadata.set(key, query);
} }
} }
this.#event_bus.topic('save_index').emit(); this._eventBus.topic('save_index').emit();
} }
private async refresh_index(block: BaseBlock<B, C>) { private async refresh_index(block: BaseBlock<B, C>) {
@ -185,14 +185,14 @@ export class BlockIndexer<
BlockFlavors.reference, BlockFlavors.reference,
]; ];
if (filter.includes(block.flavor)) { if (filter.includes(block.flavor)) {
this.#delay_index.documents = produce( this._delayIndex.documents = produce(
this.#delay_index.documents, this._delayIndex.documents,
draft => { draft => {
draft.set(block.id, block); draft.set(block.id, block);
} }
); );
this.#event_bus.topic('reindex').emit(); this._eventBus.topic('reindex').emit();
return true; return true;
} }
logger_debug(`skip index ${block.flavor}: ${block.id}`); logger_debug(`skip index ${block.flavor}: ${block.id}`);
@ -202,19 +202,19 @@ export class BlockIndexer<
async refreshIndex(id: string, state: ChangedState) { async refreshIndex(id: string, state: ChangedState) {
JWT_DEV && logger(`refreshArticleIndex: ${id}`); JWT_DEV && logger(`refreshArticleIndex: ${id}`);
if (state === 'delete') { if (state === 'delete') {
this.#delay_index.documents = produce( this._delayIndex.documents = produce(
this.#delay_index.documents, this._delayIndex.documents,
draft => { draft => {
this.#block_indexer.remove(id); this._blockIndexer.remove(id);
this.#block_metadata.delete(id); this._blockMetadata.delete(id);
draft.delete(id); draft.delete(id);
} }
); );
return; return;
} }
const block = await this.#adapter.getBlock(id); const block = await this._adapter.getBlock(id);
if (block?.id === id) { if (block?.id === id) {
if (await this.refresh_index(await this.#block_builder(block))) { if (await this.refresh_index(await this._blockBuilder(block))) {
JWT_DEV && JWT_DEV &&
logger( logger(
state state
@ -230,43 +230,43 @@ export class BlockIndexer<
} }
async loadIndex() { async loadIndex() {
for (const key of await this.#idb.index.keys()) { for (const key of await this._idb.index.keys()) {
const content = await this.#idb.index.get(key); const content = await this._idb.index.get(key);
if (content) { if (content) {
const decoded = strFromU8(inflateSync(new Uint8Array(content))); const decoded = strFromU8(inflateSync(new Uint8Array(content)));
try { try {
await this.#block_indexer.import(key, decoded as any); await this._blockIndexer.import(key, decoded as any);
} catch (e) { } catch (e) {
console.error(`Failed to load index ${key}`, e); console.error(`Failed to load index ${key}`, e);
} }
} }
} }
for (const key of await this.#idb.metadata.keys()) { for (const key of await this._idb.metadata.keys()) {
const content = await this.#idb.metadata.get(key); const content = await this._idb.metadata.get(key);
if (content) { if (content) {
const decoded = strFromU8(inflateSync(new Uint8Array(content))); const decoded = strFromU8(inflateSync(new Uint8Array(content)));
try { try {
await this.#block_indexer.import(key, JSON.parse(decoded)); await this._blockIndexer.import(key, JSON.parse(decoded));
} catch (e) { } catch (e) {
console.error(`Failed to load index ${key}`, e); console.error(`Failed to load index ${key}`, e);
} }
} }
} }
return Array.from(this.#block_metadata.keys()); return Array.from(this._blockMetadata.keys());
} }
private async save_index() { private async save_index() {
const idb = this.#idb; const idb = this._idb;
await idb.index await idb.index
.keys() .keys()
.then(keys => Promise.all(keys.map(key => idb.index.delete(key)))); .then(keys => Promise.all(keys.map(key => idb.index.delete(key))));
await this.#block_indexer.export((key, data) => { await this._blockIndexer.export((key, data) => {
return idb.index.set( return idb.index.set(
String(key), String(key),
deflateSync(strToU8(data as any)) deflateSync(strToU8(data as any))
); );
}); });
const metadata = this.#block_metadata; const metadata = this._blockMetadata;
await idb.metadata await idb.metadata
.keys() .keys()
.then(keys => .then(keys =>
@ -286,7 +286,7 @@ export class BlockIndexer<
public async inspectIndex() { public async inspectIndex() {
const index: Record<string | number, any> = {}; const index: Record<string | number, any> = {};
await this.#block_indexer.export((key, data) => { await this._blockIndexer.export((key, data) => {
index[key] = data; index[key] = data;
}); });
} }
@ -296,13 +296,13 @@ export class BlockIndexer<
| string | string
| Partial<DocumentSearchOptions<boolean>> | Partial<DocumentSearchOptions<boolean>>
) { ) {
return this.#block_indexer.search(part_of_title_or_content as string); return this._blockIndexer.search(part_of_title_or_content as string);
} }
public query(query: QueryIndexMetadata) { public query(query: QueryIndexMetadata) {
const matches: string[] = []; const matches: string[] = [];
const filter = sift<QueryMetadata>(query); const filter = sift<QueryMetadata>(query);
this.#block_metadata.forEach((value, key) => { this._blockMetadata.forEach((value, key) => {
if (filter(value)) matches.push(key); if (filter(value)) matches.push(key);
}); });
return matches; return matches;
@ -310,7 +310,7 @@ export class BlockIndexer<
public getMetadata(ids: string[]): Array<BlockMetadata> { public getMetadata(ids: string[]): Array<BlockMetadata> {
return ids return ids
.filter(id => this.#block_metadata.has(id)) .filter(id => this._blockMetadata.has(id))
.map(id => ({ ...this.#block_metadata.get(id)!, id })); .map(id => ({ ...this._blockMetadata.get(id)!, id }));
} }
} }

View File

@ -69,14 +69,14 @@ export class BlockClient<
B extends BlockInstance<C>, B extends BlockInstance<C>,
C extends ContentOperation C extends ContentOperation
> { > {
readonly #adapter: A; readonly _adapter: A;
readonly #workspace: string; readonly _workspace: string;
// Maximum cache Block 8192, ttl 30 minutes // Maximum cache Block 8192, ttl 30 minutes
readonly #block_caches: LRUCache<string, BaseBlock<B, C>>; readonly _blockCaches: LRUCache<string, BaseBlock<B, C>>;
readonly #block_indexer: BlockIndexer<A, B, C>; readonly _blockIndexer: BlockIndexer<A, B, C>;
readonly #exporters: { readonly _exporters: {
readonly content: BlockExporters<string>; readonly content: BlockExporters<string>;
readonly metadata: BlockExporters< readonly metadata: BlockExporters<
Array<[string, number | string | string[]]> Array<[string, number | string | string[]]>
@ -84,84 +84,83 @@ export class BlockClient<
readonly tag: BlockExporters<string[]>; readonly tag: BlockExporters<string[]>;
}; };
readonly #event_bus: BlockEventBus; readonly _eventBus: BlockEventBus;
readonly #parent_mapping: Map<string, string[]>; readonly _parentMapping: Map<string, string[]>;
readonly #page_mapping: Map<string, string>; readonly _pageMapping: Map<string, string>;
readonly #root: { node?: BaseBlock<B, C> }; readonly _root: { node?: BaseBlock<B, C> };
private constructor( private constructor(
adapter: A, adapter: A,
workspace: string, workspace: string,
options?: BlockClientOptions options?: BlockClientOptions
) { ) {
this.#adapter = adapter; this._adapter = adapter;
this.#workspace = workspace; this._workspace = workspace;
this.#block_caches = new LRUCache({ max: 8192, ttl: 1000 * 60 * 30 }); this._blockCaches = new LRUCache({ max: 8192, ttl: 1000 * 60 * 30 });
this.#exporters = { this._exporters = {
content: options?.content || new Map(), content: options?.content || new Map(),
metadata: options?.metadata || new Map(), metadata: options?.metadata || new Map(),
tag: options?.tagger || new Map(), tag: options?.tagger || new Map(),
}; };
this.#event_bus = new BlockEventBus(); this._eventBus = new BlockEventBus();
this.#block_indexer = new BlockIndexer( this._blockIndexer = new BlockIndexer(
this.#adapter, this._adapter,
this.#workspace, this._workspace,
this.block_builder.bind(this), this.block_builder.bind(this),
this.#event_bus.topic('indexer') this._eventBus.topic('indexer')
); );
this.#parent_mapping = new Map(); this._parentMapping = new Map();
this.#page_mapping = new Map(); this._pageMapping = new Map();
this.#adapter.on('editing', (states: ChangedStates) => this._adapter.on('editing', (states: ChangedStates) =>
this.#event_bus.topic('editing').emit(states) this._eventBus.topic('editing').emit(states)
); );
this.#adapter.on('updated', (states: ChangedStates) => this._adapter.on('updated', (states: ChangedStates) =>
this.#event_bus.topic('updated').emit(states) this._eventBus.topic('updated').emit(states)
); );
this.#adapter.on( this._adapter.on(
'connectivity', 'connectivity',
(states: ChangedStates<Connectivity>) => (states: ChangedStates<Connectivity>) =>
this.#event_bus.topic('connectivity').emit(states) this._eventBus.topic('connectivity').emit(states)
); );
this.#event_bus this._eventBus
.topic<string[]>('rebuild_index') .topic<string[]>('rebuild_index')
.on('rebuild_index', this.rebuild_index.bind(this), { .on('rebuild_index', this.rebuild_index.bind(this), {
debounce: { wait: 1000, maxWait: 1000 }, debounce: { wait: 1000, maxWait: 1000 },
}); });
this.#root = {}; this._root = {};
} }
public addBlockListener(tag: string, listener: BlockListener) { public addBlockListener(tag: string, listener: BlockListener) {
const bus = this.#event_bus.topic<ChangedStates>('updated'); const bus = this._eventBus.topic<ChangedStates>('updated');
if (tag !== 'index' || !bus.has(tag)) bus.on(tag, listener); if (tag !== 'index' || !bus.has(tag)) bus.on(tag, listener);
else console.error(`block listener ${tag} is reserved or exists`); else console.error(`block listener ${tag} is reserved or exists`);
} }
public removeBlockListener(tag: string) { public removeBlockListener(tag: string) {
this.#event_bus.topic('updated').off(tag); this._eventBus.topic('updated').off(tag);
} }
public addEditingListener( public addEditingListener(
tag: string, tag: string,
listener: BlockListener<Set<string>> listener: BlockListener<Set<string>>
) { ) {
const bus = const bus = this._eventBus.topic<ChangedStates<Set<string>>>('editing');
this.#event_bus.topic<ChangedStates<Set<string>>>('editing');
if (tag !== 'index' || !bus.has(tag)) bus.on(tag, listener); if (tag !== 'index' || !bus.has(tag)) bus.on(tag, listener);
else console.error(`editing listener ${tag} is reserved or exists`); else console.error(`editing listener ${tag} is reserved or exists`);
} }
public removeEditingListener(tag: string) { public removeEditingListener(tag: string) {
this.#event_bus.topic('editing').off(tag); this._eventBus.topic('editing').off(tag);
} }
public addConnectivityListener( public addConnectivityListener(
@ -169,31 +168,31 @@ export class BlockClient<
listener: BlockListener<Connectivity> listener: BlockListener<Connectivity>
) { ) {
const bus = const bus =
this.#event_bus.topic<ChangedStates<Connectivity>>('connectivity'); this._eventBus.topic<ChangedStates<Connectivity>>('connectivity');
if (tag !== 'index' || !bus.has(tag)) bus.on(tag, listener); if (tag !== 'index' || !bus.has(tag)) bus.on(tag, listener);
else else
console.error(`connectivity listener ${tag} is reserved or exists`); console.error(`connectivity listener ${tag} is reserved or exists`);
} }
public removeConnectivityListener(tag: string) { public removeConnectivityListener(tag: string) {
this.#event_bus.topic('connectivity').off(tag); this._eventBus.topic('connectivity').off(tag);
} }
private inspector() { private inspector() {
return { return {
...this.#adapter.inspector(), ...this._adapter.inspector(),
indexed: () => this.#block_indexer.inspectIndex(), indexed: () => this._blockIndexer.inspectIndex(),
}; };
} }
private async rebuild_index(exists_ids?: string[]) { private async rebuild_index(exists_ids?: string[]) {
JWT_DEV && logger(`rebuild index`); JWT_DEV && logger(`rebuild index`);
const blocks = await this.#adapter.getBlockByType(BlockTypes.block); const blocks = await this._adapter.getBlockByType(BlockTypes.block);
const excluded = exists_ids || []; const excluded = exists_ids || [];
await Promise.all( await Promise.all(
blocks blocks
.filter(id => !excluded.includes(id)) .filter(id => !excluded.includes(id))
.map(id => this.#block_indexer.refreshIndex(id, 'add')) .map(id => this._blockIndexer.refreshIndex(id, 'add'))
); );
} }
@ -201,13 +200,13 @@ export class BlockClient<
JWT_DEV && logger(`buildIndex: start`); JWT_DEV && logger(`buildIndex: start`);
// Skip the block index that exists in the metadata, assuming that the index of the block existing in the metadata is the latest, and modify this part if there is a problem // Skip the block index that exists in the metadata, assuming that the index of the block existing in the metadata is the latest, and modify this part if there is a problem
// Although there may be cases where the index is refreshed but the metadata is not refreshed, re-indexing will be automatically triggered after the block is changed // Although there may be cases where the index is refreshed but the metadata is not refreshed, re-indexing will be automatically triggered after the block is changed
const exists_ids = await this.#block_indexer.loadIndex(); const exists_ids = await this._blockIndexer.loadIndex();
await this.rebuild_index(exists_ids); await this.rebuild_index(exists_ids);
this.addBlockListener('index', async states => { this.addBlockListener('index', async states => {
await Promise.allSettled( await Promise.allSettled(
Array.from(states.entries()).map(([id, state]) => { Array.from(states.entries()).map(([id, state]) => {
if (state === 'delete') this.#block_caches.delete(id); if (state === 'delete') this._blockCaches.delete(id);
return this.#block_indexer.refreshIndex(id, state); return this._blockIndexer.refreshIndex(id, state);
}) })
); );
}); });
@ -223,10 +222,10 @@ export class BlockClient<
): Promise<Map<string, BaseBlock<B, C>>> { ): Promise<Map<string, BaseBlock<B, C>>> {
JWT_DEV && logger(`getByType: ${block_type}`); JWT_DEV && logger(`getByType: ${block_type}`);
const ids = [ const ids = [
...this.#block_indexer.query({ ...this._blockIndexer.query({
type: BlockTypes[block_type as BlockTypeKeys], type: BlockTypes[block_type as BlockTypeKeys],
}), }),
...this.#block_indexer.query({ ...this._blockIndexer.query({
flavor: BlockFlavors[block_type as BlockFlavorKeys], flavor: BlockFlavors[block_type as BlockFlavorKeys],
}), }),
]; ];
@ -256,7 +255,7 @@ export class BlockClient<
| string | string
| Partial<DocumentSearchOptions<boolean>> | Partial<DocumentSearchOptions<boolean>>
) { ) {
return this.#block_indexer.search(part_of_title_or_content); return this._blockIndexer.search(part_of_title_or_content);
} }
/** /**
@ -278,7 +277,7 @@ export class BlockClient<
const promised_pages = await Promise.all( const promised_pages = await Promise.all(
this.search(part_of_title_or_content).flatMap(({ result }) => this.search(part_of_title_or_content).flatMap(({ result }) =>
result.map(async id => { result.map(async id => {
const page = this.#page_mapping.get(id as string); const page = this._pageMapping.get(id as string);
if (page) return page; if (page) return page;
const block = await this.get(id as BlockTypeKeys); const block = await this.get(id as BlockTypeKeys);
return this.set_page(block); return this.set_page(block);
@ -289,9 +288,9 @@ export class BlockClient<
...new Set(promised_pages.filter((v): v is string => !!v)), ...new Set(promised_pages.filter((v): v is string => !!v)),
]; ];
return Promise.all( return Promise.all(
this.#block_indexer.getMetadata(pages).map(async page => ({ this._blockIndexer.getMetadata(pages).map(async page => ({
content: this.get_decoded_content( content: this.get_decoded_content(
await this.#adapter.getBlock(page.id) await this._adapter.getBlock(page.id)
), ),
...page, ...page,
})) }))
@ -303,7 +302,7 @@ export class BlockClient<
* @returns array of search results * @returns array of search results
*/ */
public query(query: QueryIndexMetadata): string[] { public query(query: QueryIndexMetadata): string[] {
return this.#block_indexer.query(query); return this._blockIndexer.query(query);
} }
/** /**
@ -330,12 +329,12 @@ export class BlockClient<
* @returns block instance * @returns block instance
*/ */
public async getWorkspace() { public async getWorkspace() {
if (!this.#root.node) { if (!this._root.node) {
this.#root.node = await this.get_named_block(this.#workspace, { this._root.node = await this.get_named_block(this._workspace, {
workspace: true, workspace: true,
}); });
} }
return this.#root.node; return this._root.node;
} }
/** /**
@ -348,38 +347,38 @@ export class BlockClient<
} }
private async get_parent(id: string) { private async get_parent(id: string) {
const parents = this.#parent_mapping.get(id); const parents = this._parentMapping.get(id);
if (parents) { if (parents) {
const parent_block_id = parents[0]; const parent_block_id = parents[0];
if (!this.#block_caches.has(parent_block_id)) { if (!this._blockCaches.has(parent_block_id)) {
this.#block_caches.set( this._blockCaches.set(
parent_block_id, parent_block_id,
await this.get(parent_block_id as BlockTypeKeys) await this.get(parent_block_id as BlockTypeKeys)
); );
} }
return this.#block_caches.get(parent_block_id); return this._blockCaches.get(parent_block_id);
} }
return undefined; return undefined;
} }
private set_parent(parent: string, child: string) { private set_parent(parent: string, child: string) {
const parents = this.#parent_mapping.get(child); const parents = this._parentMapping.get(child);
if (parents?.length) { if (parents?.length) {
if (!parents.includes(parent)) { if (!parents.includes(parent)) {
console.error('parent already exists', child, parents); console.error('parent already exists', child, parents);
this.#parent_mapping.set(child, [...parents, parent]); this._parentMapping.set(child, [...parents, parent]);
} }
} else { } else {
this.#parent_mapping.set(child, [parent]); this._parentMapping.set(child, [parent]);
} }
} }
private set_page(block: BaseBlock<B, C>) { private set_page(block: BaseBlock<B, C>) {
const page = this.#page_mapping.get(block.id); const page = this._pageMapping.get(block.id);
if (page) return page; if (page) return page;
const parent_page = block.parent_page; const parent_page = block.parent_page;
if (parent_page) { if (parent_page) {
this.#page_mapping.set(block.id, parent_page); this._pageMapping.set(block.id, parent_page);
return parent_page; return parent_page;
} }
return undefined; return undefined;
@ -390,13 +389,13 @@ export class BlockClient<
matcher: BlockMatcher, matcher: BlockMatcher,
exporter: ReadableContentExporter<string, T> exporter: ReadableContentExporter<string, T>
) { ) {
this.#exporters.content.set(name, [matcher, exporter]); this._exporters.content.set(name, [matcher, exporter]);
this.#event_bus.topic('rebuild_index').emit(); // // rebuild the index every time the content exporter is registered this._eventBus.topic('rebuild_index').emit(); // // rebuild the index every time the content exporter is registered
} }
unregisterContentExporter(name: string) { unregisterContentExporter(name: string) {
this.#exporters.content.delete(name); this._exporters.content.delete(name);
this.#event_bus.topic('rebuild_index').emit(); // Rebuild indexes every time content exporter logs out this._eventBus.topic('rebuild_index').emit(); // Rebuild indexes every time content exporter logs out
} }
registerMetadataExporter<T extends ContentTypes>( registerMetadataExporter<T extends ContentTypes>(
@ -407,13 +406,13 @@ export class BlockClient<
T T
> >
) { ) {
this.#exporters.metadata.set(name, [matcher, exporter]); this._exporters.metadata.set(name, [matcher, exporter]);
this.#event_bus.topic('rebuild_index').emit(); // // rebuild the index every time the content exporter is registered this._eventBus.topic('rebuild_index').emit(); // // rebuild the index every time the content exporter is registered
} }
unregisterMetadataExporter(name: string) { unregisterMetadataExporter(name: string) {
this.#exporters.metadata.delete(name); this._exporters.metadata.delete(name);
this.#event_bus.topic('rebuild_index').emit(); // Rebuild indexes every time content exporter logs out this._eventBus.topic('rebuild_index').emit(); // Rebuild indexes every time content exporter logs out
} }
registerTagExporter<T extends ContentTypes>( registerTagExporter<T extends ContentTypes>(
@ -421,13 +420,13 @@ export class BlockClient<
matcher: BlockMatcher, matcher: BlockMatcher,
exporter: ReadableContentExporter<string[], T> exporter: ReadableContentExporter<string[], T>
) { ) {
this.#exporters.tag.set(name, [matcher, exporter]); this._exporters.tag.set(name, [matcher, exporter]);
this.#event_bus.topic('rebuild_index').emit(); // Reindex every tag exporter registration this._eventBus.topic('rebuild_index').emit(); // Reindex every tag exporter registration
} }
unregisterTagExporter(name: string) { unregisterTagExporter(name: string) {
this.#exporters.tag.delete(name); this._exporters.tag.delete(name);
this.#event_bus.topic('rebuild_index').emit(); // Reindex every time tag exporter logs out this._eventBus.topic('rebuild_index').emit(); // Reindex every time tag exporter logs out
} }
private get_exporters<R>( private get_exporters<R>(
@ -453,7 +452,7 @@ export class BlockClient<
private get_decoded_content(block?: BlockInstance<C>) { private get_decoded_content(block?: BlockInstance<C>) {
if (block) { if (block) {
const [exporter] = this.get_exporters( const [exporter] = this.get_exporters(
this.#exporters.content, this._exporters.content,
block block
); );
if (exporter) { if (exporter) {
@ -474,10 +473,10 @@ export class BlockClient<
(await this.get_parent(block.id)) || root, (await this.get_parent(block.id)) || root,
{ {
content: block => content: block =>
this.get_exporters(this.#exporters.content, block), this.get_exporters(this._exporters.content, block),
metadata: block => metadata: block =>
this.get_exporters(this.#exporters.metadata, block), this.get_exporters(this._exporters.metadata, block),
tag: block => this.get_exporters(this.#exporters.tag, block), tag: block => this.get_exporters(this._exporters.tag, block),
} }
); );
} }
@ -506,13 +505,13 @@ export class BlockClient<
binary, binary,
[namedUuid]: is_named_uuid, [namedUuid]: is_named_uuid,
} = options || {}; } = options || {};
if (block_id_or_type && this.#block_caches.has(block_id_or_type)) { if (block_id_or_type && this._blockCaches.has(block_id_or_type)) {
return this.#block_caches.get(block_id_or_type) as BaseBlock<B, C>; return this._blockCaches.get(block_id_or_type) as BaseBlock<B, C>;
} else { } else {
const block = const block =
(block_id_or_type && (block_id_or_type &&
(await this.#adapter.getBlock(block_id_or_type))) || (await this._adapter.getBlock(block_id_or_type))) ||
(await this.#adapter.createBlock({ (await this._adapter.createBlock({
uuid: is_named_uuid ? block_id_or_type : undefined, uuid: is_named_uuid ? block_id_or_type : undefined,
binary, binary,
type: type:
@ -540,7 +539,7 @@ export class BlockClient<
this.set_parent(parent, abstract_block.id); this.set_parent(parent, abstract_block.id);
this.set_page(abstract_block); this.set_page(abstract_block);
}); });
this.#block_caches.set(abstract_block.id, abstract_block); this._blockCaches.set(abstract_block.id, abstract_block);
if (root && abstract_block.flavor === BlockFlavors.page) { if (root && abstract_block.flavor === BlockFlavors.page) {
root.insertChildren(abstract_block); root.insertChildren(abstract_block);
@ -552,15 +551,15 @@ export class BlockClient<
public async getBlockByFlavor( public async getBlockByFlavor(
flavor: BlockItem<C>['flavor'] flavor: BlockItem<C>['flavor']
): Promise<string[]> { ): Promise<string[]> {
return await this.#adapter.getBlockByFlavor(flavor); return await this._adapter.getBlockByFlavor(flavor);
} }
public getUserId(): string { public getUserId(): string {
return this.#adapter.getUserId(); return this._adapter.getUserId();
} }
public has(block_ids: string[]): Promise<boolean> { public has(block_ids: string[]): Promise<boolean> {
return this.#adapter.checkBlocks(block_ids); return this._adapter.checkBlocks(block_ids);
} }
/** /**
@ -568,11 +567,11 @@ export class BlockClient<
* @param suspend true: suspend monitoring, false: resume monitoring * @param suspend true: suspend monitoring, false: resume monitoring
*/ */
suspend(suspend: boolean) { suspend(suspend: boolean) {
this.#adapter.suspend(suspend); this._adapter.suspend(suspend);
} }
public get history(): HistoryManager { public get history(): HistoryManager {
return this.#adapter.history(); return this._adapter.history();
} }
public static async init( public static async init(

View File

@ -7,14 +7,14 @@ declare const JWT_DEV: boolean;
const logger = getLogger('BlockDB:event_bus'); const logger = getLogger('BlockDB:event_bus');
export class BlockEventBus { export class BlockEventBus {
readonly #event_bus: EventTarget; readonly _eventBus: EventTarget;
readonly #event_callback_cache: Map<string, any>; readonly _eventCallbackCache: Map<string, any>;
readonly #scoped_cache: Map<string, BlockScopedEventBus<any>>; readonly _scopedCache: Map<string, BlockScopedEventBus<any>>;
constructor(event_bus?: EventTarget) { constructor(event_bus?: EventTarget) {
this.#event_bus = event_bus || new EventTarget(); this._eventBus = event_bus || new EventTarget();
this.#event_callback_cache = new Map(); this._eventCallbackCache = new Map();
this.#scoped_cache = new Map(); this._scopedCache = new Map();
} }
protected on_listener( protected on_listener(
@ -23,9 +23,9 @@ export class BlockEventBus {
listener: (e: Event) => void listener: (e: Event) => void
) { ) {
const handler_name = `${topic}/${name}`; const handler_name = `${topic}/${name}`;
if (!this.#event_callback_cache.has(handler_name)) { if (!this._eventCallbackCache.has(handler_name)) {
this.#event_bus.addEventListener(topic, listener); this._eventBus.addEventListener(topic, listener);
this.#event_callback_cache.set(handler_name, listener); this._eventCallbackCache.set(handler_name, listener);
} else { } else {
JWT_DEV && logger(`event handler ${handler_name} is existing`); JWT_DEV && logger(`event handler ${handler_name} is existing`);
} }
@ -33,27 +33,27 @@ export class BlockEventBus {
protected off_listener(topic: string, name: string) { protected off_listener(topic: string, name: string) {
const handler_name = `${topic}/${name}`; const handler_name = `${topic}/${name}`;
const listener = this.#event_callback_cache.get(handler_name); const listener = this._eventCallbackCache.get(handler_name);
if (listener) { if (listener) {
this.#event_bus.removeEventListener(topic, listener); this._eventBus.removeEventListener(topic, listener);
this.#event_callback_cache.delete(handler_name); this._eventCallbackCache.delete(handler_name);
} else { } else {
JWT_DEV && logger(`event handler ${handler_name} is not existing`); JWT_DEV && logger(`event handler ${handler_name} is not existing`);
} }
} }
protected has_listener(topic: string, name: string) { protected has_listener(topic: string, name: string) {
return this.#event_callback_cache.has(`${topic}/${name}`); return this._eventCallbackCache.has(`${topic}/${name}`);
} }
protected emit_event<T>(topic: string, detail?: T) { protected emit_event<T>(topic: string, detail?: T) {
this.#event_bus.dispatchEvent(new CustomEvent(topic, { detail })); this._eventBus.dispatchEvent(new CustomEvent(topic, { detail }));
} }
topic<T = unknown>(topic: string): BlockScopedEventBus<T> { topic<T = unknown>(topic: string): BlockScopedEventBus<T> {
return ( return (
this.#scoped_cache.get(topic) || this._scopedCache.get(topic) ||
new BlockScopedEventBus<T>(topic, this.#event_bus) new BlockScopedEventBus<T>(topic, this._eventBus)
); );
} }
} }
@ -68,11 +68,11 @@ type ListenerOptions = {
}; };
class BlockScopedEventBus<T> extends BlockEventBus { class BlockScopedEventBus<T> extends BlockEventBus {
readonly #topic: string; readonly _topic: string;
constructor(topic: string, event_bus?: EventTarget) { constructor(topic: string, event_bus?: EventTarget) {
super(event_bus); super(event_bus);
this.#topic = topic; this._topic = topic;
} }
on( on(
@ -83,25 +83,25 @@ class BlockScopedEventBus<T> extends BlockEventBus {
if (options?.debounce) { if (options?.debounce) {
const { wait, maxWait } = options.debounce; const { wait, maxWait } = options.debounce;
const debounced = debounce(listener, wait, { maxWait }); const debounced = debounce(listener, wait, { maxWait });
this.on_listener(this.#topic, name, e => { this.on_listener(this._topic, name, e => {
debounced((e as CustomEvent)?.detail); debounced((e as CustomEvent)?.detail);
}); });
} else { } else {
this.on_listener(this.#topic, name, e => { this.on_listener(this._topic, name, e => {
listener((e as CustomEvent)?.detail); listener((e as CustomEvent)?.detail);
}); });
} }
} }
off(name: string) { off(name: string) {
this.off_listener(this.#topic, name); this.off_listener(this._topic, name);
} }
has(name: string) { has(name: string) {
return this.has_listener(this.#topic, name); return this.has_listener(this._topic, name);
} }
emit(detail?: T) { emit(detail?: T) {
this.emit_event(this.#topic, detail); this.emit_event(this._topic, detail);
} }
} }