From 9f8c232ef942a070d4449b9d7f075ff66aa2a0c8 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 26 Aug 2020 00:51:35 +0800 Subject: DataHub v3.0 . Also fix the bug that posts may be wrong if timeline has changed with same name. --- Timeline/ClientApp/src/app/data/DataHub.ts | 91 +++++++++++++++++++----------- 1 file changed, 59 insertions(+), 32 deletions(-) (limited to 'Timeline/ClientApp/src/app/data/DataHub.ts') diff --git a/Timeline/ClientApp/src/app/data/DataHub.ts b/Timeline/ClientApp/src/app/data/DataHub.ts index e6be740d..93a9b41f 100644 --- a/Timeline/ClientApp/src/app/data/DataHub.ts +++ b/Timeline/ClientApp/src/app/data/DataHub.ts @@ -9,13 +9,22 @@ export type WithSyncStatus = T & { syncing: boolean }; export class DataLine { private _current: TData | undefined = undefined; + private _syncPromise: Promise | null = null; private _syncingSubject = new BehaviorSubject(false); private _observers: Subscriber[] = []; constructor( - private config?: { destroyable?: (value: TData | undefined) => boolean } - ) {} + private config: { + sync: () => Promise; + destroyable?: (value: TData | undefined) => boolean; + disableInitSync?: boolean; + } + ) { + if (config.disableInitSync !== true) { + setImmediate(() => void this.sync()); + } + } private subscribe(subscriber: Subscriber): void { this._observers.push(subscriber); @@ -68,19 +77,33 @@ export class DataLine { } get isSyncing(): boolean { - return this._syncingSubject.value; + return this._syncPromise != null; } - beginSync(): void { - if (!this._syncingSubject.value) { + sync(): Promise { + if (this._syncPromise == null) { this._syncingSubject.next(true); + this._syncPromise = this.config.sync().then(() => { + this._syncingSubject.next(false); + this._syncPromise = null; + }); } + + return this._syncPromise; } - endSync(): void { - if (this._syncingSubject.value) { - this._syncingSubject.next(false); + syncWithAction( + syncAction: (line: DataLine) => Promise + ): Promise { + if (this._syncPromise == null) { + this._syncingSubject.next(true); + this._syncPromise = syncAction(this).then(() => { + this._syncingSubject.next(false); + this._syncPromise = null; + }); } + + return this._syncPromise; } get destroyable(): boolean { @@ -88,20 +111,15 @@ export class DataLine { return ( this._observers.length === 0 && - !this._syncingSubject.value && + !this.isSyncing && (customDestroyable != null ? customDestroyable(this._current) : true) ); } - - endSyncAndNext(value: TData): void { - this.endSync(); - this.next(value); - } } export class DataHub { + private sync: (key: TKey, line: DataLine) => Promise; private keyToString: (key: TKey) => string; - private setup?: (key: TKey, line: DataLine) => (() => void) | void; private destroyable?: (key: TKey, value: TData | undefined) => boolean; private readonly subscriptionLineMap = new Map>(); @@ -109,13 +127,14 @@ export class DataHub { private cleanTimerId = 0; // setup is called after creating line and if it returns a function as destroyer, then when the line is destroyed the destroyer will be called. - constructor(config?: { + constructor(config: { + sync: (key: TKey, line: DataLine) => Promise; keyToString?: (key: TKey) => string; - setup?: (key: TKey, line: DataLine) => void; destroyable?: (key: TKey, value: TData | undefined) => boolean; }) { + this.sync = config.sync; this.keyToString = - config?.keyToString ?? + config.keyToString ?? ((value): string => { if (typeof value === "string") return value; else @@ -124,8 +143,7 @@ export class DataHub { ); }); - this.setup = config?.setup; - this.destroyable = config?.destroyable; + this.destroyable = config.destroyable; } private cleanLines(): void { @@ -148,17 +166,16 @@ export class DataHub { } } - private createLine(key: TKey, useSetup = true): DataLine { + private createLine(key: TKey, disableInitSync = false): DataLine { const keyString = this.keyToString(key); - const { setup, destroyable } = this; - const newLine = new DataLine({ + const { destroyable } = this; + const newLine: DataLine = new DataLine({ + sync: () => this.sync(key, newLine), destroyable: destroyable != null ? (value) => destroyable(key, value) : undefined, + disableInitSync: disableInitSync, }); this.subscriptionLineMap.set(keyString, newLine); - if (useSetup) { - setup?.(key, newLine); - } if (this.subscriptionLineMap.size === 1) { this.cleanTimerId = window.setInterval(this.cleanLines.bind(this), 20000); } @@ -166,17 +183,17 @@ export class DataHub { } getObservable(key: TKey): Observable { - return this.getLineOrCreateWithSetup(key).getObservable(); + return this.getLineOrCreate(key).getObservable(); } getSyncStatusObservable(key: TKey): Observable { - return this.getLineOrCreateWithSetup(key).getSyncStatusObservable(); + return this.getLineOrCreate(key).getSyncStatusObservable(); } getDataWithSyncStatusObservable( key: TKey ): Observable> { - return this.getLineOrCreateWithSetup(key).getDataWithSyncStatusObservable(); + return this.getLineOrCreate(key).getDataWithSyncStatusObservable(); } getLine(key: TKey): DataLine | null { @@ -184,15 +201,25 @@ export class DataHub { return this.subscriptionLineMap.get(keyString) ?? null; } - getLineOrCreateWithSetup(key: TKey): DataLine { + getLineOrCreate(key: TKey): DataLine { const keyString = this.keyToString(key); return this.subscriptionLineMap.get(keyString) ?? this.createLine(key); } - getLineOrCreateWithoutSetup(key: TKey): DataLine { + getLineOrCreateWithoutInitSync(key: TKey): DataLine { const keyString = this.keyToString(key); return ( - this.subscriptionLineMap.get(keyString) ?? this.createLine(key, false) + this.subscriptionLineMap.get(keyString) ?? this.createLine(key, true) ); } + + optionalInitLineWithSyncAction( + key: TKey, + syncAction: (line: DataLine) => Promise + ): Promise { + const optionalLine = this.getLine(key); + if (optionalLine != null) return Promise.resolve(); + const line = this.createLine(key, true); + return line.syncWithAction(syncAction); + } } -- cgit v1.2.3