@auxilium/datalynk-client 0.8.1 → 0.8.2

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/index.cjs CHANGED
@@ -2176,9 +2176,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2176
2176
  class Socket {
2177
2177
  constructor(api, options = {}) {
2178
2178
  __publicField(this, "listeners", []);
2179
- __publicField(this, "reconnect", false);
2179
+ __publicField(this, "retry");
2180
2180
  __publicField(this, "socket");
2181
- __publicField(this, "_connected", false);
2181
+ __publicField(this, "open", false);
2182
2182
  this.api = api;
2183
2183
  this.options = options;
2184
2184
  const url = new URL(this.api.url.replace("http", "ws"));
@@ -2190,9 +2190,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2190
2190
  if (this.options.url !== false)
2191
2191
  api.token$.pipe(distinctUntilChanged()).subscribe(() => this.connect());
2192
2192
  }
2193
- get connected() {
2194
- return this._connected;
2195
- }
2196
2193
  /**
2197
2194
  * Add listener for all socket events
2198
2195
  *
@@ -2207,46 +2204,46 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2207
2204
  * Close socket connection
2208
2205
  */
2209
2206
  close() {
2210
- this._connected = false;
2211
- this.reconnect = false;
2212
- this.socket.close();
2207
+ var _a;
2208
+ if (this.open) console.debug("Datalynk socket: disconnected");
2209
+ this.open = false;
2210
+ (_a = this.socket) == null ? void 0 : _a.close();
2211
+ this.socket = void 0;
2212
+ if (this.retry) clearTimeout(this.retry);
2213
+ this.retry = null;
2213
2214
  }
2214
2215
  /**
2215
2216
  * Connect socket client to socket server
2216
- *
2217
- * @param {number} timeout Retry interval, defaults to 30s
2217
+ * @param {number} timeout Retry to connect every x seconds
2218
2218
  */
2219
- connect(timeout = 3e4) {
2220
- if (!this.options.url) throw new Error("Socket is disabled");
2221
- if (this.connected) {
2222
- this.reconnect = false;
2223
- this.socket.close();
2224
- }
2225
- this.socket = new WebSocket(this.options.url + (this.api.token ? `?token=${this.api.token}` : ""));
2226
- let t = setTimeout(() => {
2227
- if (this.reconnect && !this.connected) this.connect();
2228
- }, timeout);
2229
- this.socket.onmessage = (message) => {
2230
- const payload = JSON.parse(message.data);
2231
- if (payload.connected != void 0) {
2232
- if (payload.connected) {
2233
- clearTimeout(t);
2234
- t = null;
2235
- this._connected = true;
2236
- this.reconnect = true;
2237
- console.debug("Datalynk Socket: Connected");
2219
+ connect(timeout = 30) {
2220
+ if (!this.options.url) throw new Error("Datalynk socket disabled");
2221
+ if (this.open) this.close();
2222
+ this.retry = setTimeout(() => {
2223
+ if (this.open) return;
2224
+ this.close();
2225
+ this.connect();
2226
+ }, timeout * 1e3);
2227
+ if (navigator.onLine) {
2228
+ this.socket = new WebSocket(this.options.url + (this.api.token ? `?token=${this.api.token}` : ""));
2229
+ this.socket.onopen = () => clearTimeout(this.retry);
2230
+ this.socket.onclose = () => {
2231
+ if (this.open) this.connect(timeout);
2232
+ };
2233
+ this.socket.onmessage = (message) => {
2234
+ const payload = JSON.parse(message.data);
2235
+ if (payload.connected != void 0) {
2236
+ if (payload.connected) {
2237
+ this.open = true;
2238
+ console.debug("Datalynk socket: connected");
2239
+ } else {
2240
+ throw new Error(`Datalynk socket failed: ${payload.error}`);
2241
+ }
2238
2242
  } else {
2239
- throw new Error(`Socket failed to connect: ${payload.error}`);
2243
+ this.listeners.forEach((l) => l(payload));
2240
2244
  }
2241
- } else {
2242
- this.listeners.forEach((l) => l(payload));
2243
- }
2244
- };
2245
- this.socket.onclose = () => {
2246
- this._connected = false;
2247
- if (this.reconnect && !t) this.connect();
2248
- console.debug("Datalynk Socket: Disconnected");
2249
- };
2245
+ };
2246
+ }
2250
2247
  }
2251
2248
  /**
2252
2249
  * Send data to socket server
@@ -2254,7 +2251,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2254
2251
  * @param payload Data that will be serialized
2255
2252
  */
2256
2253
  send(payload) {
2257
- this.socket.send(JSON.stringify(payload));
2254
+ var _a;
2255
+ if (!this.open) throw new Error("Datalynk socket not connected");
2256
+ (_a = this.socket) == null ? void 0 : _a.send(JSON.stringify(payload));
2258
2257
  }
2259
2258
  /**
2260
2259
  * Run callback whenever the server notifies us of slice changes
@@ -2265,7 +2264,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2265
2264
  */
2266
2265
  sliceEvents(slice, callback) {
2267
2266
  let cancelled = false;
2268
- sleepWhile(() => !this.connected).then(() => {
2267
+ sleepWhile(() => !this.open).then(() => {
2269
2268
  if (!cancelled) this.send({ onSliceEvents: slice });
2270
2269
  });
2271
2270
  const unsubscribe = this.addListener((event) => {
package/dist/index.mjs CHANGED
@@ -2172,9 +2172,9 @@ class Slice {
2172
2172
  class Socket {
2173
2173
  constructor(api, options = {}) {
2174
2174
  __publicField(this, "listeners", []);
2175
- __publicField(this, "reconnect", false);
2175
+ __publicField(this, "retry");
2176
2176
  __publicField(this, "socket");
2177
- __publicField(this, "_connected", false);
2177
+ __publicField(this, "open", false);
2178
2178
  this.api = api;
2179
2179
  this.options = options;
2180
2180
  const url = new URL(this.api.url.replace("http", "ws"));
@@ -2186,9 +2186,6 @@ class Socket {
2186
2186
  if (this.options.url !== false)
2187
2187
  api.token$.pipe(distinctUntilChanged()).subscribe(() => this.connect());
2188
2188
  }
2189
- get connected() {
2190
- return this._connected;
2191
- }
2192
2189
  /**
2193
2190
  * Add listener for all socket events
2194
2191
  *
@@ -2203,46 +2200,46 @@ class Socket {
2203
2200
  * Close socket connection
2204
2201
  */
2205
2202
  close() {
2206
- this._connected = false;
2207
- this.reconnect = false;
2208
- this.socket.close();
2203
+ var _a;
2204
+ if (this.open) console.debug("Datalynk socket: disconnected");
2205
+ this.open = false;
2206
+ (_a = this.socket) == null ? void 0 : _a.close();
2207
+ this.socket = void 0;
2208
+ if (this.retry) clearTimeout(this.retry);
2209
+ this.retry = null;
2209
2210
  }
2210
2211
  /**
2211
2212
  * Connect socket client to socket server
2212
- *
2213
- * @param {number} timeout Retry interval, defaults to 30s
2213
+ * @param {number} timeout Retry to connect every x seconds
2214
2214
  */
2215
- connect(timeout = 3e4) {
2216
- if (!this.options.url) throw new Error("Socket is disabled");
2217
- if (this.connected) {
2218
- this.reconnect = false;
2219
- this.socket.close();
2220
- }
2221
- this.socket = new WebSocket(this.options.url + (this.api.token ? `?token=${this.api.token}` : ""));
2222
- let t = setTimeout(() => {
2223
- if (this.reconnect && !this.connected) this.connect();
2224
- }, timeout);
2225
- this.socket.onmessage = (message) => {
2226
- const payload = JSON.parse(message.data);
2227
- if (payload.connected != void 0) {
2228
- if (payload.connected) {
2229
- clearTimeout(t);
2230
- t = null;
2231
- this._connected = true;
2232
- this.reconnect = true;
2233
- console.debug("Datalynk Socket: Connected");
2215
+ connect(timeout = 30) {
2216
+ if (!this.options.url) throw new Error("Datalynk socket disabled");
2217
+ if (this.open) this.close();
2218
+ this.retry = setTimeout(() => {
2219
+ if (this.open) return;
2220
+ this.close();
2221
+ this.connect();
2222
+ }, timeout * 1e3);
2223
+ if (navigator.onLine) {
2224
+ this.socket = new WebSocket(this.options.url + (this.api.token ? `?token=${this.api.token}` : ""));
2225
+ this.socket.onopen = () => clearTimeout(this.retry);
2226
+ this.socket.onclose = () => {
2227
+ if (this.open) this.connect(timeout);
2228
+ };
2229
+ this.socket.onmessage = (message) => {
2230
+ const payload = JSON.parse(message.data);
2231
+ if (payload.connected != void 0) {
2232
+ if (payload.connected) {
2233
+ this.open = true;
2234
+ console.debug("Datalynk socket: connected");
2235
+ } else {
2236
+ throw new Error(`Datalynk socket failed: ${payload.error}`);
2237
+ }
2234
2238
  } else {
2235
- throw new Error(`Socket failed to connect: ${payload.error}`);
2239
+ this.listeners.forEach((l) => l(payload));
2236
2240
  }
2237
- } else {
2238
- this.listeners.forEach((l) => l(payload));
2239
- }
2240
- };
2241
- this.socket.onclose = () => {
2242
- this._connected = false;
2243
- if (this.reconnect && !t) this.connect();
2244
- console.debug("Datalynk Socket: Disconnected");
2245
- };
2241
+ };
2242
+ }
2246
2243
  }
2247
2244
  /**
2248
2245
  * Send data to socket server
@@ -2250,7 +2247,9 @@ class Socket {
2250
2247
  * @param payload Data that will be serialized
2251
2248
  */
2252
2249
  send(payload) {
2253
- this.socket.send(JSON.stringify(payload));
2250
+ var _a;
2251
+ if (!this.open) throw new Error("Datalynk socket not connected");
2252
+ (_a = this.socket) == null ? void 0 : _a.send(JSON.stringify(payload));
2254
2253
  }
2255
2254
  /**
2256
2255
  * Run callback whenever the server notifies us of slice changes
@@ -2261,7 +2260,7 @@ class Socket {
2261
2260
  */
2262
2261
  sliceEvents(slice, callback) {
2263
2262
  let cancelled = false;
2264
- sleepWhile(() => !this.connected).then(() => {
2263
+ sleepWhile(() => !this.open).then(() => {
2265
2264
  if (!cancelled) this.send({ onSliceEvents: slice });
2266
2265
  });
2267
2266
  const unsubscribe = this.addListener((event) => {
package/dist/socket.d.ts CHANGED
@@ -27,10 +27,9 @@ export declare class Socket {
27
27
  private readonly api;
28
28
  readonly options: SocketOptions;
29
29
  private listeners;
30
- private reconnect;
31
- private socket;
32
- private _connected;
33
- get connected(): boolean;
30
+ private retry?;
31
+ private socket?;
32
+ open: boolean;
34
33
  constructor(api: Api, options?: SocketOptions);
35
34
  /**
36
35
  * Add listener for all socket events
@@ -45,8 +44,7 @@ export declare class Socket {
45
44
  close(): void;
46
45
  /**
47
46
  * Connect socket client to socket server
48
- *
49
- * @param {number} timeout Retry interval, defaults to 30s
47
+ * @param {number} timeout Retry to connect every x seconds
50
48
  */
51
49
  connect(timeout?: number): void;
52
50
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"socket.d.ts","sourceRoot":"","sources":["../src/socket.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,GAAG,EAAC,MAAM,OAAO,CAAC;AAE1B,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;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AGG;IACH,OAAO,CAAC,OAAO,GAAE,MAAW;IA8B5B;;;;OAIG;IACH,IAAI,CAAC,OAAO,EAAE,GAAG;IAKjB;;;;;;OAMG;IACH,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,EAAE,EAAE,QAAQ,EAAE,cAAc,CAAC,gBAAgB,CAAC,GAAG,WAAW;CAmBlH"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@auxilium/datalynk-client",
3
3
  "description": "Datalynk client library",
4
4
  "repository": "https://gitlab.auxiliumgroup.com/auxilium/datalynk/datalynk-client",
5
- "version": "0.8.1",
5
+ "version": "0.8.2",
6
6
  "author": "Zak Timson <zaktimson@gmail.com>",
7
7
  "private": false,
8
8
  "main": "./dist/index.cjs",