jsgar 1.6.2 → 1.7.2

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.
Files changed (2) hide show
  1. package/dist/gar.umd.js +30 -29
  2. package/package.json +1 -1
package/dist/gar.umd.js CHANGED
@@ -91,6 +91,8 @@
91
91
  this.logLevel = 'INFO';
92
92
  }
93
93
 
94
+ this.activeSubscriptionGroup = 0;
95
+
94
96
  this.registerDefaultHandlers();
95
97
  }
96
98
 
@@ -210,9 +212,10 @@
210
212
  * Register a callback handler for a specific message type.
211
213
  * @param {string} messageType - Type of message
212
214
  * @param {Function} handler - Callback function
215
+ * @param subscriptionGroup - Subscription group for callback (default 0)
213
216
  */
214
- registerHandler(messageType, handler) {
215
- this.messageHandlers.set(messageType, handler);
217
+ registerHandler(messageType, handler, subscriptionGroup = 0) {
218
+ this.messageHandlers.set(subscriptionGroup ? `${messageType} ${subscriptionGroup}` : messageType, handler);
216
219
  }
217
220
 
218
221
  /**
@@ -287,28 +290,22 @@
287
290
  /**
288
291
  * Register handler for KeyIntroduction message.
289
292
  * @param {Function} handler - Callback with (keyId, name, _class)
293
+ * @param subscriptionGroup - The subscription group for callback (default 0)
290
294
  */
291
- registerKeyIntroductionHandler(handler) {
295
+ registerKeyIntroductionHandler(handler, subscriptionGroup = 0) {
292
296
  this.registerHandler('KeyIntroduction', (msg) => {
293
297
  const value = msg.value;
294
298
  handler(value.key_id, value.name, value._class || null);
295
- });
296
- }
297
-
298
- clearKeyIntroductionHandler() {
299
- this.messageHandlers.delete('KeyIntroduction');
299
+ }, subscriptionGroup);
300
300
  }
301
301
 
302
302
  /**
303
303
  * Register handler for DeleteKey message.
304
304
  * @param {Function} handler - Callback with (keyId)
305
+ * @param subscriptionGroup - The subscription group for callback (default 0)
305
306
  */
306
- registerDeleteKeyHandler(handler) {
307
- this.registerHandler('DeleteKey', (msg) => handler(msg.value.key_id));
308
- }
309
-
310
- clearDeleteKeyHandler() {
311
- this.messageHandlers.delete('DeleteKey');
307
+ registerDeleteKeyHandler(handler, subscriptionGroup = 0) {
308
+ this.registerHandler('DeleteKey', (msg) => handler(msg.value.key_id), subscriptionGroup);
312
309
  }
313
310
 
314
311
  /**
@@ -326,30 +323,24 @@
326
323
  /**
327
324
  * Register handler for DeleteRecord message.
328
325
  * @param {Function} handler - Callback with (keyId, topicId)
326
+ * @param subscriptionGroup - The subscription group for callback (default 0)
329
327
  */
330
- registerDeleteRecordHandler(handler) {
328
+ registerDeleteRecordHandler(handler, subscriptionGroup = 0) {
331
329
  this.registerHandler('DeleteRecord', (msg) => {
332
330
  handler(msg.value.key_id, msg.value.topic_id);
333
- });
334
- }
335
-
336
- clearDeleteRecordHandler() {
337
- this.messageHandlers.delete('DeleteRecord');
331
+ }, subscriptionGroup);
338
332
  }
339
333
 
340
334
  /**
341
335
  * Register handler for JSONRecordUpdate message.
342
336
  * @param {Function} handler - Callback with (keyId, topicId, value)
337
+ * @param subscriptionGroup - The subscription group for callback (default 0)
343
338
  */
344
- registerRecordUpdateHandler(handler) {
339
+ registerRecordUpdateHandler(handler, subscriptionGroup = 0) {
345
340
  this.registerHandler('JSONRecordUpdate', (msg) => {
346
341
  const recordId = msg.value.record_id;
347
342
  handler(recordId.key_id, recordId.topic_id, msg.value.value);
348
- });
349
- }
350
-
351
- clearRecordUpdateHandler() {
352
- this.messageHandlers.delete('JSONRecordUpdate');
343
+ }, subscriptionGroup);
353
344
  }
