From c5cd6aebf8516b485180ae5cadc8fed34e74291d Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 31 Jul 2020 22:23:56 +0800 Subject: Add timeline hub. --- Timeline/ClientApp/src/app/data/timeline.ts | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'Timeline/ClientApp/src') diff --git a/Timeline/ClientApp/src/app/data/timeline.ts b/Timeline/ClientApp/src/app/data/timeline.ts index 84eb3764..d84f7bc4 100644 --- a/Timeline/ClientApp/src/app/data/timeline.ts +++ b/Timeline/ClientApp/src/app/data/timeline.ts @@ -76,15 +76,77 @@ export interface TimelinePostListState { posts: TimelinePostInfo[]; } +export interface TimelineInfoLoadingState { + state: 'loading'; // Loading from cache. + timeline: null; +} + +export interface TimelineInfoNonLoadingState { + state: + | 'syncing' // Cache loaded and syncing now. If null means there is no cache for the timeline. + | 'offline' // Sync failed and use cache. + | 'synced' // Sync succeeded. If null means the timeline does not exist. + | 'new'; // This is a new timeline different from cached one. If null means the timeline does not exist. + timeline: TimelineInfo | null; +} + +export type TimelineInfoState = + | TimelineInfoLoadingState + | TimelineInfoNonLoadingState; + interface PostListInfo { idList: number[]; lastUpdated: string; } export class TimelineService { + // timeline storage structure: + // each timeline has a TimelineInfo saved with key created by getTimelineKey + + private getTimelineKey(timelineName: string): string { + return `timeline.${timelineName}`; + } + + private getCachedTimeline( + timelineName: string + ): Promise { + return dataStorage.getItem( + this.getTimelineKey(timelineName) + ); + } + + private syncTimeline(timelineName: string): Promise { + // TODO: Implement this. + throw new Error('Not implemented.'); + } + + private _timelineSubscriptionHub = new SubscriptionHub< + string, + TimelineInfoState + >( + (key) => key, + () => ({ + state: 'loading', + timeline: null, + }), + async (key) => { + const result = await this.getCachedTimeline(key); + void this.syncTimeline(key); + return { + state: 'syncing', + timeline: result, + }; + } + ); + + get timelineHub(): ISubscriptionHub { + return this._timelineSubscriptionHub; + } + // TODO: Remove this! This is currently only used to avoid multiple fetch of timeline. Because post list need to use the timeline id and call this method. But after timeline is also saved locally, this should be removed. private timelineCache = new Map>(); + // TODO: Remove this. getTimeline(timelineName: string): Observable { const cache = this.timelineCache.get(timelineName); let promise: Promise; @@ -145,6 +207,7 @@ export class TimelineService { ); } + // TODO: Remove this. getPosts(timelineName: string): Observable { const token = userService.currentUser?.token; return from(getHttpTimelineClient().listPost(timelineName, token)).pipe( -- cgit v1.2.3