wirejs-deploy-amplify-basic 0.0.108-realtime → 0.0.109-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.
@@ -3,6 +3,6 @@
3
3
  "dependencies": {
4
4
  "jsdom": "^25.0.1",
5
5
  "wirejs-dom": "^1.0.41",
6
- "wirejs-resources": "^0.1.76-realtime"
6
+ "wirejs-resources": "^0.1.77-realtime"
7
7
  }
8
8
  }
@@ -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,11 @@ export function subscribe(url, channel, token, authHost, subscriber) {
69
77
  }
70
78
  }
71
79
  };
72
- ws.onclose = event => {
80
+ const notifyClosed = () => {
73
81
  const subscriptionIds = Array.from(channelSubs.keys())
74
82
  .filter(id => id.startsWith(`${url}#`));
75
83
  for (const subscriptionId of subscriptionIds) {
84
+ subscriptionState.set(subscriptionId, 'closed');
76
85
  const subs = subscribers.get(subscriptionId);
77
86
  if (subs) {
78
87
  for (const subscriber of subs) {
@@ -90,9 +99,12 @@ export function subscribe(url, channel, token, authHost, subscriber) {
90
99
  }
91
100
  console.log('closed', event);
92
101
  };
102
+ ws.onclose = () => notifyClosed();
103
+ ws.onerror = () => notifyClosed();
93
104
  }
94
- if (!channelSubs.has(channel)) {
105
+ if (!channelSubs.has(fullChannelName)) {
95
106
  const subscriptionId = crypto.randomUUID();
107
+ subscriptionState.set(subscriptionId, 'connecting');
96
108
  const ws = connections.get(url);
97
109
  const subscribe = () => {
98
110
  ws.send(JSON.stringify({
@@ -112,7 +124,14 @@ export function subscribe(url, channel, token, authHost, subscriber) {
112
124
  }
113
125
  }
114
126
  else {
115
- subscribers.get(fullChannelName).push(subscriber);
127
+ const subscriptionId = channelSubs.get(fullChannelName);
128
+ subscribers.get(subscriptionId).push(subscriber);
129
+ if (subscriptionState.get(subscriptionId) === 'open') {
130
+ subscriber.onopen?.();
131
+ }
132
+ else if (subscriptionState.get(subscriptionId) === 'closed') {
133
+ subscriber.onclose?.();
134
+ }
116
135
  }
117
136
  }
118
137
  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.108-realtime",
3
+ "version": "0.0.109-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.76-realtime"
44
+ "wirejs-resources": "^0.1.77-realtime"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@aws-amplify/backend": "^1.14.0",