@eleven-am/pondsocket 0.1.10 → 0.1.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -4,11 +4,6 @@ export declare class Subscription {
4
4
 
5
5
  export declare class Broadcast<T, A> {
6
6
 
7
- /**
8
- * @desc Gets the number of subscribers
9
- */
10
- get subscriberCount(): number;
11
-
12
7
  /**
13
8
  * @desc Subscribe to the broadcast
14
9
  * @param handler - The handler to call when the broadcast is published
@@ -35,17 +30,21 @@ export declare class Subject<T, A> extends Broadcast<T, A> {
35
30
  */
36
31
  get value(): T;
37
32
 
33
+ /**
34
+ * @desc Get the list of observers
35
+ * @returns The list of observers
36
+ */
37
+ get observers(): Set<(data: T) => Anything<A>>;
38
+
38
39
  /**
39
40
  * @desc Subscribe to the subject
40
- * @param handler - The handler to call when the subject is published
41
41
  */
42
42
  subscribe(handler: (data: T) => A): Subscription;
43
43
 
44
44
  /**
45
45
  * @desc Publish to the subject
46
- * @param data - The data to publish
47
46
  */
48
- publish(data: T): A | undefined;
47
+ publish(data: T): Anything<A>;
49
48
  }
50
49
 
51
50
  export declare class EventPubSub<T, A> {
@@ -55,7 +54,7 @@ export declare class EventPubSub<T, A> {
55
54
  * @param event - The event to subscribe to
56
55
  * @param handler - The handler to call when the event subject is published
57
56
  */
58
- subscribe(event: string, handler: (data: T) => A): Subscription;
57
+ subscribe(event: string, handler: (data: T) => Anything<A>): Subscription;
59
58
 
60
59
  /**
61
60
  * @desc Publish to the event subject
@@ -20,12 +20,6 @@ class Broadcast {
20
20
  }
21
21
  };
22
22
  }
23
- /**
24
- * @desc Gets the number of subscribers
25
- */
26
- get subscriberCount() {
27
- return this._subscribers.size;
28
- }
29
23
  /**
30
24
  * @desc Publish to the broadcast
31
25
  * @param data - The data to publish
@@ -33,9 +27,14 @@ class Broadcast {
33
27
  publish(data) {
34
28
  let result;
35
29
  for (const subscriber of this._subscribers) {
36
- result = subscriber(data);
37
- if (result)
38
- break;
30
+ try {
31
+ result = subscriber(data);
32
+ if (result)
33
+ break;
34
+ }
35
+ catch (e) {
36
+ throw e;
37
+ }
39
38
  }
40
39
  return result;
41
40
  }
@@ -58,17 +57,22 @@ class Subject extends Broadcast {
58
57
  get value() {
59
58
  return this._value;
60
59
  }
60
+ /**
61
+ * @desc Get the list of observers
62
+ * @returns The list of observers
63
+ */
64
+ get observers() {
65
+ return this._subscribers;
66
+ }
61
67
  /**
62
68
  * @desc Subscribe to the subject
63
- * @param handler - The handler to call when the subject is published
64
69
  */
65
70
  subscribe(handler) {
66
- void handler(this._value);
71
+ handler(this._value);
67
72
  return super.subscribe(handler);
68
73
  }
69
74
  /**
70
75
  * @desc Publish to the subject
71
- * @param data - The data to publish
72
76
  */
73
77
  publish(data) {
74
78
  if (this._value !== data) {
@@ -91,7 +95,6 @@ class EventPubSub {
91
95
  const subscriber = (eventData) => {
92
96
  if (eventData.type === event)
93
97
  return handler(eventData.data);
94
- return undefined;
95
98
  };
96
99
  this._subscribers.add(subscriber);
97
100
  return {
@@ -110,7 +113,12 @@ class EventPubSub {
110
113
  */
111
114
  publish(event, data) {
112
115
  for (const subscriber of this._subscribers) {
113
- void subscriber({ type: event, data });
116
+ try {
117
+ subscriber({ type: event, data });
118
+ }
119
+ catch (e) {
120
+ throw e;
121
+ }
114
122
  }
115
123
  }
116
124
  /**
@@ -163,6 +163,15 @@ describe('Subject', () => {
163
163
  expect(subscriber3).toHaveBeenCalledWith('Hello');
164
164
  expect(subscriber2).not.toHaveBeenCalled();
165
165
  });
166
+ it('should provide the current list of observers', () => {
167
+ const subject = new pubSub_1.Subject('hi');
168
+ const subscriber1 = jest.fn();
169
+ const subscriber2 = jest.fn();
170
+ subject.subscribe(subscriber1);
171
+ subject.subscribe(subscriber2);
172
+ const set = new Set([subscriber1, subscriber2]);
173
+ expect(subject.observers).toEqual(set);
174
+ });
166
175
  });
167
176
  describe('EventSubject', () => {
168
177
  it('should be defined', () => {
@@ -7,6 +7,11 @@ export declare class Channel {
7
7
 
8
8
  constructor(name: string, receiver: Broadcast<ServerMessage, void>, broadcaster: Broadcast<ClientMessage, void>, params?: ChannelParams);
9
9
 
10
+ /**
11
+ * @desc Gets the current presence state of the channel.
12
+ */
13
+ get presence(): PondPresence[];
14
+
10
15
  /**
11
16
  * @desc Connects to the channel.
12
17
  */
@@ -53,10 +58,16 @@ export declare class Channel {
53
58
  updatePresence(presence: PondPresence): void;
54
59
 
55
60
  /**
56
- * @desc Monitors the presence state of the channel.
57
- * @param callback - The callback to call when the presence state changes.
61
+ * @desc Detects when clients join the channel.
62
+ * @param callback - The callback to call when a client joins the channel.
63
+ */
64
+ onJoin(callback: (presence: PondPresence) => void): Subscription;
65
+
66
+ /**
67
+ * @desc Detects when clients leave the channel.
68
+ * @param callback - The callback to call when a client leaves the channel.
58
69
  */
59
- onPresence(callback: (change: PondPresence | null, presence: PondPresence[]) => void): Subscription;
70
+ onLeave(callback: (presence: PondPresence) => void): Subscription;
60
71
 
61
72
  /**
62
73
  * @desc Monitors the connection state of the channel.
@@ -10,16 +10,17 @@ class Channel {
10
10
  this._broadcaster = broadcaster;
11
11
  this._connection = new pondBase_1.Subject(false);
12
12
  this._receiver = new pondBase_1.Broadcast();
13
+ this._presence = new pondBase_1.Subject({
14
+ action: pondBase_1.PondBaseActions.REMOVE_FROM_POND,
15
+ change: null,
16
+ presence: []
17
+ });
13
18
  this._subscription = receiver.subscribe(data => {
14
19
  if (data.channelName === name) {
15
20
  this._receiver.publish(data);
16
21
  this._connection.publish(true);
17
22
  }
18
23
  });
19
- this._presence = new pondBase_1.Subject({
20
- change: null,
21
- presence: []
22
- });
23
24
  }
24
25
  /**
25
26
  * @desc Connects to the channel.
@@ -33,7 +34,13 @@ class Channel {
33
34
  };
34
35
  this._receiver.subscribe(data => {
35
36
  if (data.action === pondSocket_1.ServerActions.PRESENCE) {
36
- this._presence.publish(data.payload);
37
+ const event = data.event;
38
+ const presenceData = data.payload;
39
+ this._presence.publish({
40
+ action: event,
41
+ change: presenceData.change,
42
+ presence: presenceData.presence
43
+ });
37
44
  }
38
45
  });
39
46
  this._broadcaster.publish(joinMessage);
@@ -118,14 +125,35 @@ class Channel {
118
125
  this._broadcaster.publish(message);
119
126
  }
120
127
  /**
121
- * @desc Monitors the presence state of the channel.
122
- * @param callback - The callback to call when the presence state changes.
128
+ * @desc Detects when clients join the channel.
129
+ * @param callback - The callback to call when a client joins the channel.
123
130
  */
124
- onPresence(callback) {
131
+ onJoin(callback) {
125
132
  return this._presence.subscribe(data => {
126
- callback(data.change, data.presence);
133
+ if (data.action === pondBase_1.PondBaseActions.ADD_TO_POND)
134
+ callback(data.change);
127
135
  });
128
136
  }
137
+ /**
138
+ * @desc Detects when clients leave the channel.
139
+ * @param callback - The callback to call when a client leaves the channel.
140
+ */
141
+ onLeave(callback) {
142
+ let presence = [];
143
+ return this._presence.subscribe(data => {
144
+ if (data.action === pondBase_1.PondBaseActions.REMOVE_FROM_POND) {
145
+ const missing = presence.filter(p => !data.presence.find(x => x.id === p.id));
146
+ missing.forEach(p => callback(p));
147
+ }
148
+ presence = data.presence;
149
+ });
150
+ }
151
+ /**
152
+ * @desc Gets the current presence state of the channel.
153
+ */
154
+ get presence() {
155
+ return this._presence.value.presence;
156
+ }
129
157
  /**
130
158
  * @desc Monitors the connection state of the channel.
131
159
  * @param callback - The callback to call when the connection state changes.
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PondClient = void 0;
4
4
  const channel_1 = require("./channel");
5
- const pondBase_1 = require("../pondBase");
6
5
  const pondSocket_1 = require("../pondSocket");
6
+ const pondBase_1 = require("../pondBase");
7
7
  class PondClient {
8
8
  constructor(endpoint, params) {
9
9
  let address;
@@ -99,7 +99,7 @@ class Channel extends pondBase_1.BaseClass {
99
99
  this.removeChannel();
100
100
  }
101
101
  /**
102
- * @desc Subscribes to the presence changes occuring in the channel
102
+ * @desc Subscribes to the presence changes occurring in the channel
103
103
  * @param callback - The callback to call when a presence change occurs
104
104
  */
105
105
  onPresenceChange(callback) {
@@ -114,7 +114,7 @@ class Channel extends pondBase_1.BaseClass {
114
114
  });
115
115
  }
116
116
  /**
117
- * @desc Subscribes to the message events occuring in the channel
117
+ * @desc Subscribes to the message events occurring in the channel
118
118
  * @param callback - The callback to call when a message event occurs
119
119
  */
120
120
  onMessage(callback) {