reachlo 1.1.0 → 1.2.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.
Files changed (2) hide show
  1. package/index.js +23 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -6,6 +6,7 @@ class Reachlo {
6
6
  this.url = options.url || 'wss://friendly-octo-barnacle.fly.dev';
7
7
  this.socket = null;
8
8
  this.channels = new Map();
9
+ this.pendingSubs = new Set();
9
10
  }
10
11
 
11
12
  connect() {
@@ -15,6 +16,11 @@ class Reachlo {
15
16
 
16
17
  this.socket.onopen = () => {
17
18
  console.log("Reachlo: Connected");
19
+ // Flush pending subscriptions
20
+ this.pendingSubs.forEach(channel =>
21
+ this._send({ type: 'subscribe', channel })
22
+ );
23
+ this.pendingSubs.clear();
18
24
  resolve();
19
25
  };
20
26
 
@@ -34,18 +40,32 @@ class Reachlo {
34
40
  });
35
41
  }
36
42
 
43
+ disconnect() {
44
+ if (this.socket) {
45
+ this.socket.close();
46
+ this.socket = null;
47
+ }
48
+ this.channels.clear();
49
+ this.pendingSubs.clear();
50
+ }
51
+
37
52
  channel(channelName) {
38
53
  if (!this.channels.has(channelName)) {
39
54
  const channel = new ReachloChannel(channelName, this);
40
55
  this.channels.set(channelName, channel);
41
- this._send({ type: 'subscribe', channel: channelName });
56
+
57
+ if (this.socket?.readyState === 1) { // WebSocket.OPEN is 1
58
+ this._send({ type: 'subscribe', channel: channelName });
59
+ } else {
60
+ this.pendingSubs.add(channelName);
61
+ }
42
62
  return channel;
43
63
  }
44
64
  return this.channels.get(channelName);
45
65
  }
46
66
 
47
67
  _send(payload) {
48
- if (this.socket && this.socket.readyState === WebSocket.OPEN) {
68
+ if (this.socket && this.socket.readyState === 1) { // WebSocket.OPEN
49
69
  this.socket.send(JSON.stringify(payload));
50
70
  }
51
71
  }
@@ -60,6 +80,7 @@ class ReachloChannel {
60
80
 
61
81
  subscribe(cb) {
62
82
  this.callbacks.add(cb);
83
+ return () => this.callbacks.delete(cb); // unsubscribe handle
63
84
  }
64
85
 
65
86
  publish(data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reachlo",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Real-time infrastructure for AI streaming.",
5
5
  "main": "index.js",
6
6
  "type": "module",