aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/services/timeline.ts
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-05-16 19:56:05 +0800
committercrupest <crupest@outlook.com>2021-05-16 19:56:05 +0800
commit07afe9dea19920a95391e5aa2f356264ee563916 (patch)
tree338722e85db5add8cc92ed0c77c9f971de4df685 /FrontEnd/src/app/services/timeline.ts
parent02bf6c7e92fbd18d8409f8c6b84717aaf4d5664b (diff)
downloadtimeline-07afe9dea19920a95391e5aa2f356264ee563916.tar.gz
timeline-07afe9dea19920a95391e5aa2f356264ee563916.tar.bz2
timeline-07afe9dea19920a95391e5aa2f356264ee563916.zip
fix: Enhance logic of signalr.
Diffstat (limited to 'FrontEnd/src/app/services/timeline.ts')
-rw-r--r--FrontEnd/src/app/services/timeline.ts51
1 files changed, 21 insertions, 30 deletions
diff --git a/FrontEnd/src/app/services/timeline.ts b/FrontEnd/src/app/services/timeline.ts
index ff9662ad..2dbd3e4d 100644
--- a/FrontEnd/src/app/services/timeline.ts
+++ b/FrontEnd/src/app/services/timeline.ts
@@ -1,11 +1,9 @@
import { TimelineVisibility } from "@/http/timeline";
import XRegExp from "xregexp";
import { Observable } from "rxjs";
-import { HubConnection, HubConnectionBuilder } from "@microsoft/signalr";
+import { HubConnectionBuilder } from "@microsoft/signalr";
-import { UiLogicError } from "@/common";
-
-import { token$ } from "@/http/common";
+import { getHttpToken } from "@/http/common";
const timelineNameReg = XRegExp("^[-_\\p{L}]*$", "u");
@@ -22,45 +20,38 @@ export const timelineVisibilityTooltipTranslationMap: Record<
Private: "timeline.visibilityTooltip.private",
};
-function createTimelineHubConnection(token: string | null): HubConnection {
- return new HubConnectionBuilder()
- .withUrl("/api/hub/timeline", {
- accessTokenFactory: token == null ? undefined : () => 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(
+export function getTimelinePostUpdate$(
timelineName: string
): Observable<string> {
return new Observable((subscriber) => {
- if (timelineHubConnection == null)
- throw new UiLogicError("Connection is null.");
-
- const connection = timelineHubConnection;
+ const token = getHttpToken();
+ const connection = new HubConnectionBuilder()
+ .withUrl("/api/hub/timeline", {
+ accessTokenFactory: token == null ? undefined : () => token,
+ })
+ .withAutomaticReconnect()
+ .build();
const handler = (tn: string): void => {
if (timelineName === tn) {
subscriber.next(tn);
}
};
+
connection.on("OnTimelinePostChanged", handler);
- void connection.invoke("SubscribeTimelinePostChange", timelineName);
+
+ void connection
+ .start()
+ .then(() =>
+ connection.invoke("SubscribeTimelinePostChange", timelineName)
+ );
return () => {
- void connection.invoke("UnsubscribeTimelinePostChange", timelineName);
connection.off("OnTimelinePostChanged", handler);
+
+ void connection
+ .invoke("UnsubscribeTimelinePostChange", timelineName)
+ .then(() => connection.stop());
};
});
}