arifa-client 1.0.0 → 1.0.1

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.
@@ -21,11 +21,12 @@ export declare class ArifaClient {
21
21
  private isConnected;
22
22
  private reconnectAttempts;
23
23
  private reconnectTimer;
24
- private subscriptions;
24
+ private recipient;
25
+ private listeners;
25
26
  private connectionListeners;
26
27
  constructor({ apiKey, client, wsUrl, apiEndpoint }: ArifaClientOptions);
27
28
  private safeParse;
28
- /** Subscribe to a recipient */
29
+ /** Subscribe once, add many listeners */
29
30
  subscribe(recipient: string): {
30
31
  listen: (callback: (event: ArifaEvent) => void) => void;
31
32
  unsubscribe: () => void;
@@ -7,7 +7,8 @@ export class ArifaClient {
7
7
  isConnected = false;
8
8
  reconnectAttempts = 0;
9
9
  reconnectTimer = null;
10
- subscriptions = {};
10
+ recipient = null;
11
+ listeners = [];
11
12
  connectionListeners = [];
12
13
  constructor({ apiKey, client, wsUrl, apiEndpoint }) {
13
14
  this.apiKey = apiKey;
@@ -28,18 +29,18 @@ export class ArifaClient {
28
29
  return null;
29
30
  }
30
31
  }
31
- /** Subscribe to a recipient */
32
+ /** Subscribe once, add many listeners */
32
33
  subscribe(recipient) {
33
- if (!this.subscriptions[recipient]) {
34
- this.subscriptions[recipient] = { recipient, listeners: [] };
34
+ if (!this.recipient) {
35
+ this.recipient = recipient;
36
+ this.connect();
35
37
  }
36
- this.connect(recipient);
37
38
  return {
38
39
  listen: (callback) => {
39
- this.subscriptions[recipient].listeners.push(callback);
40
+ this.listeners.push(callback);
40
41
  },
41
42
  unsubscribe: () => {
42
- delete this.subscriptions[recipient];
43
+ this.listeners = [];
43
44
  },
44
45
  };
45
46
  }
@@ -51,23 +52,22 @@ export class ArifaClient {
51
52
  this.connectionListeners.forEach((cb) => cb(state));
52
53
  }
53
54
  /** Connect WebSocket */
54
- connect(recipient) {
55
+ connect() {
56
+ if (!this.recipient)
57
+ return;
55
58
  if (this.ws && this.ws.readyState !== WebSocket.CLOSED)
56
59
  return;
57
- this.ws = new WebSocket(`${this.wsUrl}/connect?api_key=${this.apiKey}&recipient=${recipient}&client=${this.client}`);
60
+ this.ws = new WebSocket(`${this.wsUrl}/connect?api_key=${this.apiKey}&recipient=${this.recipient}&client=${this.client}`);
58
61
  this.ws.onopen = () => {
59
62
  this.isConnected = true;
60
63
  this.reconnectAttempts = 0;
61
64
  this.emitConnection("connected");
62
- console.log("ArifaClient connected");
63
65
  };
64
66
  this.ws.onmessage = (event) => {
65
67
  const parsed = this.safeParse(event.data);
66
- if (!parsed || !parsed.recipient)
68
+ if (!parsed)
67
69
  return;
68
- const sub = this.subscriptions[parsed.recipient];
69
- if (sub)
70
- sub.listeners.forEach((fn) => fn(parsed));
70
+ this.listeners.forEach((fn) => fn(parsed));
71
71
  };
72
72
  this.ws.onclose = (event) => {
73
73
  this.isConnected = false;
@@ -76,7 +76,7 @@ export class ArifaClient {
76
76
  return;
77
77
  const timeout = Math.min(30000, 2000 * 2 ** this.reconnectAttempts);
78
78
  this.reconnectAttempts++;
79
- this.reconnectTimer = window.setTimeout(() => this.connect(recipient), timeout);
79
+ this.reconnectTimer = window.setTimeout(() => this.connect(), timeout);
80
80
  };
81
81
  }
82
82
  /** Send notification */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arifa-client",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "JavaScript/TypeScript client SDK for Arifa Realtime Notification Service",
5
5
  "main": "dist/ArifaClient.js",
6
6
  "module": "dist/ArifaClient.js",