source: trip-planner-front/node_modules/socket.io-adapter/dist/cjs/index.js

Last change on this file was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

  • Property mode set to 100644
File size: 6.7 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Adapter = void 0;
4const events_1 = require("events");
5class Adapter extends events_1.EventEmitter {
6 /**
7 * In-memory adapter constructor.
8 *
9 * @param {Namespace} nsp
10 */
11 constructor(nsp) {
12 super();
13 this.nsp = nsp;
14 this.rooms = new Map();
15 this.sids = new Map();
16 this.encoder = nsp.server.encoder;
17 }
18 /**
19 * To be overridden
20 */
21 init() { }
22 /**
23 * To be overridden
24 */
25 close() { }
26 /**
27 * Adds a socket to a list of room.
28 *
29 * @param {SocketId} id the socket id
30 * @param {Set<Room>} rooms a set of rooms
31 * @public
32 */
33 addAll(id, rooms) {
34 if (!this.sids.has(id)) {
35 this.sids.set(id, new Set());
36 }
37 for (const room of rooms) {
38 this.sids.get(id).add(room);
39 if (!this.rooms.has(room)) {
40 this.rooms.set(room, new Set());
41 this.emit("create-room", room);
42 }
43 if (!this.rooms.get(room).has(id)) {
44 this.rooms.get(room).add(id);
45 this.emit("join-room", room, id);
46 }
47 }
48 }
49 /**
50 * Removes a socket from a room.
51 *
52 * @param {SocketId} id the socket id
53 * @param {Room} room the room name
54 */
55 del(id, room) {
56 if (this.sids.has(id)) {
57 this.sids.get(id).delete(room);
58 }
59 this._del(room, id);
60 }
61 _del(room, id) {
62 const _room = this.rooms.get(room);
63 if (_room != null) {
64 const deleted = _room.delete(id);
65 if (deleted) {
66 this.emit("leave-room", room, id);
67 }
68 if (_room.size === 0 && this.rooms.delete(room)) {
69 this.emit("delete-room", room);
70 }
71 }
72 }
73 /**
74 * Removes a socket from all rooms it's joined.
75 *
76 * @param {SocketId} id the socket id
77 */
78 delAll(id) {
79 if (!this.sids.has(id)) {
80 return;
81 }
82 for (const room of this.sids.get(id)) {
83 this._del(room, id);
84 }
85 this.sids.delete(id);
86 }
87 /**
88 * Broadcasts a packet.
89 *
90 * Options:
91 * - `flags` {Object} flags for this packet
92 * - `except` {Array} sids that should be excluded
93 * - `rooms` {Array} list of rooms to broadcast to
94 *
95 * @param {Object} packet the packet object
96 * @param {Object} opts the options
97 * @public
98 */
99 broadcast(packet, opts) {
100 const flags = opts.flags || {};
101 const basePacketOpts = {
102 preEncoded: true,
103 volatile: flags.volatile,
104 compress: flags.compress
105 };
106 packet.nsp = this.nsp.name;
107 const encodedPackets = this.encoder.encode(packet);
108 const packetOpts = encodedPackets.map(encodedPacket => {
109 if (typeof encodedPacket === "string") {
110 return Object.assign(Object.assign({}, basePacketOpts), { wsPreEncoded: "4" + encodedPacket // "4" being the "message" packet type in Engine.IO
111 });
112 }
113 else {
114 return basePacketOpts;
115 }
116 });
117 this.apply(opts, socket => {
118 for (let i = 0; i < encodedPackets.length; i++) {
119 socket.client.writeToEngine(encodedPackets[i], packetOpts[i]);
120 }
121 });
122 }
123 /**
124 * Gets a list of sockets by sid.
125 *
126 * @param {Set<Room>} rooms the explicit set of rooms to check.
127 */
128 sockets(rooms) {
129 const sids = new Set();
130 this.apply({ rooms }, socket => {
131 sids.add(socket.id);
132 });
133 return Promise.resolve(sids);
134 }
135 /**
136 * Gets the list of rooms a given socket has joined.
137 *
138 * @param {SocketId} id the socket id
139 */
140 socketRooms(id) {
141 return this.sids.get(id);
142 }
143 /**
144 * Returns the matching socket instances
145 *
146 * @param opts - the filters to apply
147 */
148 fetchSockets(opts) {
149 const sockets = [];
150 this.apply(opts, socket => {
151 sockets.push(socket);
152 });
153 return Promise.resolve(sockets);
154 }
155 /**
156 * Makes the matching socket instances join the specified rooms
157 *
158 * @param opts - the filters to apply
159 * @param rooms - the rooms to join
160 */
161 addSockets(opts, rooms) {
162 this.apply(opts, socket => {
163 socket.join(rooms);
164 });
165 }
166 /**
167 * Makes the matching socket instances leave the specified rooms
168 *
169 * @param opts - the filters to apply
170 * @param rooms - the rooms to leave
171 */
172 delSockets(opts, rooms) {
173 this.apply(opts, socket => {
174 rooms.forEach(room => socket.leave(room));
175 });
176 }
177 /**
178 * Makes the matching socket instances disconnect
179 *
180 * @param opts - the filters to apply
181 * @param close - whether to close the underlying connection
182 */
183 disconnectSockets(opts, close) {
184 this.apply(opts, socket => {
185 socket.disconnect(close);
186 });
187 }
188 apply(opts, callback) {
189 const rooms = opts.rooms;
190 const except = this.computeExceptSids(opts.except);
191 if (rooms.size) {
192 const ids = new Set();
193 for (const room of rooms) {
194 if (!this.rooms.has(room))
195 continue;
196 for (const id of this.rooms.get(room)) {
197 if (ids.has(id) || except.has(id))
198 continue;
199 const socket = this.nsp.sockets.get(id);
200 if (socket) {
201 callback(socket);
202 ids.add(id);
203 }
204 }
205 }
206 }
207 else {
208 for (const [id] of this.sids) {
209 if (except.has(id))
210 continue;
211 const socket = this.nsp.sockets.get(id);
212 if (socket)
213 callback(socket);
214 }
215 }
216 }
217 computeExceptSids(exceptRooms) {
218 const exceptSids = new Set();
219 if (exceptRooms && exceptRooms.size > 0) {
220 for (const room of exceptRooms) {
221 if (this.rooms.has(room)) {
222 this.rooms.get(room).forEach(sid => exceptSids.add(sid));
223 }
224 }
225 }
226 return exceptSids;
227 }
228 /**
229 * Send a packet to the other Socket.IO servers in the cluster
230 * @param packet - an array of arguments, which may include an acknowledgement callback at the end
231 */
232 serverSideEmit(packet) {
233 throw new Error("this adapter does not support the serverSideEmit() functionality");
234 }
235}
236exports.Adapter = Adapter;
Note: See TracBrowser for help on using the repository browser.