354
345
 
355
346
  /**
@@ -553,13 +544,16 @@
553
544
  */
554
545
  _processMessage(message) {
555
546
  const msgType = message.message_type;
547
+ let subscriptionGroup = 0;
556
548
  if (msgType === 'TopicIntroduction') {
557
549
  this.serverTopicIdToName.set(message.value.topic_id, message.value.name);
558
550
  this.serverTopicNameToId.set(message.value.name, message.value.topic_id);
559
551
  } else if (msgType === 'KeyIntroduction') {
552
+ subscriptionGroup = this.activeSubscriptionGroup;
560
553
  this.serverKeyIdToName.set(message.value.key_id, message.value.name);
561
554
  this.serverKeyNameToId.set(message.value.name, message.value.key_id);
562
555
  } else if (msgType === 'DeleteKey') {
556
+ subscriptionGroup = this.activeSubscriptionGroup;
563
557
  this.serverKeyNameToId.delete(this.serverKeyIdToName.get(message.value.key_id) || "");
564
558
  this.serverKeyIdToName.delete(message.value.key_id);
565
559
  } else if (msgType === 'Heartbeat') {
@@ -579,13 +573,17 @@
579
573
  this._initialGracePeriod = true;
580
574
  this._initialGraceDeadline = this.lastHeartbeatTime + this.heartbeatTimeout * 10;
581
575
  } else if (msgType === 'JSONRecordUpdate') {
576
+ subscriptionGroup = this.activeSubscriptionGroup;
582
577
  const recordId = message.value.record_id;
583
578
  const keyId = recordId.key_id;
584
579
  const topicId = recordId.topic_id;
585
580
  this.recordMap.set(`${keyId}:${topicId}`, message.value.value);
586
581
  } else if (msgType === 'DeleteRecord') {
587
- const { key_id: keyId, topic_id: topicId } = message.value;
582
+ subscriptionGroup = this.activeSubscriptionGroup;
583
+ const {key_id: keyId, topic_id: topicId} = message.value;
588
584
  this.recordMap.delete(`${keyId}:${topicId}`);
585
+ } else if (msgType === "ActiveSubscription") {
586
+ this.activeSubscriptionGroup = message["value"]["subscription_group"];
589
587
  } else if (msgType === 'Logoff') {
590
588
  this.log('INFO', 'Received Logoff from server');
591
589
  this.stop();
@@ -597,7 +595,7 @@
597
595
 
598
596
  this.checkHeartbeat();
599
597
 
600
- const handler = this.messageHandlers.get(msgType);
598
+ const handler = this.messageHandlers.get(subscriptionGroup ? `${msgType} ${subscriptionGroup}` : msgType);
601
599
  if (handler) {
602
600
  handler(message);
603
601
  }
@@ -613,6 +611,7 @@
613
611
  * @param {string|null} [keyFilter=null] - Key filter regex
614
612
  * @param {string|null} [topicFilter=null] - Topic filter regex
615
613
  * @param {boolean} [allMatchingKeys=false] - Retrieve all matching keys
614
+ * @param subscriptionGroup - Subscription group ID for isolating callbacks
616
615
  * @param {number} [snapshotSizeLimit=0] - Limit snapshot size
617
616
  */
618
617
  subscribe(
@@ -624,6 +623,7 @@
624
623
  keyFilter = null,
625
624
  topicFilter = null,
626
625
  allMatchingKeys = false,
626
+ subscriptionGroup = 0,
627
627
  snapshotSizeLimit = 0
628
628
  ) {
629
629
  const keyId = keyName ? this.getAndPossiblyIntroduceKeyId(keyName) : 0;
@@ -640,7 +640,8 @@
640
640
  topic_id: topicId,
641
641
  _class: classFilter,
642
642
  key_filter: keyFilter,
643
- topic_filter: topicFilter
643
+ topic_filter: topicFilter,
644
+ subscription_group: subscriptionGroup
644
645
  }
645
646
  };
646
647
  this.sendMessage(subMsg);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsgar",
3
- "version": "1.6.2",
3
+ "version": "1.7.2",
4
4
  "description": "A Javascript client for the GAR protocol",
5
5
  "type": "module",
6
6
  "main": "dist/gar.umd.js",