angelia.io 4.0.87 → 4.0.89

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": "angelia.io",
3
- "version": "4.0.87",
3
+ "version": "4.0.89",
4
4
  "description": "WebSockets Server and Client API for node.js and the browser, with rooms support.",
5
5
  "author": "@titoBouzout @boredofnames",
6
6
  "license": "GPL",
@@ -13,7 +13,8 @@
13
13
  "./client": {
14
14
  "types": "./types/client/index.js",
15
15
  "default": "./client/index.js"
16
- }
16
+ },
17
+ "./*": null
17
18
  },
18
19
  "dependencies": {
19
20
  "bufferutil": "^4.0.8",
package/readme.md CHANGED
@@ -213,8 +213,7 @@ As in `socket.on('connect', () => console.log('connect happened!'))`
213
213
  flag `persistent`
214
214
  4. a socket will leave automatically all rooms on disconnection
215
215
  5. `socket.rooms` is a set with all the rooms the socket joined
216
- 6. a room list is an iterable that returns all of the sockets in the
217
- room
216
+ 6. `id` is assigned to the room on creation
218
217
 
219
218
  ```js
220
219
  import { Room, Rooms } from 'angelia.io/server'
@@ -245,14 +244,14 @@ const games = new Rooms(GameRoom)
245
244
 
246
245
  class Connection {
247
246
  connect(socket) {
248
- game.join(socket, 'room id here')
249
- socket.room.id === 'room id here'
247
+ games.join(socket, 'room id here')
248
+ socket.game.id === 'room id here'
250
249
  console.log(socket.rooms)
251
- game.leave(socket, 'room id here')
250
+ games.leave(socket, 'room id here')
252
251
 
253
- game.join(socket, 'a different room')
254
- socket.room.id === 'a different room'
255
- game.leave(socket, 'a different room')
252
+ games.join(socket, 'a different room')
253
+ socket.game.id === 'a different room'
254
+ games.leave(socket, 'a different room')
256
255
  }
257
256
  }
258
257
 
@@ -262,6 +261,8 @@ Server.listen()
262
261
 
263
262
  ### `Room` Class
264
263
 
264
+ Iterating returns the sockets
265
+
265
266
  | signature | kind | description |
266
267
  | ------------------------------------- | ------- | ------------------------------------------------ |
267
268
  | `onCreate(socket)` | method | dispatched when the room is created |
@@ -272,11 +273,38 @@ Server.listen()
272
273
  | `connections` | number | number of sockets in the room |
273
274
  | `sockets` | Set | sockets in the room |
274
275
  | `emit(key, [value])` | method | emits to all sockets in the room |
275
- | `once(key, [value])` | method | emits once all sockets in the room |
276
+ | `once(key, [value])` | method | emits once to all sockets in the room |
276
277
  | `broadcast(socket, key, [value])` | method | emits to other sockets in the room |
277
278
  | `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in the room |
278
279
  | `id` | any | the room id |
279
280
 
281
+ ### `Rooms` Class
282
+
283
+ Iterating returns the rooms
284
+
285
+ | signature | kind | description |
286
+ | ------------------------------------- | -------- | --------------------------------------------- |
287
+ | `get(id)` | method | gets room with such an `id` |
288
+ | `join(socket, [id])` | method | adds socket to room `id` |
289
+ | `leave(socket, [id])` | method | removes socket from room `id` |
290
+ | `connections` | number | number of sockets in this room list |
291
+ | `sockets` | Iterator | sockets in this room list |
292
+ | `emit(key, [value])` | method | emits to all sockets in this room list |
293
+ | `once(key, [value])` | method | emits once to all sockets in this room list |
294
+ | `broadcast(socket, key, [value])` | method | emits to other sockets in this room list |
295
+ | `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in this room list |
296
+ | `map(fn)` | method | returns mapped array |
297
+ | `filter(fn)` | method | returns mapped filtered array |
298
+
299
+ ### `sockets` Object
300
+
301
+ Like `Set` but with two aditional methods
302
+
303
+ | signature | kind | description |
304
+ | ------------ | ------ | ----------------------------- |
305
+ | `map(fn)` | method | returns mapped array |
306
+ | `filter(fn)` | method | returns mapped filtered array |
307
+
280
308
  ## Authors
281
309
 
282
310
  - Tito Bouzout https://github.com/titoBouzout
package/server/Emitter.js CHANGED
@@ -1,7 +1,9 @@
1
+ import { MapeableSet } from './utils.js'
2
+
1
3
  export class Emitter {
2
4
  constructor() {
3
- /** @type Set<import('./Socket.js').Socket> */
4
- this.sockets = new Set()
5
+ /** @type MapeableSet<import('./Socket.js').Socket> */
6
+ this.sockets = new MapeableSet()
5
7
  }
6
8
 
7
9
  get connections() {
@@ -20,6 +22,7 @@ export class Emitter {
20
22
  socket.once(d)
21
23
  }
22
24
  }
25
+ /** @param me {import('./Socket.js').Socket} */
23
26
  broadcast(me, k, v) {
24
27
  const d = [k, v]
25
28
  for (const socket of this.sockets) {
@@ -28,6 +31,7 @@ export class Emitter {
28
31
  }
29
32
  }
30
33
  }
34
+ /** @param me {import('./Socket.js').Socket} */
31
35
  broadcastOnce(me, k, v) {
32
36
  const d = [k, v]
33
37
  for (const socket of this.sockets) {
package/server/Room.js CHANGED
@@ -6,11 +6,16 @@ export class Room extends Emitter {
6
6
  id
7
7
  persistent
8
8
 
9
+ /** @param socket {import('./Socket.js').Socket} */
9
10
  onCreate(socket) {}
11
+ /** @param socket {import('./Socket.js').Socket} */
10
12
  onDelete(socket) {}
13
+ /** @param socket {import('./Socket.js').Socket} */
11
14
  onJoin(socket) {}
15
+ /** @param socket {import('./Socket.js').Socket} */
12
16
  onLeave(socket) {}
13
17
 
18
+ /** @param socket {import('./Socket.js').Socket} */
14
19
  [join](socket) {
15
20
  this.sockets.add(socket)
16
21
 
@@ -18,6 +23,7 @@ export class Room extends Emitter {
18
23
 
19
24
  this.onJoin(socket)
20
25
  }
26
+ /** @param socket {import('./Socket.js').Socket} */
21
27
  [leave](socket) {
22
28
  this.sockets.delete(socket)
23
29
 
package/server/Rooms.js CHANGED
@@ -1,34 +1,74 @@
1
- import { join, leave } from './constants.js'
1
+ import { join, leave, parent } from './constants.js'
2
+ import { Emitter } from './Emitter.js'
3
+ import { arrayFrom } from './utils.js'
2
4
 
3
- export class Rooms {
4
- constructor(Room) {
5
- this.Room = Room
5
+ export class Rooms extends Emitter {
6
+ /** @param {Object} RoomClass */
7
+ constructor(RoomClass) {
8
+ super()
9
+
10
+ this.RoomClass = RoomClass
6
11
  this.rooms = new Map()
12
+
13
+ Object.defineProperty(this, 'sockets', {
14
+ get: this.Sockets.bind(this),
15
+ enumerable: true,
16
+ })
7
17
  }
18
+
19
+ get(id) {
20
+ return this.rooms.get(id)
21
+ }
22
+
8
23
  join(socket, id = undefined) {
9
24
  let room = this.rooms.get(id)
10
25
  if (!room) {
11
- room = new this.Room()
12
- room.id = room.id || id
26
+ room = new this.RoomClass()
27
+ room[parent] = this
28
+ room.id = id
13
29
  this.rooms.set(id, room)
14
30
 
15
31
  room.onCreate(socket)
16
32
  }
17
-
18
33
  room[join](socket)
19
34
  }
35
+
20
36
  leave(socket, id = undefined) {
21
37
  const room = this.rooms.get(id)
22
38
  if (room) {
23
39
  room[leave](socket)
24
-
25
- if (!room.persistent && room.size === 0) {
26
- room.onDelete(socket)
40
+ if (!room.persistent && room.sockets.size === 0) {
27
41
  this.rooms.delete(id)
42
+ room.onDelete(socket)
28
43
  }
29
44
  }
30
45
  }
31
46
 
47
+ get connections() {
48
+ let count = 0
49
+ for (const room of this.rooms.values()) {
50
+ count += room.sockets.size
51
+ }
52
+ return count
53
+ }
54
+
55
+ *Sockets() {
56
+ for (const room of this.rooms.values()) {
57
+ for (const socket of room.sockets) {
58
+ yield socket
59
+ }
60
+ }
61
+ }
62
+
63
+ map(fn) {
64
+ return arrayFrom(this.rooms.values(), fn)
65
+ }
66
+ filter(fn) {
67
+ return this.map().filter(fn)
68
+ }
69
+ toJSON() {
70
+ return this.map()
71
+ }
32
72
  [Symbol.iterator]() {
33
73
  return this.rooms.values()
34
74
  }
package/server/Server.js CHANGED
@@ -158,7 +158,7 @@ export default new (class Server extends Emitter {
158
158
  on(listener) {
159
159
  const instance = new listener()
160
160
 
161
- const methods = [
161
+ const methods = listener.listeners || [
162
162
  // todo maybe use getPrototypeOf ?
163
163
  ...Object.getOwnPropertyNames(instance.__proto__),
164
164
  ...Object.getOwnPropertyNames(instance),
@@ -175,6 +175,8 @@ export default new (class Server extends Emitter {
175
175
  }
176
176
  }
177
177
  }
178
+
179
+ return instance
178
180
  }
179
181
 
180
182
  // private
package/server/Socket.js CHANGED
@@ -1,6 +1,12 @@
1
- import { leave } from './constants.js'
1
+ import { parent } from './constants.js'
2
2
 
3
- import { fromEntries, inspect, isArray, parse } from './utils.js'
3
+ import {
4
+ fromEntries,
5
+ inspect,
6
+ isArray,
7
+ MapeableSet,
8
+ parse,
9
+ } from './utils.js'
4
10
 
5
11
  export class Socket {
6
12
  constructor(socket, server, request) {
@@ -34,7 +40,7 @@ export class Socket {
34
40
  this.ping = 0
35
41
  this.timedout = false
36
42
 
37
- this.rooms = new Set()
43
+ this.rooms = new MapeableSet()
38
44
  }
39
45
  emit(k, v, cb) {
40
46
  if (!this.messages.length) {
@@ -91,7 +97,7 @@ export class Socket {
91
97
  this.server.sockets.delete(this)
92
98
 
93
99
  for (const room of this.rooms) {
94
- room[leave](this)
100
+ room[parent].leave(this, room.id)
95
101
  }
96
102
 
97
103
  this.server.events.disconnect &&
@@ -155,7 +161,7 @@ export class Socket {
155
161
  this.server.events[m[0]](
156
162
  this,
157
163
  m[1],
158
- m[2] && this.doreply.bind(null, m[2]),
164
+ m[2] && this.doreply.bind(this, m[2]),
159
165
  )
160
166
  } else {
161
167
  this.server.messagesGarbage++
@@ -1,2 +1,3 @@
1
1
  export const join = Symbol('join')
2
2
  export const leave = Symbol('leave')
3
+ export const parent = Symbol('parent')
package/server/utils.js CHANGED
@@ -1,5 +1,3 @@
1
- // utils
2
-
3
1
  export const empty = () => Object.create(null)
4
2
 
5
3
  export const now = Date.now
@@ -16,6 +14,8 @@ export const isArray = Array.isArray
16
14
 
17
15
  export const fromEntries = Object.fromEntries
18
16
 
17
+ export const arrayFrom = Array.from
18
+
19
19
  export function ListenerTemplate() {
20
20
  const fns = []
21
21
  function Listener(...args) {
@@ -45,3 +45,12 @@ export const frame = function (
45
45
  opcode: 1,
46
46
  fin: true,
47
47
  })
48
+
49
+ export class MapeableSet extends Set {
50
+ map(fn) {
51
+ return arrayFrom(this, fn)
52
+ }
53
+ filter(fn) {
54
+ return this.map().filter(fn)
55
+ }
56
+ }
@@ -1,9 +1,12 @@
1
1
  export class Emitter {
2
- /** @type Set<import('./Socket.js').Socket> */
3
- sockets: Set<import("./Socket.js").Socket>;
4
- get connections(): number;
2
+ /** @type MapeableSet<import('./Socket.js').Socket> */
3
+ sockets: MapeableSet<import("./Socket.js").Socket>;
4
+ get connections(): any;
5
5
  emit(k: any, v: any): void;
6
6
  once(k: any, v: any): void;
7
- broadcast(me: any, k: any, v: any): void;
8
- broadcastOnce(me: any, k: any, v: any): void;
7
+ /** @param me {import('./Socket.js').Socket} */
8
+ broadcast(me: import("./Socket.js").Socket, k: any, v: any): void;
9
+ /** @param me {import('./Socket.js').Socket} */
10
+ broadcastOnce(me: import("./Socket.js").Socket, k: any, v: any): void;
9
11
  }
12
+ import { MapeableSet } from './utils.js';
@@ -1,13 +1,19 @@
1
1
  export class Room extends Emitter {
2
2
  id: any;
3
3
  persistent: any;
4
- onCreate(socket: any): void;
5
- onDelete(socket: any): void;
6
- onJoin(socket: any): void;
7
- onLeave(socket: any): void;
8
- [join](socket: any): void;
9
- [leave](socket: any): void;
10
- [Symbol.iterator](): Set<import("./Socket.js").Socket>;
4
+ /** @param socket {import('./Socket.js').Socket} */
5
+ onCreate(socket: import("./Socket.js").Socket): void;
6
+ /** @param socket {import('./Socket.js').Socket} */
7
+ onDelete(socket: import("./Socket.js").Socket): void;
8
+ /** @param socket {import('./Socket.js').Socket} */
9
+ onJoin(socket: import("./Socket.js").Socket): void;
10
+ /** @param socket {import('./Socket.js').Socket} */
11
+ onLeave(socket: import("./Socket.js").Socket): void;
12
+ /** @param socket {import('./Socket.js').Socket} */
13
+ [join](socket: import("./Socket.js").Socket): void;
14
+ /** @param socket {import('./Socket.js').Socket} */
15
+ [leave](socket: import("./Socket.js").Socket): void;
16
+ [Symbol.iterator](): any;
11
17
  }
12
18
  import { Emitter } from './Emitter.js';
13
19
  import { join } from './constants.js';
@@ -1,8 +1,15 @@
1
- export class Rooms {
2
- constructor(Room: any);
3
- Room: any;
1
+ export class Rooms extends Emitter {
2
+ /** @param {Object} RoomClass */
3
+ constructor(RoomClass: any);
4
+ RoomClass: any;
4
5
  rooms: Map<any, any>;
6
+ get(id: any): any;
5
7
  join(socket: any, id?: undefined): void;
6
8
  leave(socket: any, id?: undefined): void;
9
+ Sockets(): Generator<any, void, unknown>;
10
+ map(fn: any): any[];
11
+ filter(fn: any): any[];
12
+ toJSON(): any[];
7
13
  [Symbol.iterator](): MapIterator<any>;
8
14
  }
15
+ import { Emitter } from './Emitter.js';
@@ -49,7 +49,7 @@ declare const _default: {
49
49
  * @template T
50
50
  * @param {T} listener
51
51
  */
52
- on<T>(listener: T): void;
52
+ on<T>(listener: T): any;
53
53
  onconnect: (socket: any, request: any) => void;
54
54
  onerror: (err: any) => void;
55
55
  nextQueue(socket: any): void;
@@ -63,12 +63,12 @@ declare const _default: {
63
63
  [inspect](): any & {
64
64
  sockets: string;
65
65
  };
66
- sockets: Set<import("./Socket.js").Socket>;
67
- readonly connections: number;
66
+ sockets: import("./utils.js").MapeableSet<import("./Socket.js").Socket>;
67
+ readonly connections: any;
68
68
  emit(k: any, v: any): void;
69
69
  once(k: any, v: any): void;
70
- broadcast(me: any, k: any, v: any): void;
71
- broadcastOnce(me: any, k: any, v: any): void;
70
+ broadcast(me: import("./Socket.js").Socket, k: any, v: any): void;
71
+ broadcastOnce(me: import("./Socket.js").Socket, k: any, v: any): void;
72
72
  };
73
73
  export default _default;
74
74
  import { inspect } from './utils.js';
@@ -20,7 +20,7 @@ export class Socket {
20
20
  contacted: any;
21
21
  ping: number;
22
22
  timedout: boolean;
23
- rooms: Set<any>;
23
+ rooms: MapeableSet;
24
24
  emit(k: any, v: any, cb: any): void;
25
25
  once(k: any, v: any): void;
26
26
  disconnect(noReconnect: any): void;
@@ -44,4 +44,5 @@ export class Socket {
44
44
  readyState: any;
45
45
  };
46
46
  }
47
+ import { MapeableSet } from './utils.js';
47
48
  import { inspect } from './utils.js';
@@ -1,2 +1,3 @@
1
1
  export const join: unique symbol;
2
2
  export const leave: unique symbol;
3
+ export const parent: unique symbol;
@@ -23,4 +23,16 @@ export const fromEntries: {
23
23
  };
24
24
  (entries: Iterable<readonly any[]>): any;
25
25
  };
26
+ export const arrayFrom: {
27
+ <T>(arrayLike: ArrayLike<T>): T[];
28
+ <T, U>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[];
29
+ <T>(iterable: Iterable<T> | ArrayLike<T>): T[];
30
+ <T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[];
31
+ };
26
32
  export const frame: (data?: any) => any;
33
+ export class MapeableSet extends Set<any> {
34
+ constructor(values?: readonly any[] | null | undefined);
35
+ constructor(iterable?: Iterable<any> | null | undefined);
36
+ map(fn: any): any[];
37
+ filter(fn: any): any[];
38
+ }