@holo-js/broadcast 0.2.5 → 0.2.6
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/index.mjs +39 -0
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -199,6 +199,9 @@ function unsubscribeRealtimeSubscription(socket, id, subscription) {
|
|
|
199
199
|
logRealtimeSubscriptionCleanupError(socket.socketId, id, error);
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
|
+
function isRealtimeSubscriptionCurrent(socket, id, token) {
|
|
203
|
+
return socket.active && socket.realtimeSubscriptionTokens.get(id) === token;
|
|
204
|
+
}
|
|
202
205
|
function parseChannelKind(channel2) {
|
|
203
206
|
if (channel2.startsWith("private-")) {
|
|
204
207
|
return Object.freeze({
|
|
@@ -743,6 +746,9 @@ function createBroadcastWorkerRuntime(options) {
|
|
|
743
746
|
);
|
|
744
747
|
}
|
|
745
748
|
function sendRealtimeMessage(socket, event, data) {
|
|
749
|
+
if (!socket.active) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
746
752
|
socket.send(pusherEvent(event, data));
|
|
747
753
|
}
|
|
748
754
|
function resolveRealtimeErrorStatus(error) {
|
|
@@ -812,6 +818,7 @@ function createBroadcastWorkerRuntime(options) {
|
|
|
812
818
|
}
|
|
813
819
|
if (request.action === "unsubscribe") {
|
|
814
820
|
const subscription = socket.realtimeSubscriptions.get(request.id);
|
|
821
|
+
socket.realtimeSubscriptionTokens.delete(request.id);
|
|
815
822
|
if (subscription) {
|
|
816
823
|
unsubscribeRealtimeSubscription(socket, request.id, subscription);
|
|
817
824
|
}
|
|
@@ -822,6 +829,7 @@ function createBroadcastWorkerRuntime(options) {
|
|
|
822
829
|
return;
|
|
823
830
|
}
|
|
824
831
|
const context = createRealtimeExecutionContext(socket);
|
|
832
|
+
let pendingSubscriptionToken;
|
|
825
833
|
try {
|
|
826
834
|
if (request.action === "query") {
|
|
827
835
|
const result = await realtime.query(request.name, request.args, context);
|
|
@@ -845,24 +853,53 @@ function createBroadcastWorkerRuntime(options) {
|
|
|
845
853
|
return;
|
|
846
854
|
}
|
|
847
855
|
const previousSubscription = socket.realtimeSubscriptions.get(request.id);
|
|
856
|
+
socket.realtimeSubscriptionTokens.delete(request.id);
|
|
848
857
|
if (previousSubscription) {
|
|
849
858
|
unsubscribeRealtimeSubscription(socket, request.id, previousSubscription);
|
|
850
859
|
socket.realtimeSubscriptions.delete(request.id);
|
|
851
860
|
}
|
|
861
|
+
const subscriptionToken = {};
|
|
862
|
+
pendingSubscriptionToken = subscriptionToken;
|
|
863
|
+
socket.realtimeSubscriptionTokens.set(request.id, subscriptionToken);
|
|
852
864
|
const subscription = await realtime.subscribe(request.name, request.args, {
|
|
853
865
|
context,
|
|
854
866
|
onData(snapshot) {
|
|
867
|
+
if (!isRealtimeSubscriptionCurrent(socket, request.id, subscriptionToken)) {
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
855
870
|
sendRealtimeMessage(socket, "holo:realtime:snapshot", {
|
|
856
871
|
id: request.id,
|
|
857
872
|
snapshot
|
|
858
873
|
});
|
|
859
874
|
},
|
|
875
|
+
onPatch(patch) {
|
|
876
|
+
if (!isRealtimeSubscriptionCurrent(socket, request.id, subscriptionToken)) {
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
sendRealtimeMessage(socket, "holo:realtime:patch", {
|
|
880
|
+
id: request.id,
|
|
881
|
+
patch
|
|
882
|
+
});
|
|
883
|
+
},
|
|
860
884
|
onError(error) {
|
|
885
|
+
if (!isRealtimeSubscriptionCurrent(socket, request.id, subscriptionToken)) {
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
861
888
|
sendRealtimeError(socket, request.id, error);
|
|
862
889
|
}
|
|
863
890
|
});
|
|
891
|
+
if (!isRealtimeSubscriptionCurrent(socket, request.id, subscriptionToken)) {
|
|
892
|
+
unsubscribeRealtimeSubscription(socket, request.id, subscription);
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
864
895
|
socket.realtimeSubscriptions.set(request.id, subscription);
|
|
865
896
|
} catch (error) {
|
|
897
|
+
if (pendingSubscriptionToken) {
|
|
898
|
+
if (!isRealtimeSubscriptionCurrent(socket, request.id, pendingSubscriptionToken)) {
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
socket.realtimeSubscriptionTokens.delete(request.id);
|
|
902
|
+
}
|
|
866
903
|
sendRealtimeError(socket, request.id, error);
|
|
867
904
|
}
|
|
868
905
|
}
|
|
@@ -1203,6 +1240,7 @@ function createBroadcastWorkerRuntime(options) {
|
|
|
1203
1240
|
close: connection.close,
|
|
1204
1241
|
subscribedChannels: /* @__PURE__ */ new Set(),
|
|
1205
1242
|
realtimeSubscriptions: /* @__PURE__ */ new Map(),
|
|
1243
|
+
realtimeSubscriptionTokens: /* @__PURE__ */ new Map(),
|
|
1206
1244
|
active: true,
|
|
1207
1245
|
pendingMessage: Promise.resolve()
|
|
1208
1246
|
});
|
|
@@ -1259,6 +1297,7 @@ function createBroadcastWorkerRuntime(options) {
|
|
|
1259
1297
|
unsubscribeRealtimeSubscription(socket, subscriptionId, subscription);
|
|
1260
1298
|
}
|
|
1261
1299
|
socket.realtimeSubscriptions.clear();
|
|
1300
|
+
socket.realtimeSubscriptionTokens.clear();
|
|
1262
1301
|
const channelsToCleanup = [...socket.subscribedChannels];
|
|
1263
1302
|
const scalingCleanupTasks = channelsToCleanup.map((channel2) => {
|
|
1264
1303
|
const removedPresenceMember = removeSubscriptionLocal(socket.app.appId, socket.socketId, channel2);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holo-js/broadcast",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Holo-JS Framework - broadcast contracts, channel definitions, and driver registration seams",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"test": "vitest --run"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@holo-js/config": "^0.2.
|
|
47
|
-
"@holo-js/validation": "^0.2.
|
|
46
|
+
"@holo-js/config": "^0.2.6",
|
|
47
|
+
"@holo-js/validation": "^0.2.6",
|
|
48
48
|
"ws": "^8.18.3"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|