@pluv/io 0.3.7 → 0.3.8

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @pluv/io@0.3.7 build /home/runner/work/pluv/pluv/packages/io
2
+ > @pluv/io@0.3.8 build /home/runner/work/pluv/pluv/packages/io
3
3
  > tsup src/index.ts --format esm,cjs --dts
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -8,10 +8,10 @@
8
8
  CLI Target: es6
9
9
  ESM Build start
10
10
  CJS Build start
11
- CJS dist/index.js 29.32 KB
12
- CJS ⚡️ Build success in 63ms
13
- ESM dist/index.mjs 27.26 KB
14
- ESM ⚡️ Build success in 79ms
11
+ CJS dist/index.js 29.51 KB
12
+ CJS ⚡️ Build success in 109ms
13
+ ESM dist/index.mjs 27.45 KB
14
+ ESM ⚡️ Build success in 118ms
15
15
  DTS Build start
16
- DTS ⚡️ Build success in 2692ms
17
- DTS dist/index.d.ts 12.82 KB
16
+ DTS ⚡️ Build success in 2905ms
17
+ DTS dist/index.d.ts 12.84 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @pluv/io
2
2
 
3
+ ## 0.3.8
4
+
5
+ ### Patch Changes
6
+
7
+ - bcf1c3e: purge empty rooms before joining a new room
8
+
3
9
  ## 0.3.7
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -204,6 +204,7 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
204
204
  readonly id: string;
205
205
  constructor(id: string, config: IORoomConfig<TPlatform, TAuthorize, TContext, TInput, TOutput>);
206
206
  broadcast<TEvent extends keyof InferIOInput<this>>(event: TEvent, data: Id<InferIOInput<this>[TEvent]>): Promise<void>;
207
+ getSize(): number;
207
208
  register(webSocket: InferWebSocketType<TPlatform>, options?: WebsocketRegisterOptions): Promise<void>;
208
209
  private _broadcast;
209
210
  private _emitQuitters;
@@ -211,7 +212,6 @@ declare class IORoom<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
211
212
  private _emitStorageReceived;
212
213
  private _emitUserJoined;
213
214
  private _getAuthorizedUser;
214
- private _getSessionsSize;
215
215
  private _getEventConfig;
216
216
  private _initialize;
217
217
  private _logDebug;
@@ -256,6 +256,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
256
256
  getRoom(room: string, options?: GetRoomOptions): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutput>;
257
257
  static merge<TIO1 extends PluvIO, TIO2 extends PluvIO>(first: TIO1, second: TIO2): PluvIO<InferIOPlatform<TIO1>, InferIOAuthorize<TIO1>, InferIOContext<TIO1>, Spread<[InferIOInput<TIO1>, InferIOInput<TIO2>]>, Spread<[InferIOOutput<TIO1>, InferIOOutput<TIO2>]>>;
258
258
  private _logDebug;
259
+ private _purgeEmptyRooms;
259
260
  }
260
261
 
261
262
  type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false> = Partial<PluvIOListeners> & {
package/dist/index.js CHANGED
@@ -334,6 +334,14 @@ var IORoom = class {
334
334
  const message = { type: event, data };
335
335
  return Promise.resolve(this._broadcast({ message }));
336
336
  }
337
+ getSize() {
338
+ const currentTime = (/* @__PURE__ */ new Date()).getTime();
339
+ return Array.from(this._sessions.values()).reduce((count, session) => {
340
+ if (session.quit)
341
+ return count;
342
+ return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
343
+ }, 0);
344
+ }
337
345
  register(webSocket, options) {
338
346
  return __async(this, null, function* () {
339
347
  var _a;
@@ -465,7 +473,7 @@ var IORoom = class {
465
473
  senderId: session.id
466
474
  });
467
475
  uninitializeWs();
468
- const size2 = this._getSessionsSize();
476
+ const size2 = this.getSize();
469
477
  this._logDebug(
470
478
  `${import_kleur.default.blue(
471
479
  `Unregistered connection for room ${this.id}:`
@@ -486,7 +494,7 @@ var IORoom = class {
486
494
  `Registered connection for room ${this.id}:`
487
495
  )} ${sessionId}`
488
496
  );
489
- const size = this._getSessionsSize();
497
+ const size = this.getSize();
490
498
  this._logDebug(`${import_kleur.default.blue(`Room ${this.id} size:`)} ${size}`);
491
499
  });
492
500
  }
