@eleven-am/pondsocket 0.1.30 → 0.1.32
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/client/channel.js +38 -30
- package/package.json +1 -1
package/client/channel.js
CHANGED
|
@@ -7,14 +7,14 @@ class Channel {
|
|
|
7
7
|
constructor(publisher, clientState, name, receiver, params = {}) {
|
|
8
8
|
this._name = name;
|
|
9
9
|
this._queue = [];
|
|
10
|
+
this._presence = [];
|
|
10
11
|
this._finished = false;
|
|
11
12
|
this._joinParams = params;
|
|
12
|
-
this._receiver = receiver;
|
|
13
13
|
this._publisher = publisher;
|
|
14
14
|
this._clientState = clientState;
|
|
15
|
+
this._receiver = new subjectUtils_1.SimpleSubject();
|
|
15
16
|
this._joinState = new subjectUtils_1.SimpleBehaviorSubject(false);
|
|
16
|
-
this.
|
|
17
|
-
this._presenceSub = this._init();
|
|
17
|
+
this._presenceSub = this._init(receiver);
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* @desc Connects to the channel.
|
|
@@ -47,25 +47,24 @@ class Channel {
|
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* @desc Monitors the channel for messages.
|
|
50
|
-
* @param event - The event to monitor.
|
|
51
50
|
* @param callback - The callback to call when a message is received.
|
|
52
51
|
*/
|
|
53
|
-
|
|
52
|
+
onMessage(callback) {
|
|
54
53
|
return this._receiver.subscribe((data) => {
|
|
55
|
-
if (data.action
|
|
56
|
-
return callback(data.payload);
|
|
54
|
+
if (data.action !== enums_1.ServerActions.PRESENCE) {
|
|
55
|
+
return callback(data.event, data.payload);
|
|
57
56
|
}
|
|
58
57
|
});
|
|
59
58
|
}
|
|
60
59
|
/**
|
|
61
60
|
* @desc Monitors the channel for messages.
|
|
61
|
+
* @param event - The event to monitor.
|
|
62
62
|
* @param callback - The callback to call when a message is received.
|
|
63
63
|
*/
|
|
64
|
-
|
|
65
|
-
return this.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return callback(data.event, data.payload);
|
|
64
|
+
onMessageEvent(event, callback) {
|
|
65
|
+
return this.onMessage((eventReceived, message) => {
|
|
66
|
+
if (eventReceived === event) {
|
|
67
|
+
return callback(message);
|
|
69
68
|
}
|
|
70
69
|
});
|
|
71
70
|
}
|
|
@@ -83,9 +82,9 @@ class Channel {
|
|
|
83
82
|
* @param callback - The callback to call when a client joins the channel.
|
|
84
83
|
*/
|
|
85
84
|
onJoin(callback) {
|
|
86
|
-
return this.
|
|
87
|
-
if (
|
|
88
|
-
return callback(
|
|
85
|
+
return this._subscribeToPresence((event, payload) => {
|
|
86
|
+
if (event === enums_1.PresenceEventTypes.JOIN) {
|
|
87
|
+
return callback(payload.changed);
|
|
89
88
|
}
|
|
90
89
|
});
|
|
91
90
|
}
|
|
@@ -94,9 +93,9 @@ class Channel {
|
|
|
94
93
|
* @param callback - The callback to call when a client leaves the channel.
|
|
95
94
|
*/
|
|
96
95
|
onLeave(callback) {
|
|
97
|
-
return this.
|
|
98
|
-
if (
|
|
99
|
-
return callback(
|
|
96
|
+
return this._subscribeToPresence((event, payload) => {
|
|
97
|
+
if (event === enums_1.PresenceEventTypes.LEAVE) {
|
|
98
|
+
return callback(payload.changed);
|
|
100
99
|
}
|
|
101
100
|
});
|
|
102
101
|
}
|
|
@@ -105,9 +104,9 @@ class Channel {
|
|
|
105
104
|
* @param callback - The callback to call when a client changes their presence in the channel.
|
|
106
105
|
*/
|
|
107
106
|
onPresenceChange(callback) {
|
|
108
|
-
return this.
|
|
109
|
-
if (
|
|
110
|
-
return callback(
|
|
107
|
+
return this._subscribeToPresence((event, payload) => {
|
|
108
|
+
if (event === enums_1.PresenceEventTypes.UPDATE) {
|
|
109
|
+
return callback(payload);
|
|
111
110
|
}
|
|
112
111
|
});
|
|
113
112
|
}
|
|
@@ -152,16 +151,14 @@ class Channel {
|
|
|
152
151
|
* @desc Gets the current presence of the channel.
|
|
153
152
|
*/
|
|
154
153
|
getPresence() {
|
|
155
|
-
return this._presence
|
|
154
|
+
return this._presence;
|
|
156
155
|
}
|
|
157
156
|
/**
|
|
158
157
|
* @desc Monitors the presence of the channel.
|
|
159
158
|
* @param callback - The callback to call when the presence changes.
|
|
160
159
|
*/
|
|
161
160
|
onUsersChange(callback) {
|
|
162
|
-
return this.
|
|
163
|
-
callback(data);
|
|
164
|
-
});
|
|
161
|
+
return this._subscribeToPresence((event, payload) => callback(payload.presence));
|
|
165
162
|
}
|
|
166
163
|
_send(event, payload, receivers = 'all_users') {
|
|
167
164
|
const message = {
|
|
@@ -180,7 +177,19 @@ class Channel {
|
|
|
180
177
|
}
|
|
181
178
|
this._publisher(data);
|
|
182
179
|
}
|
|
183
|
-
|
|
180
|
+
_subscribeToPresence(callback) {
|
|
181
|
+
return this._receiver.subscribe((data) => {
|
|
182
|
+
if (data.action === enums_1.ServerActions.PRESENCE) {
|
|
183
|
+
return callback(data.event, data.payload);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
_init(receiver) {
|
|
188
|
+
const unsubMessages = receiver.subscribe((data) => {
|
|
189
|
+
if (data.channelName === this._name) {
|
|
190
|
+
this._receiver.publish(data);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
184
193
|
const unsubStateChange = this._clientState.subscribe((state) => {
|
|
185
194
|
if (state === 'OPEN') {
|
|
186
195
|
const joinMessage = {
|
|
@@ -196,12 +205,11 @@ class Channel {
|
|
|
196
205
|
this._queue = [];
|
|
197
206
|
}
|
|
198
207
|
});
|
|
199
|
-
const unsubPresence = this.
|
|
200
|
-
|
|
201
|
-
this._presence.publish(data.payload.presence);
|
|
202
|
-
}
|
|
208
|
+
const unsubPresence = this._subscribeToPresence((_, payload) => {
|
|
209
|
+
this._presence = payload.presence;
|
|
203
210
|
});
|
|
204
211
|
return () => {
|
|
212
|
+
unsubMessages();
|
|
205
213
|
unsubStateChange();
|
|
206
214
|
unsubPresence();
|
|
207
215
|
};
|