@eleven-am/pondsocket 0.1.74 → 0.1.76

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/lobby/lobby.js CHANGED
@@ -64,7 +64,10 @@ class LobbyEngine {
64
64
  */
65
65
  broadcast(event, payload, channelName) {
66
66
  if (channelName) {
67
- const channel = this.getChannel(channelName) || this.createChannel(channelName);
67
+ const channel = this.getChannel(channelName);
68
+ if (!channel) {
69
+ throw new Error(`GatewayEngine: Channel ${channelName} does not exist`);
70
+ }
68
71
  channel.sendMessage(enums_1.SystemSender.CHANNEL, enums_1.ChannelReceiver.ALL_USERS, enums_1.ServerActions.SYSTEM, event, payload);
69
72
  }
70
73
  else {
package/nest.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  OnJoinRequest,
3
+ ChannelInstance,
3
4
  DChannel as Channel,
4
5
  Channels, Endpoints,
5
6
  OnConnectionRequest,
@@ -17,6 +18,7 @@ import {
17
18
 
18
19
  export {
19
20
  OnJoinRequest,
21
+ ChannelInstance,
20
22
  Endpoint, PondSocketModule,
21
23
  OnConnectionRequest, OnEvent,
22
24
  Channel, Channels, Endpoints,
package/nest.js CHANGED
@@ -20,7 +20,7 @@ var __rest = (this && this.__rest) || function (s, e) {
20
20
  return t;
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.PondSocketModule = exports.Endpoints = exports.Endpoint = exports.Channels = exports.Channel = exports.OnConnectionRequest = exports.OnEvent = exports.OnJoinRequest = exports.GetConnectionQuery = exports.GetConnectionHeaders = exports.GetConnectionParams = exports.GetConnectionRequestId = exports.GetConnectionResponse = exports.GetConnectionRequest = exports.GetEventRequest = exports.GetEventResponse = exports.GetEventQuery = exports.GetEventParams = exports.GetEventPayload = exports.GetUserPresence = exports.GetInternalChannel = exports.GetUserData = exports.GetJoinParams = exports.GetJoinResponse = exports.GetJoinRequest = void 0;
23
+ exports.PondSocketModule = exports.Endpoints = exports.Endpoint = exports.Channels = exports.ChannelInstance = exports.Channel = exports.OnConnectionRequest = exports.OnEvent = exports.OnJoinRequest = exports.GetConnectionQuery = exports.GetConnectionHeaders = exports.GetConnectionParams = exports.GetConnectionRequestId = exports.GetConnectionResponse = exports.GetConnectionRequest = exports.GetEventRequest = exports.GetEventResponse = exports.GetEventQuery = exports.GetEventParams = exports.GetEventPayload = exports.GetUserPresence = exports.GetInternalChannel = exports.GetUserData = exports.GetJoinParams = exports.GetJoinResponse = exports.GetJoinRequest = void 0;
24
24
  require("reflect-metadata");
25
25
  const common_1 = require("@nestjs/common");
26
26
  const pondSocket_1 = require("./server/pondSocket");
@@ -46,6 +46,7 @@ const onEventHandlerKey = Symbol('onEventHandlerKey');
46
46
  const onConnectionHandlerKey = Symbol('onConnectionHandlerKey');
47
47
  const channelsKey = Symbol('channelsKey');
48
48
  const endpointsKey = Symbol('endpointsKey');
49
+ const channelInstanceKey = Symbol('channelInstanceKey');
49
50
  function createParamDecorator(key, error) {
50
51
  return (target, propertyKey, parameterIndex) => {
51
52
  const existingParams = Reflect.getMetadata(key, target, propertyKey);
@@ -72,20 +73,37 @@ function createClassDecorator(key, target) {
72
73
  },
73
74
  };
74
75
  }
76
+ function manageClassData(key, target) {
77
+ return {
78
+ get() {
79
+ return Reflect.getMetadata(key, target);
80
+ },
81
+ set(value) {
82
+ Reflect.defineMetadata(key, value, target);
83
+ },
84
+ };
85
+ }
75
86
  function manageHandlers(key, target) {
87
+ const { get, set } = manageClassData(key, target);
76
88
  return {
77
89
  get() {
78
- return (Reflect.getMetadata(key, target) || []);
90
+ return get() || [];
79
91
  },
80
92
  set(path, value) {
81
- const handlers = Reflect.getMetadata(key, target) || [];
82
- Reflect.defineMetadata(key, [
83
- ...handlers, { path,
84
- value },
85
- ], target);
93
+ const handlers = get() || [];
94
+ set([
95
+ ...handlers,
96
+ {
97
+ path,
98
+ value,
99
+ },
100
+ ]);
86
101
  },
87
102
  };
88
103
  }
104
+ function manageChannelInstance(target) {
105
+ return manageClassData(channelInstanceKey, target);
106
+ }
89
107
  function manageJoinHandlers(target) {
90
108
  return manageHandlers(onJoinHandlerKey, target);
91
109
  }
@@ -420,9 +438,10 @@ function Channel(path = '*') {
420
438
  response.accept();
421
439
  }
422
440
  }));
441
+ const { set } = manageChannelInstance(this);
423
442
  const { get } = manageEventHandlers(this);
424
- const handlers = get();
425
- handlers.forEach((handler) => {
443
+ set(channel);
444
+ get().forEach((handler) => {
426
445
  channel.onEvent(handler.path, (request, response) => __awaiter(this, void 0, void 0, function* () {
427
446
  yield handler.value(request, response);
428
447
  }));
@@ -431,24 +450,33 @@ function Channel(path = '*') {
431
450
  };
432
451
  }
433
452
  exports.Channel = Channel;
453
+ function ChannelInstance() {
454
+ return (target, propertyKey) => {
455
+ const { get } = manageChannelInstance(target.constructor.prototype);
456
+ Object.defineProperty(target, propertyKey, {
457
+ get() {
458
+ return get();
459
+ },
460
+ set() {
461
+ throw new Error('ChannelInstance is readonly');
462
+ },
463
+ });
464
+ };
465
+ }
466
+ exports.ChannelInstance = ChannelInstance;
434
467
  function Channels(channels) {
435
468
  return (constructor) => {
436
469
  const { set } = manageChannels(constructor.prototype);
437
470
  set(channels);
438
- return class extends constructor {
439
- constructor() {
440
- super(...arguments);
441
- this._channels = channels;
442
- }
443
- };
471
+ return constructor;
444
472
  };
445
473
  }
446
474
  exports.Channels = Channels;
447
475
  function Endpoint(path = '*') {
448
476
  return (constructor) => class extends constructor {
449
477
  _setSocket(moduleRef, socket) {
450
- var _a;
451
478
  const { get } = manageConnectionHandlers(this);
479
+ const { get: getChannels } = manageChannels(this);
452
480
  const [handler] = get();
453
481
  const endpoint = socket.createEndpoint(path, (request, response) => __awaiter(this, void 0, void 0, function* () {
454
482
  if (handler) {
@@ -458,8 +486,7 @@ function Endpoint(path = '*') {
458
486
  response.accept();
459
487
  }
460
488
  }));
461
- const channels = (_a = this._channels) !== null && _a !== void 0 ? _a : [];
462
- channels.forEach((channel) => {
489
+ getChannels().forEach((channel) => {
463
490
  const chan = moduleRef.get(channel, {
464
491
  strict: false,
465
492
  });
@@ -496,9 +523,10 @@ class PondSocketModule {
496
523
  this.socket = new pondSocket_1.PondSocket(server);
497
524
  expressInstance.listen = (...args) => {
498
525
  const { get } = manageEndpoints(this);
499
- get().forEach((endpoint) => {
526
+ get().map((endpoint) => {
500
527
  const instance = this.moduleRef.get(endpoint, { strict: false });
501
528
  instance._setSocket(this.moduleRef, this.socket);
529
+ return instance;
502
530
  });
503
531
  this.socket.listen(...args);
504
532
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.74",
3
+ "version": "0.1.76",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
package/types.d.ts CHANGED
@@ -746,6 +746,11 @@ declare function OnConnectionRequest(): MethodDecorator;
746
746
  */
747
747
  declare function DChannel(path?: string): ClassDecorator;
748
748
 
749
+ /**
750
+ * Decorator to retrieve the channel instance as a read-only property in a class.
751
+ */
752
+ declare function ChannelInstance(): PropertyDecorator;
753
+
749
754
  /**
750
755
  * Decorator to mark a class as having multiple channels.
751
756
  * @param channels - The array of channels.