@xelis/sdk 0.5.3 → 0.5.4

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/lib/websocket.js +17 -6
  2. package/package.json +1 -1
package/lib/websocket.js CHANGED
@@ -23,6 +23,7 @@ export class WS {
23
23
  if (this.socket && this.socket.readyState === WebSocket.OPEN) {
24
24
  this.socket.close();
25
25
  }
26
+ this.events = {};
26
27
  this.connectionTries = 0;
27
28
  return new Promise((resolve, reject) => {
28
29
  this.socket = new WebSocket(endpoint);
@@ -33,7 +34,7 @@ export class WS {
33
34
  this.socket.addEventListener(`close`, (event) => {
34
35
  if (this.reconnectOnConnectionLoss && !event.wasClean) {
35
36
  this.tryReconnect();
36
- reject(new Error(`Reconnecting.`));
37
+ reject(new Error(`Unhandled close. Reconnecting...`));
37
38
  }
38
39
  else {
39
40
  reject(event);
@@ -50,6 +51,9 @@ export class WS {
50
51
  return;
51
52
  }
52
53
  this.socket = new WebSocket(this.endpoint);
54
+ this.socket.addEventListener(`open`, () => {
55
+ this.connectionTries = 0;
56
+ });
53
57
  this.socket.addEventListener(`close`, (event) => {
54
58
  this.tryReconnect();
55
59
  });
@@ -101,7 +105,7 @@ export class WS {
101
105
  this.events[event].listeners.push(onMessage);
102
106
  }
103
107
  else {
104
- // important if multiple listenEvent are called without await atleast we store listener before getting id
108
+ // important if multiple listenEvent are called without await at least we store listener before getting id
105
109
  this.events[event] = { listeners: [onMessage] };
106
110
  const [err, res] = await to(this.call(`subscribe`, { notify: event }));
107
111
  if (err) {
@@ -121,12 +125,19 @@ export class WS {
121
125
  break;
122
126
  }
123
127
  }
128
+ // no more listener so we unsubscribe from daemon websocket if socket still open
124
129
  if (listeners.length === 0) {
125
- this.events[event].unsubscribeTimeoutId = setTimeout(async () => {
126
- // no more listener so we unsubscribe from daemon websocket
127
- this.call(`unsubscribe`, { notify: event });
130
+ if (this.socket && this.socket.readyState === WebSocket.OPEN) {
131
+ // we use a grace period to unsubscribe (mostly because of react useEffect and avoid unecessary subscribe)
132
+ this.events[event].unsubscribeTimeoutId = setTimeout(async () => {
133
+ this.call(`unsubscribe`, { notify: event });
134
+ Reflect.deleteProperty(this.events, event);
135
+ }, this.unsubscribeSuspense);
136
+ }
137
+ else {
138
+ // socket is closed so we don't send unsubscribe and no grace period delete right away
128
139
  Reflect.deleteProperty(this.events, event);
129
- }, this.unsubscribeSuspense);
140
+ }
130
141
  }
131
142
  }
132
143
  this.socket && this.socket.removeEventListener(`message`, onMessage);
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.5.3",
2
+ "version": "0.5.4",
3
3
  "name": "@xelis/sdk",
4
4
  "description": "Xelis software development kit for JS",
5
5
  "repository": {