jsgar 1.6.2 → 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 +29 -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
  }
@@ -624,6 +622,7 @@
624
622
  keyFilter = null,
625
623
  topicFilter = null,
626
624
  allMatchingKeys = false,
625
+ subscriptionGroup = 0,
627
626
  snapshotSizeLimit = 0
628
627
  ) {
629
628
  const keyId = keyName ? this.getAndPossiblyIntroduceKeyId(keyName) : 0;
@@ -640,7 +639,8 @@
640
639
  topic_id: topicId,
641
640
  _class: classFilter,
642
641
  key_filter: keyFilter,
643
- topic_filter: topicFilter
642
+ topic_filter: topicFilter,
643
+ subscription_group: subscriptionGroup
644
644
  }
645
645
  };
646
646
  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.1",
4
4
  "description": "A Javascript client for the GAR protocol",
5
5
  "type": "module",
6
6
  "main": "dist/gar.umd.js",