@@ -590,14 +598,6 @@ var IORoom = class {
590
598
  }
591
599
  });
592
600
  }
593
- _getSessionsSize() {
594
- const currentTime = (/* @__PURE__ */ new Date()).getTime();
595
- return Array.from(this._sessions.values()).reduce((count, session) => {
596
- if (session.quit)
597
- return count;
598
- return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
599
- }, 0);
600
- }
601
601
  _getEventConfig(message) {
602
602
  var _a;
603
603
  return (_a = this._events[message.type]) != null ? _a : null;
@@ -864,6 +864,7 @@ var PluvIO = class {
864
864
  getRoom(room, options = {}) {
865
865
  var _a;
866
866
  const { debug } = options;
867
+ this._purgeEmptyRooms();
867
868
  if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
868
869
  throw new Error("Unsupported room name");
869
870
  }
@@ -928,6 +929,14 @@ var PluvIO = class {
928
929
  _logDebug(...data) {
929
930
  this._debug && console.log(...data);
930
931
  }
932
+ _purgeEmptyRooms() {
933
+ const rooms = Array.from(this._rooms.entries());
934
+ rooms.forEach(([id, room]) => {
935
+ if (room.getSize())
936
+ return;
937
+ this._rooms.delete(id);
938
+ });
939
+ }
931
940
  };
932
941
 
933
942
  // src/createIO.ts
package/dist/index.mjs CHANGED
@@ -297,6 +297,14 @@ var IORoom = class {
297
297
  const message = { type: event, data };
298
298
  return Promise.resolve(this._broadcast({ message }));
299
299
  }
300
+ getSize() {
301
+ const currentTime = (/* @__PURE__ */ new Date()).getTime();
302
+ return Array.from(this._sessions.values()).reduce((count, session) => {
303
+ if (session.quit)
304
+ return count;
305
+ return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
306
+ }, 0);
307
+ }
300
308
  register(webSocket, options) {
301
309
  return __async(this, null, function* () {
302
310
  var _a;
@@ -428,7 +436,7 @@ var IORoom = class {
428
436
  senderId: session.id
429
437
  });
430
438
  uninitializeWs();
431
- const size2 = this._getSessionsSize();
439
+ const size2 = this.getSize();
432
440
  this._logDebug(
433
441
  `${colors.blue(
434
442
  `Unregistered connection for room ${this.id}:`
@@ -449,7 +457,7 @@ var IORoom = class {
449
457
  `Registered connection for room ${this.id}:`
450
458
  )} ${sessionId}`
451
459
  );
452
- const size = this._getSessionsSize();
460
+ const size = this.getSize();
453
461
  this._logDebug(`${colors.blue(`Room ${this.id} size:`)} ${size}`);
454
462
  });
455
463
  }
@@ -553,14 +561,6 @@ var IORoom = class {
553
561
  }
554
562
  });
555
563
  }
