@eleven-am/pondsocket-client 0.0.2
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.d.ts +34 -0
- package/browser/client.js +105 -0
- package/browser/client.test.d.ts +1 -0
- package/browser/client.test.js +123 -0
- package/core/channel.d.ts +125 -0
- package/core/channel.js +302 -0
- package/core/channel.test.d.ts +1 -0
- package/core/channel.test.js +762 -0
- package/index.d.ts +3 -0
- package/index.js +8 -0
- package/node/node.d.ts +7 -0
- package/node/node.js +31 -0
- package/package.json +66 -0
- package/types.d.ts +54 -0
- package/types.js +2 -0
package/core/channel.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
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_queue, _Channel_presence, _Channel_publisher, _Channel_joinParams, _Channel_presenceSub, _Channel_receiver, _Channel_clientState, _Channel_joinState, _Channel_send, _Channel_publish, _Channel_subscribeToPresence, _Channel_init, _Channel_emptyQueue, _Channel_buildJoinMessage, _Channel_onMessage;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Channel = void 0;
|
|
16
|
+
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
17
|
+
class Channel {
|
|
18
|
+
constructor(publisher, clientState, name, receiver, params) {
|
|
19
|
+
_Channel_instances.add(this);
|
|
20
|
+
_Channel_name.set(this, void 0);
|
|
21
|
+
_Channel_queue.set(this, void 0);
|
|
22
|
+
_Channel_presence.set(this, void 0);
|
|
23
|
+
_Channel_publisher.set(this, void 0);
|
|
24
|
+
_Channel_joinParams.set(this, void 0);
|
|
25
|
+
_Channel_presenceSub.set(this, void 0);
|
|
26
|
+
_Channel_receiver.set(this, void 0);
|
|
27
|
+
_Channel_clientState.set(this, void 0);
|
|
28
|
+
_Channel_joinState.set(this, void 0);
|
|
29
|
+
__classPrivateFieldSet(this, _Channel_name, name, "f");
|
|
30
|
+
__classPrivateFieldSet(this, _Channel_queue, [], "f");
|
|
31
|
+
__classPrivateFieldSet(this, _Channel_presence, [], "f");
|
|
32
|
+
__classPrivateFieldSet(this, _Channel_joinParams, params, "f");
|
|
33
|
+
__classPrivateFieldSet(this, _Channel_publisher, publisher, "f");
|
|
34
|
+
__classPrivateFieldSet(this, _Channel_clientState, clientState, "f");
|
|
35
|
+
__classPrivateFieldSet(this, _Channel_receiver, new pondsocket_common_1.SimpleSubject(), "f");
|
|
36
|
+
__classPrivateFieldSet(this, _Channel_joinState, new pondsocket_common_1.SimpleBehaviorSubject(pondsocket_common_1.ChannelState.IDLE), "f");
|
|
37
|
+
__classPrivateFieldSet(this, _Channel_presenceSub, __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_init).call(this, receiver), "f");
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @desc Gets the current connection state of the channel.
|
|
41
|
+
*/
|
|
42
|
+
get channelState() {
|
|
43
|
+
return __classPrivateFieldGet(this, _Channel_joinState, "f").value;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @desc Connects to the channel.
|
|
47
|
+
*/
|
|
48
|
+
join() {
|
|
49
|
+
if (__classPrivateFieldGet(this, _Channel_joinState, "f").value === pondsocket_common_1.ChannelState.CLOSED) {
|
|
50
|
+
throw new Error('This channel has been closed');
|
|
51
|
+
}
|
|
52
|
+
const joinMessage = __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_buildJoinMessage).call(this);
|
|
53
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(pondsocket_common_1.ChannelState.JOINING);
|
|
54
|
+
if (__classPrivateFieldGet(this, _Channel_clientState, "f").value) {
|
|
55
|
+
__classPrivateFieldGet(this, _Channel_publisher, "f").call(this, joinMessage);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(pondsocket_common_1.ChannelState.STALLED);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @desc Disconnects from the channel.
|
|
63
|
+
*/
|
|
64
|
+
leave() {
|
|
65
|
+
const leaveMessage = {
|
|
66
|
+
addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
|
|
67
|
+
action: pondsocket_common_1.ClientActions.LEAVE_CHANNEL,
|
|
68
|
+
event: pondsocket_common_1.ClientActions.LEAVE_CHANNEL,
|
|
69
|
+
channelName: __classPrivateFieldGet(this, _Channel_name, "f"),
|
|
70
|
+
requestId: (0, pondsocket_common_1.uuid)(),
|
|
71
|
+
payload: {},
|
|
72
|
+
};
|
|
73
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_publish).call(this, leaveMessage);
|
|
74
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(pondsocket_common_1.ChannelState.CLOSED);
|
|
75
|
+
__classPrivateFieldGet(this, _Channel_presenceSub, "f").call(this);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @desc Monitors the channel for messages.
|
|
79
|
+
* @param callback - The callback to call when a message is received.
|
|
80
|
+
*/
|
|
81
|
+
onMessage(callback) {
|
|
82
|
+
return __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_onMessage).call(this, (event, message) => {
|
|
83
|
+
callback(event, message);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @desc Monitors the channel for messages.
|
|
88
|
+
* @param event - The event to monitor.
|
|
89
|
+
* @param callback - The callback to call when a message is received.
|
|
90
|
+
*/
|
|
91
|
+
onMessageEvent(event, callback) {
|
|
92
|
+
return this.onMessage((eventReceived, message) => {
|
|
93
|
+
if (eventReceived === event) {
|
|
94
|
+
return callback(message);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @desc Monitors the channel state of the channel.
|
|
100
|
+
* @param callback - The callback to call when the connection state changes.
|
|
101
|
+
*/
|
|
102
|
+
onChannelStateChange(callback) {
|
|
103
|
+
return __classPrivateFieldGet(this, _Channel_joinState, "f").subscribe((data) => {
|
|
104
|
+
callback(data);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* @desc Detects when clients join the channel.
|
|
109
|
+
* @param callback - The callback to call when a client joins the channel.
|
|
110
|
+
*/
|
|
111
|
+
onJoin(callback) {
|
|
112
|
+
return __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (event, payload) => {
|
|
113
|
+
if (event === pondsocket_common_1.PresenceEventTypes.JOIN) {
|
|
114
|
+
return callback(payload.changed);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* @desc Detects when clients leave the channel.
|
|
120
|
+
* @param callback - The callback to call when a client leaves the channel.
|
|
121
|
+
*/
|
|
122
|
+
onLeave(callback) {
|
|
123
|
+
return __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (event, payload) => {
|
|
124
|
+
if (event === pondsocket_common_1.PresenceEventTypes.LEAVE) {
|
|
125
|
+
return callback(payload.changed);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @desc Detects when clients change their presence in the channel.
|
|
131
|
+
* @param callback - The callback to call when a client changes their presence in the channel.
|
|
132
|
+
*/
|
|
133
|
+
onPresenceChange(callback) {
|
|
134
|
+
return __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (event, payload) => {
|
|
135
|
+
if (event === pondsocket_common_1.PresenceEventTypes.UPDATE) {
|
|
136
|
+
return callback(payload);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @desc Sends a message to specific clients in the channel.
|
|
142
|
+
* @param event - The event to send.
|
|
143
|
+
* @param payload - The message to send.
|
|
144
|
+
* @param recipient - The clients to send the message to.
|
|
145
|
+
*/
|
|
146
|
+
sendMessage(event, payload, recipient) {
|
|
147
|
+
const requestId = (0, pondsocket_common_1.uuid)();
|
|
148
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, requestId, payload, recipient);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @desc Sends a message to the server and waits for a response.
|
|
152
|
+
* @param sentEvent - The event to send.
|
|
153
|
+
* @param payload - The message to send.
|
|
154
|
+
*/
|
|
155
|
+
sendForResponse(sentEvent, payload) {
|
|
156
|
+
const requestId = (0, pondsocket_common_1.uuid)();
|
|
157
|
+
return new Promise((resolve) => {
|
|
158
|
+
const unsub = __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_onMessage).call(this, (_, message, responseId) => {
|
|
159
|
+
if (requestId === responseId) {
|
|
160
|
+
resolve(message);
|
|
161
|
+
unsub();
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, sentEvent, requestId, payload);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* @desc Broadcasts a message to every other client in the channel except yourself.
|
|
169
|
+
* @param event - The event to send.
|
|
170
|
+
* @param payload - The message to send.
|
|
171
|
+
*/
|
|
172
|
+
broadcastFrom(event, payload) {
|
|
173
|
+
const requestId = (0, pondsocket_common_1.uuid)();
|
|
174
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, requestId, payload, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* @desc Broadcasts a message to the channel, including yourself.
|
|
178
|
+
* @param event - The event to send.
|
|
179
|
+
* @param payload - The message to send.
|
|
180
|
+
*/
|
|
181
|
+
broadcast(event, payload) {
|
|
182
|
+
const requestId = (0, pondsocket_common_1.uuid)();
|
|
183
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, requestId, payload);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* @desc Gets the current presence of the channel.
|
|
187
|
+
*/
|
|
188
|
+
getPresence() {
|
|
189
|
+
return __classPrivateFieldGet(this, _Channel_presence, "f");
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* @desc Monitors the presence of the channel.
|
|
193
|
+
* @param callback - The callback to call when the presence changes.
|
|
194
|
+
*/
|
|
195
|
+
onUsersChange(callback) {
|
|
196
|
+
return __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (_event, payload) => callback(payload.presence));
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* @desc Checks if the channel is connected.
|
|
200
|
+
*/
|
|
201
|
+
isConnected() {
|
|
202
|
+
return __classPrivateFieldGet(this, _Channel_joinState, "f").value === pondsocket_common_1.ChannelState.JOINED || __classPrivateFieldGet(this, _Channel_joinState, "f").value === pondsocket_common_1.ChannelState.STALLED;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* @desc Checks if the channel is stalled.
|
|
206
|
+
*/
|
|
207
|
+
isStalled() {
|
|
208
|
+
return __classPrivateFieldGet(this, _Channel_joinState, "f").value === pondsocket_common_1.ChannelState.STALLED;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* @desc Checks if the channel is closed.
|
|
212
|
+
*/
|
|
213
|
+
isClosed() {
|
|
214
|
+
return __classPrivateFieldGet(this, _Channel_joinState, "f").value === pondsocket_common_1.ChannelState.CLOSED;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* @desc Monitors the connection state of the channel.
|
|
218
|
+
* @param callback - The callback to call when the connection state changes.
|
|
219
|
+
*/
|
|
220
|
+
onConnectionChange(callback) {
|
|
221
|
+
return this.onChannelStateChange((state) => {
|
|
222
|
+
callback(state === pondsocket_common_1.ChannelState.JOINED || state === pondsocket_common_1.ChannelState.STALLED);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
exports.Channel = Channel;
|
|
227
|
+
_Channel_name = new WeakMap(), _Channel_queue = new WeakMap(), _Channel_presence = new WeakMap(), _Channel_publisher = new WeakMap(), _Channel_joinParams = new WeakMap(), _Channel_presenceSub = new WeakMap(), _Channel_receiver = new WeakMap(), _Channel_clientState = new WeakMap(), _Channel_joinState = new WeakMap(), _Channel_instances = new WeakSet(), _Channel_send = function _Channel_send(event, requestId, payload, receivers = pondsocket_common_1.ChannelReceiver.ALL_USERS) {
|
|
228
|
+
const message = {
|
|
229
|
+
action: pondsocket_common_1.ClientActions.BROADCAST,
|
|
230
|
+
channelName: __classPrivateFieldGet(this, _Channel_name, "f"),
|
|
231
|
+
addresses: receivers,
|
|
232
|
+
requestId,
|
|
233
|
+
event,
|
|
234
|
+
payload,
|
|
235
|
+
};
|
|
236
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_publish).call(this, message);
|
|
237
|
+
}, _Channel_publish = function _Channel_publish(data) {
|
|
238
|
+
if (__classPrivateFieldGet(this, _Channel_clientState, "f").value) {
|
|
239
|
+
if (__classPrivateFieldGet(this, _Channel_joinState, "f").value === pondsocket_common_1.ChannelState.JOINED) {
|
|
240
|
+
__classPrivateFieldGet(this, _Channel_publisher, "f").call(this, data);
|
|
241
|
+
}
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
__classPrivateFieldGet(this, _Channel_queue, "f").push(data);
|
|
245
|
+
}, _Channel_subscribeToPresence = function _Channel_subscribeToPresence(callback) {
|
|
246
|
+
return __classPrivateFieldGet(this, _Channel_receiver, "f").subscribe((data) => {
|
|
247
|
+
if (data.action === pondsocket_common_1.ServerActions.PRESENCE) {
|
|
248
|
+
return callback(data.event, data.payload);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}, _Channel_init = function _Channel_init(receiver) {
|
|
252
|
+
const unsubMessages = receiver.subscribe((data) => {
|
|
253
|
+
if (data.channelName === __classPrivateFieldGet(this, _Channel_name, "f")) {
|
|
254
|
+
if (data.event === pondsocket_common_1.Events.ACKNOWLEDGE) {
|
|
255
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(pondsocket_common_1.ChannelState.JOINED);
|
|
256
|
+
__classPrivateFieldGet(this, _Channel_instances, "m", _Channel_emptyQueue).call(this);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
__classPrivateFieldGet(this, _Channel_receiver, "f").publish(data);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
const unsubStateChange = __classPrivateFieldGet(this, _Channel_clientState, "f").subscribe((state) => {
|
|
264
|
+
if (state && __classPrivateFieldGet(this, _Channel_joinState, "f").value === pondsocket_common_1.ChannelState.STALLED) {
|
|
265
|
+
const joinMessage = __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_buildJoinMessage).call(this);
|
|
266
|
+
__classPrivateFieldGet(this, _Channel_publisher, "f").call(this, joinMessage);
|
|
267
|
+
}
|
|
268
|
+
else if (!state && __classPrivateFieldGet(this, _Channel_joinState, "f").value === pondsocket_common_1.ChannelState.JOINED) {
|
|
269
|
+
__classPrivateFieldGet(this, _Channel_joinState, "f").publish(pondsocket_common_1.ChannelState.STALLED);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
const unsubPresence = __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_subscribeToPresence).call(this, (_, payload) => {
|
|
273
|
+
__classPrivateFieldSet(this, _Channel_presence, payload.presence, "f");
|
|
274
|
+
});
|
|
275
|
+
return () => {
|
|
276
|
+
unsubMessages();
|
|
277
|
+
unsubStateChange();
|
|
278
|
+
unsubPresence();
|
|
279
|
+
};
|
|
280
|
+
}, _Channel_emptyQueue = function _Channel_emptyQueue() {
|
|
281
|
+
__classPrivateFieldGet(this, _Channel_queue, "f")
|
|
282
|
+
.filter((message) => message.action !== pondsocket_common_1.ClientActions.JOIN_CHANNEL)
|
|
283
|
+
.forEach((message) => {
|
|
284
|
+
__classPrivateFieldGet(this, _Channel_publisher, "f").call(this, message);
|
|
285
|
+
});
|
|
286
|
+
__classPrivateFieldSet(this, _Channel_queue, [], "f");
|
|
287
|
+
}, _Channel_buildJoinMessage = function _Channel_buildJoinMessage() {
|
|
288
|
+
return {
|
|
289
|
+
addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
|
|
290
|
+
action: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
|
|
291
|
+
event: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
|
|
292
|
+
payload: __classPrivateFieldGet(this, _Channel_joinParams, "f"),
|
|
293
|
+
channelName: __classPrivateFieldGet(this, _Channel_name, "f"),
|
|
294
|
+
requestId: (0, pondsocket_common_1.uuid)(),
|
|
295
|
+
};
|
|
296
|
+
}, _Channel_onMessage = function _Channel_onMessage(callback) {
|
|
297
|
+
return __classPrivateFieldGet(this, _Channel_receiver, "f").subscribe((data) => {
|
|
298
|
+
if (data.action !== pondsocket_common_1.ServerActions.PRESENCE) {
|
|
299
|
+
return callback(data.event, data.payload, data.requestId);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|