@eleven-am/pondsocket 0.1.47 → 0.1.49

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 CHANGED
@@ -74,10 +74,10 @@ class Channel {
74
74
  });
75
75
  }
76
76
  /**
77
- * @desc Monitors the connection state of the channel.
77
+ * @desc Monitors the channel state of the channel.
78
78
  * @param callback - The callback to call when the connection state changes.
79
79
  */
80
- onConnectionChange(callback) {
80
+ onChannelStateChange(callback) {
81
81
  return this._joinState.subscribe((data) => {
82
82
  callback(data);
83
83
  });
@@ -159,6 +159,21 @@ class Channel {
159
159
  onUsersChange(callback) {
160
160
  return this._subscribeToPresence((_event, payload) => callback(payload.presence));
161
161
  }
162
+ /**
163
+ * @desc Gets the current connection state of the channel.
164
+ */
165
+ isConnected() {
166
+ return this._joinState.value === enums_1.ChannelState.JOINED || this._joinState.value === enums_1.ChannelState.JOINING;
167
+ }
168
+ /**
169
+ * @desc Monitors the connection state of the channel.
170
+ * @param callback - The callback to call when the connection state changes.
171
+ */
172
+ onConnectionChange(callback) {
173
+ return this.onChannelStateChange((state) => {
174
+ callback(state === enums_1.ChannelState.JOINED || state === enums_1.ChannelState.JOINING);
175
+ });
176
+ }
162
177
  _send(event, payload, receivers = 'all_users') {
163
178
  const message = {
164
179
  action: enums_1.ClientActions.BROADCAST,
@@ -83,7 +83,7 @@ describe('Channel', () => {
83
83
  it('should notify subscribers when the channel state changes', () => {
84
84
  const { channel, receiver } = createChannel();
85
85
  const stateListener = jest.fn();
86
- channel.onConnectionChange(stateListener);
86
+ channel.onChannelStateChange(stateListener);
87
87
  expect(stateListener).toHaveBeenCalledWith(enums_1.ChannelState.IDLE);
88
88
  channel.join();
89
89
  receiver.next({
@@ -98,6 +98,24 @@ describe('Channel', () => {
98
98
  channel.leave();
99
99
  expect(stateListener).toHaveBeenCalledWith(enums_1.ChannelState.CLOSED);
100
100
  });
101
+ it('should notify subscribers when state changes BOOLEAN', () => {
102
+ const { channel, receiver } = createChannel();
103
+ const stateListener = jest.fn();
104
+ channel.onConnectionChange(stateListener);
105
+ expect(stateListener).toHaveBeenCalledWith(false);
106
+ channel.join();
107
+ receiver.next({
108
+ action: 'SYSTEM',
109
+ channelName: 'test',
110
+ event: 'SYSTEM',
111
+ payload: {
112
+ event: 'JOIN',
113
+ },
114
+ });
115
+ expect(stateListener).toHaveBeenCalledWith(true);
116
+ channel.leave();
117
+ expect(stateListener).toHaveBeenCalledWith(false);
118
+ });
101
119
  it('should correctly send a message', () => {
102
120
  const { channel, publisher, state } = createChannel();
103
121
  expect(channel.channelState).toBe(enums_1.ChannelState.IDLE);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.47",
3
+ "version": "0.1.49",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
package/types.d.ts CHANGED
@@ -5,6 +5,8 @@ import internal from 'stream';
5
5
  import { Express } from 'express';
6
6
  import { WebSocketServer, WebSocket } from 'ws';
7
7
 
8
+ import { ClientActions, PresenceEventTypes, ChannelState } from './enums';
9
+
8
10
  export type PondPath = string | RegExp;
9
11
  type NextFunction = () => void;
10
12
  export type JoinParams = Record<string, any>;
@@ -15,24 +17,27 @@ export type EndpointHandler = (req: IncomingConnection, res: ConnectionResponse)
15
17
  type SocketCache = Pick<RequestCache, 'socket' | 'clientId' | 'assigns'>;
16
18
  type AuthorizeMiddleware = (request: JoinRequest, response: JoinResponse) => void | Promise<void>;
17
19
  type SocketMiddlewareFunction = (req: IncomingMessage, socket: internal.Duplex, head: Buffer, next: NextFunction) => void;
20
+ export type ChannelReceivers = 'all_users' | 'all_except_sender' | string[];
21
+ export type ChannelEvent = Event | PresenceEvent;
18
22
 
19
- declare enum ClientActions {
20
- JOIN_CHANNEL = 'JOIN_CHANNEL',
21
- LEAVE_CHANNEL = 'LEAVE_CHANNEL',
22
- BROADCAST = 'BROADCAST',
23
+ interface Event {
24
+ action: 'SYSTEM' | 'BROADCAST' | 'ERROR';
25
+ event: string;
26
+ payload: PondMessage;
27
+ channelName: string;
23
28
  }
24
- declare enum PondState {
25
- CONNECTING = 'CONNECTING',
26
- OPEN = 'OPEN',
27
- CLOSING = 'CLOSING',
28
- CLOSED = 'CLOSED',
29
+ export interface ClientMessage {
30
+ action: ClientActions;
31
+ channelName: string;
32
+ event: string;
33
+ payload: Record<string, any>;
34
+ addresses?: ChannelReceivers;
29
35
  }
30
- declare enum ChannelState {
31
- IDLE = 'IDLE',
32
- JOINING = 'JOINING',
33
- JOINED = 'JOINED',
34
- STALLED = 'STALLED',
35
- CLOSED = 'CLOSED',
36
+ interface PresenceEvent {
37
+ action: 'PRESENCE';
38
+ event: PresenceEventTypes;
39
+ channelName: string;
40
+ payload: PresencePayload;
36
41
  }
37
42
  interface RequestCache {
38
43
  clientId: string;
@@ -99,6 +104,7 @@ interface PondSocketExpressApp extends Express {
99
104
  */
100
105
  upgrade(path: PondPath, handler: EndpointHandler): Endpoint;
101
106
  }
107
+ export type PondState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
102
108
  type Unsubscribe = () => void;
103
109
 
104
110
  export interface PresencePayload {
@@ -134,7 +140,13 @@ export declare class Channel {
134
140
  * @desc Monitors the connection state of the channel.
135
141
  * @param callback - The callback to call when the connection state changes.
136
142
  */
137
- public onConnectionChange(callback: (connected: ChannelState) => void): Unsubscribe;
143
+ public onConnectionChange(callback: (connected: boolean) => void): Unsubscribe;
144
+
145
+ /**
146
+ * @desc Monitors the channel state of the channel.
147
+ * @param callback - The callback to call when the connection state changes.
148
+ */
149
+ public onChannelStateChange (callback: (connected: ChannelState) => void): Unsubscribe;
138
150
 
139
151
  /**
140
152
  * @desc Detects when clients join the channel.
@@ -191,6 +203,11 @@ export declare class Channel {
191
203
  * @param callback - The callback to call when the presence changes.
192
204
  */
193
205
  public onUsersChange(callback: (users: PondPresence[]) => void): Unsubscribe;
206
+
207
+ /**
208
+ * @desc Gets the current connection state of the channel.
209
+ */
210
+ public isConnected (): boolean;
194
211
  }
195
212
 
196
213
  declare class PondSocketClient {