Browse Source

Create standalone debounce() function.

Afshin T. Darian 6 years ago
parent
commit
ef5c19eb62
3 changed files with 25 additions and 56 deletions
  1. 24 0
      packages/coreutils/src/debounce.ts
  2. 1 0
      packages/coreutils/src/index.ts
  3. 0 56
      packages/coreutils/src/poll.ts

+ 24 - 0
packages/coreutils/src/debounce.ts

@@ -0,0 +1,24 @@
+import { PromiseDelegate } from '@phosphor/coreutils';
+
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+/**
+ * Returns a debounced function that can be called multiple times safely and
+ * only executes the underlying function once per `interval`.
+ *
+ * @param fn - The function to debounce.
+ *
+ * @param interval - The debounce interval; defaults to 500ms.
+ */
+export function debounce(fn: () => any, interval = 500): () => Promise<void> {
+  let debouncer = 0;
+  return () => {
+    const delegate = new PromiseDelegate<void>();
+    clearTimeout(debouncer);
+    debouncer = setTimeout(async () => {
+      delegate.resolve(void (await fn()));
+    }, interval);
+    return delegate.promise;
+  };
+}

+ 1 - 0
packages/coreutils/src/index.ts

@@ -3,6 +3,7 @@
 
 export * from './activitymonitor';
 export * from './dataconnector';
+export * from './debounce';
 export * from './interfaces';
 export * from './markdowncodeblocks';
 export * from './nbformat';

+ 0 - 56
packages/coreutils/src/poll.ts

@@ -562,62 +562,6 @@ export namespace Poll {
   export const MAX_INTERVAL = 2147483647;
 }
 
-/**
- * A poll that only schedules ticks manually.
- */
-export class Debouncer extends Poll<void, void, 'debounced'> {
-  /**
-   * Instantiate a debouncer poll.
-   *
-   * @param factory - The factory function being debounced.
-   *
-   * @param interval - The debounce interval.
-   */
-  constructor(
-    factory: Poll.Factory<void, void, 'debounced'>,
-    public interval: number
-  ) {
-    super({ factory });
-    this.frequency = { backoff: false, interval: Infinity, max: Infinity };
-    void super.stop();
-  }
-
-  /**
-   * The debouncer frequency.
-   */
-  readonly frequency: IPoll.Frequency;
-
-  /**
-   * The debouncer poll standby value.
-   */
-  readonly standby = Private.DEFAULT_STANDBY;
-
-  /**
-   * Invokes the debounced function after the interval has elapsed.
-   */
-  async debounce(): Promise<void> {
-    await this.schedule({
-      cancel: last => last.phase === 'debounced',
-      interval: this.interval,
-      phase: 'debounced'
-    });
-    await this.tick;
-  }
-}
-
-/**
- * Returns a debounced function that can be called multiple times safely and
- * only executes the underlying function once per `interval`.
- *
- * @param fn - The function to debounce.
- *
- * @param interval - The debounce interval; defaults to 500ms.
- */
-export function debounce(fn: () => any, interval = 500): () => Promise<void> {
-  const debouncer = new Debouncer(async () => fn(), interval);
-  return () => debouncer.debounce();
-}
-
 /**
  * A namespace for private module data.
  */