-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
utils.ts
397 lines (371 loc) · 11.3 KB
/
utils.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* External dependencies
*/
import {
useMemo as _useMemo,
useCallback as _useCallback,
useEffect as _useEffect,
useLayoutEffect as _useLayoutEffect,
type EffectCallback,
type Inputs,
} from 'preact/hooks';
import { effect } from '@preact/signals';
/**
* Internal dependencies
*/
import { getScope, setScope, resetScope } from './scopes';
import { getNamespace, setNamespace, resetNamespace } from './namespaces';
interface Flusher {
readonly flush: () => void;
readonly dispose: () => void;
}
/**
* Executes a callback function after the next frame is rendered.
*
* @param callback The callback function to be executed.
* @return A promise that resolves after the callback function is executed.
*/
const afterNextFrame = ( callback: () => void ) => {
return new Promise< void >( ( resolve ) => {
const done = () => {
clearTimeout( timeout );
window.cancelAnimationFrame( raf );
setTimeout( () => {
callback();
resolve();
} );
};
const timeout = setTimeout( done, 100 );
const raf = window.requestAnimationFrame( done );
} );
};
/**
* Returns a promise that resolves after yielding to main.
*
* @return Promise
*/
export const splitTask = () => {
return new Promise( ( resolve ) => {
// TODO: Use scheduler.yield() when available.
setTimeout( resolve, 0 );
} );
};
/**
* Creates a Flusher object that can be used to flush computed values and notify listeners.
*
* Using the mangled properties:
* this.c: this._callback
* this.x: this._compute
* https://github.com/preactjs/signals/blob/main/mangle.json
*
* @param compute The function that computes the value to be flushed.
* @param notify The function that notifies listeners when the value is flushed.
* @return The Flusher object with `flush` and `dispose` properties.
*/
function createFlusher( compute: () => unknown, notify: () => void ): Flusher {
let flush: () => void = () => undefined;
const dispose = effect( function ( this: any ) {
flush = this.c.bind( this );
this.x = compute;
this.c = notify;
return compute();
} );
return { flush, dispose } as const;
}
/**
* Custom hook that executes a callback function whenever a signal is triggered.
* Version of `useSignalEffect` with a `useEffect`-like execution. This hook
* implementation comes from this PR, but we added short-cirtuiting to avoid
* infinite loops: https://github.com/preactjs/signals/pull/290
*
* @param callback The callback function to be executed.
*/
export function useSignalEffect( callback: () => unknown ) {
_useEffect( () => {
let eff: Flusher | null = null;
let isExecuting = false;
const notify = async () => {
if ( eff && ! isExecuting ) {
isExecuting = true;
await afterNextFrame( eff.flush );
isExecuting = false;
}
};
eff = createFlusher( callback, notify );
return eff.dispose;
}, [] );
}
/**
* Returns the passed function wrapped with the current scope so it is
* accessible whenever the function runs. This is primarily to make the scope
* available inside hook callbacks.
*
* Asyncronous functions should use generators that yield promises instead of awaiting them.
* See the documentation for details: https://developer.wordpress.org/block-editor/reference-guides/packages/packages-interactivity/packages-interactivity-api-reference/#the-store
*
* @param func The passed function.
* @return The wrapped function.
*/
export function withScope<
Func extends ( ...args: any[] ) => Generator< any, any >,
>(
func: Func
): (
...args: Parameters< Func >
) => ReturnType< Func > extends Generator< any, infer Return >
? Promise< Return >
: never;
export function withScope< Func extends Function >( func: Func ): Func;
export function withScope( func: ( ...args: unknown[] ) => unknown ) {
const scope = getScope();
const ns = getNamespace();
if ( func?.constructor?.name === 'GeneratorFunction' ) {
return async ( ...args: Parameters< typeof func > ) => {
const gen = func( ...args ) as Generator;
let value: any;
let it: any;
while ( true ) {
setNamespace( ns );
setScope( scope );
try {
it = gen.next( value );
} finally {
resetScope();
resetNamespace();
}
try {
value = await it.value;
} catch ( e ) {
setNamespace( ns );
setScope( scope );
gen.throw( e );
} finally {
resetScope();
resetNamespace();
}
if ( it.done ) {
break;
}
}
return value;
};
}
return ( ...args: Parameters< typeof func > ) => {
setNamespace( ns );
setScope( scope );
try {
return func( ...args );
} finally {
resetNamespace();
resetScope();
}
};
}
/**
* Accepts a function that contains imperative code which runs whenever any of
* the accessed _reactive_ properties (e.g., values from the global state or the
* context) is modified.
*
* This hook makes the element's scope available so functions like
* `getElement()` and `getContext()` can be used inside the passed callback.
*
* @param callback The hook callback.
*/
export function useWatch( callback: () => unknown ) {
useSignalEffect( withScope( callback ) );
}
/**
* Accepts a function that contains imperative code which runs only after the
* element's first render, mainly useful for intialization logic.
*
* This hook makes the element's scope available so functions like
* `getElement()` and `getContext()` can be used inside the passed callback.
*
* @param callback The hook callback.
*/
export function useInit( callback: EffectCallback ) {
_useEffect( withScope( callback ), [] );
}
/**
* Accepts a function that contains imperative, possibly effectful code. The
* effects run after browser paint, without blocking it.
*
* This hook is equivalent to Preact's `useEffect` and makes the element's scope
* available so functions like `getElement()` and `getContext()` can be used
* inside the passed callback.
*
* @param callback Imperative function that can return a cleanup
* function.
* @param inputs If present, effect will only activate if the
* values in the list change (using `===`).
*/
export function useEffect( callback: EffectCallback, inputs: Inputs ) {
_useEffect( withScope( callback ), inputs );
}
/**
* Accepts a function that contains imperative, possibly effectful code. Use
* this to read layout from the DOM and synchronously re-render.
*
* This hook is equivalent to Preact's `useLayoutEffect` and makes the element's
* scope available so functions like `getElement()` and `getContext()` can be
* used inside the passed callback.
*
* @param callback Imperative function that can return a cleanup
* function.
* @param inputs If present, effect will only activate if the
* values in the list change (using `===`).
*/
export function useLayoutEffect( callback: EffectCallback, inputs: Inputs ) {
_useLayoutEffect( withScope( callback ), inputs );
}
/**
* Returns a memoized version of the callback that only changes if one of the
* inputs has changed (using `===`).
*
* This hook is equivalent to Preact's `useCallback` and makes the element's
* scope available so functions like `getElement()` and `getContext()` can be
* used inside the passed callback.
*
* @param callback Callback function.
* @param inputs If present, the callback will only be updated if the
* values in the list change (using `===`).
*
* @return The callback function.
*/
export function useCallback< T extends Function >(
callback: T,
inputs: Inputs
): T {
return _useCallback< T >( withScope( callback ), inputs );
}
/**
* Pass a factory function and an array of inputs. `useMemo` will only recompute
* the memoized value when one of the inputs has changed.
*
* This hook is equivalent to Preact's `useMemo` and makes the element's scope
* available so functions like `getElement()` and `getContext()` can be used
* inside the passed factory function.
*
* @param factory Factory function that returns that value for memoization.
* @param inputs If present, the factory will only be run to recompute if
* the values in the list change (using `===`).
*
* @return The memoized value.
*/
export function useMemo< T >( factory: () => T, inputs: Inputs ): T {
return _useMemo( withScope( factory ), inputs );
}
/**
* Creates a root fragment by replacing a node or an array of nodes in a parent element.
* For wrapperless hydration.
* See https://gist.github.com/developit/f4c67a2ede71dc2fab7f357f39cff28c
*
* @param parent The parent element where the nodes will be replaced.
* @param replaceNode The node or array of nodes to replace in the parent element.
* @return The created root fragment.
*/
export const createRootFragment = (
parent: Element,
replaceNode: Node | Node[]
) => {
replaceNode = ( [] as Node[] ).concat( replaceNode );
const sibling = replaceNode[ replaceNode.length - 1 ].nextSibling;
function insert( child: any, root: any ) {
parent.insertBefore( child, root || sibling );
}
return ( ( parent as any ).__k = {
nodeType: 1,
parentNode: parent,
firstChild: replaceNode[ 0 ],
childNodes: replaceNode,
insertBefore: insert,
appendChild: insert,
removeChild( c: Node ) {
parent.removeChild( c );
},
} );
};
/**
* Transforms a kebab-case string to camelCase.
*
* @param str The kebab-case string to transform to camelCase.
* @return The transformed camelCase string.
*/
export function kebabToCamelCase( str: string ): string {
return str
.replace( /^-+|-+$/g, '' )
.toLowerCase()
.replace( /-([a-z])/g, function ( _match, group1: string ) {
return group1.toUpperCase();
} );
}
const logged: Set< string > = new Set();
/**
* Shows a warning with `message` if environment is not `production`.
*
* Based on the `@wordpress/warning` package.
*
* @param message Message to show in the warning.
*/
export const warn = ( message: string ): void => {
if ( globalThis.SCRIPT_DEBUG ) {
if ( logged.has( message ) ) {
return;
}
// eslint-disable-next-line no-console
console.warn( message );
// Throwing an error and catching it immediately to improve debugging
// A consumer can use 'pause on caught exceptions'
try {
throw Error( message );
} catch ( e ) {
// Do nothing.
}
logged.add( message );
}
};
/**
* Checks if the passed `candidate` is a plain object with just the `Object`
* prototype.
*
* @param candidate The item to check.
* @return Whether `candidate` is a plain object.
*/
export const isPlainObject = (
candidate: unknown
): candidate is Record< string, unknown > =>
Boolean(
candidate &&
typeof candidate === 'object' &&
candidate.constructor === Object
);
export const deepMerge = (
target: any,
source: any,
override: boolean = true
) => {
if ( isPlainObject( target ) && isPlainObject( source ) ) {
for ( const key in source ) {
const desc = Object.getOwnPropertyDescriptor( source, key );
if (
typeof desc?.get === 'function' ||
typeof desc?.set === 'function'
) {
if ( override || ! ( key in target ) ) {
Object.defineProperty( target, key, {
...desc,
configurable: true,
enumerable: true,
} );
}
} else if ( isPlainObject( source[ key ] ) ) {
if ( ! target[ key ] ) {
target[ key ] = {};
}
deepMerge( target[ key ], source[ key ], override );
} else if ( override || ! ( key in target ) ) {
Object.defineProperty( target, key, desc! );
}
}
}
};