just-another-http-api 1.2.5 → 1.2.6

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": "just-another-http-api",
3
- "version": "1.2.5",
3
+ "version": "1.2.6",
4
4
  "description": "A framework built on top of fastify aimed at removing the need for any network or server configuration. ",
5
5
  "homepage": "https://github.com/OllieEdge/just-another-http-api#readme",
6
6
  "repository": {
@@ -4,8 +4,10 @@ const { redis } = require ( 'eip-cloud-services' );
4
4
  class WebsocketGroup {
5
5
 
6
6
  constructor ( groupName, destroy, messageReceivedHandler, connectionClosedHandler ) {
7
+ this.timeOfCreation = Date.now ();
7
8
  this.groupName = groupName;
8
9
  this.destroy = destroy;
10
+ this.groupUuid = crypto.randomUUID ().substring ( 0, 8 );
9
11
  this.connections = new Map ();
10
12
  this.messageReceivedHandler = messageReceivedHandler || ( async () => {} );
11
13
  this.connectionClosedHandler = connectionClosedHandler;
@@ -79,22 +81,23 @@ class WebsocketGroup {
79
81
  }
80
82
 
81
83
  async #clean (connectionId) {
82
- if ( this.connections.size === 0 ) {
83
- this.connections.clear ();
84
- await redis.unsubscribe ( `${this.groupName}_individualMessage`, this.#handleIndividualMessage.bind ( this ) );
85
- await redis.unsubscribe ( `${this.groupName}_broadcast`, this.#broadcastMessageToClients.bind ( this ) );
86
- await redis.unsubscribe ( `${this.groupName}_messageReceived`, this.#handleUserMessage.bind ( this ) );
87
- this.destroy(this.groupName);
88
- }
89
84
 
90
85
  if ( this.connectionClosedHandler !== null && typeof this.connectionClosedHandler === 'function') {
91
86
  try{
92
- await this.connectionClosedHandler ( {groupName:this.groupName, connectionId} );
87
+ await this.connectionClosedHandler ( {groupName:this.groupName, connectionId} );
93
88
  }
94
89
  catch (error) {
95
90
  console.error('Error in connectionClosedHandler:', error);
96
91
  }
97
92
  }
93
+
94
+ if ( this.connections.size === 0 ) {
95
+ this.connections.clear ();
96
+ await redis.unsubscribe ( `${this.groupName}_individualMessage` );
97
+ await redis.unsubscribe ( `${this.groupName}_broadcast` );
98
+ await redis.unsubscribe ( `${this.groupName}_messageReceived` );
99
+ this.destroy(this.groupName);
100
+ }
98
101
  }
99
102
  }
100
103
 
@@ -3,12 +3,12 @@ const WebsocketGroup = require ( './WebsocketGroup' );
3
3
  const websocketGroups = {};
4
4
 
5
5
  const getGroupInstance = async ( groupName, messageReceivedHandler, connectionClosedHandler ) => {
6
- if ( !websocketGroups[ groupName ] ) {
6
+ if ( !websocketGroups[ groupName ] && messageReceivedHandler && connectionClosedHandler ) {
7
7
  websocketGroups[ groupName ] = new WebsocketGroup ( groupName, removeGroupInstance, messageReceivedHandler, connectionClosedHandler );
8
8
  await websocketGroups[ groupName ].initialize ();
9
9
  }
10
10
 
11
- return websocketGroups[ groupName ];
11
+ return websocketGroups[ groupName ] || null;
12
12
  };
13
13
 
14
14
  const removeGroupInstance = async groupName => {