|
2 | 2 |
|
3 | 3 | Object.defineProperty(exports, '__esModule', { value: true }); |
4 | 4 |
|
| 5 | +let scheduleTimeout = setTimeout; |
| 6 | + |
| 7 | +let cancelTimeout = clearTimeout; |
| 8 | + |
| 9 | +let hasOwnProperty = Object.prototype.hasOwnProperty; |
| 10 | + |
| 11 | +let slice = Array.prototype.slice; |
| 12 | + |
| 13 | +let isString = (value) => { |
| 14 | + return typeof value === 'string'; |
| 15 | +}; |
| 16 | + |
| 17 | +let isNumber = (value) => { |
| 18 | + return typeof value === 'number'; |
| 19 | +}; |
| 20 | + |
| 21 | +let isFunction = (value) => { |
| 22 | + return typeof value === 'function'; |
| 23 | +}; |
| 24 | + |
| 25 | +let isArray = (value) => { |
| 26 | + return value instanceof Array; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Object.is equivalence. |
| 31 | + * https://github.com/facebook/fbjs/blob/main/packages/fbjs/src/core/shallowEqual.js#L22 |
| 32 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is |
| 33 | + * |
| 34 | + * @param {any} x |
| 35 | + * @param {any} y |
| 36 | + * @returns {boolean} |
| 37 | + */ |
| 38 | + let theSame = (x, y) => { |
| 39 | + // SameValue algorithm |
| 40 | + if (x === y) { |
| 41 | + // +0 != -0 |
| 42 | + return x !== 0 || 1 / x === 1 / y; |
| 43 | + } else { |
| 44 | + // NaN == NaN |
| 45 | + return x !== x && y !== y; |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * |
| 51 | + * @param {{}} A |
| 52 | + * @param {{}} B |
| 53 | + * @returns {boolean} |
| 54 | + */ |
| 55 | +let objectsShallowEqual = (A, B) => { |
| 56 | + if (A === B) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + let kA = Object.keys(A); |
| 60 | + let kB = Object.keys(B); |
| 61 | + if (kA.length !== kB.length) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + for (let k, i = kA.length - 1; i >= 0; --i) { |
| 65 | + k = kA[i]; |
| 66 | + if (!( |
| 67 | + hasOwnProperty.call(B, k) && |
| 68 | + theSame(A[k], B[k]) |
| 69 | + )) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + return true; |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * |
| 78 | + * @param {[]} A |
| 79 | + * @param {[]} B |
| 80 | + * @returns {boolean} |
| 81 | + */ |
| 82 | + let arraysShallowEqual = (A, B) => { |
| 83 | + if (A === B) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + if (A.length !== B.length) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + for (let i = A.length - 1; i >= 0; --i) { |
| 90 | + if (!theSame(A[i], B[i])) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + return true; |
| 95 | +}; |
| 96 | + |
5 | 97 | // Do not support namespace MathML as almost browsers do not support as well |
6 | 98 | const NAMESPACE_HTML = 0; |
7 | 99 | const NAMESPACE_SVG = 1; |
@@ -268,94 +360,6 @@ let walkNativeChildren = (callback, parent, stopBefore) => { |
268 | 360 | } |
269 | 361 | }; |
270 | 362 |
|
271 | | -let hasOwnProperty = Object.prototype.hasOwnProperty; |
272 | | - |
273 | | -let slice = Array.prototype.slice; |
274 | | - |
275 | | -let isString = (value) => { |
276 | | - return typeof value === 'string'; |
277 | | -}; |
278 | | - |
279 | | -let isNumber = (value) => { |
280 | | - return typeof value === 'number'; |
281 | | -}; |
282 | | - |
283 | | -let isFunction = (value) => { |
284 | | - return typeof value === 'function'; |
285 | | -}; |
286 | | - |
287 | | -let isArray = (value) => { |
288 | | - return value instanceof Array; |
289 | | -}; |
290 | | - |
291 | | -/** |
292 | | - * Object.is equivalence. |
293 | | - * https://github.com/facebook/fbjs/blob/main/packages/fbjs/src/core/shallowEqual.js#L22 |
294 | | - * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is |
295 | | - * |
296 | | - * @param {any} x |
297 | | - * @param {any} y |
298 | | - * @returns {boolean} |
299 | | - */ |
300 | | - let theSame = (x, y) => { |
301 | | - // SameValue algorithm |
302 | | - if (x === y) { |
303 | | - // +0 != -0 |
304 | | - return x !== 0 || 1 / x === 1 / y; |
305 | | - } else { |
306 | | - // NaN == NaN |
307 | | - return x !== x && y !== y; |
308 | | - } |
309 | | -}; |
310 | | - |
311 | | -/** |
312 | | - * |
313 | | - * @param {{}} A |
314 | | - * @param {{}} B |
315 | | - * @returns {boolean} |
316 | | - */ |
317 | | -let objectsShallowEqual = (A, B) => { |
318 | | - if (A === B) { |
319 | | - return true; |
320 | | - } |
321 | | - let kA = Object.keys(A); |
322 | | - let kB = Object.keys(B); |
323 | | - if (kA.length !== kB.length) { |
324 | | - return false; |
325 | | - } |
326 | | - for (let k, i = kA.length - 1; i >= 0; --i) { |
327 | | - k = kA[i]; |
328 | | - if (!( |
329 | | - hasOwnProperty.call(B, k) && |
330 | | - theSame(A[k], B[k]) |
331 | | - )) { |
332 | | - return false; |
333 | | - } |
334 | | - } |
335 | | - return true; |
336 | | -}; |
337 | | - |
338 | | -/** |
339 | | - * |
340 | | - * @param {[]} A |
341 | | - * @param {[]} B |
342 | | - * @returns {boolean} |
343 | | - */ |
344 | | - let arraysShallowEqual = (A, B) => { |
345 | | - if (A === B) { |
346 | | - return true; |
347 | | - } |
348 | | - if (A.length !== B.length) { |
349 | | - return false; |
350 | | - } |
351 | | - for (let i = A.length - 1; i >= 0; --i) { |
352 | | - if (!theSame(A[i], B[i])) { |
353 | | - return false; |
354 | | - } |
355 | | - } |
356 | | - return true; |
357 | | -}; |
358 | | - |
359 | 363 | /** |
360 | 364 | * |
361 | 365 | * @param {Text} node |
@@ -938,7 +942,7 @@ let _setState = function (value) { |
938 | 942 | this.value_ = newValue; |
939 | 943 |
|
940 | 944 | // Schedule a work to update the UI |
941 | | - this.context_.updateId_ = setTimeout(_flushUpdates, 0, this); |
| 945 | + this.context_.updateId_ = scheduleTimeout(_flushUpdates, 0, this); |
942 | 946 | } |
943 | 947 | }; |
944 | 948 |
|
@@ -969,7 +973,7 @@ let catchError = (error, vnode) => { |
969 | 973 | if (hook.tag_ === STATE_ERROR) { |
970 | 974 | // Throw the error asynchorously |
971 | 975 | // to avoid blocking effect callbacks |
972 | | - setTimeout(() => { |
| 976 | + scheduleTimeout(() => { |
973 | 977 | hook.setValue_((prevError) => { |
974 | 978 | if (prevError != null) { |
975 | 979 | return prevError; |
@@ -1584,7 +1588,7 @@ let renderTree = (current) => { |
1584 | 1588 | runEffectQueue(EFFECT_LAYOUT, effectQueue); |
1585 | 1589 |
|
1586 | 1590 | // Schedule to run useEffect callbacks |
1587 | | - setTimeout(runEffectQueue, 0, EFFECT_NORMAL, effectQueue); |
| 1591 | + scheduleTimeout(runEffectQueue, 0, EFFECT_NORMAL, effectQueue); |
1588 | 1592 | }; |
1589 | 1593 |
|
1590 | 1594 | // Effect flags |
@@ -1624,7 +1628,7 @@ let _performUnitOfWork = (current, root, effectQueue) => { |
1624 | 1628 | // Do this before reconciliation because the current node can |
1625 | 1629 | // be scheduled for another update while the reconciliation |
1626 | 1630 | if (current.updateId_ !== null) { |
1627 | | - clearTimeout(current.updateId_); |
| 1631 | + cancelTimeout(current.updateId_); |
1628 | 1632 | current.updateId_ = null; |
1629 | 1633 | } |
1630 | 1634 |
|
@@ -1692,7 +1696,7 @@ let _performUnitOfWork = (current, root, effectQueue) => { |
1692 | 1696 | // Important!!! |
1693 | 1697 | // Cancel the update schedule on the deleted nodes |
1694 | 1698 | if (deleted.updateId_ !== null) { |
1695 | | - clearTimeout(deleted.updateId_); |
| 1699 | + cancelTimeout(deleted.updateId_); |
1696 | 1700 | deleted.updateId_ = null; |
1697 | 1701 | } |
1698 | 1702 | // Never skip any node when handling deletions |
@@ -1823,11 +1827,13 @@ let useCallback = (callback, deps) => { |
1823 | 1827 |
|
1824 | 1828 | exports.Fragment = Fragment; |
1825 | 1829 | exports.Text = Text; |
| 1830 | +exports.cancelTimeout = cancelTimeout; |
1826 | 1831 | exports.createElement = createElement; |
1827 | 1832 | exports.createPortal = createPortal; |
1828 | 1833 | exports.createRef = createRef; |
1829 | 1834 | exports.jsx = createElement; |
1830 | 1835 | exports.render = render; |
| 1836 | +exports.scheduleTimeout = scheduleTimeout; |
1831 | 1837 | exports.useCallback = useCallback; |
1832 | 1838 | exports.useCatch = useCatch; |
1833 | 1839 | exports.useEffect = useEffect; |
|
0 commit comments