@eleven-am/pondsocket 0.1.50 → 0.1.52
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/abstracts/abstractRequest.js +58 -0
- package/abstracts/middleware.js +51 -0
- package/channel/channel.js +253 -0
- package/{server/channel → channel}/eventRequest.js +4 -1
- package/channel/eventResponse.js +150 -0
- package/client/channel.js +120 -97
- package/client.d.ts +2 -3
- package/client.js +34 -20
- package/endpoint/endpoint.js +233 -0
- package/endpoint/response.js +86 -0
- package/enums.js +14 -10
- package/errors/pondError.js +27 -0
- package/express.d.ts +2 -2
- package/express.js +3 -3
- package/index.d.ts +1 -2
- package/index.js +1 -4
- package/lobby/joinRequest.js +39 -0
- package/lobby/joinResponse.js +125 -0
- package/lobby/lobby.js +174 -0
- package/matcher/matcher.js +94 -0
- package/node.d.ts +2 -3
- package/node.js +3 -4
- package/package.json +5 -4
- package/presence/presence.js +113 -0
- package/server/pondSocket.js +123 -0
- package/subjects/subject.js +93 -0
- package/types.d.ts +274 -323
- package/client/channel.test.js +0 -546
- package/server/abstracts/abstractRequest.js +0 -40
- package/server/abstracts/abstractRequest.test.js +0 -41
- package/server/abstracts/middleware.js +0 -38
- package/server/abstracts/middleware.test.js +0 -70
- package/server/channel/channelEngine.js +0 -280
- package/server/channel/channelEngine.test.js +0 -377
- package/server/channel/channelRequest.test.js +0 -29
- package/server/channel/channelResponse.test.js +0 -164
- package/server/channel/eventResponse.js +0 -153
- package/server/endpoint/connectionResponse.js +0 -64
- package/server/endpoint/endpoint.js +0 -253
- package/server/endpoint/endpoint.test.js +0 -428
- package/server/endpoint/endpointResponse.test.js +0 -43
- package/server/pondChannel/joinRequest.js +0 -29
- package/server/pondChannel/joinResponse.js +0 -103
- package/server/pondChannel/pondChannel.js +0 -185
- package/server/pondChannel/pondChannelResponse.test.js +0 -109
- package/server/presence/presenceEngine.js +0 -107
- package/server/presence/presenceEngine.test.js +0 -105
- package/server/server/pondSocket.js +0 -121
- package/server/server/server.test.js +0 -121
- package/server/utils/matchPattern.js +0 -108
- package/server/utils/matchPattern.test.js +0 -76
- package/server/utils/subjectUtils.js +0 -68
- package/server/utils/subjectUtils.test.js +0 -128
- /package/{server/abstracts → abstracts}/abstractResponse.js +0 -0
package/client/channel.js
CHANGED
|
@@ -1,39 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
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
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _Channel_instances, _Channel_name, _Channel_joinParams, _Channel_receiver, _Channel_clientState, _Channel_joinState, _Channel_publisher, _Channel_queue, _Channel_presence, _Channel_presenceSub, _Channel_send, _Channel_publish, _Channel_subscribeToPresence, _Channel_init, _Channel_emptyQueue;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.Channel = void 0;
|
|
4
16
|
const enums_1 = require("../enums");
|
|
5
|
-
const
|
|
17
|
+
const subject_1 = require("../subjects/subject");
|
|
6
18
|
class Channel {
|
|
7
19
|
constructor(publisher, clientState, name, receiver, params = {}) {
|
|
8
|
-
this
|
|
9
|
-
this
|
|
10
|
-
this
|
|
11
|
-
this
|
|
12
|
-
this
|
|
13
|
-
this
|
|
14
|
-
this
|
|
15
|
-
this
|
|
16
|
-
this
|
|
20
|
+
_Channel_instances.add(this);
|
|
21
|
+
_Channel_name.set(this, void 0);
|
|
22
|
+
_Channel_joinParams.set(this, void 0);
|
|
23
|
+
_Channel_receiver.set(this, void 0);
|
|
24
|
+
_Channel_clientState.set(this, void 0);
|
|
25
|
+
_Channel_joinState.set(this, void 0);
|
|
26
|
+
_Channel_publisher.set(this, void 0);
|
|
27
|
+
_Channel_queue.set(this, void 0);
|
|
28
|
+
_Channel_presence.set(this, void 0);
|
|
29
|
+
_Channel_presenceSub.set(this, void 0);
|
|
30
|
+
__classPrivateFieldSet(this, _Channel_name, name, "f");
|
|
31
|
+
__classPrivateFieldSet(this, _Channel_queue, [], "f");
|
|
32
|
+
__classPrivateFieldSet(this, _Channel_presence, [], "f");
|
|
33
|
+
__classPrivateFieldSet(this, _Channel_joinParams, params, "f");
|
|
34
|
+
__classPrivateFieldSet(this, _Channel_publisher, publisher, "f");
|
|
35
|
+
__classPrivateFieldSet(this, _Channel_clientState, clientState, "f");
|
|
36
|
+
__classPrivateFieldSet(this, _Channel_receiver, new subject_1.SimpleSubject(), "f");
|
|
37
|
+
__classPrivateFieldSet(this, _Channel_joinState, new subject_1.SimpleBehaviorSubject(enums_1.ChannelState.IDLE), "f");
|
|
38
|
+
__classPrivateFieldSet(this, _Channel_presenceSub, __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_init).call(this, receiver), "f");
|
|
17
39
|
}
|
|
18
40
|
/**
|
|
19
41
|
* @desc Connects to the channel.
|
|
20
42
|
*/
|
|
21
43
|
join() {
|
|
22
|
-
if (this.
|
|
44
|
+
if (__classPrivateFieldGet(this, _Channel_joinState, "f").value === enums_1.ChannelState.CLOSED) {
|
|
23
45
|
throw new Error('This channel has been closed');
|
|
24
46
|
}
|
|
25
47
|
const joinMessage = {
|
|
26
48
|
action: enums_1.ClientActions.JOIN_CHANNEL,
|
|
27
|
-
channelName: this
|
|
49
|
+
channelName: __classPrivateFieldGet(this, _Channel_name, "f"),
|
|
28
50
|
event: enums_1.ClientActions.JOIN_CHANNEL,
|
|
29
|
-
payload: this
|
|
51
|
+
payload: __classPrivateFieldGet(this, _Channel_joinParams, "f"),
|
|
30
52
|
};
|
|
31
|
-
this.
|
|
32
|
-
if (this
|
|
33
|
-
this.
|
|
53
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(enums_1.ChannelState.JOINING);
|
|
54
|
+
if (__classPrivateFieldGet(this, _Channel_clientState, "f").value) {
|
|
55
|
+
__classPrivateFieldGet(this, _Channel_publisher, "f").call(this, joinMessage);
|
|
34
56
|
}
|
|
35
57
|
else {
|
|
36
|
-
this.
|
|
58
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(enums_1.ChannelState.STALLED);
|
|
37
59
|
}
|
|
38
60
|
}
|
|
39
61
|
/**
|
|
@@ -42,20 +64,20 @@ class Channel {
|
|
|
42
64
|
leave() {
|
|
43
65
|
const leaveMessage = {
|
|
44
66
|
action: enums_1.ClientActions.LEAVE_CHANNEL,
|
|
45
|
-
channelName: this
|
|
67
|
+
channelName: __classPrivateFieldGet(this, _Channel_name, "f"),
|
|
46
68
|
event: enums_1.ClientActions.LEAVE_CHANNEL,
|
|
47
69
|
payload: {},
|
|
48
70
|
};
|
|
49
|
-
this.
|
|
50
|
-
this.
|
|
51
|
-
this.
|
|
71
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_publish).call(this, leaveMessage);
|
|
72
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(enums_1.ChannelState.CLOSED);
|
|
73
|
+
__classPrivateFieldGet(this, _Channel_presenceSub, "f").call(this);
|
|
52
74
|
}
|
|
53
75
|
/**
|
|
54
76
|
* @desc Monitors the channel for messages.
|
|
55
77
|
* @param callback - The callback to call when a message is received.
|
|
56
78
|
*/
|
|
57
79
|
onMessage(callback) {
|
|
58
|
-
return this.
|
|
80
|
+
return __classPrivateFieldGet(this, _Channel_receiver, "f").subscribe((data) => {
|
|
59
81
|
if (data.action !== enums_1.ServerActions.PRESENCE) {
|
|
60
82
|
return callback(data.event, data.payload);
|
|
61
83
|
}
|
|
@@ -78,7 +100,7 @@ class Channel {
|
|
|
78
100
|
* @param callback - The callback to call when the connection state changes.
|
|
79
101
|
*/
|
|
80
102
|
onChannelStateChange(callback) {
|
|
81
|
-
return this.
|
|
103
|
+
return __classPrivateFieldGet(this, _Channel_joinState, "f").subscribe((data) => {
|
|
82
104
|
callback(data);
|
|
83
105
|
});
|
|
84
106
|
}
|
|
@@ -87,7 +109,7 @@ class Channel {
|
|
|
87
109
|
* @param callback - The callback to call when a client joins the channel.
|
|
88
110
|
*/
|
|
89
111
|
onJoin(callback) {
|
|
90
|
-
return this.
|
|
112
|
+
return __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (event, payload) => {
|
|
91
113
|
if (event === enums_1.PresenceEventTypes.JOIN) {
|
|
92
114
|
return callback(payload.changed);
|
|
93
115
|
}
|
|
@@ -98,7 +120,7 @@ class Channel {
|
|
|
98
120
|
* @param callback - The callback to call when a client leaves the channel.
|
|
99
121
|
*/
|
|
100
122
|
onLeave(callback) {
|
|
101
|
-
return this.
|
|
123
|
+
return __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (event, payload) => {
|
|
102
124
|
if (event === enums_1.PresenceEventTypes.LEAVE) {
|
|
103
125
|
return callback(payload.changed);
|
|
104
126
|
}
|
|
@@ -109,7 +131,7 @@ class Channel {
|
|
|
109
131
|
* @param callback - The callback to call when a client changes their presence in the channel.
|
|
110
132
|
*/
|
|
111
133
|
onPresenceChange(callback) {
|
|
112
|
-
return this.
|
|
134
|
+
return __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (event, payload) => {
|
|
113
135
|
if (event === enums_1.PresenceEventTypes.UPDATE) {
|
|
114
136
|
return callback(payload);
|
|
115
137
|
}
|
|
@@ -122,7 +144,7 @@ class Channel {
|
|
|
122
144
|
* @param recipient - The clients to send the message to.
|
|
123
145
|
*/
|
|
124
146
|
sendMessage(event, payload, recipient) {
|
|
125
|
-
this.
|
|
147
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, payload, recipient);
|
|
126
148
|
}
|
|
127
149
|
/**
|
|
128
150
|
* @desc Broadcasts a message to every other client in the channel except yourself.
|
|
@@ -130,7 +152,7 @@ class Channel {
|
|
|
130
152
|
* @param payload - The message to send.
|
|
131
153
|
*/
|
|
132
154
|
broadcastFrom(event, payload) {
|
|
133
|
-
this.
|
|
155
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, payload, enums_1.ChannelReceiver.ALL_EXCEPT_SENDER);
|
|
134
156
|
}
|
|
135
157
|
/**
|
|
136
158
|
* @desc Broadcasts a message to the channel, including yourself.
|
|
@@ -138,32 +160,32 @@ class Channel {
|
|
|
138
160
|
* @param payload - The message to send.
|
|
139
161
|
*/
|
|
140
162
|
broadcast(event, payload) {
|
|
141
|
-
this.
|
|
163
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, payload);
|
|
142
164
|
}
|
|
143
165
|
/**
|
|
144
166
|
* @desc Gets the current connection state of the channel.
|
|
145
167
|
*/
|
|
146
168
|
get channelState() {
|
|
147
|
-
return this.
|
|
169
|
+
return __classPrivateFieldGet(this, _Channel_joinState, "f").value;
|
|
148
170
|
}
|
|
149
171
|
/**
|
|
150
172
|
* @desc Gets the current presence of the channel.
|
|
151
173
|
*/
|
|
152
174
|
getPresence() {
|
|
153
|
-
return this
|
|
175
|
+
return __classPrivateFieldGet(this, _Channel_presence, "f");
|
|
154
176
|
}
|
|
155
177
|
/**
|
|
156
178
|
* @desc Monitors the presence of the channel.
|
|
157
179
|
* @param callback - The callback to call when the presence changes.
|
|
158
180
|
*/
|
|
159
181
|
onUsersChange(callback) {
|
|
160
|
-
return this.
|
|
182
|
+
return __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (_event, payload) => callback(payload.presence));
|
|
161
183
|
}
|
|
162
184
|
/**
|
|
163
185
|
* @desc Gets the current connection state of the channel.
|
|
164
186
|
*/
|
|
165
187
|
isConnected() {
|
|
166
|
-
return this.
|
|
188
|
+
return __classPrivateFieldGet(this, _Channel_joinState, "f").value === enums_1.ChannelState.JOINED || __classPrivateFieldGet(this, _Channel_joinState, "f").value === enums_1.ChannelState.STALLED;
|
|
167
189
|
}
|
|
168
190
|
/**
|
|
169
191
|
* @desc Monitors the connection state of the channel.
|
|
@@ -171,73 +193,74 @@ class Channel {
|
|
|
171
193
|
*/
|
|
172
194
|
onConnectionChange(callback) {
|
|
173
195
|
return this.onChannelStateChange((state) => {
|
|
174
|
-
callback(state === enums_1.ChannelState.JOINED || state === enums_1.ChannelState.
|
|
196
|
+
callback(state === enums_1.ChannelState.JOINED || state === enums_1.ChannelState.STALLED);
|
|
175
197
|
});
|
|
176
198
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return;
|
|
199
|
+
}
|
|
200
|
+
exports.Channel = Channel;
|
|
201
|
+
_Channel_name = new WeakMap(), _Channel_joinParams = new WeakMap(), _Channel_receiver = new WeakMap(), _Channel_clientState = new WeakMap(), _Channel_joinState = new WeakMap(), _Channel_publisher = new WeakMap(), _Channel_queue = new WeakMap(), _Channel_presence = new WeakMap(), _Channel_presenceSub = new WeakMap(), _Channel_instances = new WeakSet(), _Channel_send = function _Channel_send(event, payload, receivers = enums_1.ChannelReceiver.ALL_USERS) {
|
|
202
|
+
const message = {
|
|
203
|
+
action: enums_1.ClientActions.BROADCAST,
|
|
204
|
+
channelName: __classPrivateFieldGet(this, _Channel_name, "f"),
|
|
205
|
+
event,
|
|
206
|
+
payload,
|
|
207
|
+
addresses: receivers,
|
|
208
|
+
};
|
|
209
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_publish).call(this, message);
|
|
210
|
+
}, _Channel_publish = function _Channel_publish(data) {
|
|
211
|
+
if (__classPrivateFieldGet(this, _Channel_clientState, "f").value) {
|
|
212
|
+
if (__classPrivateFieldGet(this, _Channel_joinState, "f").value === enums_1.ChannelState.JOINED) {
|
|
213
|
+
__classPrivateFieldGet(this, _Channel_publisher, "f").call(this, data);
|
|
193
214
|
}
|
|
194
|
-
|
|
215
|
+
return;
|
|
195
216
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
this._receiver.next(data);
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
const unsubStateChange = this._clientState.subscribe((state) => {
|
|
213
|
-
if (state === enums_1.PondState.OPEN && this._queue.length > 0) {
|
|
214
|
-
const joinMessage = {
|
|
215
|
-
action: enums_1.ClientActions.JOIN_CHANNEL,
|
|
216
|
-
channelName: this._name,
|
|
217
|
-
event: enums_1.ClientActions.JOIN_CHANNEL,
|
|
218
|
-
payload: this._joinParams,
|
|
219
|
-
};
|
|
220
|
-
this._publisher(joinMessage);
|
|
221
|
-
this._queue
|
|
222
|
-
.filter((message) => message.action !== enums_1.ClientActions.JOIN_CHANNEL)
|
|
223
|
-
.forEach((message) => {
|
|
224
|
-
this._publisher(message);
|
|
225
|
-
});
|
|
226
|
-
this._joinState.next(enums_1.ChannelState.JOINED);
|
|
227
|
-
this._queue = [];
|
|
217
|
+
__classPrivateFieldGet(this, _Channel_queue, "f").push(data);
|
|
218
|
+
}, _Channel_subscribeToPresence = function _Channel_subscribeToPresence(callback) {
|
|
219
|
+
return __classPrivateFieldGet(this, _Channel_receiver, "f").subscribe((data) => {
|
|
220
|
+
if (data.action === enums_1.ServerActions.PRESENCE) {
|
|
221
|
+
return callback(data.event, data.payload);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}, _Channel_init = function _Channel_init(receiver) {
|
|
225
|
+
const unsubMessages = receiver.subscribe((data) => {
|
|
226
|
+
if (data.channelName === __classPrivateFieldGet(this, _Channel_name, "f")) {
|
|
227
|
+
if (data.event === enums_1.Events.ACKNOWLEDGE) {
|
|
228
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(enums_1.ChannelState.JOINED);
|
|
229
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_emptyQueue).call(this);
|
|
228
230
|
}
|
|
229
|
-
else
|
|
230
|
-
this.
|
|
231
|
+
else {
|
|
232
|
+
__classPrivateFieldGet(this, _Channel_receiver, "f").publish(data);
|
|
231
233
|
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
const unsubStateChange = __classPrivateFieldGet(this, _Channel_clientState, "f").subscribe((state) => {
|
|
237
|
+
if (state && __classPrivateFieldGet(this, _Channel_joinState, "f").value === enums_1.ChannelState.STALLED) {
|
|
238
|
+
const joinMessage = {
|
|
239
|
+
action: enums_1.ClientActions.JOIN_CHANNEL,
|
|
240
|
+
channelName: __classPrivateFieldGet(this, _Channel_name, "f"),
|
|
241
|
+
event: enums_1.ClientActions.JOIN_CHANNEL,
|
|
242
|
+
payload: __classPrivateFieldGet(this, _Channel_joinParams, "f"),
|
|
243
|
+
};
|
|
244
|
+
__classPrivateFieldGet(this, _Channel_publisher, "f").call(this, joinMessage);
|
|
245
|
+
}
|
|
246
|
+
else if (!state && __classPrivateFieldGet(this, _Channel_joinState, "f").value === enums_1.ChannelState.JOINED) {
|
|
247
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(enums_1.ChannelState.STALLED);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
const unsubPresence = __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (_, payload) => {
|
|
251
|
+
__classPrivateFieldSet(this, _Channel_presence, payload.presence, "f");
|
|
252
|
+
});
|
|
253
|
+
return () => {
|
|
254
|
+
unsubMessages();
|
|
255
|
+
unsubStateChange();
|
|
256
|
+
unsubPresence();
|
|
257
|
+
};
|
|
258
|
+
}, _Channel_emptyQueue = function _Channel_emptyQueue() {
|
|
259
|
+
__classPrivateFieldGet(this, _Channel_queue, "f")
|
|
260
|
+
.filter((message) => message.action !== enums_1.ClientActions.JOIN_CHANNEL)
|
|
261
|
+
.forEach((message) => {
|
|
262
|
+
__classPrivateFieldGet(this, _Channel_publisher, "f").call(this, message);
|
|
263
|
+
});
|
|
264
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(enums_1.ChannelState.JOINED);
|
|
265
|
+
__classPrivateFieldSet(this, _Channel_queue, [], "f");
|
|
266
|
+
};
|
package/client.d.ts
CHANGED
package/client.js
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
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
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _PondClient_instances, _PondClient_channels, _PondClient_createPublisher;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
const channel_1 = require("./client/channel");
|
|
4
16
|
const enums_1 = require("./enums");
|
|
5
|
-
const
|
|
17
|
+
const subject_1 = require("./subjects/subject");
|
|
6
18
|
class PondClient {
|
|
7
19
|
constructor(endpoint, params = {}) {
|
|
20
|
+
_PondClient_instances.add(this);
|
|
21
|
+
_PondClient_channels.set(this, void 0);
|
|
8
22
|
let address;
|
|
9
23
|
try {
|
|
10
24
|
address = new URL(endpoint);
|
|
@@ -20,9 +34,9 @@ class PondClient {
|
|
|
20
34
|
address.protocol = protocol;
|
|
21
35
|
}
|
|
22
36
|
this._address = address;
|
|
23
|
-
this
|
|
24
|
-
this._broadcaster = new
|
|
25
|
-
this._connectionState = new
|
|
37
|
+
__classPrivateFieldSet(this, _PondClient_channels, {}, "f");
|
|
38
|
+
this._broadcaster = new subject_1.SimpleSubject();
|
|
39
|
+
this._connectionState = new subject_1.SimpleBehaviorSubject(false);
|
|
26
40
|
}
|
|
27
41
|
/**
|
|
28
42
|
* @desc Connects to the server and returns the socket.
|
|
@@ -30,14 +44,14 @@ class PondClient {
|
|
|
30
44
|
connect(backoff = 1) {
|
|
31
45
|
const socket = new WebSocket(this._address.toString());
|
|
32
46
|
socket.onopen = () => {
|
|
33
|
-
this._connectionState.
|
|
47
|
+
this._connectionState.publish(true);
|
|
34
48
|
};
|
|
35
49
|
socket.onmessage = (message) => {
|
|
36
50
|
const data = JSON.parse(message.data);
|
|
37
|
-
this._broadcaster.
|
|
51
|
+
this._broadcaster.publish(data);
|
|
38
52
|
};
|
|
39
53
|
socket.onerror = () => {
|
|
40
|
-
this._connectionState.
|
|
54
|
+
this._connectionState.publish(false);
|
|
41
55
|
setTimeout(() => {
|
|
42
56
|
this.connect(backoff * 2);
|
|
43
57
|
}, backoff * 1000);
|
|
@@ -55,9 +69,9 @@ class PondClient {
|
|
|
55
69
|
*/
|
|
56
70
|
disconnect() {
|
|
57
71
|
var _a;
|
|
58
|
-
Object.values(this
|
|
72
|
+
Object.values(__classPrivateFieldGet(this, _PondClient_channels, "f")).forEach((channel) => channel.leave());
|
|
59
73
|
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
60
|
-
this
|
|
74
|
+
__classPrivateFieldSet(this, _PondClient_channels, {}, "f");
|
|
61
75
|
}
|
|
62
76
|
/**
|
|
63
77
|
* @desc Creates a channel with the given name and params.
|
|
@@ -65,12 +79,12 @@ class PondClient {
|
|
|
65
79
|
* @param params - The params to send to the server.
|
|
66
80
|
*/
|
|
67
81
|
createChannel(name, params) {
|
|
68
|
-
if (this
|
|
69
|
-
return this
|
|
82
|
+
if (__classPrivateFieldGet(this, _PondClient_channels, "f")[name] && __classPrivateFieldGet(this, _PondClient_channels, "f")[name].channelState !== enums_1.ChannelState.CLOSED) {
|
|
83
|
+
return __classPrivateFieldGet(this, _PondClient_channels, "f")[name];
|
|
70
84
|
}
|
|
71
|
-
const publisher = this.
|
|
85
|
+
const publisher = __classPrivateFieldGet(this, _PondClient_instances, "m", _PondClient_createPublisher).call(this);
|
|
72
86
|
const channel = new channel_1.Channel(publisher, this._connectionState, name, this._broadcaster, params);
|
|
73
|
-
this
|
|
87
|
+
__classPrivateFieldGet(this, _PondClient_channels, "f")[name] = channel;
|
|
74
88
|
return channel;
|
|
75
89
|
}
|
|
76
90
|
/**
|
|
@@ -80,12 +94,12 @@ class PondClient {
|
|
|
80
94
|
onConnectionChange(callback) {
|
|
81
95
|
return this._connectionState.subscribe(callback);
|
|
82
96
|
}
|
|
83
|
-
_createPublisher() {
|
|
84
|
-
return (message) => {
|
|
85
|
-
if (this._connectionState.value === enums_1.PondState.OPEN) {
|
|
86
|
-
this._socket.send(JSON.stringify(message));
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
97
|
}
|
|
91
98
|
exports.default = PondClient;
|
|
99
|
+
_PondClient_channels = new WeakMap(), _PondClient_instances = new WeakSet(), _PondClient_createPublisher = function _PondClient_createPublisher() {
|
|
100
|
+
return (message) => {
|
|
101
|
+
if (this._connectionState.value) {
|
|
102
|
+
this._socket.send(JSON.stringify(message));
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
};
|