aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-07-31 22:23:56 +0800
committercrupest <crupest@outlook.com>2020-07-31 22:23:56 +0800
commitc5cd6aebf8516b485180ae5cadc8fed34e74291d (patch)
treef08e121d15dd7541c196d95724e63a9ab4fc3927
parent3d014eee30cf91c2e2c19af5c5140db137ccf225 (diff)
downloadtimeline-c5cd6aebf8516b485180ae5cadc8fed34e74291d.tar.gz
timeline-c5cd6aebf8516b485180ae5cadc8fed34e74291d.tar.bz2
timeline-c5cd6aebf8516b485180ae5cadc8fed34e74291d.zip
Add timeline hub.
-rw-r--r--Timeline/ClientApp/src/app/data/timeline.ts63
1 files changed, 63 insertions, 0 deletions
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<TimelineInfo | null> {
+ return dataStorage.getItem<TimelineInfo | null>(
+ this.getTimelineKey(timelineName)
+ );
+ }
+
+ private syncTimeline(timelineName: string): Promise<TimelineInfo> {
+ // 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<string, TimelineInfoState> {
+ 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<string, Promise<TimelineInfo>>();
+ // TODO: Remove this.
getTimeline(timelineName: string): Observable<TimelineInfo> {
const cache = this.timelineCache.get(timelineName);
let promise: Promise<TimelineInfo>;
@@ -145,6 +207,7 @@ export class TimelineService {
);
}
+ // TODO: Remove this.
getPosts(timelineName: string): Observable<TimelinePostInfo[]> {
const token = userService.currentUser?.token;
return from(getHttpTimelineClient().listPost(timelineName, token)).pipe(