refactor: jwt internal version migration

This commit is contained in:
DarkSky
2022-08-23 17:07:16 +08:00
parent eedb4864df
commit 6df2676c88
18 changed files with 274 additions and 145 deletions
+20 -15
View File
@@ -1,3 +1,10 @@
import {
BlockFlavorKeys,
BlockFlavors,
BlockTypeKeys,
BlockTypes,
} from '../types';
import { getLogger } from '../utils';
import {
BlockInstance,
BlockListener,
@@ -6,14 +13,7 @@ import {
ContentTypes,
HistoryManager,
MapOperation,
} from '../adapter';
import {
BlockFlavorKeys,
BlockFlavors,
BlockTypeKeys,
BlockTypes,
} from '../types';
import { getLogger } from '../utils';
} from '../yjs/types';
declare const JWT_DEV: boolean;
const logger = getLogger('BlockDB:block');
@@ -29,10 +29,10 @@ export class AbstractBlock<
private readonly _id: string;
private readonly _block: BlockInstance<C>;
private readonly _history: HistoryManager;
private readonly _root?: AbstractBlock<B, C>;
private readonly _root: AbstractBlock<B, C> | undefined;
private readonly _parentListener: Map<string, BlockListener>;
private _parent?: AbstractBlock<B, C>;
private _parent: AbstractBlock<B, C> | undefined;
private _changeParent?: () => void;
constructor(
@@ -48,7 +48,9 @@ export class AbstractBlock<
this._parentListener = new Map();
JWT_DEV && logger_debug(`init: exists ${this._id}`);
if (parent) this._refreshParent(parent);
if (parent) {
this._refreshParent(parent);
}
}
public get root() {
@@ -146,7 +148,7 @@ export class AbstractBlock<
return new Date(timestamp)
.toISOString()
.split('T')[0]
.replace(/-/g, '');
?.replace(/-/g, '');
}
// eslint-disable-next-line no-empty
} catch (e) {}
@@ -259,7 +261,7 @@ export class AbstractBlock<
}
/**
* Insert sub-Block
* Insert children block
* @param block Block instance
* @param position Insertion position, if it is empty, it will be inserted at the end. If the block already exists, the position will be moved
* @returns
@@ -270,9 +272,12 @@ export class AbstractBlock<
) {
JWT_DEV && logger(`insertChildren: start`);
if (block.id === this._id) return; // avoid self-reference
if (block.id === this._id) {
// avoid self-reference
return;
}
if (
this.type !== BlockTypes.block || // binary cannot insert subblocks
this.type !== BlockTypes.block || // binary cannot insert children blocks
(block.type !== BlockTypes.block &&
this.flavor !== BlockFlavors.workspace) // binary can only be inserted into workspace
) {
+23 -15
View File
@@ -1,3 +1,5 @@
import { BlockItem } from '../types';
import { getLogger } from '../utils';
import {
ArrayOperation,
BlockInstance,
@@ -5,9 +7,7 @@ import {
ContentTypes,
InternalPlainObject,
MapOperation,
} from '../adapter';
import { BlockItem } from '../types';
import { getLogger } from '../utils';
} from '../yjs/types';
import { AbstractBlock } from './abstract';
import { BlockCapability } from './capability';
@@ -23,8 +23,8 @@ export interface Decoration extends InternalPlainObject {
type Validator = <T>(value: T | undefined) => boolean | void;
export type IndexMetadata = Readonly<{
content?: string;
reference?: string;
content: string | undefined;
reference: string | undefined;
tags: string[];
}>;
export type QueryMetadata = Readonly<
@@ -51,7 +51,7 @@ export class BaseBlock<
B extends BlockInstance<C>,
C extends ContentOperation
> extends AbstractBlock<B, C> {
private readonly _exporters?: Exporters;
private readonly _exporters: Exporters | undefined;
private readonly _contentExportersGetter: () => Map<
string,
ReadableContentExporter<string, any>
@@ -68,7 +68,7 @@ export class BaseBlock<
ReadableContentExporter<string[], any>
>;
validators: Map<string, Validator> = new Map();
private readonly _validators: Map<string, Validator> = new Map();
constructor(
block: B,
@@ -157,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;
}
@@ -179,7 +179,7 @@ export class BaseBlock<
}
/**
* Get an instance of the child Block
* Get an instance of the child block
* @param blockId block id
*/
private get_children_instance(blockId?: string): BaseBlock<B, C>[] {
@@ -201,9 +201,15 @@ export class BaseBlock<
}
try {
const parent_page = this._getParentPage(false);
if (parent_page) metadata['page'] = parent_page;
if (this.group) metadata['group'] = this.group.id;
if (this.parent) metadata['parent'] = this.parent.id;
if (parent_page) {
metadata['page'] = parent_page;
}
if (this.group) {
metadata['group'] = this.group.id;
}
if (this.parent) {
metadata['parent'] = this.parent.id;
}
} catch (e) {
logger(`Failed to export default metadata`, e);
}
@@ -228,7 +234,9 @@ export class BaseBlock<
for (const [name, exporter] of this._contentExportersGetter()) {
try {
const content = exporter(this.getContent());
if (content) contents.push(content);
if (content) {
contents.push(content);
}
} catch (err) {
logger(`Failed to export content: ${name}`, err);
}
+23 -10
View File
@@ -7,14 +7,14 @@ import produce from 'immer';
import LRUCache from 'lru-cache';
import sift, { Query } from 'sift';
import { BlockFlavors } from '../types';
import { BlockEventBus, getLogger } from '../utils';
import {
AsyncDatabaseAdapter,
BlockInstance,
ChangedStates,
ContentOperation,
} from '../adapter';
import { BlockFlavors } from '../types';
import { BlockEventBus, getLogger } from '../utils';
} from '../yjs/types';
import { BaseBlock, IndexMetadata, QueryMetadata } from './base';
@@ -315,7 +315,9 @@ export class BlockIndexer<
private _testMetaKey(key: string) {
try {
const metadata = this._blockMetadata.values().next().value;
if (!metadata || typeof metadata !== 'object') return false;
if (!metadata || typeof metadata !== 'object') {
return false;
}
return !!(key in metadata);
} catch (e) {
return false;
@@ -324,8 +326,11 @@ export class BlockIndexer<
private _getSortedMetadata(sort: string, desc?: boolean) {
const sorter = naturalSort(Array.from(this._blockMetadata.entries()));
if (desc) return sorter.desc(([, m]) => m[sort]);
else return sorter.asc(([, m]) => m[sort]);
if (desc) {
return sorter.desc(([, m]) => m[sort]);
} else {
return sorter.asc(([, m]) => m[sort]);
}
}
public query(query: QueryIndexMetadata) {
@@ -337,15 +342,23 @@ export class BlockIndexer<
if ($sort && this._testMetaKey($sort)) {
const metadata = this._getSortedMetadata($sort, $desc);
metadata.forEach(([key, value]) => {
if (matches.length > limit) return;
if (filter(value)) matches.push(key);
if (matches.length > limit) {
return;
}
if (filter(value)) {
matches.push(key);
}
});
return matches;
} else {
this._blockMetadata.forEach((value, key) => {
if (matches.length > limit) return;
if (filter(value)) matches.push(key);
if (matches.length > limit) {
return;
}
if (filter(value)) {
matches.push(key);
}
});
return matches;