@eleven-am/pondsocket-client 0.0.20 → 0.0.21
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 -10
- package/browser/client.test.js +0 -22
- package/package.json +5 -5
package/browser/client.js
CHANGED
|
@@ -34,7 +34,7 @@ class PondClient {
|
|
|
34
34
|
address.protocol = protocol;
|
|
35
35
|
}
|
|
36
36
|
this._address = address;
|
|
37
|
-
__classPrivateFieldSet(this, _PondClient_channels,
|
|
37
|
+
__classPrivateFieldSet(this, _PondClient_channels, new Map(), "f");
|
|
38
38
|
this._broadcaster = new pondsocket_common_1.Subject();
|
|
39
39
|
this._connectionState = new pondsocket_common_1.BehaviorSubject(false);
|
|
40
40
|
__classPrivateFieldGet(this, _PondClient_instances, "m", _PondClient_init).call(this);
|
|
@@ -73,11 +73,10 @@ class PondClient {
|
|
|
73
73
|
*/
|
|
74
74
|
disconnect() {
|
|
75
75
|
var _a;
|
|
76
|
-
Object.values(__classPrivateFieldGet(this, _PondClient_channels, "f")).forEach((channel) => channel.leave());
|
|
77
76
|
this._connectionState.publish(false);
|
|
78
77
|
this._disconnecting = true;
|
|
79
78
|
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
80
|
-
|
|
79
|
+
__classPrivateFieldGet(this, _PondClient_channels, "f").clear();
|
|
81
80
|
}
|
|
82
81
|
/**
|
|
83
82
|
* @desc Creates a channel with the given name and params.
|
|
@@ -85,13 +84,14 @@ class PondClient {
|
|
|
85
84
|
* @param params - The params to send to the server.
|
|
86
85
|
*/
|
|
87
86
|
createChannel(name, params) {
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
const channel = __classPrivateFieldGet(this, _PondClient_channels, "f").get(name);
|
|
88
|
+
if (channel && channel.channelState !== pondsocket_common_1.ChannelState.CLOSED) {
|
|
89
|
+
return channel;
|
|
90
90
|
}
|
|
91
91
|
const publisher = __classPrivateFieldGet(this, _PondClient_instances, "m", _PondClient_createPublisher).call(this);
|
|
92
|
-
const
|
|
93
|
-
__classPrivateFieldGet(this, _PondClient_channels, "f")
|
|
94
|
-
return
|
|
92
|
+
const newChannel = new channel_1.Channel(publisher, this._connectionState, name, params || {});
|
|
93
|
+
__classPrivateFieldGet(this, _PondClient_channels, "f").set(name, newChannel);
|
|
94
|
+
return newChannel;
|
|
95
95
|
}
|
|
96
96
|
/**
|
|
97
97
|
* @desc Subscribes to the connection state.
|
|
@@ -109,8 +109,8 @@ _PondClient_channels = new WeakMap(), _PondClient_instances = new WeakSet(), _Po
|
|
|
109
109
|
};
|
|
110
110
|
}, _PondClient_handleAcknowledge = function _PondClient_handleAcknowledge(message) {
|
|
111
111
|
var _a;
|
|
112
|
-
const channel = (_a = __classPrivateFieldGet(this, _PondClient_channels, "f")
|
|
113
|
-
__classPrivateFieldGet(this, _PondClient_channels, "f")
|
|
112
|
+
const channel = (_a = __classPrivateFieldGet(this, _PondClient_channels, "f").get(message.channelName)) !== null && _a !== void 0 ? _a : new channel_1.Channel(__classPrivateFieldGet(this, _PondClient_instances, "m", _PondClient_createPublisher).call(this), this._connectionState, message.channelName, {});
|
|
113
|
+
__classPrivateFieldGet(this, _PondClient_channels, "f").set(message.channelName, channel);
|
|
114
114
|
channel.acknowledge(this._broadcaster);
|
|
115
115
|
}, _PondClient_init = function _PondClient_init() {
|
|
116
116
|
this._broadcaster.subscribe((message) => {
|
package/browser/client.test.js
CHANGED
|
@@ -87,28 +87,6 @@ describe('PondClient', () => {
|
|
|
87
87
|
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
88
88
|
expect(mockCallback).toHaveBeenCalledWith(true);
|
|
89
89
|
});
|
|
90
|
-
test('disconnect method should close the socket and leave all channels', () => {
|
|
91
|
-
const mockCallback = jest.fn();
|
|
92
|
-
pondClient.onConnectionChange(mockCallback);
|
|
93
|
-
pondClient.connect();
|
|
94
|
-
const mockWebSocket = pondClient['_socket'];
|
|
95
|
-
const acknowledgeEvent = {
|
|
96
|
-
event: pondsocket_common_1.Events.CONNECTION,
|
|
97
|
-
action: pondsocket_common_1.ServerActions.CONNECT,
|
|
98
|
-
channelName: 'exampleChannel',
|
|
99
|
-
requestId: '123',
|
|
100
|
-
payload: {},
|
|
101
|
-
};
|
|
102
|
-
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
103
|
-
expect(mockCallback).toHaveBeenCalledWith(true);
|
|
104
|
-
const channel = pondClient.createChannel('exampleChannel');
|
|
105
|
-
const spyOnLeave = jest.spyOn(channel, 'leave');
|
|
106
|
-
mockCallback.mockClear();
|
|
107
|
-
pondClient.disconnect();
|
|
108
|
-
expect(mockCallback).toHaveBeenCalledWith(false);
|
|
109
|
-
expect(mockWebSocket.close).toHaveBeenCalled();
|
|
110
|
-
expect(spyOnLeave).toHaveBeenCalled();
|
|
111
|
-
});
|
|
112
90
|
test('createChannel method should create a new channel or return an existing one', () => {
|
|
113
91
|
const mockChannel = pondClient.createChannel('exampleChannel');
|
|
114
92
|
const mockExistingChannel = pondClient.createChannel('exampleChannel');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eleven-am/pondsocket-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.21",
|
|
4
4
|
"description": "PondSocket is a fast simple socket server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"socket",
|
|
@@ -39,12 +39,12 @@
|
|
|
39
39
|
"eslint-plugin-file-progress": "^1.3.0",
|
|
40
40
|
"eslint-plugin-import": "^2.29.1",
|
|
41
41
|
"jest": "^29.7.0",
|
|
42
|
-
"prettier": "^3.2
|
|
43
|
-
"supertest": "^
|
|
44
|
-
"ts-jest": "^29.1.
|
|
42
|
+
"prettier": "^3.3.2",
|
|
43
|
+
"supertest": "^7.0.0",
|
|
44
|
+
"ts-jest": "^29.1.5",
|
|
45
45
|
"ts-loader": "^9.5.1",
|
|
46
46
|
"ts-node": "^10.9.2",
|
|
47
|
-
"typescript": "^5.
|
|
47
|
+
"typescript": "^5.5.2"
|
|
48
48
|
},
|
|
49
49
|
"jest": {
|
|
50
50
|
"moduleFileExtensions": [
|