isomorfeus-preact 10.6.0 → 10.6.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/lib/preact/version.rb +1 -1
  3. data/node_modules/.package-lock.json +3 -3
  4. data/node_modules/preact/LICENSE +21 -21
  5. data/node_modules/preact/README.md +187 -187
  6. data/node_modules/preact/compat/dist/compat.js +1 -1
  7. data/node_modules/preact/compat/dist/compat.js.map +1 -1
  8. data/node_modules/preact/compat/dist/compat.mjs +1 -1
  9. data/node_modules/preact/compat/dist/compat.module.js +1 -1
  10. data/node_modules/preact/compat/dist/compat.module.js.map +1 -1
  11. data/node_modules/preact/compat/dist/compat.umd.js +1 -1
  12. data/node_modules/preact/compat/dist/compat.umd.js.map +1 -1
  13. data/node_modules/preact/compat/jsx-dev-runtime.js +1 -1
  14. data/node_modules/preact/compat/jsx-dev-runtime.mjs +1 -1
  15. data/node_modules/preact/compat/jsx-runtime.js +1 -1
  16. data/node_modules/preact/compat/jsx-runtime.mjs +1 -1
  17. data/node_modules/preact/compat/package.json +19 -19
  18. data/node_modules/preact/compat/scheduler.js +15 -15
  19. data/node_modules/preact/compat/scheduler.mjs +23 -23
  20. data/node_modules/preact/compat/server.js +15 -15
  21. data/node_modules/preact/compat/server.mjs +4 -4
  22. data/node_modules/preact/compat/src/Children.js +21 -21
  23. data/node_modules/preact/compat/src/PureComponent.js +15 -15
  24. data/node_modules/preact/compat/src/forwardRef.js +51 -51
  25. data/node_modules/preact/compat/src/index.d.ts +141 -141
  26. data/node_modules/preact/compat/src/index.js +187 -187
  27. data/node_modules/preact/compat/src/internal.d.ts +47 -47
  28. data/node_modules/preact/compat/src/memo.js +34 -34
  29. data/node_modules/preact/compat/src/portals.js +80 -80
  30. data/node_modules/preact/compat/src/render.js +230 -226
  31. data/node_modules/preact/compat/src/suspense-list.d.ts +14 -14
  32. data/node_modules/preact/compat/src/suspense-list.js +126 -126
  33. data/node_modules/preact/compat/src/suspense.d.ts +15 -15
  34. data/node_modules/preact/compat/src/suspense.js +270 -270
  35. data/node_modules/preact/compat/src/util.js +28 -28
  36. data/node_modules/preact/compat/test-utils.js +1 -1
  37. data/node_modules/preact/debug/dist/debug.js.map +1 -1
  38. data/node_modules/preact/debug/dist/debug.module.js.map +1 -1
  39. data/node_modules/preact/debug/dist/debug.umd.js.map +1 -1
  40. data/node_modules/preact/debug/package.json +18 -18
  41. data/node_modules/preact/debug/src/check-props.js +54 -54
  42. data/node_modules/preact/debug/src/component-stack.js +146 -146
  43. data/node_modules/preact/debug/src/constants.js +3 -3
  44. data/node_modules/preact/debug/src/debug.js +442 -442
  45. data/node_modules/preact/debug/src/index.js +6 -6
  46. data/node_modules/preact/debug/src/internal.d.ts +82 -82
  47. data/node_modules/preact/debug/src/util.js +11 -11
  48. data/node_modules/preact/devtools/dist/devtools.js +1 -1
  49. data/node_modules/preact/devtools/dist/devtools.js.map +1 -1
  50. data/node_modules/preact/devtools/dist/devtools.mjs +1 -1
  51. data/node_modules/preact/devtools/dist/devtools.module.js +1 -1
  52. data/node_modules/preact/devtools/dist/devtools.module.js.map +1 -1
  53. data/node_modules/preact/devtools/dist/devtools.umd.js +1 -1
  54. data/node_modules/preact/devtools/dist/devtools.umd.js.map +1 -1
  55. data/node_modules/preact/devtools/package.json +16 -16
  56. data/node_modules/preact/devtools/src/devtools.js +10 -10
  57. data/node_modules/preact/devtools/src/index.d.ts +8 -8
  58. data/node_modules/preact/devtools/src/index.js +15 -15
  59. data/node_modules/preact/dist/preact.js +1 -1
  60. data/node_modules/preact/dist/preact.js.map +1 -1
  61. data/node_modules/preact/dist/preact.min.js +1 -1
  62. data/node_modules/preact/dist/preact.min.js.map +1 -1
  63. data/node_modules/preact/dist/preact.mjs +1 -1
  64. data/node_modules/preact/dist/preact.module.js +1 -1
  65. data/node_modules/preact/dist/preact.module.js.map +1 -1
  66. data/node_modules/preact/dist/preact.umd.js +1 -1
  67. data/node_modules/preact/dist/preact.umd.js.map +1 -1
  68. data/node_modules/preact/hooks/dist/hooks.js +1 -1
  69. data/node_modules/preact/hooks/dist/hooks.js.map +1 -1
  70. data/node_modules/preact/hooks/dist/hooks.mjs +1 -1
  71. data/node_modules/preact/hooks/dist/hooks.module.js +1 -1
  72. data/node_modules/preact/hooks/dist/hooks.module.js.map +1 -1
  73. data/node_modules/preact/hooks/dist/hooks.umd.js +1 -1
  74. data/node_modules/preact/hooks/dist/hooks.umd.js.map +1 -1
  75. data/node_modules/preact/hooks/package.json +26 -26
  76. data/node_modules/preact/hooks/src/index.d.ts +139 -139
  77. data/node_modules/preact/hooks/src/index.js +388 -379
  78. data/node_modules/preact/hooks/src/internal.d.ts +75 -75
  79. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js.map +1 -1
  80. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js.map +1 -1
  81. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js.map +1 -1
  82. data/node_modules/preact/jsx-runtime/package.json +19 -19
  83. data/node_modules/preact/jsx-runtime/src/index.d.ts +50 -50
  84. data/node_modules/preact/jsx-runtime/src/index.js +77 -77
  85. data/node_modules/preact/package.json +287 -281
  86. data/node_modules/preact/src/cjs.js +3 -3
  87. data/node_modules/preact/src/clone-element.js +34 -34
  88. data/node_modules/preact/src/component.js +225 -225
  89. data/node_modules/preact/src/constants.js +3 -3
  90. data/node_modules/preact/src/create-context.js +68 -68
  91. data/node_modules/preact/src/create-element.js +98 -98
  92. data/node_modules/preact/src/diff/catch-error.js +38 -38
  93. data/node_modules/preact/src/diff/children.js +335 -335
  94. data/node_modules/preact/src/diff/index.js +516 -516
  95. data/node_modules/preact/src/diff/props.js +158 -158
  96. data/node_modules/preact/src/index.d.ts +310 -310
  97. data/node_modules/preact/src/index.js +13 -13
  98. data/node_modules/preact/src/internal.d.ts +146 -146
  99. data/node_modules/preact/src/jsx.d.ts +1005 -1005
  100. data/node_modules/preact/src/options.js +16 -16
  101. data/node_modules/preact/src/render.js +75 -75
  102. data/node_modules/preact/src/util.js +27 -27
  103. data/node_modules/preact/test-utils/dist/testUtils.js.map +1 -1
  104. data/node_modules/preact/test-utils/dist/testUtils.module.js.map +1 -1
  105. data/node_modules/preact/test-utils/dist/testUtils.umd.js.map +1 -1
  106. data/node_modules/preact/test-utils/package.json +19 -19
  107. data/node_modules/preact/test-utils/src/index.d.ts +3 -3
  108. data/node_modules/preact/test-utils/src/index.js +117 -117
  109. data/package.json +1 -1
  110. metadata +15 -35
  111. data/node_modules/preact/compat/dist/compat.mjs.map +0 -1
  112. data/node_modules/preact/compat/dist/compat.modern.js +0 -2
  113. data/node_modules/preact/compat/dist/compat.modern.js.map +0 -1
  114. data/node_modules/preact/debug/dist/debug.mjs.map +0 -1
  115. data/node_modules/preact/debug/dist/debug.modern.js +0 -2
  116. data/node_modules/preact/debug/dist/debug.modern.js.map +0 -1
  117. data/node_modules/preact/devtools/dist/devtools.mjs.map +0 -1
  118. data/node_modules/preact/devtools/dist/devtools.modern.js +0 -2
  119. data/node_modules/preact/devtools/dist/devtools.modern.js.map +0 -1
  120. data/node_modules/preact/dist/index.d.ts +0 -1295
  121. data/node_modules/preact/dist/preact.mjs.map +0 -1
  122. data/node_modules/preact/hooks/dist/hooks.mjs.map +0 -1
  123. data/node_modules/preact/hooks/dist/hooks.modern.js +0 -2
  124. data/node_modules/preact/hooks/dist/hooks.modern.js.map +0 -1
  125. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.mjs.map +0 -1
  126. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.modern.js +0 -2
  127. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.modern.js.map +0 -1
  128. data/node_modules/preact/test-utils/dist/testUtils.mjs.map +0 -1
  129. data/node_modules/preact/test-utils/dist/testUtils.modern.js +0 -2
  130. data/node_modules/preact/test-utils/dist/testUtils.modern.js.map +0 -1
