aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/services
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
commit04737cd734eda58eca5da59d5a6a35c0dc1e9364 (patch)
tree0a1ba6761e1f59255a34e22782f1af7ad4c92c02 /FrontEnd/src/app/services
parent37fa764e9f23e058766bd444ce4e2e3806a1dcc2 (diff)
downloadtimeline-04737cd734eda58eca5da59d5a6a35c0dc1e9364.tar.gz
timeline-04737cd734eda58eca5da59d5a6a35c0dc1e9364.tar.bz2
timeline-04737cd734eda58eca5da59d5a6a35c0dc1e9364.zip
fix: Enhance logic of signalr.
Diffstat (limited to 'FrontEnd/src/app/services')
-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());
};
});
}