@eleven-am/pondsocket-client 0.0.19 → 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 +12 -11
- package/browser/client.test.js +26 -24
- package/package.json +6 -6
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);
|
|
@@ -47,7 +47,8 @@ class PondClient {
|
|
|
47
47
|
const socket = new WebSocket(this._address.toString());
|
|
48
48
|
socket.onmessage = (message) => {
|
|
49
49
|
const data = JSON.parse(message.data);
|
|
50
|
-
|
|
50
|
+
const event = pondsocket_common_1.channelEventSchema.parse(data);
|
|
51
|
+
this._broadcaster.publish(event);
|
|
51
52
|
};
|
|
52
53
|
socket.onerror = () => socket.close();
|
|
53
54
|
socket.onclose = () => {
|
|
@@ -72,11 +73,10 @@ class PondClient {
|
|
|
72
73
|
*/
|
|
73
74
|
disconnect() {
|
|
74
75
|
var _a;
|
|
75
|
-
Object.values(__classPrivateFieldGet(this, _PondClient_channels, "f")).forEach((channel) => channel.leave());
|
|
76
76
|
this._connectionState.publish(false);
|
|
77
77
|
this._disconnecting = true;
|
|
78
78
|
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
79
|
-
|
|
79
|
+
__classPrivateFieldGet(this, _PondClient_channels, "f").clear();
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
82
|
* @desc Creates a channel with the given name and params.
|
|
@@ -84,13 +84,14 @@ class PondClient {
|
|
|
84
84
|
* @param params - The params to send to the server.
|
|
85
85
|
*/
|
|
86
86
|
createChannel(name, params) {
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
const channel = __classPrivateFieldGet(this, _PondClient_channels, "f").get(name);
|
|
88
|
+
if (channel && channel.channelState !== pondsocket_common_1.ChannelState.CLOSED) {
|
|
89
|
+
return channel;
|
|
89
90
|
}
|
|
90
91
|
const publisher = __classPrivateFieldGet(this, _PondClient_instances, "m", _PondClient_createPublisher).call(this);
|
|
91
|
-
const
|
|
92
|
-
__classPrivateFieldGet(this, _PondClient_channels, "f")
|
|
93
|
-
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;
|
|
94
95
|
}
|
|
95
96
|
/**
|
|
96
97
|
* @desc Subscribes to the connection state.
|
|
@@ -108,8 +109,8 @@ _PondClient_channels = new WeakMap(), _PondClient_instances = new WeakSet(), _Po
|
|
|
108
109
|
};
|
|
109
110
|
}, _PondClient_handleAcknowledge = function _PondClient_handleAcknowledge(message) {
|
|
110
111
|
var _a;
|
|
111
|
-
const channel = (_a = __classPrivateFieldGet(this, _PondClient_channels, "f")
|
|
112
|
-
__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);
|
|
113
114
|
channel.acknowledge(this._broadcaster);
|
|
114
115
|
}, _PondClient_init = function _PondClient_init() {
|
|
115
116
|
this._broadcaster.subscribe((message) => {
|
package/browser/client.test.js
CHANGED
|
@@ -43,9 +43,33 @@ describe('PondClient', () => {
|
|
|
43
43
|
pondClient.connect();
|
|
44
44
|
const mockWebSocket = pondClient['_socket'];
|
|
45
45
|
const broadcasterSpy = jest.spyOn(pondClient['_broadcaster'], 'publish');
|
|
46
|
-
mockWebSocket.onmessage({
|
|
46
|
+
mockWebSocket.onmessage({
|
|
47
|
+
data: JSON.stringify({
|
|
48
|
+
action: pondsocket_common_1.ServerActions.SYSTEM,
|
|
49
|
+
channelName: 'exampleChannel',
|
|
50
|
+
requestId: '123',
|
|
51
|
+
payload: {},
|
|
52
|
+
event: 'exampleEvent',
|
|
53
|
+
}),
|
|
54
|
+
});
|
|
47
55
|
expect(broadcasterSpy).toHaveBeenCalledTimes(1);
|
|
48
|
-
expect(broadcasterSpy).toHaveBeenCalledWith({
|
|
56
|
+
expect(broadcasterSpy).toHaveBeenCalledWith({
|
|
57
|
+
action: pondsocket_common_1.ServerActions.SYSTEM,
|
|
58
|
+
channelName: 'exampleChannel',
|
|
59
|
+
requestId: '123',
|
|
60
|
+
payload: {},
|
|
61
|
+
event: 'exampleEvent',
|
|
62
|
+
});
|
|
63
|
+
broadcasterSpy.mockClear();
|
|
64
|
+
expect(() => {
|
|
65
|
+
mockWebSocket.onmessage({ data: 'invalid json' });
|
|
66
|
+
}).toThrow();
|
|
67
|
+
expect(broadcasterSpy).not.toHaveBeenCalled();
|
|
68
|
+
broadcasterSpy.mockClear();
|
|
69
|
+
expect(() => {
|
|
70
|
+
mockWebSocket.onmessage({ data: JSON.stringify({}) });
|
|
71
|
+
}).toThrow();
|
|
72
|
+
expect(broadcasterSpy).not.toHaveBeenCalled();
|
|
49
73
|
});
|
|
50
74
|
test('socket should only pass to publish state when acknowledged event is received', () => {
|
|
51
75
|
pondClient.connect();
|
|
@@ -63,28 +87,6 @@ describe('PondClient', () => {
|
|
|
63
87
|
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
64
88
|
expect(mockCallback).toHaveBeenCalledWith(true);
|
|
65
89
|
});
|
|
66
|
-
test('disconnect method should close the socket and leave all channels', () => {
|
|
67
|
-
const mockCallback = jest.fn();
|
|
68
|
-
pondClient.onConnectionChange(mockCallback);
|
|
69
|
-
pondClient.connect();
|
|
70
|
-
const mockWebSocket = pondClient['_socket'];
|
|
71
|
-
const acknowledgeEvent = {
|
|
72
|
-
event: pondsocket_common_1.Events.CONNECTION,
|
|
73
|
-
action: pondsocket_common_1.ServerActions.CONNECT,
|
|
74
|
-
channelName: 'exampleChannel',
|
|
75
|
-
requestId: '123',
|
|
76
|
-
payload: {},
|
|
77
|
-
};
|
|
78
|
-
mockWebSocket.onmessage({ data: JSON.stringify(acknowledgeEvent) });
|
|
79
|
-
expect(mockCallback).toHaveBeenCalledWith(true);
|
|
80
|
-
const channel = pondClient.createChannel('exampleChannel');
|
|
81
|
-
const spyOnLeave = jest.spyOn(channel, 'leave');
|
|
82
|
-
mockCallback.mockClear();
|
|
83
|
-
pondClient.disconnect();
|
|
84
|
-
expect(mockCallback).toHaveBeenCalledWith(false);
|
|
85
|
-
expect(mockWebSocket.close).toHaveBeenCalled();
|
|
86
|
-
expect(spyOnLeave).toHaveBeenCalled();
|
|
87
|
-
});
|
|
88
90
|
test('createChannel method should create a new channel or return an existing one', () => {
|
|
89
91
|
const mockChannel = pondClient.createChannel('exampleChannel');
|
|
90
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",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"pipeline": "npm run test && npm run build && npm run push"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@eleven-am/pondsocket-common": "^0.0.
|
|
32
|
+
"@eleven-am/pondsocket-common": "^0.0.20",
|
|
33
33
|
"websocket": "^1.0.34"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
@@ -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": [
|