@@ -1,379 +1,388 @@
1
- import { options } from 'preact';
2
-
3
- /** @type {number} */
4
- let currentIndex;
5
-
6
- /** @type {import('./internal').Component} */
7
- let currentComponent;
8
-
9
- /** @type {number} */
10
- let currentHook = 0;
11
-
12
- /** @type {Array<import('./internal').Component>} */
13
- let afterPaintEffects = [];
14
-
15
- let oldBeforeDiff = options._diff;
16
- let oldBeforeRender = options._render;
17
- let oldAfterDiff = options.diffed;
18
- let oldCommit = options._commit;
19
- let oldBeforeUnmount = options.unmount;
20
-
21
- const RAF_TIMEOUT = 100;
22
- let prevRaf;
23
-
24
- options._diff = vnode => {
25
- currentComponent = null;
26
- if (oldBeforeDiff) oldBeforeDiff(vnode);
27
- };
28
-
29
- options._render = vnode => {
30
- if (oldBeforeRender) oldBeforeRender(vnode);
31
-
32
- currentComponent = vnode._component;
33
- currentIndex = 0;
34
-
35
- const hooks = currentComponent.__hooks;
36
- if (hooks) {
37
- hooks._pendingEffects.forEach(invokeCleanup);
38
- hooks._pendingEffects.forEach(invokeEffect);
39
- hooks._pendingEffects = [];
40
- }
41
- };
42
-
43
- options.diffed = vnode => {
44
- if (oldAfterDiff) oldAfterDiff(vnode);
45
-
46
- const c = vnode._component;
47
- if (c && c.__hooks && c.__hooks._pendingEffects.length) {
48
- afterPaint(afterPaintEffects.push(c));
49
- }
50
- currentComponent = null;
51
- };
52
-
53
- options._commit = (vnode, commitQueue) => {
54
- commitQueue.some(component => {
55
- try {
56
- component._renderCallbacks.forEach(invokeCleanup);
57
- component._renderCallbacks = component._renderCallbacks.filter(cb =>
58
- cb._value ? invokeEffect(cb) : true
59
- );
60
- } catch (e) {
61
- commitQueue.some(c => {
62
- if (c._renderCallbacks) c._renderCallbacks = [];
63
- });
64
- commitQueue = [];
65
- options._catchError(e, component._vnode);
66
- }
67
- });
68
-
69
- if (oldCommit) oldCommit(vnode, commitQueue);
70
- };
71
-
72
- options.unmount = vnode => {
73
- if (oldBeforeUnmount) oldBeforeUnmount(vnode);
74
-
75
- const c = vnode._component;
76
- if (c && c.__hooks) {
77
- try {
78
- c.__hooks._list.forEach(invokeCleanup);
79
- } catch (e) {
80
- options._catchError(e, c._vnode);
81
- }
82
- }
83
- };
84
-
85
- /**
86
- * Get a hook's state from the currentComponent
87
- * @param {number} index The index of the hook to get
88
- * @param {number} type The index of the hook to get
89
- * @returns {any}
90
- */
91
- function getHookState(index, type) {
92
- if (options._hook) {
93
- options._hook(currentComponent, index, currentHook || type);
94
- }
95
- currentHook = 0;
96
-
97
- // Largely inspired by:
98
- // * https://github.com/michael-klein/funcy.js/blob/f6be73468e6ec46b0ff5aa3cc4c9baf72a29025a/src/hooks/core_hooks.mjs
99
- // * https://github.com/michael-klein/funcy.js/blob/650beaa58c43c33a74820a3c98b3c7079cf2e333/src/renderer.mjs
100
- // Other implementations to look at:
101
- // * https://codesandbox.io/s/mnox05qp8
102
- const hooks =
103
- currentComponent.__hooks ||
104
- (currentComponent.__hooks = {
105
- _list: [],
106
- _pendingEffects: []
107
- });
108
-
109
- if (index >= hooks._list.length) {
110
- hooks._list.push({});
111
- }
112
- return hooks._list[index];
113
- }
114
-
115
- /**
116
- * @param {import('./index').StateUpdater<any>} [initialState]
117
- */
118
- export function useState(initialState) {
119
- currentHook = 1;
120
- return useReducer(invokeOrReturn, initialState);
121
- }
122
-
123
- /**
124
- * @param {import('./index').Reducer<any, any>} reducer
125
- * @param {import('./index').StateUpdater<any>} initialState
126
- * @param {(initialState: any) => void} [init]
127
- * @returns {[ any, (state: any) => void ]}
128
- */
129
- export function useReducer(reducer, initialState, init) {
130
- /** @type {import('./internal').ReducerHookState} */
131
- const hookState = getHookState(currentIndex++, 2);
132
- hookState._reducer = reducer;
133
- if (!hookState._component) {
134
- hookState._value = [
135
- !init ? invokeOrReturn(undefined, initialState) : init(initialState),
136
-
137
- action => {
138
- const nextValue = hookState._reducer(hookState._value[0], action);
139
- if (hookState._value[0] !== nextValue) {
140
- hookState._value = [nextValue, hookState._value[1]];
141
- hookState._component.setState({});
142
- }
143
- }
144
- ];
145
-
146
- hookState._component = currentComponent;
147
- }
148
-
149
- return hookState._value;
150
- }
151
-
152
- /**
153
- * @param {import('./internal').Effect} callback
154
- * @param {any[]} args
155
- */
156
- export function useEffect(callback, args) {
157
- /** @type {import('./internal').EffectHookState} */
158
- const state = getHookState(currentIndex++, 3);
159
- if (!options._skipEffects && argsChanged(state._args, args)) {
160
- state._value = callback;
161
- state._args = args;
162
-
163
- currentComponent.__hooks._pendingEffects.push(state);
164
- }
165
- }
166
-
167
- /**
168
- * @param {import('./internal').Effect} callback
169
- * @param {any[]} args
170
- */
171
- export function useLayoutEffect(callback, args) {
172
- /** @type {import('./internal').EffectHookState} */
173
- const state = getHookState(currentIndex++, 4);
174
- if (!options._skipEffects && argsChanged(state._args, args)) {
175
- state._value = callback;
176
- state._args = args;
177
-
178
- currentComponent._renderCallbacks.push(state);
179
- }
180
- }
181
-
182
- export function useRef(initialValue) {
183
- currentHook = 5;
184
- return useMemo(() => ({ current: initialValue }), []);
185
- }
186
-
187
- /**
188
- * @param {object} ref
189
- * @param {() => object} createHandle
190
- * @param {any[]} args
191
- */
192
- export function useImperativeHandle(ref, createHandle, args) {
193
- currentHook = 6;
194
- useLayoutEffect(
195
- () => {
196
- if (typeof ref == 'function') ref(createHandle());
197
- else if (ref) ref.current = createHandle();
198
- },
199
- args == null ? args : args.concat(ref)
200
- );
201
- }
202
-
203
- /**
204
- * @param {() => any} factory
205
- * @param {any[]} args
206
- */
207
- export function useMemo(factory, args) {
208
- /** @type {import('./internal').MemoHookState} */
209
- const state = getHookState(currentIndex++, 7);
210
- if (argsChanged(state._args, args)) {
211
- state._value = factory();
212
- state._args = args;
213
- state._factory = factory;
214
- }
215
-
216
- return state._value;
217
- }
218
-
219
- /**
220
- * @param {() => void} callback
221
- * @param {any[]} args
222
- */
223
- export function useCallback(callback, args) {
224
- currentHook = 8;
225
- return useMemo(() => callback, args);
226
- }
227
-
228
- /**
229
- * @param {import('./internal').PreactContext} context
230
- */
231
- export function useContext(context) {
232
- const provider = currentComponent.context[context._id];
233
- // We could skip this call here, but than we'd not call
234
- // `options._hook`. We need to do that in order to make
235
- // the devtools aware of this hook.
236
- /** @type {import('./internal').ContextHookState} */
237
- const state = getHookState(currentIndex++, 9);
238
- // The devtools needs access to the context object to
239
- // be able to pull of the default value when no provider
240
- // is present in the tree.
241
- state._context = context;
242
- if (!provider) return context._defaultValue;
243
- // This is probably not safe to convert to "!"
244
- if (state._value == null) {
245
- state._value = true;
246
- provider.sub(currentComponent);
247
- }
248
- return provider.props.value;
249
- }
250
-
251
- /**
252
- * Display a custom label for a custom hook for the devtools panel
253
- * @type {<T>(value: T, cb?: (value: T) => string | number) => void}
254
- */
255
- export function useDebugValue(value, formatter) {
256
- if (options.useDebugValue) {
257
- options.useDebugValue(formatter ? formatter(value) : value);
258
- }
259
- }
260
-
261
- /**
262
- * @param {(error: any) => void} cb
263
- */
264
- export function useErrorBoundary(cb) {
265
- /** @type {import('./internal').ErrorBoundaryHookState} */
266
- const state = getHookState(currentIndex++, 10);
267
- const errState = useState();
268
- state._value = cb;
269
- if (!currentComponent.componentDidCatch) {
270
- currentComponent.componentDidCatch = err => {
271
- if (state._value) state._value(err);
272
- errState[1](err);
273
- };
274
- }
275
- return [
276
- errState[0],
277
- () => {
278
- errState[1](undefined);
279
- }
280
- ];
281
- }
282
-
283
- /**
284
- * After paint effects consumer.
285
- */
286
- function flushAfterPaintEffects() {
287
- afterPaintEffects.forEach(component => {
288
- if (component._parentDom) {
289
- try {
290
- component.__hooks._pendingEffects.forEach(invokeCleanup);
291
- component.__hooks._pendingEffects.forEach(invokeEffect);
292
- component.__hooks._pendingEffects = [];
293
- } catch (e) {
294
- component.__hooks._pendingEffects = [];
295
- options._catchError(e, component._vnode);
296
- }
297
- }
298
- });
299
- afterPaintEffects = [];
300
- }
301
-
302
- let HAS_RAF = typeof requestAnimationFrame == 'function';
303
-
304
- /**
305
- * Schedule a callback to be invoked after the browser has a chance to paint a new frame.
306
- * Do this by combining requestAnimationFrame (rAF) + setTimeout to invoke a callback after
307
- * the next browser frame.
308
- *
309
- * Also, schedule a timeout in parallel to the the rAF to ensure the callback is invoked
310
- * even if RAF doesn't fire (for example if the browser tab is not visible)
311
- *
312
- * @param {() => void} callback
313
- */
314
- function afterNextFrame(callback) {
315
- const done = () => {
316
- clearTimeout(timeout);
317
- if (HAS_RAF) cancelAnimationFrame(raf);
318
- setTimeout(callback);
319
- };
320
- const timeout = setTimeout(done, RAF_TIMEOUT);
321
-
322
- let raf;
323
- if (HAS_RAF) {
324
- raf = requestAnimationFrame(done);
325
- }
326
- }
327
-
328
- // Note: if someone used options.debounceRendering = requestAnimationFrame,
329
- // then effects will ALWAYS run on the NEXT frame instead of the current one, incurring a ~16ms delay.
330
- // Perhaps this is not such a big deal.
331
- /**
332
- * Schedule afterPaintEffects flush after the browser paints
333
- * @param {number} newQueueLength
334
- */
335
- function afterPaint(newQueueLength) {
336
- if (newQueueLength === 1 || prevRaf !== options.requestAnimationFrame) {
337
- prevRaf = options.requestAnimationFrame;
338
- (prevRaf || afterNextFrame)(flushAfterPaintEffects);
339
- }
340
- }
341
-
342
- /**
343
- * @param {import('./internal').EffectHookState} hook
344
- */
345
- function invokeCleanup(hook) {
346
- // A hook cleanup can introduce a call to render which creates a new root, this will call options.vnode
347
- // and move the currentComponent away.
348
- const comp = currentComponent;
349
- if (typeof hook._cleanup == 'function') hook._cleanup();
350
- currentComponent = comp;
351
- }
352
-
353
- /**
354
- * Invoke a Hook's effect
355
- * @param {import('./internal').EffectHookState} hook
356
- */
357
- function invokeEffect(hook) {
358
- // A hook call can introduce a call to render which creates a new root, this will call options.vnode
359
- // and move the currentComponent away.
360
- const comp = currentComponent;
361
- hook._cleanup = hook._value();
362
- currentComponent = comp;
363
- }
364
-
365
- /**
366
- * @param {any[]} oldArgs
367
- * @param {any[]} newArgs
368
- */
369
- function argsChanged(oldArgs, newArgs) {
370
- return (
371
- !oldArgs ||
372
- oldArgs.length !== newArgs.length ||
373
- newArgs.some((arg, index) => arg !== oldArgs[index])
374
- );
375
- }
376
-
377
- function invokeOrReturn(arg, f) {
378
- return typeof f == 'function' ? f(arg) : f;
379
- }
1
+ import { options } from 'preact';
2
+
3
+ /** @type {number} */
4
+ let currentIndex;
5
+
6
+ /** @type {import('./internal').Component} */
7
+ let currentComponent;
8
+
9
+ /** @type {number} */
10
+ let currentHook = 0;
11
+
12
+ /** @type {Array<import('./internal').Component>} */
13
+ let afterPaintEffects = [];
14
+
15
+ let oldBeforeDiff = options._diff;
16
+ let oldBeforeRender = options._render;
17
+ let oldAfterDiff = options.diffed;
18
+ let oldCommit = options._commit;
19
+ let oldBeforeUnmount = options.unmount;
20
+
21
+ const RAF_TIMEOUT = 100;
22
+ let prevRaf;
23
+
24
+ options._diff = vnode => {
25
+ currentComponent = null;
26
+ if (oldBeforeDiff) oldBeforeDiff(vnode);
27
+ };
28
+
29
+ options._render = vnode => {
30
+ if (oldBeforeRender) oldBeforeRender(vnode);
31
+
32
+ currentComponent = vnode._component;
33
+ currentIndex = 0;
34
+
35
+ const hooks = currentComponent.__hooks;
36
+ if (hooks) {
37
+ hooks._pendingEffects.forEach(invokeCleanup);
38
+ hooks._pendingEffects.forEach(invokeEffect);
39
+ hooks._pendingEffects = [];
40
+ }
41
+ };
42
+
43
+ options.diffed = vnode => {
44
+ if (oldAfterDiff) oldAfterDiff(vnode);
45
+
46
+ const c = vnode._component;
47
+ if (c && c.__hooks && c.__hooks._pendingEffects.length) {
48
+ afterPaint(afterPaintEffects.push(c));
49
+ }
50
+ currentComponent = null;
51
+ };
52
+
53
+ options._commit = (vnode, commitQueue) => {
54
+ commitQueue.some(component => {
55
+ try {
56
+ component._renderCallbacks.forEach(invokeCleanup);
57
+ component._renderCallbacks = component._renderCallbacks.filter(cb =>
58
+ cb._value ? invokeEffect(cb) : true
59
+ );
60
+ } catch (e) {
61
+ commitQueue.some(c => {
62
+ if (c._renderCallbacks) c._renderCallbacks = [];
63
+ });
64
+ commitQueue = [];
65
+ options._catchError(e, component._vnode);
66
+ }
67
+ });
68
+
69
+ if (oldCommit) oldCommit(vnode, commitQueue);
70
+ };
71
+
72
+ options.unmount = vnode => {
73
+ if (oldBeforeUnmount) oldBeforeUnmount(vnode);
74
+
75
+ const c = vnode._component;
76
+ if (c && c.__hooks) {
77
+ let hasErrored;
78
+ c.__hooks._list.forEach(s => {
79
+ try {
80
+ invokeCleanup(s);
81
+ } catch (e) {
82
+ hasErrored = e;
83
+ }
84
+ });
85
+ if (hasErrored) options._catchError(hasErrored, c._vnode);
86
+ }
87
+ };
88
+
89
+ /**
90
+ * Get a hook's state from the currentComponent
91
+ * @param {number} index The index of the hook to get
92
+ * @param {number} type The index of the hook to get
93
+ * @returns {any}
94
+ */
95
+ function getHookState(index, type) {
96
+ if (options._hook) {
97
+ options._hook(currentComponent, index, currentHook || type);
98
+ }
99
+ currentHook = 0;
100
+
101
+ // Largely inspired by:
102
+ // * https://github.com/michael-klein/funcy.js/blob/f6be73468e6ec46b0ff5aa3cc4c9baf72a29025a/src/hooks/core_hooks.mjs
103
+ // * https://github.com/michael-klein/funcy.js/blob/650beaa58c43c33a74820a3c98b3c7079cf2e333/src/renderer.mjs
104
+ // Other implementations to look at:
105
+ // * https://codesandbox.io/s/mnox05qp8
106
+ const hooks =
107
+ currentComponent.__hooks ||
108
+ (currentComponent.__hooks = {
109
+ _list: [],
110
+ _pendingEffects: []
111
+ });
112
+
113
+ if (index >= hooks._list.length) {
114
+ hooks._list.push({});
115
+ }
116
+ return hooks._list[index];
117
+ }
118
+
119
+ /**
120
+ * @param {import('./index').StateUpdater<any>} [initialState]
121
+ */
122
+ export function useState(initialState) {
123
+ currentHook = 1;
124
+ return useReducer(invokeOrReturn, initialState);
125
+ }
126
+
127
+ /**
128
+ * @param {import('./index').Reducer<any, any>} reducer
129
+ * @param {import('./index').StateUpdater<any>} initialState
130
+ * @param {(initialState: any) => void} [init]
131
+ * @returns {[ any, (state: any) => void ]}
132
+ */
133
+ export function useReducer(reducer, initialState, init) {
134
+ /** @type {import('./internal').ReducerHookState} */
135
+ const hookState = getHookState(currentIndex++, 2);
136
+ hookState._reducer = reducer;
137
+ if (!hookState._component) {
138
+ hookState._value = [
139
+ !init ? invokeOrReturn(undefined, initialState) : init(initialState),
140
+
141
+ action => {
142
+ const nextValue = hookState._reducer(hookState._value[0], action);
143
+ if (hookState._value[0] !== nextValue) {
144
+ hookState._value = [nextValue, hookState._value[1]];
145
+ hookState._component.setState({});
146
+ }
147
+ }
148
+ ];
149
+
150
+ hookState._component = currentComponent;
151
+ }
152
+
153
+ return hookState._value;
154
+ }
155
+
156
+ /**
157
+ * @param {import('./internal').Effect} callback
158
+ * @param {any[]} args
159
+ */
160
+ export function useEffect(callback, args) {
161
+ /** @type {import('./internal').EffectHookState} */
162
+ const state = getHookState(currentIndex++, 3);
163
+ if (!options._skipEffects && argsChanged(state._args, args)) {
164
+ state._value = callback;
165
+ state._args = args;
166
+
167
+ currentComponent.__hooks._pendingEffects.push(state);
168
+ }
169
+ }
170
+
171
+ /**
172
+ * @param {import('./internal').Effect} callback
173
+ * @param {any[]} args
174
+ */
175
+ export function useLayoutEffect(callback, args) {
176
+ /** @type {import('./internal').EffectHookState} */
177
+ const state = getHookState(currentIndex++, 4);
178
+ if (!options._skipEffects && argsChanged(state._args, args)) {
179
+ state._value = callback;
180
+ state._args = args;
181
+
182
+ currentComponent._renderCallbacks.push(state);
183
+ }
184
+ }
185
+
186
+ export function useRef(initialValue) {
187
+ currentHook = 5;
188
+ return useMemo(() => ({ current: initialValue }), []);
189
+ }
190
+
191
+ /**
192
+ * @param {object} ref
193
+ * @param {() => object} createHandle
194
+ * @param {any[]} args
195
+ */
196
+ export function useImperativeHandle(ref, createHandle, args) {
197
+ currentHook = 6;
198
+ useLayoutEffect(
199
+ () => {
200
+ if (typeof ref == 'function') ref(createHandle());
201
+ else if (ref) ref.current = createHandle();
202
+ },
203
+ args == null ? args : args.concat(ref)
204
+ );
205
+ }
206
+
207
+ /**
208
+ * @param {() => any} factory
209
+ * @param {any[]} args
210
+ */
211
+ export function useMemo(factory, args) {
212
+ /** @type {import('./internal').MemoHookState} */
213
+ const state = getHookState(currentIndex++, 7);
214
+ if (argsChanged(state._args, args)) {
215
+ state._value = factory();
216
+ state._args = args;
217
+ state._factory = factory;
218
+ }
219
+
220
+ return state._value;
221
+ }
222
+
223
+ /**
224
+ * @param {() => void} callback
225
+ * @param {any[]} args
226
+ */
227
+ export function useCallback(callback, args) {
228
+ currentHook = 8;
229
+ return useMemo(() => callback, args);
230
+ }
231
+
232
+ /**
233
+ * @param {import('./internal').PreactContext} context
234
+ */
235
+ export function useContext(context) {
236
+ const provider = currentComponent.context[context._id];
237
+ // We could skip this call here, but than we'd not call
238
+ // `options._hook`. We need to do that in order to make
239
+ // the devtools aware of this hook.
240
+ /** @type {import('./internal').ContextHookState} */
241
+ const state = getHookState(currentIndex++, 9);
242
+ // The devtools needs access to the context object to
243
+ // be able to pull of the default value when no provider
244
+ // is present in the tree.
245
+ state._context = context;
246
+ if (!provider) return context._defaultValue;
247
+ // This is probably not safe to convert to "!"
248
+ if (state._value == null) {
249
+ state._value = true;
250
+ provider.sub(currentComponent);
251
+ }
252
+ return provider.props.value;
253
+ }
254
+
255
+ /**
256
+ * Display a custom label for a custom hook for the devtools panel
257
+ * @type {<T>(value: T, cb?: (value: T) => string | number) => void}
258
+ */
259
+ export function useDebugValue(value, formatter) {
260
+ if (options.useDebugValue) {
261
+ options.useDebugValue(formatter ? formatter(value) : value);
262
+ }
263
+ }
264
+
265
+ /**
266
+ * @param {(error: any) => void} cb
267
+ */
268
+ export function useErrorBoundary(cb) {
269
+ /** @type {import('./internal').ErrorBoundaryHookState} */
270
+ const state = getHookState(currentIndex++, 10);
271
+ const errState = useState();
272
+ state._value = cb;
273
+ if (!currentComponent.componentDidCatch) {
274
+ currentComponent.componentDidCatch = err => {
275
+ if (state._value) state._value(err);
276
+ errState[1](err);
277
+ };
278
+ }
279
+ return [
280
+ errState[0],
281
+ () => {
282
+ errState[1](undefined);
283
+ }
284
+ ];
285
+ }
286
+
287
+ /**
288
+ * After paint effects consumer.
289
+ */
290
+ function flushAfterPaintEffects() {
291
+ let component;
292
+ // sort the queue by depth (outermost to innermost)
293
+ afterPaintEffects.sort((a, b) => a._vnode._depth - b._vnode._depth);
294
+ while (component = afterPaintEffects.pop()) {
295
+ if (!component._parentDom) continue;
296
+ try {
297
+ component.__hooks._pendingEffects.forEach(invokeCleanup);
298
+ component.__hooks._pendingEffects.forEach(invokeEffect);
299
+ component.__hooks._pendingEffects = [];
300
+ } catch (e) {
301
+ component.__hooks._pendingEffects = [];
302
+ options._catchError(e, component._vnode);
303
+ }
304
+ }
305
+ }
306
+
307
+ let HAS_RAF = typeof requestAnimationFrame == 'function';
308
+
309
+ /**
310
+ * Schedule a callback to be invoked after the browser has a chance to paint a new frame.
311
+ * Do this by combining requestAnimationFrame (rAF) + setTimeout to invoke a callback after
312
+ * the next browser frame.
313
+ *
314
+ * Also, schedule a timeout in parallel to the the rAF to ensure the callback is invoked
315
+ * even if RAF doesn't fire (for example if the browser tab is not visible)
316
+ *
317
+ * @param {() => void} callback
318
+ */
319
+ function afterNextFrame(callback) {
320
+ const done = () => {
321
+ clearTimeout(timeout);
322
+ if (HAS_RAF) cancelAnimationFrame(raf);
323
+ setTimeout(callback);
324
+ };
325
+ const timeout = setTimeout(done, RAF_TIMEOUT);
326
+
327
+ let raf;
328
+ if (HAS_RAF) {
329
+ raf = requestAnimationFrame(done);
330
+ }
331
+ }
332
+
333
+ // Note: if someone used options.debounceRendering = requestAnimationFrame,
334
+ // then effects will ALWAYS run on the NEXT frame instead of the current one, incurring a ~16ms delay.
335
+ // Perhaps this is not such a big deal.
336
+ /**
337
+ * Schedule afterPaintEffects flush after the browser paints
338
+ * @param {number} newQueueLength
339
+ */
340
+ function afterPaint(newQueueLength) {
341
+ if (newQueueLength === 1 || prevRaf !== options.requestAnimationFrame) {
342
+ prevRaf = options.requestAnimationFrame;
343
+ (prevRaf || afterNextFrame)(flushAfterPaintEffects);
344
+ }
345
+ }
346
+
347
+ /**
348
+ * @param {import('./internal').EffectHookState} hook
349
+ */
350
+ function invokeCleanup(hook) {
351
+ // A hook cleanup can introduce a call to render which creates a new root, this will call options.vnode
352
+ // and move the currentComponent away.
353
+ const comp = currentComponent;
354
+ let cleanup = hook._cleanup;
355
+ if (typeof cleanup == 'function') {
356
+ hook._cleanup = undefined;
357
+ cleanup();
358
+ }
359
+ currentComponent = comp;
360
+ }
361
+
362
+ /**
363
+ * Invoke a Hook's effect
364
+ * @param {import('./internal').EffectHookState} hook
365
+ */
366
+ function invokeEffect(hook) {
367
+ // A hook call can introduce a call to render which creates a new root, this will call options.vnode
368
+ // and move the currentComponent away.
369
+ const comp = currentComponent;
370
+ hook._cleanup = hook._value();
371
+ currentComponent = comp;
372
+ }
373
+
374
+ /**
375
+ * @param {any[]} oldArgs
376
+ * @param {any[]} newArgs
377
+ */
378
+ function argsChanged(oldArgs, newArgs) {
379
+ return (
380
+ !oldArgs ||
381
+ oldArgs.length !== newArgs.length ||
382
+ newArgs.some((arg, index) => arg !== oldArgs[index])
383
+ );
384
+ }
385
+
386
+ function invokeOrReturn(arg, f) {
387
+ return typeof f == 'function' ? f(arg) : f;
388
+ }