wirejs-deploy-amplify-basic 0.0.108-realtime → 0.0.110-realtime
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/client/realtime.js
CHANGED
|
@@ -14,6 +14,13 @@ const channelSubs = new Map();
|
|
|
14
14
|
* subscription ID -> subscriber
|
|
15
15
|
*/
|
|
16
16
|
const subscribers = new Map();
|
|
17
|
+
/**
|
|
18
|
+
* subcription ID -> connection state
|
|
19
|
+
*
|
|
20
|
+
* For when new subscribers are added, we'll want to broadcast the current
|
|
21
|
+
* state of the connection.
|
|
22
|
+
*/
|
|
23
|
+
const subscriptionState = new Map();
|
|
17
24
|
/**
|
|
18
25
|
* Encodes an object into Base64 URL format.
|
|
19
26
|
* From https://docs.aws.amazon.com/appsync/latest/eventapi/event-api-websocket-protocol.html#websocket-connection-handshake
|
|
@@ -59,6 +66,7 @@ export function subscribe(url, channel, token, authHost, subscriber) {
|
|
|
59
66
|
}
|
|
60
67
|
}
|
|
61
68
|
else if (data.type === 'subscribe_success') {
|
|
69
|
+
subscriptionState.set(data.id, 'open');
|
|
62
70
|
for (const subscriber of subscribers.get(data.id) || []) {
|
|
63
71
|
try {
|
|
64
72
|
subscriber.onopen?.();
|
|
@@ -69,10 +77,12 @@ export function subscribe(url, channel, token, authHost, subscriber) {
|
|
|
69
77
|
}
|
|
70
78
|
}
|
|
71
79
|
};
|
|
72
|
-
|
|
73
|
-
const subscriptionIds = Array.from(channelSubs.
|
|
74
|
-
.filter(
|
|
80
|
+
const notifyClosed = () => {
|
|
81
|
+
const subscriptionIds = Array.from(channelSubs.entries())
|
|
82
|
+
.filter(([urlChannel, _subId]) => urlChannel.startsWith(`${url}#`))
|
|
83
|
+
.map(([_urlChannel, subId]) => subId);
|
|
75
84
|
for (const subscriptionId of subscriptionIds) {
|
|
85
|
+
subscriptionState.set(subscriptionId, 'closed');
|
|
76
86
|
const subs = subscribers.get(subscriptionId);
|
|
77
87
|
if (subs) {
|
|
78
88
|
for (const subscriber of subs) {
|
|
@@ -90,9 +100,12 @@ export function subscribe(url, channel, token, authHost, subscriber) {
|
|
|
90
100
|
}
|
|
91
101
|
console.log('closed', event);
|
|
92
102
|
};
|
|
103
|
+
ws.onclose = () => notifyClosed();
|
|
104
|
+
ws.onerror = () => notifyClosed();
|
|
93
105
|
}
|
|
94
|
-
if (!channelSubs.has(
|
|
106
|
+
if (!channelSubs.has(fullChannelName)) {
|
|
95
107
|
const subscriptionId = crypto.randomUUID();
|
|
108
|
+
subscriptionState.set(subscriptionId, 'connecting');
|
|
96
109
|
const ws = connections.get(url);
|
|
97
110
|
const subscribe = () => {
|
|
98
111
|
ws.send(JSON.stringify({
|
|
@@ -112,7 +125,14 @@ export function subscribe(url, channel, token, authHost, subscriber) {
|
|
|
112
125
|
}
|
|
113
126
|
}
|
|
114
127
|
else {
|
|
115
|
-
|
|
128
|
+
const subscriptionId = channelSubs.get(fullChannelName);
|
|
129
|
+
subscribers.get(subscriptionId).push(subscriber);
|
|
130
|
+
if (subscriptionState.get(subscriptionId) === 'open') {
|
|
131
|
+
subscriber.onopen?.();
|
|
132
|
+
}
|
|
133
|
+
else if (subscriptionState.get(subscriptionId) === 'closed') {
|
|
134
|
+
subscriber.onclose?.();
|
|
135
|
+
}
|
|
116
136
|
}
|
|
117
137
|
}
|
|
118
138
|
export function unsubscribe(url, channel, subscriber) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wirejs-deploy-amplify-basic",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.110-realtime",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"recursive-copy": "^2.0.14",
|
|
42
42
|
"rimraf": "^6.0.1",
|
|
43
43
|
"wirejs-dom": "^1.0.41",
|
|
44
|
-
"wirejs-resources": "^0.1.
|
|
44
|
+
"wirejs-resources": "^0.1.78-realtime"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@aws-amplify/backend": "^1.14.0",
|