wirejs-deploy-amplify-basic 0.0.107-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.75-realtime"
6
+ "wirejs-resources": "^0.1.77-realtime"
7
7
  }
8
8
  }
@@ -1,11 +1,8 @@
1
- export type Subscriber = {
2
- onmessage: (data: any) => void;
3
- onclose?: () => void;
4
- };
1
+ import type { MessageStreamSubscriber } from "wirejs-resources";
5
2
  export type ChannelEvent<T = any> = {
6
- type: "data" | "broadcast_error" | "ka";
3
+ type: "subscribe_success" | "data" | "broadcast_error" | "ka";
7
4
  id: string;
8
5
  event: any;
9
6
  };
10
- export declare function subscribe(url: string, channel: string, token: string, authHost: string, subscriber: Subscriber): void;
11
- export declare function unsubscribe(url: string, channel: string, subscriber: Subscriber): void;
7
+ export declare function subscribe(url: string, channel: string, token: string, authHost: string, subscriber: MessageStreamSubscriber): void;
8
+ export declare function unsubscribe(url: string, channel: string, subscriber: MessageStreamSubscriber): void;
@@ -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
@@ -58,11 +65,23 @@ export function subscribe(url, channel, token, authHost, subscriber) {
58
65
  }
59
66
  }
60
67
  }
68
+ else if (data.type === 'subscribe_success') {
69
+ subscriptionState.set(data.id, 'open');
70
+ for (const subscriber of subscribers.get(data.id) || []) {
71
+ try {
72
+ subscriber.onopen?.();
73
+ }
74
+ catch (error) {
75
+ console.error('Error in subscriber onopen:', error);
76
+ }
77
+ }
78
+ }
61
79
  };
62
- ws.onclose = event => {
80
+ const notifyClosed = () => {
63
81
  const subscriptionIds = Array.from(channelSubs.keys())
64
82
  .filter(id => id.startsWith(`${url}#`));
65
83
  for (const subscriptionId of subscriptionIds) {
84
+ subscriptionState.set(subscriptionId, 'closed');
66
85
  const subs = subscribers.get(subscriptionId);
67
86
  if (subs) {
68
87
  for (const subscriber of subs) {
@@ -80,9 +99,12 @@ export function subscribe(url, channel, token, authHost, subscriber) {
80
99
  }
81
100
  console.log('closed', event);
82
101
  };
102
+ ws.onclose = () => notifyClosed();
103
+ ws.onerror = () => notifyClosed();
83
104
  }
84
- if (!channelSubs.has(channel)) {
105
+ if (!channelSubs.has(fullChannelName)) {
85
106
  const subscriptionId = crypto.randomUUID();
107
+ subscriptionState.set(subscriptionId, 'connecting');
86
108
  const ws = connections.get(url);
87
109
  const subscribe = () => {
88
110
  ws.send(JSON.stringify({
@@ -102,7 +124,14 @@ export function subscribe(url, channel, token, authHost, subscriber) {
102
124
  }
103
125
  }
104
126
  else {
105
- 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
+ }
106
135
  }
107
136
  }
108
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.107-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.75-realtime"
44
+ "wirejs-resources": "^0.1.77-realtime"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@aws-amplify/backend": "^1.14.0",