UNPKG

2.85 kBTypeScriptView Raw
1// tslint:disable:variable-name Describing an API that's defined elsewhere.
2// tslint:disable:no-any describes the API as best we are able today
3
4export {Debouncer};
5
6declare class Debouncer {
7 constructor();
8
9 /**
10 * Creates a debouncer if no debouncer is passed as a parameter
11 * or it cancels an active debouncer otherwise. The following
12 * example shows how a debouncer can be called multiple times within a
13 * microtask and "debounced" such that the provided callback function is
14 * called once. Add this method to a custom element:
15 *
16 * ```js
17 * import {microTask} from '@polymer/polymer/lib/utils/async.js';
18 * import {Debouncer} from '@polymer/polymer/lib/utils/debounce.js';
19 * // ...
20 *
21 * _debounceWork() {
22 * this._debounceJob = Debouncer.debounce(this._debounceJob,
23 * microTask, () => this._doWork());
24 * }
25 * ```
26 *
27 * If the `_debounceWork` method is called multiple times within the same
28 * microtask, the `_doWork` function will be called only once at the next
29 * microtask checkpoint.
30 *
31 * Note: In testing it is often convenient to avoid asynchrony. To accomplish
32 * this with a debouncer, you can use `enqueueDebouncer` and
33 * `flush`. For example, extend the above example by adding
34 * `enqueueDebouncer(this._debounceJob)` at the end of the
35 * `_debounceWork` method. Then in a test, call `flush` to ensure
36 * the debouncer has completed.
37 *
38 * @param debouncer Debouncer object.
39 * @param asyncModule Object with Async interface
40 * @param callback Callback to run.
41 * @returns Returns a debouncer object.
42 */
43 static debounce(debouncer: Debouncer|null, asyncModule: AsyncInterface, callback: () => any): Debouncer;
44
45 /**
46 * Sets the scheduler; that is, a module with the Async interface,
47 * a callback and optional arguments to be passed to the run function
48 * from the async module.
49 *
50 * @param asyncModule Object with Async interface.
51 * @param callback Callback to run.
52 */
53 setConfig(asyncModule: AsyncInterface, callback: () => any): void;
54
55 /**
56 * Cancels an active debouncer and returns a reference to itself.
57 */
58 cancel(): void;
59
60 /**
61 * Cancels a debouncer's async callback.
62 */
63 _cancelAsync(): void;
64
65 /**
66 * Flushes an active debouncer and returns a reference to itself.
67 */
68 flush(): void;
69
70 /**
71 * Returns true if the debouncer is active.
72 *
73 * @returns True if active.
74 */
75 isActive(): boolean;
76}
77
78export {enqueueDebouncer};
79
80
81/**
82 * Adds a `Debouncer` to a list of globally flushable tasks.
83 */
84declare function enqueueDebouncer(debouncer: Debouncer): void;
85
86export {flushDebouncers};
87
88
89/**
90 * Flushes any enqueued debouncers
91 *
92 * @returns Returns whether any debouncers were flushed
93 */
94declare function flushDebouncers(): boolean;
95
96import {AsyncInterface} from '../../interfaces';
97
\No newline at end of file