UNPKG

@exmg/lit-base

Version:
102 lines (101 loc) 3.73 kB
/** @license Copyright (c) 2017 The Polymer Project Authors. All rights reserved. This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as part of the polymer project is also subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt */ import './async.js'; import { AsyncInterface } from './async.js'; /** * @summary Collapse multiple callbacks into one invocation after a timer. */ export declare class Debouncer { _asyncModule: AsyncInterface | null; _callback: Function | null; _timer: number | null; /** * Sets the scheduler; that is, a module with the Async interface, * a callback and optional arguments to be passed to the run function * from the async module. * * @param {!AsyncInterface} asyncModule Object with Async interface. * @param {function()} callback Callback to run. * @return {void} */ setConfig(asyncModule: AsyncInterface, callback: Function): void; /** * Cancels an active debouncer and returns a reference to itself. * * @return {void} */ cancel(): void; /** * Cancels a debouncer's async callback. * * @return {void} */ _cancelAsync(): void; /** * Flushes an active debouncer and returns a reference to itself. * * @return {void} */ flush(): void; /** * Returns true if the debouncer is active. * * @return {boolean} True if active. */ isActive(): boolean; /** * Creates a debouncer if no debouncer is passed as a parameter * or it cancels an active debouncer otherwise. The following * example shows how a debouncer can be called multiple times within a * microtask and "debounced" such that the provided callback function is * called once. Add this method to a custom element: * * ```js * import {microTask} from '@polymer/polymer/lib/utils/async.js'; * import {Debouncer} from '@polymer/polymer/lib/utils/debounce.js'; * // ... * * _debounceWork() { * this._debounceJob = Debouncer.debounce(this._debounceJob, * microTask, () => this._doWork()); * } * ``` * * If the `_debounceWork` method is called multiple times within the same * microtask, the `_doWork` function will be called only once at the next * microtask checkpoint. * * Note: In testing it is often convenient to avoid asynchrony. To accomplish * this with a debouncer, you can use `enqueueDebouncer` and * `flush`. For example, extend the above example by adding * `enqueueDebouncer(this._debounceJob)` at the end of the * `_debounceWork` method. Then in a test, call `flush` to ensure * the debouncer has completed. * * @param {Debouncer?} debouncer Debouncer object. * @param {!AsyncInterface} asyncModule Object with Async interface * @param {function()} callback Callback to run. * @return {!Debouncer} Returns a debouncer object. */ static debounce(debouncer: Debouncer, asyncModule: AsyncInterface, callback: Function): Debouncer; } /** * Adds a `Debouncer` to a list of globally flushable tasks. * * @param {!Debouncer} debouncer Debouncer to enqueue * @return {void} */ export declare const enqueueDebouncer: (debouncer: any) => void; /** * Flushes any enqueued debouncers * * @return {boolean} Returns whether any debouncers were flushed */ export declare const flushDebouncers: () => boolean;