UNPKG

4.56 kBTypeScriptView Raw
1/**
2 * Bugs often appear in async code when stuff gets disposed, but async operations don't get canceled.
3 * This Async helper class solves these issues by tying async code to the lifetime of a disposable object.
4 *
5 * Usage: Anything class extending from BaseModel can access this helper via this.async. Otherwise create a
6 * new instance of the class and remember to call dispose() during your code's dispose handler.
7 *
8 * @public
9 */
10export declare class Async {
11 private _timeoutIds;
12 private _immediateIds;
13 private _intervalIds;
14 private _animationFrameIds;
15 private _isDisposed;
16 private _parent;
17 private _onErrorHandler;
18 private _noop;
19 constructor(parent?: object, onError?: (e: any) => void);
20 /**
21 * Dispose function, clears all async operations.
22 */
23 dispose(): void;
24 /**
25 * SetTimeout override, which will auto cancel the timeout during dispose.
26 * @param callback - Callback to execute.
27 * @param duration - Duration in milliseconds.
28 * @returns The setTimeout id.
29 */
30 setTimeout(callback: () => void, duration: number): number;
31 /**
32 * Clears the timeout.
33 * @param id - Id to cancel.
34 */
35 clearTimeout(id: number): void;
36 /**
37 * SetImmediate override, which will auto cancel the immediate during dispose.
38 * @param callback - Callback to execute.
39 * @param targetElement - Optional target element to use for identifying the correct window.
40 * @returns The setTimeout id.
41 */
42 setImmediate(callback: () => void, targetElement?: Element | null): number;
43 /**
44 * Clears the immediate.
45 * @param id - Id to cancel.
46 * @param targetElement - Optional target element to use for identifying the correct window.
47 */
48 clearImmediate(id: number, targetElement?: Element | null): void;
49 /**
50 * SetInterval override, which will auto cancel the timeout during dispose.
51 * @param callback - Callback to execute.
52 * @param duration - Duration in milliseconds.
53 * @returns The setTimeout id.
54 */
55 setInterval(callback: () => void, duration: number): number;
56 /**
57 * Clears the interval.
58 * @param id - Id to cancel.
59 */
60 clearInterval(id: number): void;
61 /**
62 * Creates a function that, when executed, will only call the func function at most once per
63 * every wait milliseconds. Provide an options object to indicate that func should be invoked
64 * on the leading and/or trailing edge of the wait timeout. Subsequent calls to the throttled
65 * function will return the result of the last func call.
66 *
67 * Note: If leading and trailing options are true func will be called on the trailing edge of
68 * the timeout only if the throttled function is invoked more than once during the wait timeout.
69 *
70 * @param func - The function to throttle.
71 * @param wait - The number of milliseconds to throttle executions to. Defaults to 0.
72 * @param options - The options object.
73 * @returns The new throttled function.
74 */
75 throttle<T extends (...args: any[]) => any>(func: T, wait?: number, options?: {
76 leading?: boolean;
77 trailing?: boolean;
78 }): T;
79 /**
80 * Creates a function that will delay the execution of func until after wait milliseconds have
81 * elapsed since the last time it was invoked. Provide an options object to indicate that func
82 * should be invoked on the leading and/or trailing edge of the wait timeout. Subsequent calls
83 * to the debounced function will return the result of the last func call.
84 *
85 * Note: If leading and trailing options are true func will be called on the trailing edge of
86 * the timeout only if the debounced function is invoked more than once during the wait
87 * timeout.
88 *
89 * @param func - The function to debounce.
90 * @param wait - The number of milliseconds to delay.
91 * @param options - The options object.
92 * @returns The new debounced function.
93 */
94 debounce<T extends (...args: any[]) => any>(func: T, wait?: number, options?: {
95 leading?: boolean;
96 maxWait?: number;
97 trailing?: boolean;
98 }): ICancelable<T> & T;
99 requestAnimationFrame(callback: () => void, targetElement?: Element | null): number;
100 cancelAnimationFrame(id: number, targetElement?: Element | null): void;
101 protected _logError(e: any): void;
102}
103export declare type ICancelable<T extends (...args: any[]) => any> = {
104 flush: () => ReturnType<T>;
105 cancel: () => void;
106 pending: () => boolean;
107};