Ver Fonte

Add removeCommTarget and removeMessageHook functions to kernels.

Jason Grout há 6 anos atrás
pai
commit
dd50e935d4

+ 32 - 22
packages/services/src/kernel/default.ts

@@ -629,13 +629,24 @@ class DefaultKernel implements Kernel.IKernel {
    * it is to store the disposable. Since there is only one callback, you don't even
    * need to store the callback.
    */
-  registerCommTarget(targetName: string, callback: (comm: Kernel.IComm, msg: KernelMessage.ICommOpenMsg) => void | PromiseLike<void>): IDisposable {
+  registerCommTarget(targetName: string, callback: (comm: Kernel.IComm, msg: KernelMessage.ICommOpenMsg) => void | PromiseLike<void>): void {
     this._targetRegistry[targetName] = callback;
-    return new DisposableDelegate(() => {
-      if (!this.isDisposed) {
-        delete this._targetRegistry[targetName];
-      }
-    });
+  }
+
+  /**
+   * Remove a comm target handler.
+   *
+   * @param targetName - The name of the comm target to remove.
+   *
+   * @param callback - The callback to remove.
+   *
+   * #### Notes
+   * The comm target is only removed if it matches the callback argument.
+   */
+  removeCommTarget(targetName: string, callback: (comm: Kernel.IComm, msg: KernelMessage.ICommOpenMsg) => void | PromiseLike<void>): void {
+    if (!this.isDisposed && this._targetRegistry[targetName] === callback) {
+      delete this._targetRegistry[targetName];
+    }
   }
 
   /**
@@ -645,8 +656,6 @@ class DefaultKernel implements Kernel.IKernel {
    *
    * @param hook - The callback invoked for the message.
    *
-   * @returns A disposable used to unregister the message hook.
-   *
    * #### Notes
    * The IOPub hook system allows you to preempt the handlers for IOPub
    * messages that are responses to a given message id.
@@ -662,27 +671,28 @@ class DefaultKernel implements Kernel.IKernel {
    * deactivated immediately.
    *
    * See also [[IFuture.registerMessageHook]].
-   *
-   * TODO: like phosphor, perhaps we shouldn't return a disposable, which keeps
-   * a basically just keeps a reference to the msgId. Instead, just make the
-   * user call a new removeMessageHook function directly (presumably it's just
-   * as easy to keep the message id and hook function as it is to keep the
-   * disposable, so might as well avoid the allocation).
    */
-  registerMessageHook(msgId: string, hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>): IDisposable {
+  registerMessageHook(msgId: string, hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>): void {
     let future = this._futures && this._futures.get(msgId);
     if (future) {
       future.registerMessageHook(hook);
     }
-
-    return new DisposableDelegate(() => {
-      let future = this._futures && this._futures.get(msgId);
-      if (future) {
-        future.removeMessageHook(hook);
-      }
-    });
   }
 
+  /**
+   * Remove an IOPub message hook.
+   *
+   * @param msg_id - The parent_header message id the hook intercepted.
+   *
+   * @param hook - The callback invoked for the message.
+   *
+   */
+  removeMessageHook(msgId: string, hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>): void {
+    let future = this._futures && this._futures.get(msgId);
+    if (future) {
+      future.removeMessageHook(hook);
+    }
+  }
 
 
   /**

+ 24 - 6
packages/services/src/kernel/kernel.ts

@@ -314,8 +314,6 @@ namespace Kernel {
      *
      * @param callback - The callback invoked for a comm open message.
      *
-     * @returns A disposable used to unregister the comm target.
-     *
      * #### Notes
      * Only one comm target can be registered to a target name at a time, an
      * existing callback for the same target name will be overidden.  A registered
@@ -325,7 +323,19 @@ namespace Kernel {
      * If the callback returns a promise, kernel message processing will pause
      * until the returned promise is fulfilled.
      */
-    registerCommTarget(targetName: string, callback: (comm: Kernel.IComm, msg: KernelMessage.ICommOpenMsg) => void | PromiseLike<void>): IDisposable;
+    registerCommTarget(targetName: string, callback: (comm: Kernel.IComm, msg: KernelMessage.ICommOpenMsg) => void | PromiseLike<void>): void;
+
+    /**
+     * Remove a comm target handler.
+     *
+     * @param targetName - The name of the comm target to remove.
+     *
+     * @param callback - The callback to remove.
+     *
+     * #### Notes
+     * The comm target is only removed if it matches the callback argument.
+     */
+    removeCommTarget(targetName: string, callback: (comm: Kernel.IComm, msg: KernelMessage.ICommOpenMsg) => void | PromiseLike<void>): void;
 
     /**
      * Register an IOPub message hook.
@@ -335,8 +345,6 @@ namespace Kernel {
      *
      * @param hook - The callback invoked for the message.
      *
-     * @returns A disposable used to unregister the message hook.
-     *
      * #### Notes
      * The IOPub hook system allows you to preempt the handlers for IOPub
      * messages with a given parent_header message id. The most recently
@@ -349,7 +357,17 @@ namespace Kernel {
      *
      * See also [[IFuture.registerMessageHook]].
      */
-    registerMessageHook(msgId: string, hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>): IDisposable;
+    registerMessageHook(msgId: string, hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>): void;
+
+    /**
+     * Remove an IOPub message hook.
+     *
+     * @param msg_id - The parent_header message id the hook intercepted.
+     *
+     * @param hook - The callback invoked for the message.
+     *
+     */
+    removeMessageHook(msgId: string, hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>): void;
   }
 
   /**