File size: 3,162 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { TimerHandle } from 'calypso/types';

export interface Cancelable {
	cancel: () => void;
}

/**
 * Creates a delayed function that invokes `func` as soon as possible after the next layout
 * flush. At that time, it is very unlikely that the DOM has been written to since the last
 * layout and it should be safe to read from DOM without causing a synchronous reflow, i.e.,
 * cheaply and without any performance hit.
 *
 * The returned wrapped function comes with a `cancel` method that cancels the delayed invocations.
 *
 * Inspired by the Firefox performance best practices MDN article at:
 * https://developer.mozilla.org/en-US/Firefox/Performance_best_practices_for_Firefox_fe_engineers
 * @param func - The function to be invoked after the layout flush
 * @returns The new delayed function
 */
export default function afterLayoutFlush< T extends ( ...args: any[] ) => any >( func: T ) {
	let timeoutHandle: TimerHandle | undefined = undefined;
	let rafHandle: number | undefined = undefined;

	const hasRAF = typeof requestAnimationFrame === 'function';

	let lastThis: any;
	let lastArgs: any[] | undefined;

	const scheduleRAF = function ( rafFunc: T ) {
		return function ( this: any, ...args: any[] ) {
			// eslint-disable-next-line @typescript-eslint/no-this-alias
			lastThis = this;
			lastArgs = args;

			// if a RAF is already scheduled and not yet executed, don't schedule another one
			if ( rafHandle !== undefined ) {
				return;
			}

			rafHandle = requestAnimationFrame( () => {
				// clear the handle to signal that the scheduled RAF has been executed
				rafHandle = undefined;
				rafFunc();
			} );
		} as T;
	};

	const scheduleTimeout = function ( timeoutFunc: T ) {
		return function ( this: any, ...args: any[] ) {
			if ( ! hasRAF ) {
				// eslint-disable-next-line @typescript-eslint/no-this-alias
				lastThis = this;
				lastArgs = args;
			}

			// If a timeout is already scheduled and not yet executed, don't schedule another one.
			// Multiple `requestAnimationFrame` handlers can be scheduled and executed before the
			// browser decides to finally execute the timeout handler.
			if ( timeoutHandle !== undefined ) {
				return;
			}

			timeoutHandle = setTimeout( () => {
				const callArgs = lastArgs !== undefined ? lastArgs : [];
				const callThis = lastThis;

				lastArgs = undefined;
				lastThis = undefined;

				// clear the handle to signal that the timeout handler has been executed
				timeoutHandle = undefined;
				timeoutFunc.apply( callThis, callArgs );
			}, 0 );
		} as T;
	};

	// if RAF is not supported (in Node.js test environment), the wrapped function
	// will only set a timeout.
	let wrappedFunc: T = scheduleTimeout( func );
	if ( hasRAF ) {
		wrappedFunc = scheduleRAF( wrappedFunc );
	}

	const wrappedWithCancel = wrappedFunc as T & Cancelable;
	wrappedWithCancel.cancel = () => {
		lastThis = undefined;
		lastArgs = undefined;

		if ( rafHandle !== undefined ) {
			cancelAnimationFrame( rafHandle );
			rafHandle = undefined;
		}

		if ( timeoutHandle !== undefined ) {
			clearTimeout( timeoutHandle );
			timeoutHandle = undefined;
		}
	};

	return wrappedWithCancel;
}