diff options
Diffstat (limited to 'FrontEnd/src/app/services/timeline.ts')
-rw-r--r-- | FrontEnd/src/app/services/timeline.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/FrontEnd/src/app/services/timeline.ts b/FrontEnd/src/app/services/timeline.ts index a24ec8eb..c49ba654 100644 --- a/FrontEnd/src/app/services/timeline.ts +++ b/FrontEnd/src/app/services/timeline.ts @@ -1,5 +1,11 @@ import { TimelineVisibility } from "@/http/timeline"; import XRegExp from "xregexp"; +import { Observable } from "rxjs"; +import { HubConnection, HubConnectionBuilder } from "@microsoft/signalr"; + +import { UiLogicError } from "@/common"; + +import { token$ } from "@/http/common"; const timelineNameReg = XRegExp("^[-_\\p{L}]*$", "u"); @@ -15,3 +21,48 @@ export const timelineVisibilityTooltipTranslationMap: Record< Register: "timeline.visibilityTooltip.register", Private: "timeline.visibilityTooltip.private", }; + +function generateTimelineHubUrl(token: string | null): string { + return `/api/hub/timeline${token == null ? "" : "?token=" + token}`; +} + +function createTimelineHubConnection(token: string | null): HubConnection { + return new HubConnectionBuilder() + .withUrl(generateTimelineHubUrl(token)) + .withAutomaticReconnect() + .build(); +} + +let timelineHubConnection: HubConnection | null = null; + +token$.subscribe((token) => { + if (timelineHubConnection != null) { + void timelineHubConnection.stop(); + } + timelineHubConnection = createTimelineHubConnection(token); + void timelineHubConnection.start(); +}); + +export function getTimelinePostUpdate( + timelineName: string +): Observable<string> { + return new Observable((subscriber) => { + if (timelineHubConnection == null) + throw new UiLogicError("Connection is null."); + + const connection = timelineHubConnection; + + const handler = (tn: string): void => { + if (timelineName === tn) { + subscriber.next(tn); + } + }; + connection.on("OnTimelinePostChanged", handler); + void connection.invoke("SubscribeTimelinePostChange", timelineName); + + return () => { + void connection.invoke("UnsubscribeTimelinePostChange", timelineName); + connection.off("OnTimelinePostChanged", handler); + }; + }); +} |