@eleven-am/pondsocket-client 0.0.16 → 0.0.17
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/browser/client.js +10 -3
- package/browser/client.test.js +3 -3
- package/package.json +1 -1
package/browser/client.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _PondClient_instances, _PondClient_channels, _PondClient_createPublisher, _PondClient_handleAcknowledge, _PondClient_init;
|
|
13
|
+
var _PondClient_instances, _PondClient_channels, _PondClient_disconnecting, _PondClient_createPublisher, _PondClient_handleAcknowledge, _PondClient_init;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
16
16
|
const channel_1 = require("../core/channel");
|
|
@@ -18,6 +18,7 @@ class PondClient {
|
|
|
18
18
|
constructor(endpoint, params = {}) {
|
|
19
19
|
_PondClient_instances.add(this);
|
|
20
20
|
_PondClient_channels.set(this, void 0);
|
|
21
|
+
_PondClient_disconnecting.set(this, void 0);
|
|
21
22
|
let address;
|
|
22
23
|
try {
|
|
23
24
|
address = new URL(endpoint);
|
|
@@ -26,6 +27,7 @@ class PondClient {
|
|
|
26
27
|
address = new URL(window.location.toString());
|
|
27
28
|
address.pathname = endpoint;
|
|
28
29
|
}
|
|
30
|
+
__classPrivateFieldSet(this, _PondClient_disconnecting, false, "f");
|
|
29
31
|
const query = new URLSearchParams(params);
|
|
30
32
|
address.search = query.toString();
|
|
31
33
|
const protocol = address.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
@@ -42,6 +44,7 @@ class PondClient {
|
|
|
42
44
|
* @desc Connects to the server and returns the socket.
|
|
43
45
|
*/
|
|
44
46
|
connect(backoff = 1) {
|
|
47
|
+
__classPrivateFieldSet(this, _PondClient_disconnecting, false, "f");
|
|
45
48
|
const socket = new WebSocket(this._address.toString());
|
|
46
49
|
socket.onopen = () => {
|
|
47
50
|
this._connectionState.publish(true);
|
|
@@ -50,8 +53,11 @@ class PondClient {
|
|
|
50
53
|
const data = JSON.parse(message.data);
|
|
51
54
|
this._broadcaster.publish(data);
|
|
52
55
|
};
|
|
53
|
-
socket.
|
|
56
|
+
socket.onclose = () => {
|
|
54
57
|
this._connectionState.publish(false);
|
|
58
|
+
if (__classPrivateFieldGet(this, _PondClient_disconnecting, "f")) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
55
61
|
setTimeout(() => {
|
|
56
62
|
this.connect(backoff * 2);
|
|
57
63
|
}, backoff * 1000);
|
|
@@ -71,6 +77,7 @@ class PondClient {
|
|
|
71
77
|
var _a;
|
|
72
78
|
Object.values(__classPrivateFieldGet(this, _PondClient_channels, "f")).forEach((channel) => channel.leave());
|
|
73
79
|
this._connectionState.publish(false);
|
|
80
|
+
__classPrivateFieldSet(this, _PondClient_disconnecting, true, "f");
|
|
74
81
|
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
75
82
|
__classPrivateFieldSet(this, _PondClient_channels, {}, "f");
|
|
76
83
|
}
|
|
@@ -96,7 +103,7 @@ class PondClient {
|
|
|
96
103
|
return this._connectionState.subscribe(callback);
|
|
97
104
|
}
|
|
98
105
|
}
|
|
99
|
-
_PondClient_channels = new WeakMap(), _PondClient_instances = new WeakSet(), _PondClient_createPublisher = function _PondClient_createPublisher() {
|
|
106
|
+
_PondClient_channels = new WeakMap(), _PondClient_disconnecting = new WeakMap(), _PondClient_instances = new WeakSet(), _PondClient_createPublisher = function _PondClient_createPublisher() {
|
|
100
107
|
return (message) => {
|
|
101
108
|
if (this._connectionState.value) {
|
|
102
109
|
this._socket.send(JSON.stringify(message));
|
package/browser/client.test.js
CHANGED
|
@@ -38,7 +38,7 @@ describe('PondClient', () => {
|
|
|
38
38
|
const mockWebSocket = pondClient['_socket'];
|
|
39
39
|
expect(mockWebSocket.onopen).toBeInstanceOf(Function);
|
|
40
40
|
expect(mockWebSocket.onmessage).toBeInstanceOf(Function);
|
|
41
|
-
expect(mockWebSocket.
|
|
41
|
+
expect(mockWebSocket.onclose).toBeInstanceOf(Function);
|
|
42
42
|
});
|
|
43
43
|
test('disconnect method should close the socket and leave all channels', () => {
|
|
44
44
|
const mockCallback = jest.fn();
|
|
@@ -101,12 +101,12 @@ describe('PondClient', () => {
|
|
|
101
101
|
let mockWebSocket = pondClient['_socket'];
|
|
102
102
|
expect(connectSpy).toHaveBeenCalledTimes(1);
|
|
103
103
|
connectSpy.mockClear();
|
|
104
|
-
mockWebSocket.
|
|
104
|
+
mockWebSocket.onclose();
|
|
105
105
|
yield new Promise((resolve) => setTimeout(resolve, 1000));
|
|
106
106
|
expect(connectSpy).toHaveBeenCalledTimes(1);
|
|
107
107
|
connectSpy.mockClear();
|
|
108
108
|
mockWebSocket = pondClient['_socket'];
|
|
109
|
-
mockWebSocket.
|
|
109
|
+
mockWebSocket.onclose();
|
|
110
110
|
yield new Promise((resolve) => setTimeout(resolve, 2000));
|
|
111
111
|
expect(connectSpy).toHaveBeenCalledTimes(1);
|
|
112
112
|
}));
|