quidproquo-web 0.0.205 → 0.0.207

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.
@@ -12,7 +12,7 @@ type WebsocketSendPayload = string | ArrayBufferLike | Blob | ArrayBufferView;
12
12
  export type WebSocketServiceSubscriptionFunction = (websocketService: WebsocketService, event?: Event) => any;
13
13
  export type WebSocketServiceEventSubscriptionFunction<E extends AnyEventMessage> = (websocketService: WebsocketService, event: E) => void;
14
14
  export declare class WebsocketService {
15
- private url;
15
+ readonly url: string;
16
16
  private socket;
17
17
  private eventListeners;
18
18
  private isDestroyed;
@@ -22,9 +22,11 @@ export declare class WebsocketService {
22
22
  destroy(): void;
23
23
  close(): void;
24
24
  isConnected(): boolean;
25
+ hasBeenDestroyed(): boolean;
26
+ getSocket(): WebSocket | null;
25
27
  subscribe(subscriptionType: WebsocketServiceEvent, callback: WebSocketServiceSubscriptionFunction): SubscriptionHandle;
26
28
  subscribeToEvent<E extends AnyEventMessage>(subscriptionType: E['type'], callback: WebSocketServiceEventSubscriptionFunction<E>): SubscriptionHandle;
27
- unsubscribe(subscriptionHandle: SubscriptionHandle): boolean;
29
+ unsubscribe(subscriptionHandle: SubscriptionHandle): void;
28
30
  unsubscribeAll(): void;
29
31
  private connect;
30
32
  private addEventListener;
@@ -37,6 +37,12 @@ class WebsocketService {
37
37
  var _a;
38
38
  return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN;
39
39
  }
40
+ hasBeenDestroyed() {
41
+ return this.isDestroyed;
42
+ }
43
+ getSocket() {
44
+ return this.socket;
45
+ }
40
46
  subscribe(subscriptionType, callback) {
41
47
  const subscriptionHandle = {
42
48
  type: subscriptionType,
@@ -49,19 +55,19 @@ class WebsocketService {
49
55
  if (event) {
50
56
  const data = event.data;
51
57
  try {
52
- const event = JSON.parse(data);
53
- if (event.type === subscriptionType) {
54
- callback(websocketService, event);
58
+ const parsedEvent = JSON.parse(data);
59
+ if (parsedEvent.type === subscriptionType) {
60
+ callback(websocketService, parsedEvent);
55
61
  }
56
62
  }
57
63
  catch (e) {
58
- // Must of been some other message format / type
64
+ // Must have been some other message format / type do nothing.
59
65
  }
60
66
  }
61
67
  });
62
68
  }
63
69
  unsubscribe(subscriptionHandle) {
64
- return this.subscriptions[subscriptionHandle.type].delete(subscriptionHandle);
70
+ this.subscriptions[subscriptionHandle.type].delete(subscriptionHandle);
65
71
  }
66
72
  unsubscribeAll() {
67
73
  for (const subscriptionType in this.subscriptions) {
@@ -73,10 +79,10 @@ class WebsocketService {
73
79
  connect() {
74
80
  this.removeAllEventListeners();
75
81
  this.socket = new WebSocket(this.url);
76
- this.addEventListener('open', this.onConnect.bind(this));
77
- this.addEventListener('close', this.onClose.bind(this));
78
- this.addEventListener('message', this.onMessage.bind(this));
79
- this.addEventListener('error', this.onError.bind(this));
82
+ this.addEventListener(WebsocketServiceEvent.OPEN, this.onConnect.bind(this));
83
+ this.addEventListener(WebsocketServiceEvent.CLOSE, this.onClose.bind(this));
84
+ this.addEventListener(WebsocketServiceEvent.MESSAGE, this.onMessage.bind(this));
85
+ this.addEventListener(WebsocketServiceEvent.ERROR, this.onError.bind(this));
80
86
  }
81
87
  addEventListener(event, listener) {
82
88
  if (this.socket) {
@@ -97,7 +103,6 @@ class WebsocketService {
97
103
  }
98
104
  }
99
105
  }
100
- // Clear the eventListeners object
101
106
  this.eventListeners = {};
102
107
  }
103
108
  reconnectIfNotDestroyed() {
@@ -108,8 +113,7 @@ class WebsocketService {
108
113
  }, 1000);
109
114
  }
110
115
  onConnect() {
111
- this.notifySubscribers(this.subscriptions.open);
112
- // Send any pending messages once we are connected
116
+ this.notifySubscribers(WebsocketServiceEvent.OPEN);
113
117
  const messages = this.pendingMessages;
114
118
  this.pendingMessages = [];
115
119
  messages.forEach((message) => {
@@ -119,21 +123,22 @@ class WebsocketService {
119
123
  onClose() {
120
124
  this.removeAllEventListeners();
121
125
  this.reconnectIfNotDestroyed();
122
- this.notifySubscribers(this.subscriptions.close);
126
+ this.notifySubscribers(WebsocketServiceEvent.CLOSE);
123
127
  }
124
128
  onMessage(event) {
125
- this.notifySubscribers(this.subscriptions.message, event);
129
+ this.notifySubscribers(WebsocketServiceEvent.MESSAGE, event);
126
130
  }
127
131
  onError(event) {
128
- this.notifySubscribers(this.subscriptions.error, event);
132
+ this.notifySubscribers(WebsocketServiceEvent.ERROR, event);
129
133
  }
130
- notifySubscribers(subscibers, event) {
131
- subscibers.forEach((callback) => {
134
+ notifySubscribers(subType, event) {
135
+ this.subscriptions[subType].forEach((callback) => {
132
136
  callback(this, event);
133
137
  });
134
138
  }
135
139
  send(data) {
136
- if (this.socket && this.socket.readyState === WebSocket.OPEN) {
140
+ var _a;
141
+ if (((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
137
142
  this.socket.send(data);
138
143
  }
139
144
  else {
@@ -12,7 +12,7 @@ type WebsocketSendPayload = string | ArrayBufferLike | Blob | ArrayBufferView;
12
12
  export type WebSocketServiceSubscriptionFunction = (websocketService: WebsocketService, event?: Event) => any;
13
13
  export type WebSocketServiceEventSubscriptionFunction<E extends AnyEventMessage> = (websocketService: WebsocketService, event: E) => void;
14
14
  export declare class WebsocketService {
15
- private url;
15
+ readonly url: string;
16
16
  private socket;
17
17
  private eventListeners;
18
18
  private isDestroyed;
@@ -22,9 +22,11 @@ export declare class WebsocketService {
22
22
  destroy(): void;
23
23
  close(): void;
24
24
  isConnected(): boolean;
25
+ hasBeenDestroyed(): boolean;
26
+ getSocket(): WebSocket | null;
25
27
  subscribe(subscriptionType: WebsocketServiceEvent, callback: WebSocketServiceSubscriptionFunction): SubscriptionHandle;
26
28
  subscribeToEvent<E extends AnyEventMessage>(subscriptionType: E['type'], callback: WebSocketServiceEventSubscriptionFunction<E>): SubscriptionHandle;
27
- unsubscribe(subscriptionHandle: SubscriptionHandle): boolean;
29
+ unsubscribe(subscriptionHandle: SubscriptionHandle): void;
28
30
  unsubscribeAll(): void;
29
31
  private connect;
30
32
  private addEventListener;
@@ -32,6 +32,12 @@ export class WebsocketService {
32
32
  isConnected() {
33
33
  return this.socket?.readyState === WebSocket.OPEN;
34
34
  }
35
+ hasBeenDestroyed() {
36
+ return this.isDestroyed;
37
+ }
38
+ getSocket() {
39
+ return this.socket;
40
+ }
35
41
  subscribe(subscriptionType, callback) {
36
42
  const subscriptionHandle = {
37
43
  type: subscriptionType,
@@ -44,19 +50,19 @@ export class WebsocketService {
44
50
  if (event) {
45
51
  const data = event.data;
46
52
  try {
47
- const event = JSON.parse(data);
48
- if (event.type === subscriptionType) {
49
- callback(websocketService, event);
53
+ const parsedEvent = JSON.parse(data);
54
+ if (parsedEvent.type === subscriptionType) {
55
+ callback(websocketService, parsedEvent);
50
56
  }
51
57
  }
52
58
  catch (e) {
53
- // Must of been some other message format / type
59
+ // Must have been some other message format / type do nothing.
54
60
  }
55
61
  }
56
62
  });
57
63
  }
58
64
  unsubscribe(subscriptionHandle) {
59
- return this.subscriptions[subscriptionHandle.type].delete(subscriptionHandle);
65
+ this.subscriptions[subscriptionHandle.type].delete(subscriptionHandle);
60
66
  }
61
67
  unsubscribeAll() {
62
68
  for (const subscriptionType in this.subscriptions) {
@@ -68,10 +74,10 @@ export class WebsocketService {
68
74
  connect() {
69
75
  this.removeAllEventListeners();
70
76
  this.socket = new WebSocket(this.url);
71
- this.addEventListener('open', this.onConnect.bind(this));
72
- this.addEventListener('close', this.onClose.bind(this));
73
- this.addEventListener('message', this.onMessage.bind(this));
74
- this.addEventListener('error', this.onError.bind(this));
77
+ this.addEventListener(WebsocketServiceEvent.OPEN, this.onConnect.bind(this));
78
+ this.addEventListener(WebsocketServiceEvent.CLOSE, this.onClose.bind(this));
79
+ this.addEventListener(WebsocketServiceEvent.MESSAGE, this.onMessage.bind(this));
80
+ this.addEventListener(WebsocketServiceEvent.ERROR, this.onError.bind(this));
75
81
  }
76
82
  addEventListener(event, listener) {
77
83
  if (this.socket) {
@@ -92,7 +98,6 @@ export class WebsocketService {
92
98
  }
93
99
  }
94
100
  }
95
- // Clear the eventListeners object
96
101
  this.eventListeners = {};
97
102
  }
98
103
  reconnectIfNotDestroyed() {
@@ -103,8 +108,7 @@ export class WebsocketService {
103
108
  }, 1000);
104
109
  }
105
110
  onConnect() {
106
- this.notifySubscribers(this.subscriptions.open);
107
- // Send any pending messages once we are connected
111
+ this.notifySubscribers(WebsocketServiceEvent.OPEN);
108
112
  const messages = this.pendingMessages;
109
113
  this.pendingMessages = [];
110
114
  messages.forEach((message) => {
@@ -114,21 +118,21 @@ export class WebsocketService {
114
118
  onClose() {
115
119
  this.removeAllEventListeners();
116
120
  this.reconnectIfNotDestroyed();
117
- this.notifySubscribers(this.subscriptions.close);
121
+ this.notifySubscribers(WebsocketServiceEvent.CLOSE);
118
122
  }
119
123
  onMessage(event) {
120
- this.notifySubscribers(this.subscriptions.message, event);
124
+ this.notifySubscribers(WebsocketServiceEvent.MESSAGE, event);
121
125
  }
122
126
  onError(event) {
123
- this.notifySubscribers(this.subscriptions.error, event);
127
+ this.notifySubscribers(WebsocketServiceEvent.ERROR, event);
124
128
  }
125
- notifySubscribers(subscibers, event) {
126
- subscibers.forEach((callback) => {
129
+ notifySubscribers(subType, event) {
130
+ this.subscriptions[subType].forEach((callback) => {
127
131
  callback(this, event);
128
132
  });
129
133
  }
130
134
  send(data) {
131
- if (this.socket && this.socket.readyState === WebSocket.OPEN) {
135
+ if (this.socket?.readyState === WebSocket.OPEN) {
132
136
  this.socket.send(data);
133
137
  }
134
138
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quidproquo-web",
3
- "version": "0.0.205",
3
+ "version": "0.0.207",
4
4
  "description": "",
5
5
  "main": "./lib/commonjs/index.js",
6
6
  "module": "./lib/esm/index.js",
@@ -29,9 +29,9 @@
29
29
  },
30
30
  "homepage": "https://github.com/joe-coady/quidproquo#readme",
31
31
  "devDependencies": {
32
- "quidproquo-core": "0.0.205",
33
- "quidproquo-tsconfig": "0.0.205",
34
- "quidproquo-webserver": "0.0.205",
32
+ "quidproquo-core": "0.0.207",
33
+ "quidproquo-tsconfig": "0.0.207",
34
+ "quidproquo-webserver": "0.0.207",
35
35
  "typescript": "^4.9.3"
36
36
  }
37
37
  }