From d3085ff3483c063b07b9553b539f3836b666cac8 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 27 Jul 2020 23:27:56 +0800 Subject: Add timeline post list state. --- Timeline/ClientApp/src/app/data/timeline.ts | 43 +++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'Timeline/ClientApp/src') diff --git a/Timeline/ClientApp/src/app/data/timeline.ts b/Timeline/ClientApp/src/app/data/timeline.ts index 7ade8f56..ab01c86c 100644 --- a/Timeline/ClientApp/src/app/data/timeline.ts +++ b/Timeline/ClientApp/src/app/data/timeline.ts @@ -62,6 +62,15 @@ export interface PostKey { postId: number; } +export interface TimelinePostListState { + state: + | 'loading' // Loading posts from cache. `posts` is empty array. + | 'syncing' // Cache loaded and syncing now. + | 'synced' // Sync succeeded. + | 'offline'; // Sync failed and use cache. + posts: TimelinePostInfo[]; +} + export class TimelineService { getTimeline(timelineName: string): Observable { return from(getHttpTimelineClient().getTimeline(timelineName)).pipe( @@ -128,24 +137,34 @@ export class TimelineService { private _postListSubscriptionHub = new SubscriptionHub< string, - TimelinePostInfo[] + TimelinePostListState >( (key) => key, - () => [], + () => ({ + state: 'loading', + posts: [], + }), async (key) => { - return ( - await getHttpTimelineClient().listPost( - key, - userService.currentUser?.token - ) - ).map((post) => ({ - ...post, - timelineName: key, - })); + // TODO: Implement cache + return { + state: 'synced', + posts: ( + await getHttpTimelineClient().listPost( + key, + userService.currentUser?.token + ) + ).map((post) => ({ + ...post, + timelineName: key, + })), + }; } ); - get postListSubscriptionHub(): ISubscriptionHub { + get postListSubscriptionHub(): ISubscriptionHub< + string, + TimelinePostListState + > { return this._postListSubscriptionHub; } -- cgit v1.2.3