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

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

primeNG components

  • Property mode set to 100644
File size: 6.3 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 packetOpts = {
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 this.apply(opts, socket => {
109 socket.client.writeToEngine(encodedPackets, packetOpts);
110 });
111 }
112 /**
113 * Gets a list of sockets by sid.
114 *
115 * @param {Set<Room>} rooms the explicit set of rooms to check.
116 */
117 sockets(rooms) {
118 const sids = new Set();
119 this.apply({ rooms }, socket => {
120 sids.add(socket.id);
121 });
122 return Promise.resolve(sids);
123 }
124 /**
125 * Gets the list of rooms a given socket has joined.
126 *
127 * @param {SocketId} id the socket id
128 */
129 socketRooms(id) {
130 return this.sids.get(id);
131 }
132 /**
133 * Returns the matching socket instances
134 *
135 * @param opts - the filters to apply
136 */
137 fetchSockets(opts) {
138 const sockets = [];
139 this.apply(opts, socket => {
140 sockets.push(socket);
141 });
142 return Promise.resolve(sockets);
143 }
144 /**
145 * Makes the matching socket instances join the specified rooms
146 *
147 * @param opts - the filters to apply
148 * @param rooms - the rooms to join
149 */
150 addSockets(opts, rooms) {
151 this.apply(opts, socket => {
152 socket.join(rooms);
153 });
154 }
155 /**
156 * Makes the matching socket instances leave the specified rooms
157 *
158 * @param opts - the filters to apply
159 * @param rooms - the rooms to leave
160 */
161 delSockets(opts, rooms) {
162 this.apply(opts, socket => {
163 rooms.forEach(room => socket.leave(room));
164 });
165 }
166 /**
167 * Makes the matching socket instances disconnect
168 *
169 * @param opts - the filters to apply
170 * @param close - whether to close the underlying connection
171 */
172 disconnectSockets(opts, close) {
173 this.apply(opts, socket => {
174 socket.disconnect(close);
175 });
176 }
177 apply(opts, callback) {
178 const rooms = opts.rooms;
179 const except = this.computeExceptSids(opts.except);
180 if (rooms.size) {
181 const ids = new Set();
182 for (const room of rooms) {
183 if (!this.rooms.has(room))
184 continue;
185 for (const id of this.rooms.get(room)) {
186 if (ids.has(id) || except.has(id))
187 continue;
188 const socket = this.nsp.sockets.get(id);
189 if (socket) {
190 callback(socket);
191 ids.add(id);
192 }
193 }
194 }
195 }
196 else {
197 for (const [id] of this.sids) {
198 if (except.has(id))
199 continue;
200 const socket = this.nsp.sockets.get(id);
201 if (socket)
202 callback(socket);
203 }
204 }
205 }
206 computeExceptSids(exceptRooms) {
207 const exceptSids = new Set();
208 if (exceptRooms && exceptRooms.size > 0) {
209 for (const room of exceptRooms) {
210 if (this.rooms.has(room)) {
211 this.rooms.get(room).forEach(sid => exceptSids.add(sid));
212 }
213 }
214 }
215 return exceptSids;
216 }
217 /**
218 * Send a packet to the other Socket.IO servers in the cluster
219 * @param packet - an array of arguments, which may include an acknowledgement callback at the end
220 */
221 serverSideEmit(packet) {
222 throw new Error("this adapter does not support the serverSideEmit() functionality");
223 }
224}
225exports.Adapter = Adapter;
Note: See TracBrowser for help on using the repository browser.