@eleven-am/pondsocket 0.1.11 → 0.1.12
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/dist/client/channel.js +200 -0
- package/dist/client/index.d.ts +122 -0
- package/{pondClient/socket.js → dist/client/index.js} +30 -44
- package/dist/express/index.d.ts +36 -0
- package/dist/express/index.js +16 -0
- package/dist/index.d.ts +340 -0
- package/dist/index.js +4 -0
- package/dist/server/abstracts/abstractRequest.js +40 -0
- package/dist/server/abstracts/abstractRequest.test.js +41 -0
- package/dist/server/abstracts/abstractResponse.js +6 -0
- package/dist/server/abstracts/middleware.js +38 -0
- package/dist/server/abstracts/middleware.test.js +70 -0
- package/dist/server/channel/channelEngine.js +279 -0
- package/dist/server/channel/channelEngine.test.js +377 -0
- package/dist/server/channel/channelRequest.test.js +29 -0
- package/dist/server/channel/channelResponse.test.js +134 -0
- package/dist/server/channel/eventRequest.js +18 -0
- package/dist/server/channel/eventResponse.js +141 -0
- package/dist/server/endpoint/connectionResponse.js +50 -0
- package/dist/server/endpoint/endpoint.js +269 -0
- package/dist/server/endpoint/endpoint.test.js +406 -0
- package/dist/server/endpoint/endpointResponse.test.js +43 -0
- package/dist/server/pondChannel/joinRequest.js +29 -0
- package/dist/server/pondChannel/joinResponse.js +96 -0
- package/dist/server/pondChannel/pondChannel.js +161 -0
- package/dist/server/pondChannel/pondChannelResponse.test.js +108 -0
- package/dist/server/presence/presenceEngine.js +112 -0
- package/dist/server/presence/presenceEngine.test.js +104 -0
- package/dist/server/server/pondSocket.js +122 -0
- package/{pondSocket → dist/server/server}/server.test.js +7 -21
- package/{pondBase/baseClass.js → dist/server/utils/matchPattern.js} +33 -43
- package/{pondBase/baseClass.test.js → dist/server/utils/matchPattern.test.js} +16 -25
- package/dist/server/utils/subjectUtils.js +68 -0
- package/package.json +28 -12
- package/.eslintrc.js +0 -28
- package/base.d.ts +0 -1
- package/base.js +0 -17
- package/client.d.ts +0 -1
- package/client.js +0 -17
- package/index.d.ts +0 -1
- package/index.js +0 -17
- package/jest.config.js +0 -11
- package/pondBase/baseClass.d.ts +0 -55
- package/pondBase/enums.d.ts +0 -9
- package/pondBase/enums.js +0 -14
- package/pondBase/index.d.ts +0 -6
- package/pondBase/index.js +0 -22
- package/pondBase/pondBase.d.ts +0 -41
- package/pondBase/pondBase.js +0 -60
- package/pondBase/pondBase.test.js +0 -101
- package/pondBase/pubSub.d.ts +0 -82
- package/pondBase/pubSub.js +0 -158
- package/pondBase/pubSub.test.js +0 -332
- package/pondBase/simpleBase.d.ts +0 -126
- package/pondBase/simpleBase.js +0 -211
- package/pondBase/simpleBase.test.js +0 -153
- package/pondBase/types.d.ts +0 -2
- package/pondBase/types.js +0 -2
- package/pondClient/channel.d.ts +0 -77
- package/pondClient/channel.js +0 -167
- package/pondClient/index.d.ts +0 -2
- package/pondClient/index.js +0 -18
- package/pondClient/socket.d.ts +0 -41
- package/pondSocket/channel.d.ts +0 -129
- package/pondSocket/channel.js +0 -287
- package/pondSocket/channel.test.js +0 -377
- package/pondSocket/channelMiddleWare.d.ts +0 -28
- package/pondSocket/channelMiddleWare.js +0 -36
- package/pondSocket/endpoint.d.ts +0 -90
- package/pondSocket/endpoint.js +0 -320
- package/pondSocket/endpoint.test.js +0 -490
- package/pondSocket/enums.d.ts +0 -19
- package/pondSocket/enums.js +0 -25
- package/pondSocket/index.d.ts +0 -7
- package/pondSocket/index.js +0 -23
- package/pondSocket/pondChannel.d.ts +0 -79
- package/pondSocket/pondChannel.js +0 -211
- package/pondSocket/pondChannel.test.js +0 -430
- package/pondSocket/pondResponse.d.ts +0 -25
- package/pondSocket/pondResponse.js +0 -120
- package/pondSocket/pondSocket.d.ts +0 -47
- package/pondSocket/pondSocket.js +0 -94
- package/pondSocket/socketMiddleWare.d.ts +0 -6
- package/pondSocket/socketMiddleWare.js +0 -32
- package/pondSocket/types.d.ts +0 -74
- package/pondSocket/types.js +0 -2
- package/socket.d.ts +0 -1
- package/socket.js +0 -17
- package/tsconfig.eslint.json +0 -5
- package/tsconfig.json +0 -90
package/pondClient/channel.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Channel = void 0;
|
|
4
|
-
const pondSocket_1 = require("../pondSocket");
|
|
5
|
-
const pondBase_1 = require("../pondBase");
|
|
6
|
-
class Channel {
|
|
7
|
-
constructor(name, receiver, broadcaster, params) {
|
|
8
|
-
this._name = name;
|
|
9
|
-
this._joinParams = params || {};
|
|
10
|
-
this._broadcaster = broadcaster;
|
|
11
|
-
this._connection = new pondBase_1.Subject(false);
|
|
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
|
-
});
|
|
18
|
-
this._subscription = receiver.subscribe(data => {
|
|
19
|
-
if (data.channelName === name) {
|
|
20
|
-
this._receiver.publish(data);
|
|
21
|
-
this._connection.publish(true);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* @desc Connects to the channel.
|
|
27
|
-
*/
|
|
28
|
-
join() {
|
|
29
|
-
const joinMessage = {
|
|
30
|
-
action: pondSocket_1.ClientActions.JOIN_CHANNEL,
|
|
31
|
-
channelName: this._name,
|
|
32
|
-
event: pondSocket_1.ClientActions.JOIN_CHANNEL,
|
|
33
|
-
payload: this._joinParams
|
|
34
|
-
};
|
|
35
|
-
this._receiver.subscribe(data => {
|
|
36
|
-
if (data.action === pondSocket_1.ServerActions.PRESENCE) {
|
|
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
|
-
});
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
this._broadcaster.publish(joinMessage);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* @desc Disconnects from the channel.
|
|
50
|
-
*/
|
|
51
|
-
leave() {
|
|
52
|
-
const leaveMessage = {
|
|
53
|
-
action: pondSocket_1.ClientActions.LEAVE_CHANNEL,
|
|
54
|
-
channelName: this._name,
|
|
55
|
-
event: pondSocket_1.ClientActions.LEAVE_CHANNEL,
|
|
56
|
-
payload: {}
|
|
57
|
-
};
|
|
58
|
-
this._broadcaster.publish(leaveMessage);
|
|
59
|
-
this._connection.publish(false);
|
|
60
|
-
this._subscription.unsubscribe();
|
|
61
|
-
this._connection.clear();
|
|
62
|
-
this._receiver.clear();
|
|
63
|
-
this._presence.clear();
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* @desc Monitors the channel for messages.
|
|
67
|
-
* @param event - The event to monitor.
|
|
68
|
-
* @param callback - The callback to call when a message is received.
|
|
69
|
-
*/
|
|
70
|
-
onMessage(event, callback) {
|
|
71
|
-
return this._receiver.subscribe(data => {
|
|
72
|
-
if (data.action === pondSocket_1.ServerActions.MESSAGE && data.event === event)
|
|
73
|
-
callback(data.payload);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* @desc Broadcasts a message to the channel, including yourself.
|
|
78
|
-
* @param event - The event to send.
|
|
79
|
-
* @param payload - The message to send.
|
|
80
|
-
*/
|
|
81
|
-
broadcast(event, payload) {
|
|
82
|
-
const message = {
|
|
83
|
-
action: pondSocket_1.ClientActions.BROADCAST, channelName: this._name, payload: payload, event: event
|
|
84
|
-
};
|
|
85
|
-
this._broadcaster.publish(message);
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* @desc Broadcasts a message to every other client in the channel except yourself.
|
|
89
|
-
* @param event - The event to send.
|
|
90
|
-
* @param payload - The message to send.
|
|
91
|
-
*/
|
|
92
|
-
broadcastFrom(event, payload) {
|
|
93
|
-
const message = {
|
|
94
|
-
action: pondSocket_1.ClientActions.BROADCAST_FROM, channelName: this._name, payload: payload, event: event
|
|
95
|
-
};
|
|
96
|
-
this._broadcaster.publish(message);
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* @desc Sends a message to specific clients in the channel.
|
|
100
|
-
* @param event - The event to send.
|
|
101
|
-
* @param payload - The message to send.
|
|
102
|
-
* @param recipient - The clients to send the message to.
|
|
103
|
-
*/
|
|
104
|
-
sendMessage(event, payload, recipient) {
|
|
105
|
-
const message = {
|
|
106
|
-
action: pondSocket_1.ClientActions.SEND_MESSAGE_TO_USER,
|
|
107
|
-
channelName: this._name,
|
|
108
|
-
payload: payload,
|
|
109
|
-
event: event,
|
|
110
|
-
addresses: recipient
|
|
111
|
-
};
|
|
112
|
-
this._broadcaster.publish(message);
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* @desc Updates the presence state of the current client in the channel.
|
|
116
|
-
* @param presence - The presence state to update.
|
|
117
|
-
*/
|
|
118
|
-
updatePresence(presence) {
|
|
119
|
-
const message = {
|
|
120
|
-
action: pondSocket_1.ClientActions.UPDATE_PRESENCE,
|
|
121
|
-
channelName: this._name,
|
|
122
|
-
payload: presence,
|
|
123
|
-
event: pondSocket_1.ClientActions.UPDATE_PRESENCE
|
|
124
|
-
};
|
|
125
|
-
this._broadcaster.publish(message);
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* @desc Detects when clients join the channel.
|
|
129
|
-
* @param callback - The callback to call when a client joins the channel.
|
|
130
|
-
*/
|
|
131
|
-
onJoin(callback) {
|
|
132
|
-
return this._presence.subscribe(data => {
|
|
133
|
-
if (data.action === pondBase_1.PondBaseActions.ADD_TO_POND)
|
|
134
|
-
callback(data.change);
|
|
135
|
-
});
|
|
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
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* @desc Monitors the connection state of the channel.
|
|
159
|
-
* @param callback - The callback to call when the connection state changes.
|
|
160
|
-
*/
|
|
161
|
-
onConnectionChange(callback) {
|
|
162
|
-
return this._connection.subscribe(data => {
|
|
163
|
-
callback(data);
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
exports.Channel = Channel;
|
package/pondClient/index.d.ts
DELETED
package/pondClient/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./socket"), exports);
|
|
18
|
-
__exportStar(require("./channel"), exports);
|
package/pondClient/socket.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import {Channel, ChannelParams} from "./channel";
|
|
2
|
-
import {PondMessage} from "../pondSocket";
|
|
3
|
-
|
|
4
|
-
declare type PondParams = {
|
|
5
|
-
[key: string]: string;
|
|
6
|
-
};
|
|
7
|
-
declare type PondState = "CONNECTING" | "OPEN" | "CLOSING" | "CLOSED";
|
|
8
|
-
|
|
9
|
-
export declare class PondClient {
|
|
10
|
-
|
|
11
|
-
constructor(endpoint: string, params?: PondParams);
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @desc Returns the current state of the socket.
|
|
15
|
-
*/
|
|
16
|
-
getState(): PondState;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @desc Connects to the server and returns the socket.
|
|
20
|
-
*/
|
|
21
|
-
connect(backoff?: number): void;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @desc Disconnects the socket from the server.
|
|
25
|
-
*/
|
|
26
|
-
disconnect(): void;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @desc An event that is triggered when the socket receives a message.
|
|
30
|
-
* @param event - The event to subscribe to.
|
|
31
|
-
* @param callback - The callback to be called when the event is triggered.
|
|
32
|
-
*/
|
|
33
|
-
onMessage(event: string, callback: (message: PondMessage) => void): import("../pondBase").Subscription;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @desc Creates a channel with the given name and params.
|
|
37
|
-
* @param name - The name of the channel.
|
|
38
|
-
* @param params - The params to send to the server.
|
|
39
|
-
*/
|
|
40
|
-
createChannel(name: string, params?: ChannelParams): Channel;
|
|
41
|
-
}
|
package/pondSocket/channel.d.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import {default_t, Subscription} from "../pondBase";
|
|
2
|
-
import {NewUser, PondAssigns, PondChannelData, PondMessage, PondPresence, ServerMessage} from "./types";
|
|
3
|
-
import {PondSenders, ServerActions} from "./enums";
|
|
4
|
-
import {ChannelHandler} from "./channelMiddleWare";
|
|
5
|
-
|
|
6
|
-
export interface ChannelInfo {
|
|
7
|
-
name: string;
|
|
8
|
-
channelData: PondChannelData;
|
|
9
|
-
presence: PondPresence[];
|
|
10
|
-
assigns: PondPresence[];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export declare class Channel {
|
|
14
|
-
readonly name: string;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @desc Returns the channel info
|
|
18
|
-
*/
|
|
19
|
-
get info(): Readonly<ChannelInfo>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @desc Gets the channel's data
|
|
23
|
-
*/
|
|
24
|
-
get data(): Readonly<PondChannelData>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @desc Sets the channel's data
|
|
28
|
-
* @param data
|
|
29
|
-
*/
|
|
30
|
-
set data(data: PondChannelData);
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @desc Gets the channel's presence
|
|
34
|
-
*/
|
|
35
|
-
get presence(): PondPresence[];
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @desc Gets the channel's assigns
|
|
39
|
-
*/
|
|
40
|
-
get assigns(): PondAssigns[];
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @desc Gets a user's information
|
|
44
|
-
* @param clientId - The clientId of the user
|
|
45
|
-
*/
|
|
46
|
-
getUserInfo(clientId: string): {
|
|
47
|
-
presence: PondPresence;
|
|
48
|
-
assigns: PondAssigns;
|
|
49
|
-
} | null;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* @desc Checks if a user exists in the channel
|
|
53
|
-
* @param clientId - The clientId of the user
|
|
54
|
-
*/
|
|
55
|
-
hasUser(clientId: string): boolean;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* @desc Adds a new user to the channel
|
|
59
|
-
* @param user - The user to add to the channel
|
|
60
|
-
*/
|
|
61
|
-
addUser(user: NewUser): void;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @desc Removes a user or group of users from the channel
|
|
65
|
-
* @param clientIds - The clientIds of the users to remove
|
|
66
|
-
*/
|
|
67
|
-
removeUser(clientIds: string | string[]): void;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @desc Subscribes to the presence changes occuring in the channel
|
|
71
|
-
* @param callback - The callback to call when a presence change occurs
|
|
72
|
-
*/
|
|
73
|
-
onPresenceChange(callback: (message: ServerMessage) => void): Subscription;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* @desc Subscribes to the message events occuring in the channel
|
|
77
|
-
* @param callback - The callback to call when a message event occurs
|
|
78
|
-
*/
|
|
79
|
-
onMessage(callback: ChannelHandler): void;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @desc Updates the state of a user in the channel
|
|
83
|
-
* @param clientId - The clientId of the user to update
|
|
84
|
-
* @param presence - The new presence of the user
|
|
85
|
-
* @param assigns - The new assigns of the user
|
|
86
|
-
*/
|
|
87
|
-
updateUser(clientId: string, presence: PondPresence, assigns: PondAssigns): void;
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* @desc Broadcasts a message to all users in the channel
|
|
91
|
-
* @param event - The event name
|
|
92
|
-
* @param message - The message to send
|
|
93
|
-
* @param sender - The sender of the message
|
|
94
|
-
*/
|
|
95
|
-
broadcast(event: string, message: PondMessage, sender?: PondSenders | string): void;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* @desc Broadcasts a message to all users in the channel except the sender
|
|
99
|
-
* @param event - The event name
|
|
100
|
-
* @param message - The message to send
|
|
101
|
-
* @param clientId - The client id of the sender
|
|
102
|
-
*/
|
|
103
|
-
broadcastFrom(event: string, message: PondMessage, clientId: string): void;
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* @desc Sends a message to a specific user or group of users
|
|
107
|
-
* @param event - The event name
|
|
108
|
-
* @param clientId - The client id of the user to send the message to
|
|
109
|
-
* @param message - The message to send
|
|
110
|
-
* @param sender - The client id of the sender
|
|
111
|
-
*/
|
|
112
|
-
sendTo(event: string, message: PondMessage, sender: string, clientId: string | string[]): void;
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* @desc Subscribes to a channel event, used only by the sockets to subscribe to all events
|
|
116
|
-
* @param clientId - The client id of the user to send the message to
|
|
117
|
-
* @param callback - The callback to call when a message is received
|
|
118
|
-
*/
|
|
119
|
-
subscribeToMessages(clientId: string, callback: (message: ServerMessage) => void): Subscription;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* @desc Sends a message to a specific user without running the middleware
|
|
123
|
-
* @param event - The event name
|
|
124
|
-
* @param message - The message to send
|
|
125
|
-
* @param client - The client id of the user to send the message to
|
|
126
|
-
* @param action - The action to send
|
|
127
|
-
*/
|
|
128
|
-
respondToClient(event: string, message: default_t, client: string, action?: ServerActions): void;
|
|
129
|
-
}
|
package/pondSocket/channel.js
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Channel = void 0;
|
|
4
|
-
const pondBase_1 = require("../pondBase");
|
|
5
|
-
const enums_1 = require("./enums");
|
|
6
|
-
const channelMiddleWare_1 = require("./channelMiddleWare");
|
|
7
|
-
class Channel extends pondBase_1.BaseClass {
|
|
8
|
-
constructor(name, middleWare, removeChannel) {
|
|
9
|
-
super();
|
|
10
|
-
this.name = name;
|
|
11
|
-
this._channelPresence = new pondBase_1.PondBase();
|
|
12
|
-
this._channelAssigns = new pondBase_1.SimpleBase();
|
|
13
|
-
this._channelData = {};
|
|
14
|
-
this._middleWare = new channelMiddleWare_1.ChannelMiddleware();
|
|
15
|
-
this.removeChannel = removeChannel;
|
|
16
|
-
this._messages = new pondBase_1.Broadcast();
|
|
17
|
-
this._middleWare.merge(middleWare);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* @desc Returns the channel info
|
|
21
|
-
*/
|
|
22
|
-
get info() {
|
|
23
|
-
const data = {
|
|
24
|
-
name: this.name, channelData: this.data, presence: this.presence, assigns: this.assigns
|
|
25
|
-
};
|
|
26
|
-
return Object.freeze(data);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* @desc Gets the channel's data
|
|
30
|
-
*/
|
|
31
|
-
get data() {
|
|
32
|
-
const result = Object.assign({}, this._channelData);
|
|
33
|
-
return Object.freeze(result);
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* @desc Sets the channel's data
|
|
37
|
-
* @param data
|
|
38
|
-
*/
|
|
39
|
-
set data(data) {
|
|
40
|
-
this._channelData = Object.assign(Object.assign({}, this._channelData), data);
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* @desc Gets the channel's presence
|
|
44
|
-
*/
|
|
45
|
-
get presence() {
|
|
46
|
-
return this._channelPresence.map(presence => presence);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* @desc Gets the channel's assigns
|
|
50
|
-
*/
|
|
51
|
-
get assigns() {
|
|
52
|
-
return this._channelAssigns.map(assigns => assigns);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* @desc Gets a user's information
|
|
56
|
-
* @param clientId - The clientId of the user
|
|
57
|
-
*/
|
|
58
|
-
getUserInfo(clientId) {
|
|
59
|
-
const client = this._retrieveUser(clientId);
|
|
60
|
-
if (!client)
|
|
61
|
-
return null;
|
|
62
|
-
return {
|
|
63
|
-
presence: client.presence.doc, assigns: client.assigns.doc
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* @desc Checks if a user exists in the channel
|
|
68
|
-
* @param clientId - The clientId of the user
|
|
69
|
-
*/
|
|
70
|
-
hasUser(clientId) {
|
|
71
|
-
return this._channelPresence.has(clientId);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* @desc Adds a new user to the channel
|
|
75
|
-
* @param user - The user to add to the channel
|
|
76
|
-
*/
|
|
77
|
-
addUser(user) {
|
|
78
|
-
const clientId = user.client.clientId;
|
|
79
|
-
if (this.hasUser(clientId))
|
|
80
|
-
throw new Error(`User with clientId ${clientId} already exists in channel ${this.name}`);
|
|
81
|
-
this._channelPresence.set(clientId, Object.assign(Object.assign({}, user.presence), { id: clientId }));
|
|
82
|
-
this._channelAssigns.set(clientId, user.assigns);
|
|
83
|
-
this._channelData = Object.assign(Object.assign(Object.assign({}, this._channelData), user.channelData));
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* @desc Removes a user or group of users from the channel
|
|
87
|
-
* @param clientIds - The clientIds of the users to remove
|
|
88
|
-
*/
|
|
89
|
-
removeUser(clientIds) {
|
|
90
|
-
const clients = Array.isArray(clientIds) ? clientIds : [clientIds];
|
|
91
|
-
clients.forEach(clientId => {
|
|
92
|
-
const client = this._retrieveUser(clientId);
|
|
93
|
-
if (client) {
|
|
94
|
-
client.presence.removeDoc();
|
|
95
|
-
client.assigns.removeDoc();
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
if (this._channelPresence.size === 0)
|
|
99
|
-
this.removeChannel();
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* @desc Subscribes to the presence changes occurring in the channel
|
|
103
|
-
* @param callback - The callback to call when a presence change occurs
|
|
104
|
-
*/
|
|
105
|
-
onPresenceChange(callback) {
|
|
106
|
-
return this._channelPresence.subscribe((docs, change, action) => {
|
|
107
|
-
const message = {
|
|
108
|
-
action: enums_1.ServerActions.PRESENCE,
|
|
109
|
-
payload: { presence: docs, change },
|
|
110
|
-
event: action,
|
|
111
|
-
channelName: this.name,
|
|
112
|
-
};
|
|
113
|
-
callback(message);
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* @desc Subscribes to the message events occurring in the channel
|
|
118
|
-
* @param callback - The callback to call when a message event occurs
|
|
119
|
-
*/
|
|
120
|
-
onMessage(callback) {
|
|
121
|
-
this._middleWare.use(callback);
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* @desc Updates the state of a user in the channel
|
|
125
|
-
* @param clientId - The clientId of the user to update
|
|
126
|
-
* @param presence - The new presence of the user
|
|
127
|
-
* @param assigns - The new assigns of the user
|
|
128
|
-
*/
|
|
129
|
-
updateUser(clientId, presence, assigns) {
|
|
130
|
-
const client = this._retrieveUser(clientId);
|
|
131
|
-
if (client) {
|
|
132
|
-
client.assigns.updateDoc(Object.assign(Object.assign({}, client.assigns.doc), assigns));
|
|
133
|
-
const presenceDoc = Object.assign(Object.assign({}, client.presence.doc), presence);
|
|
134
|
-
if (!this.areEqual(presenceDoc, client.presence.doc))
|
|
135
|
-
client.presence.updateDoc(presenceDoc);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* @desc Broadcasts a message to all users in the channel
|
|
140
|
-
* @param event - The event name
|
|
141
|
-
* @param message - The message to send
|
|
142
|
-
* @param sender - The sender of the message
|
|
143
|
-
*/
|
|
144
|
-
broadcast(event, message, sender = enums_1.PondSenders.POND_CHANNEL) {
|
|
145
|
-
const client = this._retrieveUser(sender);
|
|
146
|
-
if (!client && !Object.values(enums_1.PondSenders).includes(sender))
|
|
147
|
-
throw new Error(`Client with clientId ${sender} does not exist in channel ${this.name}`);
|
|
148
|
-
const channelEvent = {
|
|
149
|
-
client: {
|
|
150
|
-
clientId: sender,
|
|
151
|
-
clientAssigns: client ? client.assigns.doc : {},
|
|
152
|
-
clientPresence: client ? client.presence.doc : {}
|
|
153
|
-
},
|
|
154
|
-
event: event,
|
|
155
|
-
payload: message,
|
|
156
|
-
channel: this,
|
|
157
|
-
};
|
|
158
|
-
const newMessage = {
|
|
159
|
-
action: enums_1.ServerActions.MESSAGE, payload: message, event: event, channelName: this.name,
|
|
160
|
-
};
|
|
161
|
-
void this._middleWare.run(channelEvent, hasErrored => {
|
|
162
|
-
if (!hasErrored)
|
|
163
|
-
this._sendToClients(this._channelAssigns.keys, newMessage);
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* @desc Broadcasts a message to all users in the channel except the sender
|
|
168
|
-
* @param event - The event name
|
|
169
|
-
* @param message - The message to send
|
|
170
|
-
* @param clientId - The client id of the sender
|
|
171
|
-
*/
|
|
172
|
-
broadcastFrom(event, message, clientId) {
|
|
173
|
-
const client = this._retrieveUser(clientId);
|
|
174
|
-
if (!client)
|
|
175
|
-
throw new Error(`Client with clientId ${clientId} does not exist in channel ${this.name}`);
|
|
176
|
-
const newMessage = {
|
|
177
|
-
action: enums_1.ServerActions.MESSAGE, payload: message, event: event, channelName: this.name,
|
|
178
|
-
};
|
|
179
|
-
const channelEvent = {
|
|
180
|
-
client: {
|
|
181
|
-
clientId: clientId,
|
|
182
|
-
clientAssigns: client.assigns.doc,
|
|
183
|
-
clientPresence: client.presence.doc
|
|
184
|
-
},
|
|
185
|
-
event: event,
|
|
186
|
-
payload: message,
|
|
187
|
-
channel: this,
|
|
188
|
-
};
|
|
189
|
-
void this._middleWare.run(channelEvent, hasErrored => {
|
|
190
|
-
if (!hasErrored) {
|
|
191
|
-
const clientIds = this._channelAssigns.keys.filter(id => id !== clientId);
|
|
192
|
-
this._sendToClients(clientIds, newMessage);
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* @desc Sends a message to a specific user or group of users
|
|
198
|
-
* @param event - The event name
|
|
199
|
-
* @param clientId - The client id of the user to send the message to
|
|
200
|
-
* @param message - The message to send
|
|
201
|
-
* @param sender - The client id of the sender
|
|
202
|
-
*/
|
|
203
|
-
sendTo(event, message, sender, clientId) {
|
|
204
|
-
const client = this._retrieveUser(sender);
|
|
205
|
-
if (!client && !Object.values(enums_1.PondSenders).includes(sender))
|
|
206
|
-
throw new Error('Client not found');
|
|
207
|
-
const clientIds = Array.isArray(clientId) ? clientId : [clientId];
|
|
208
|
-
const notFound = clientIds.filter(id => !this._channelAssigns.has(id));
|
|
209
|
-
if (notFound.length > 0)
|
|
210
|
-
throw new Error(`Client(s) with clientId(s) ${notFound.join(', ')} were not found in channel ${this.name}`);
|
|
211
|
-
const newMessage = {
|
|
212
|
-
action: enums_1.ServerActions.MESSAGE, payload: message, event: event, channelName: this.name,
|
|
213
|
-
};
|
|
214
|
-
const channelEvent = {
|
|
215
|
-
client: {
|
|
216
|
-
clientId: sender,
|
|
217
|
-
clientAssigns: client ? client.assigns.doc : {},
|
|
218
|
-
clientPresence: client ? client.presence.doc : {}
|
|
219
|
-
},
|
|
220
|
-
channel: this,
|
|
221
|
-
event: event,
|
|
222
|
-
payload: message,
|
|
223
|
-
};
|
|
224
|
-
void this._middleWare.run(channelEvent, hasErrored => {
|
|
225
|
-
if (!hasErrored)
|
|
226
|
-
this._sendToClients(clientIds, newMessage);
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* @desc Subscribes to a channel event, used only by the sockets to subscribe to all events
|
|
231
|
-
* @param clientId - The client id of the user to send the message to
|
|
232
|
-
* @param callback - The callback to call when a message is received
|
|
233
|
-
*/
|
|
234
|
-
subscribeToMessages(clientId, callback) {
|
|
235
|
-
const sub1 = this._messages.subscribe(({ clients, message }) => {
|
|
236
|
-
if (clients.includes(clientId))
|
|
237
|
-
callback(message);
|
|
238
|
-
});
|
|
239
|
-
const sub2 = this.onPresenceChange(callback);
|
|
240
|
-
return {
|
|
241
|
-
unsubscribe: () => {
|
|
242
|
-
sub1.unsubscribe();
|
|
243
|
-
sub2.unsubscribe();
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* @desc Sends a message to a specific user without running the middleware
|
|
249
|
-
* @param event - The event name
|
|
250
|
-
* @param message - The message to send
|
|
251
|
-
* @param client - The client id of the user to send the message to
|
|
252
|
-
* @param action - The action to send
|
|
253
|
-
*/
|
|
254
|
-
respondToClient(event, message, client, action = enums_1.ServerActions.MESSAGE) {
|
|
255
|
-
const newMessage = {
|
|
256
|
-
action: action,
|
|
257
|
-
payload: message, event: event,
|
|
258
|
-
channelName: this.name,
|
|
259
|
-
};
|
|
260
|
-
this._sendToClients([client], newMessage);
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* @desc Sends a message to a specific user or group of users except the sender
|
|
264
|
-
* @param clients - The client id of the user to send the message to
|
|
265
|
-
* @param message - The message to send
|
|
266
|
-
* @private
|
|
267
|
-
*/
|
|
268
|
-
_sendToClients(clients, message) {
|
|
269
|
-
void this._messages.publish({ clients, message });
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* @desc Retrieves a user from the channel
|
|
273
|
-
* @param clientId - The client id of the user to retrieve
|
|
274
|
-
* @private
|
|
275
|
-
*/
|
|
276
|
-
_retrieveUser(clientId) {
|
|
277
|
-
const assigns = this._channelAssigns.get(clientId);
|
|
278
|
-
const presence = this._channelPresence.get(clientId);
|
|
279
|
-
if (assigns && presence !== null) {
|
|
280
|
-
return {
|
|
281
|
-
assigns: assigns, presence: presence,
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
return null;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
exports.Channel = Channel;
|