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
+24 -24
View File
@@ -26,47 +26,47 @@ export class AbstractBlock<
B extends BlockInstance<C>,
C extends ContentOperation
> {
readonly #id: string;
readonly _id: string;
readonly #block: BlockInstance<C>;
readonly #history: HistoryManager;
readonly #root?: AbstractBlock<B, C>;
readonly #parent_listener: Map<string, BlockListener>;
readonly _history: HistoryManager;
readonly _root?: AbstractBlock<B, C>;
readonly _parentListener: Map<string, BlockListener>;
#parent?: AbstractBlock<B, C>;
_parent?: AbstractBlock<B, C>;
constructor(
block: B,
root?: AbstractBlock<B, C>,
parent?: AbstractBlock<B, C>
) {
this.#id = block.id;
this._id = block.id;
this.#block = block;
this.#history = this.#block.scopedHistory([this.#id]);
this._history = this.#block.scopedHistory([this._id]);
this.#root = root;
this.#parent_listener = new Map();
this.#parent = parent;
JWT_DEV && logger_debug(`init: exists ${this.#id}`);
this._root = root;
this._parentListener = new Map();
this._parent = parent;
JWT_DEV && logger_debug(`init: exists ${this._id}`);
}
public get root() {
return this.#root;
return this._root;
}
protected get parent_node() {
return this.#parent;
return this._parent;
}
protected _getParentPage(warning = true): string | undefined {
if (this.flavor === 'page') {
return this.#block.id;
} else if (!this.#parent) {
} else if (!this._parent) {
if (warning && this.flavor !== 'workspace') {
console.warn('parent not found');
}
return undefined;
} else {
return this.#parent.parent_page;
return this._parent.parent_page;
}
}
@@ -80,7 +80,7 @@ export class AbstractBlock<
callback: BlockListener
) {
if (event === 'parent') {
this.#parent_listener.set(name, callback);
this._parentListener.set(name, callback);
} else {
this.#block.on(event, name, callback);
}
@@ -88,7 +88,7 @@ export class AbstractBlock<
public off(event: 'content' | 'children' | 'parent', name: string) {
if (event === 'parent') {
this.#parent_listener.delete(name);
this._parentListener.delete(name);
} else {
this.#block.off(event, name);
}
@@ -117,7 +117,7 @@ export class AbstractBlock<
return this.#block.content.asMap() as MapOperation<T>;
}
throw new Error(
`this block not a structured block: ${this.#id}, ${
`this block not a structured block: ${this._id}, ${
this.#block.type
}`
);
@@ -181,9 +181,9 @@ export class AbstractBlock<
}
[SET_PARENT](parent: AbstractBlock<B, C>) {
this.#parent = parent;
this._parent = parent;
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);
}
}
@@ -196,7 +196,7 @@ export class AbstractBlock<
const updated = this.last_updated_date;
return [
`id:${this.#id}`,
`id:${this._id}`,
`type:${this.type}`,
`type:${this.flavor}`,
this.flavor === BlockFlavors.page && `type:doc`, // normal documentation
@@ -211,7 +211,7 @@ export class AbstractBlock<
* current document instance id
*/
public get id(): string {
return this.#id;
return this._id;
}
/**
@@ -249,7 +249,7 @@ export class AbstractBlock<
) {
JWT_DEV && logger(`insertChildren: start`);
if (block.id === this.#id) return; // avoid self-reference
if (block.id === this._id) return; // avoid self-reference
if (
this.type !== BlockTypes.block || // binary cannot insert subblocks
(block.type !== BlockTypes.block &&
@@ -367,6 +367,6 @@ export class AbstractBlock<
* TODO: scoped history
*/
public get history(): HistoryManager {
return this.#history;
return this._history;
}
}
+16 -17
View File
@@ -51,24 +51,24 @@ export class BaseBlock<
B extends BlockInstance<C>,
C extends ContentOperation
> extends AbstractBlock<B, C> {
readonly #exporters?: Exporters;
readonly #content_exporters_getter: () => Map<
readonly _exporters?: Exporters;
readonly _contentExportersGetter: () => Map<
string,
ReadableContentExporter<string, any>
>;
readonly #metadata_exporters_getter: () => Map<
readonly _metadataExportersGetter: () => Map<
string,
ReadableContentExporter<
Array<[string, number | string | string[]]>,
any
>
>;
readonly #tag_exporters_getter: () => Map<
readonly _tagExportersGetter: () => Map<
string,
ReadableContentExporter<string[], any>
>;
#validators: Map<string, Validator> = new Map();
validators: Map<string, Validator> = new Map();
constructor(
block: B,
@@ -78,12 +78,11 @@ export class BaseBlock<
) {
super(block, root, parent);
this.#exporters = exporters;
this.#content_exporters_getter = () =>
new Map(exporters?.content(block));
this.#metadata_exporters_getter = () =>
this._exporters = exporters;
this._contentExportersGetter = () => new Map(exporters?.content(block));
this._metadataExportersGetter = () =>
new Map(exporters?.metadata(block));
this.#tag_exporters_getter = () => new Map(exporters?.tag(block));
this._tagExportersGetter = () => new Map(exporters?.tag(block));
}
get parent() {
@@ -158,14 +157,14 @@ export class BaseBlock<
setValidator(key: string, validator?: Validator) {
if (validator) {
this.#validators.set(key, validator);
this.validators.set(key, validator);
} else {
this.#validators.delete(key);
this.validators.delete(key);
}
}
private validate(key: string, value: unknown): boolean {
const validate = this.#validators.get(key);
const validate = this.validators.get(key);
if (validate) {
return validate(value) === false ? false : true;
}
@@ -185,13 +184,13 @@ export class BaseBlock<
*/
private get_children_instance(blockId?: string): BaseBlock<B, C>[] {
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() {
const metadata: Record<string, number | string | string[]> = {};
for (const [name, exporter] of this.#metadata_exporters_getter()) {
for (const [name, exporter] of this._metadataExportersGetter()) {
try {
for (const [key, val] of exporter(this.getContent())) {
metadata[key] = val;
@@ -226,7 +225,7 @@ export class BaseBlock<
private get_indexable_content(): string | undefined {
const contents = [];
for (const [name, exporter] of this.#content_exporters_getter()) {
for (const [name, exporter] of this._contentExportersGetter()) {
try {
const content = exporter(this.getContent());
if (content) contents.push(content);
@@ -246,7 +245,7 @@ export class BaseBlock<
private get_indexable_tags(): string[] {
const tags: string[] = [];
for (const [name, exporter] of this.#tag_exporters_getter()) {
for (const [name, exporter] of this._tagExportersGetter()) {
try {
tags.push(...exporter(this.getContent()));
} catch (err) {
+45 -45
View File
@@ -94,18 +94,18 @@ export class BlockIndexer<
B extends BlockInstance<C>,
C extends ContentOperation
> {
readonly #adapter: A;
readonly #idb: BlockIdbInstance;
readonly _adapter: A;
readonly _idb: BlockIdbInstance;
readonly #block_indexer: DocumentIndexer<IndexMetadata>;
readonly #block_metadata: LRUCache<string, QueryMetadata>;
readonly #event_bus: BlockEventBus;
readonly _blockIndexer: DocumentIndexer<IndexMetadata>;
readonly _blockMetadata: LRUCache<string, QueryMetadata>;
readonly _eventBus: BlockEventBus;
readonly #block_builder: (
readonly _blockBuilder: (
block: BlockInstance<C>
) => Promise<BaseBlock<B, C>>;
readonly #delay_index: { documents: Map<string, BaseBlock<B, C>> };
readonly _delayIndex: { documents: Map<string, BaseBlock<B, C>> };
constructor(
adapter: A,
@@ -113,10 +113,10 @@ export class BlockIndexer<
block_builder: (block: BlockInstance<C>) => Promise<BaseBlock<B, C>>,
event_bus: BlockEventBus
) {
this.#adapter = adapter;
this.#idb = initIndexIdb(workspace);
this._adapter = adapter;
this._idb = initIndexIdb(workspace);
this.#block_indexer = new DocumentIndexer({
this._blockIndexer = new DocumentIndexer({
document: {
id: 'id',
index: ['content', 'reference'],
@@ -126,23 +126,23 @@ export class BlockIndexer<
tokenize: 'forward',
context: true,
});
this.#block_metadata = new LRUCache({
this._blockMetadata = new LRUCache({
max: 10240,
ttl: 1000 * 60 * 30,
});
this.#block_builder = block_builder;
this.#event_bus = event_bus;
this._blockBuilder = block_builder;
this._eventBus = event_bus;
this.#delay_index = { documents: new Map() };
this._delayIndex = { documents: new Map() };
this.#event_bus
this._eventBus
.topic('reindex')
.on('reindex', this.content_reindex.bind(this), {
debounce: { wait: 1000, maxWait: 1000 * 10 },
});
this.#event_bus
this._eventBus
.topic('save_index')
.on('save_index', this.save_index.bind(this), {
debounce: { wait: 1000 * 10, maxWait: 1000 * 20 },
@@ -152,8 +152,8 @@ export class BlockIndexer<
private async content_reindex() {
const paddings: Record<string, BlockIndexedContent> = {};
this.#delay_index.documents = produce(
this.#delay_index.documents,
this._delayIndex.documents = produce(
this._delayIndex.documents,
draft => {
for (const [k, block] of draft) {
paddings[k] = {
@@ -166,11 +166,11 @@ export class BlockIndexer<
);
for (const [key, { index, query }] of Object.entries(paddings)) {
if (index.content) {
await this.#block_indexer.addAsync(key, index);
this.#block_metadata.set(key, query);
await this._blockIndexer.addAsync(key, index);
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>) {
@@ -185,14 +185,14 @@ export class BlockIndexer<
BlockFlavors.reference,
];
if (filter.includes(block.flavor)) {
this.#delay_index.documents = produce(
this.#delay_index.documents,
this._delayIndex.documents = produce(
this._delayIndex.documents,
draft => {
draft.set(block.id, block);
}
);
this.#event_bus.topic('reindex').emit();
this._eventBus.topic('reindex').emit();
return true;
}
logger_debug(`skip index ${block.flavor}: ${block.id}`);
@@ -202,19 +202,19 @@ export class BlockIndexer<
async refreshIndex(id: string, state: ChangedState) {
JWT_DEV && logger(`refreshArticleIndex: ${id}`);
if (state === 'delete') {
this.#delay_index.documents = produce(
this.#delay_index.documents,
this._delayIndex.documents = produce(
this._delayIndex.documents,
draft => {
this.#block_indexer.remove(id);
this.#block_metadata.delete(id);
this._blockIndexer.remove(id);
this._blockMetadata.delete(id);
draft.delete(id);
}
);
return;
}
const block = await this.#adapter.getBlock(id);
const block = await this._adapter.getBlock(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 &&
logger(
state
@@ -230,43 +230,43 @@ export class BlockIndexer<
}
async loadIndex() {
for (const key of await this.#idb.index.keys()) {
const content = await this.#idb.index.get(key);
for (const key of await this._idb.index.keys()) {
const content = await this._idb.index.get(key);
if (content) {
const decoded = strFromU8(inflateSync(new Uint8Array(content)));
try {
await this.#block_indexer.import(key, decoded as any);
await this._blockIndexer.import(key, decoded as any);
} catch (e) {
console.error(`Failed to load index ${key}`, e);
}
}
}
for (const key of await this.#idb.metadata.keys()) {
const content = await this.#idb.metadata.get(key);
for (const key of await this._idb.metadata.keys()) {
const content = await this._idb.metadata.get(key);
if (content) {
const decoded = strFromU8(inflateSync(new Uint8Array(content)));
try {
await this.#block_indexer.import(key, JSON.parse(decoded));
await this._blockIndexer.import(key, JSON.parse(decoded));
} catch (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() {
const idb = this.#idb;
const idb = this._idb;
await idb.index
.keys()
.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(
String(key),
deflateSync(strToU8(data as any))
);
});
const metadata = this.#block_metadata;
const metadata = this._blockMetadata;
await idb.metadata
.keys()
.then(keys =>
@@ -286,7 +286,7 @@ export class BlockIndexer<
public async inspectIndex() {
const index: Record<string | number, any> = {};
await this.#block_indexer.export((key, data) => {
await this._blockIndexer.export((key, data) => {
index[key] = data;
});
}
@@ -296,13 +296,13 @@ export class BlockIndexer<
| string
| 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) {
const matches: string[] = [];
const filter = sift<QueryMetadata>(query);
this.#block_metadata.forEach((value, key) => {
this._blockMetadata.forEach((value, key) => {
if (filter(value)) matches.push(key);
});
return matches;
@@ -310,7 +310,7 @@ export class BlockIndexer<
public getMetadata(ids: string[]): Array<BlockMetadata> {
return ids
.filter(id => this.#block_metadata.has(id))
.map(id => ({ ...this.#block_metadata.get(id)!, id }));
.filter(id => this._blockMetadata.has(id))
.map(id => ({ ...this._blockMetadata.get(id)!, id }));
}
}