556
- _getSessionsSize() {
557
- const currentTime = (/* @__PURE__ */ new Date()).getTime();
558
- return Array.from(this._sessions.values()).reduce((count, session) => {
559
- if (session.quit)
560
- return count;
561
- return currentTime - session.timers.ping > PING_TIMEOUT_MS ? count : count + 1;
562
- }, 0);
563
- }
564
564
  _getEventConfig(message) {
565
565
  var _a;
566
566
  return (_a = this._events[message.type]) != null ? _a : null;
@@ -827,6 +827,7 @@ var PluvIO = class {
827
827
  getRoom(room, options = {}) {
828
828
  var _a;
829
829
  const { debug } = options;
830
+ this._purgeEmptyRooms();
830
831
  if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
831
832
  throw new Error("Unsupported room name");
832
833
  }
@@ -891,6 +892,14 @@ var PluvIO = class {
891
892
  _logDebug(...data) {
892
893
  this._debug && console.log(...data);
893
894
  }
895
+ _purgeEmptyRooms() {
896
+ const rooms = Array.from(this._rooms.entries());
897
+ rooms.forEach(([id, room]) => {
898
+ if (room.getSize())
899
+ return;
900
+ this._rooms.delete(id);
901
+ });
902
+ }
894
903
  };
895
904
 
896
905
  // src/createIO.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/io",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "description": "@pluv/io server-side websocket manager",
5
5
  "author": "leedavidcs",
6
6
  "license": "MIT",
package/src/IORoom.ts CHANGED
@@ -126,6 +126,24 @@ export class IORoom<
126
126
  return Promise.resolve(this._broadcast({ message }));
127
127
  }
128
128
 
129
+ public getSize(): number {
130
+ const currentTime = new Date().getTime();
131
+
132
+ /**
133
+ * @description Doing this instead of .size because some sessions
134
+ * in the map can be considered as "omitted".
135
+ * @author leedavidcs
136
+ * @date December 21, 2022
137
+ */
138
+ return Array.from(this._sessions.values()).reduce((count, session) => {
139
+ if (session.quit) return count;
140
+
141
+ return currentTime - session.timers.ping > PING_TIMEOUT_MS
142
+ ? count
143
+ : count + 1;
144
+ }, 0);
145
+ }
146
+
129
147
  public async register(
130
148
  webSocket: InferWebSocketType<TPlatform>,
131
149
  options?: WebsocketRegisterOptions
@@ -285,7 +303,7 @@ export class IORoom<
285
303
 
286
304
  uninitializeWs();
287
305
 
288
- const size = this._getSessionsSize();
306
+ const size = this.getSize();
289
307
 
290
308
  this._logDebug(
291
309
  `${colors.blue(
@@ -311,7 +329,7 @@ export class IORoom<
311
329
  )} ${sessionId}`
312
330
  );
313
331
 
314
- const size = this._getSessionsSize();
332
+ const size = this.getSize();
315
333
 
316
334
  this._logDebug(`${colors.blue(`Room ${this.id} size:`)} ${size}`);
317
335
  }
@@ -439,24 +457,6 @@ export class IORoom<
439
457
  }
440
458
  }
441
459
 
442
- private _getSessionsSize(): number {
443
- const currentTime = new Date().getTime();
444
-
445
- /**
446
- * @description Doing this instead of .size because some sessions
447
- * in the map can be considered as "omitted".
448
- * @author leedavidcs
449
- * @date December 21, 2022
450
- */
451
- return Array.from(this._sessions.values()).reduce((count, session) => {
452
- if (session.quit) return count;
453
-
454
- return currentTime - session.timers.ping > PING_TIMEOUT_MS
455
- ? count
456
- : count + 1;
457
- }, 0);
458
- }
459
-
460
460
  private _getEventConfig(
461
461
  message: EventMessage<string, any>
462
462
  ): InferEventConfig<TContext, TInput, TOutput>[keyof TInput] | null {
package/src/PluvIO.ts CHANGED
@@ -262,6 +262,8 @@ export class PluvIO<
262
262
  ): IORoom<TPlatform, TAuthorize, TContext, TInput, TOutput> {
263
263
  const { debug } = options;
264
264
 
265
+ this._purgeEmptyRooms();
266
+
265
267
  if (!/^[a-z0-9]+[a-z0-9\-_]+[a-z0-9]+$/i.test(room)) {
266
268
  throw new Error("Unsupported room name");
267
269
  }
@@ -353,4 +355,14 @@ export class PluvIO<
353
355
  private _logDebug(...data: any[]): void {
354
356
  this._debug && console.log(...data);
355
357
  }
358
+
359
+ private _purgeEmptyRooms(): void {
360
+ const rooms = Array.from(this._rooms.entries());
361
+
362
+ rooms.forEach(([id, room]) => {
363
+ if (room.getSize()) return;
364
+
365
+ this._rooms.delete(id);
366
+ });
367
+ }
356
368
  }