adminforth 2.70.0 → 2.71.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/spa/src/websocket.ts +44 -6
- package/package.json +1 -1
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
type WebsocketCallback = (data: any) => void;
|
|
3
|
+
type Unsubscribe = () => void;
|
|
4
|
+
type Subscription = {
|
|
5
|
+
id: number;
|
|
6
|
+
callback: WebsocketCallback;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const subscriptions: { [topic: string]: Subscription[] } = {};
|
|
10
|
+
let nextSubscriptionId = 1;
|
|
3
11
|
|
|
4
12
|
interface ExtendedWebSocket extends WebSocket {
|
|
5
13
|
connected?: boolean;
|
|
@@ -65,8 +73,8 @@ async function connect () {
|
|
|
65
73
|
const topic = message.topic;
|
|
66
74
|
const data = message.data;
|
|
67
75
|
if (subscriptions[topic]) {
|
|
68
|
-
for (const
|
|
69
|
-
callback(data);
|
|
76
|
+
for (const subscription of subscriptions[topic]) {
|
|
77
|
+
subscription.callback(data);
|
|
70
78
|
}
|
|
71
79
|
}
|
|
72
80
|
}
|
|
@@ -104,19 +112,49 @@ setInterval(() => {
|
|
|
104
112
|
}, 10_000);
|
|
105
113
|
|
|
106
114
|
|
|
115
|
+
function unsubscribeSubscription(topic: string, subscriptionId: number): void {
|
|
116
|
+
if (!subscriptions[topic]) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
subscriptions[topic] = subscriptions[topic].filter((subscription) => subscription.id !== subscriptionId);
|
|
121
|
+
if (subscriptions[topic].length > 0) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
delete subscriptions[topic];
|
|
126
|
+
if (state.status === 'connected') {
|
|
127
|
+
doPhysicalUnsubscribe(topic);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
107
131
|
export default {
|
|
108
|
-
subscribe(topic: string, callback:
|
|
132
|
+
subscribe(topic: string, callback: WebsocketCallback): Unsubscribe {
|
|
109
133
|
const isFirstSubscription = !subscriptions[topic];
|
|
110
134
|
if (!subscriptions[topic]) {
|
|
111
135
|
subscriptions[topic] = [];
|
|
112
136
|
}
|
|
113
|
-
|
|
137
|
+
const subscriptionId = nextSubscriptionId++;
|
|
138
|
+
subscriptions[topic].push({
|
|
139
|
+
id: subscriptionId,
|
|
140
|
+
callback,
|
|
141
|
+
});
|
|
114
142
|
if (isFirstSubscription && state.status === 'connected') {
|
|
115
143
|
doPhysicalSubscribe(topic);
|
|
116
144
|
}
|
|
145
|
+
|
|
146
|
+
let isSubscribed = true;
|
|
147
|
+
return () => {
|
|
148
|
+
if (!isSubscribed) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
isSubscribed = false;
|
|
152
|
+
unsubscribeSubscription(topic, subscriptionId);
|
|
153
|
+
};
|
|
117
154
|
},
|
|
118
155
|
|
|
119
156
|
unsubscribe(topic: string): void {
|
|
157
|
+
console.warn('This method is deprecated because it removes all subscriptions for the topic. Use the unsubscribe function returned by subscribe(), or use unsubscribeByPrefix() when you explicitly need to unsubscribe by topic prefix.');
|
|
120
158
|
if (!subscriptions[topic]) {
|
|
121
159
|
return;
|
|
122
160
|
}
|
|
@@ -146,4 +184,4 @@ export default {
|
|
|
146
184
|
});
|
|
147
185
|
}
|
|
148
186
|
|
|
149
|
-
}
|
|
187
|
+
}
|