jsgar 1.6.1 → 1.7.1

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
  /**
@@ -482,6 +473,7 @@
482
473
  if (this.stoppedCallback) {
483
474
  this.stoppedCallback();
484
475
  }
476
+ this.log('INFO', 'GAR client stopped');
485
477
  }
486
478
 
487
479
  /**
@@ -552,13 +544,16 @@
552
544
  */
553
545
  _processMessage(message) {
554
546
  const msgType = message.message_type;
547
+ let subscriptionGroup = 0;
555
548
  if (msgType === 'TopicIntroduction') {
556
549
  this.serverTopicIdToName.set(message.value.topic_id, message.value.name);
557
550
  this.serverTopicNameToId.set(message.value.name, message.value.topic_id);
558
551
  } else if (msgType === 'KeyIntroduction') {
552
+ subscriptionGroup = this.activeSubscriptionGroup;
559
553
  this.serverKeyIdToName.set(message.value.key_id, message.value.name);
560
554
  this.serverKeyNameToId.set(message.value.name, message.value.key_id);
561
555
  } else if (msgType === 'DeleteKey') {
556
+ subscriptionGroup = this.activeSubscriptionGroup;
562
557
  this.serverKeyNameToId.delete(this.serverKeyIdToName.get(message.value.key_id) || "");
563
558
  this.serverKeyIdToName.delete(message.value.key_id);
564
559
  } else if (msgType === 'Heartbeat') {
@@ -578,13 +573,17 @@
578
573
  this._initialGracePeriod = true;
579
574
  this._initialGraceDeadline = this.lastHeartbeatTime + this.heartbeatTimeout * 10;
580
575
  } else if (msgType === 'JSONRecordUpdate') {
576
+ subscriptionGroup = this.activeSubscriptionGroup;
581
577
  const recordId = message.value.record_id;
582
578
  const keyId = recordId.key_id;
583
579
  const topicId = recordId.topic_id;
584
580
  this.recordMap.set(`${keyId}:${topicId}`, message.value.value);
585
581
  } else if (msgType === 'DeleteRecord') {
586
- const { key_id: keyId, topic_id: topicId } = message.value;
582
+ subscriptionGroup = this.activeSubscriptionGroup;
583
+ const {key_id: keyId, topic_id: topicId} = message.value;
587
584
  this.recordMap.delete(`${keyId}:${topicId}`);
585
+ } else if (msgType === "ActiveSubscription") {
586
+ this.activeSubscriptionGroup = message["value"]["subscription_group"];
588
587
  } else if (msgType === 'Logoff') {
589
588
  this.log('INFO', 'Received Logoff from server');
590
589
  this.stop();
@@ -596,7 +595,7 @@
596
595
 
597
596
  this.checkHeartbeat();
598
597
 
599
- const handler = this.messageHandlers.get(msgType);
598
+ const handler = this.messageHandlers.get(subscriptionGroup ? `${msgType} ${subscriptionGroup}` : msgType);
600
599
  if (handler) {
601
600
  handler(message);
602
601
  }
@@ -623,6 +622,7 @@
623
622
  keyFilter = null,
624
623
  topicFilter = null,
625
624
  allMatchingKeys = false,
625
+ subscriptionGroup = 0,
626
626
  snapshotSizeLimit = 0
627
627
  ) {
628
628
  const keyId = keyName ? this.getAndPossiblyIntroduceKeyId(keyName) : 0;
@@ -639,7 +639,8 @@
639
639
  topic_id: topicId,
640
640
  _class: classFilter,
641
641
  key_filter: keyFilter,
642
- topic_filter: topicFilter
642
+ topic_filter: topicFilter,
643
+ subscription_group: subscriptionGroup
643
644
  }
644
645
  };
645
646
  this.sendMessage(subMsg);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsgar",
3
- "version": "1.6.1",
3
+ "version": "1.7.1",
4
4
  "description": "A Javascript client for the GAR protocol",
5
5
  "type": "module",
6
6
  "main": "dist/gar.umd.js",