@eleven-am/pondsocket 0.1.31 → 0.1.33

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.
Files changed (2) hide show
  1. package/client/channel.js +49 -34
  2. 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._presence = new subjectUtils_1.SimpleBehaviorSubject([]);
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
- onMessageEvent(event, callback) {
52
+ onMessage(callback) {
54
53
  return this._receiver.subscribe((data) => {
55
- if (data.action === enums_1.ServerActions.BROADCAST && data.event === event && data.channelName === this._name) {
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
- onMessage(callback) {
65
- return this._receiver.subscribe((data) => {
66
- console.log(data.channelName, this._name, 2);
67
- if (data.action === enums_1.ServerActions.BROADCAST && data.channelName === this._name) {
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._receiver.subscribe((data) => {
87
- if (data.action === enums_1.ServerActions.PRESENCE && data.event === enums_1.PresenceEventTypes.JOIN && data.channelName === this._name) {
88
- return callback(data.payload.changed);
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._receiver.subscribe((data) => {
98
- if (data.action === enums_1.ServerActions.PRESENCE && data.event === enums_1.PresenceEventTypes.LEAVE && data.channelName === this._name) {
99
- return callback(data.payload.changed);
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._receiver.subscribe((data) => {
109
- if (data.action === enums_1.ServerActions.PRESENCE && data.event === enums_1.PresenceEventTypes.UPDATE && data.channelName === this._name) {
110
- return callback(data.payload);
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.value;
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._presence.subscribe((data) => {
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 = {
@@ -174,15 +171,30 @@ class Channel {
174
171
  this._publish(message);
175
172
  }
176
173
  _publish(data) {
177
- if (!this._joinState.value || this._clientState.value !== 'OPEN') {
178
- this._queue.push(data);
174
+ if (this._joinState.value && this._clientState.value !== 'OPEN') {
175
+ this._publisher(data);
179
176
  return;
180
177
  }
181
- this._publisher(data);
178
+ this._queue.push(data);
182
179
  }
183
- _init() {
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
+ if (!this._joinState.value) {
191
+ this._joinState.publish(true);
192
+ }
193
+ this._receiver.publish(data);
194
+ }
195
+ });
184
196
  const unsubStateChange = this._clientState.subscribe((state) => {
185
- if (state === 'OPEN') {
197
+ if (state === 'OPEN' && this._queue.length > 0) {
186
198
  const joinMessage = {
187
199
  action: enums_1.ClientActions.JOIN_CHANNEL,
188
200
  channelName: this._name,
@@ -193,15 +205,18 @@ class Channel {
193
205
  this._queue.forEach((message) => {
194
206
  this._publisher(message);
195
207
  });
208
+ this._joinState.publish(true);
196
209
  this._queue = [];
197
210
  }
198
- });
199
- const unsubPresence = this._receiver.subscribe((data) => {
200
- if (data.action === enums_1.ServerActions.PRESENCE && data.channelName === this._name) {
201
- this._presence.publish(data.payload.presence);
211
+ else if (state !== 'OPEN') {
212
+ this._joinState.publish(false);
202
213
  }
203
214
  });
215
+ const unsubPresence = this._subscribeToPresence((_, payload) => {
216
+ this._presence = payload.presence;
217
+ });
204
218
  return () => {
219
+ unsubMessages();
205
220
  unsubStateChange();
206
221
  unsubPresence();
207
222
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.31",
3
+ "version": "0.1.33",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",