From c10218d8d5ec01ae29c0b2880a8d6af371a562e5 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 27 Jul 2020 17:43:46 +0800 Subject: Add post list subscription hub. --- Timeline/ClientApp/src/app/data/SubscriptionHub.ts | 32 +++++++++++++--------- Timeline/ClientApp/src/app/data/timeline.ts | 22 +++++++++++++++ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/Timeline/ClientApp/src/app/data/SubscriptionHub.ts b/Timeline/ClientApp/src/app/data/SubscriptionHub.ts index 2bc6de56..92a54bc7 100644 --- a/Timeline/ClientApp/src/app/data/SubscriptionHub.ts +++ b/Timeline/ClientApp/src/app/data/SubscriptionHub.ts @@ -3,6 +3,8 @@ // 2. We need a way to finalize the last object. For example, if it has an object url, we need to revoke it. // 3. Make api easier to use and write less boilerplate codes. // +// Currently updator will wait for last update or creation to finish. So the old data passed to it will always be right. We may add feature for just cancel last one but not wait for it. +// // There might be some bugs, especially memory leaks and in asynchronization codes. import * as rxjs from 'rxjs'; @@ -23,7 +25,7 @@ class SubscriptionToken { } class SubscriptionLine { - private _lastDataPromise: Promise; + private _lastDataPromise: Promise; private _dataSubject = new rxjs.BehaviorSubject(undefined); private _data$: rxjs.Observable = this._dataSubject.pipe( filter((d) => d !== undefined) @@ -32,11 +34,12 @@ class SubscriptionLine { constructor( _creator: () => Promise, - private _destroyer: (data: TData) => void, + private _destroyer: ((data: TData) => void) | undefined, private _onZeroRef: (self: SubscriptionLine) => void ) { this._lastDataPromise = _creator().then((data) => { this._dataSubject.next(data); + return data; }); } @@ -50,25 +53,25 @@ class SubscriptionLine { token._subscription.unsubscribe(); this._refCount -= 1; if (this._refCount === 0) { - void this._lastDataPromise.then(() => { - const last = this._dataSubject.value; - if (last !== undefined) { - this._destroyer(last); + void this._lastDataPromise.then((data) => { + if (this._destroyer != null && data !== undefined) { + this._destroyer(data); } }); this._onZeroRef(this); } } - next(updator: () => Promise): void { + next(updator: (old: TData) => Promise): void { this._lastDataPromise = this._lastDataPromise - .then(() => updator()) + .then((old) => updator(old)) .then((data) => { const last = this._dataSubject.value; - if (last !== undefined) { + if (this._destroyer != null && last !== undefined) { this._destroyer(last); } this._dataSubject.next(data); + return data; }); } } @@ -82,7 +85,7 @@ export class SubscriptionHub constructor( public keyToString: (key: TKey) => string, public creator: (key: TKey) => Promise, - public destroyer: (key: TKey, data: TData) => void + public destroyer?: (key: TKey, data: TData) => void ) {} private subscriptionLineMap = new Map>(); @@ -92,11 +95,14 @@ export class SubscriptionHub const line = (() => { const savedLine = this.subscriptionLineMap.get(keyString); if (savedLine == null) { + const { destroyer } = this; const newLine = new SubscriptionLine( () => this.creator(key), - (data) => { - this.destroyer(key, data); - }, + destroyer != null + ? (data) => { + destroyer(key, data); + } + : undefined, () => { this.subscriptionLineMap.delete(keyString); } diff --git a/Timeline/ClientApp/src/app/data/timeline.ts b/Timeline/ClientApp/src/app/data/timeline.ts index dde204be..f2c3fdda 100644 --- a/Timeline/ClientApp/src/app/data/timeline.ts +++ b/Timeline/ClientApp/src/app/data/timeline.ts @@ -126,6 +126,28 @@ export class TimelineService { ); } + private _postListSubscriptionHub = new SubscriptionHub< + string, + TimelinePostInfo[] + >( + (key) => key, + async (key) => { + return ( + await getHttpTimelineClient().listPost( + key, + userService.currentUser?.token + ) + ).map((post) => ({ + ...post, + timelineName: key, + })); + } + ); + + get postListSubscriptionHub(): ISubscriptionHub { + return this._postListSubscriptionHub; + } + private _postDataSubscriptionHub = new SubscriptionHub( (key) => `${key.timelineName}/${key.postId}`, async (key) => { -- cgit v1.2.3