Просмотр исходного кода

WIP: impl removeMessageHandler

Yuki Takei 5 лет назад
Родитель
Сommit
667235adf8
2 измененных файлов с 32 добавлено и 6 удалено
  1. 16 0
      src/server/service/config-pubsub/base.js
  2. 16 6
      src/server/service/config-pubsub/nchan.js

+ 16 - 0
src/server/service/config-pubsub/base.js

@@ -39,6 +39,22 @@ class ConfigPubsubDelegator {
       logger.debug('Unsupported instance: ', handlable);
       return;
     }
+
+    this.handlableList.push(handlable);
+  }
+
+  /**
+   * Remove message handler
+   * @param {ConfigPubsubMessageHandlable} handlable
+   */
+  removeMessageHandler(handlable) {
+    if (!(handlable instanceof ConfigPubsubMessageHandlable)) {
+      logger.warn('Unsupported instance');
+      logger.debug('Unsupported instance: ', handlable);
+      return;
+    }
+
+    this.handlableList = this.handlableList.filter(h => h !== handlable);
   }
 
 }

+ 16 - 6
src/server/service/config-pubsub/nchan.js

@@ -32,6 +32,10 @@ class NchanDelegator extends ConfigPubsubDelegator {
    * @inheritdoc
    */
   shouldResubscribe() {
+    if (this.client == null) {
+      return false;
+    }
+
     if (this.connection != null && this.connection.connected) {
       return false;
     }
@@ -49,15 +53,15 @@ class NchanDelegator extends ConfigPubsubDelegator {
       }
     }
 
+    if (this.shouldResubscribe()) {
+      logger.info('The connection to config pubsub server is offline. Try to reconnect...');
+    }
+
     // init client
     if (this.client == null) {
       this.initClient();
     }
 
-    if (this.shouldResubscribe()) {
-      logger.info('The connection to config pubsub server is offline. Try to reconnect...');
-    }
-
     // connect
     this.isConnecting = true;
     const url = this.constructUrl(this.subscribePath).toString();
@@ -82,8 +86,6 @@ class NchanDelegator extends ConfigPubsubDelegator {
   addMessageHandler(handlable) {
     super.addMessageHandler(handlable);
 
-    this.handlableList.push(handlable);
-
     if (this.connection != null) {
       this.connection.on('message', (messageObj) => {
         this.handleMessage(messageObj, handlable);
@@ -91,6 +93,14 @@ class NchanDelegator extends ConfigPubsubDelegator {
     }
   }
 
+  /**
+   * @inheritdoc
+   */
+  removeMessageHandler(handlable) {
+    super.removeMessageHandler(handlable);
+    this.subscribe(true);
+  }
+
   constructUrl(basepath) {
     const pathname = this.channelId == null
       ? basepath //                                 /pubsub