isomorfeus-preact 10.7.2 → 10.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/preact/version.rb +1 -1
- data/node_modules/.package-lock.json +3 -3
- data/node_modules/preact/compat/client.js +11 -5
- data/node_modules/preact/compat/client.mjs +7 -3
- data/node_modules/preact/compat/dist/compat.js +1 -1
- data/node_modules/preact/compat/dist/compat.js.map +1 -1
- data/node_modules/preact/compat/dist/compat.mjs +1 -1
- data/node_modules/preact/compat/dist/compat.module.js +1 -1
- data/node_modules/preact/compat/dist/compat.module.js.map +1 -1
- data/node_modules/preact/compat/dist/compat.umd.js +1 -1
- data/node_modules/preact/compat/dist/compat.umd.js.map +1 -1
- data/node_modules/preact/compat/package.json +47 -17
- data/node_modules/preact/compat/server.browser.js +4 -0
- data/node_modules/preact/compat/src/render.js +9 -0
- data/node_modules/preact/debug/package.json +24 -16
- data/node_modules/preact/devtools/dist/devtools.js +1 -1
- data/node_modules/preact/devtools/dist/devtools.js.map +1 -1
- data/node_modules/preact/devtools/dist/devtools.mjs +1 -1
- data/node_modules/preact/devtools/dist/devtools.module.js +1 -1
- data/node_modules/preact/devtools/dist/devtools.module.js.map +1 -1
- data/node_modules/preact/devtools/dist/devtools.umd.js +1 -1
- data/node_modules/preact/devtools/dist/devtools.umd.js.map +1 -1
- data/node_modules/preact/devtools/package.json +9 -0
- data/node_modules/preact/devtools/src/devtools.js +1 -1
- data/node_modules/preact/dist/preact.js +1 -1
- data/node_modules/preact/dist/preact.js.map +1 -1
- data/node_modules/preact/dist/preact.min.js +1 -1
- data/node_modules/preact/dist/preact.min.js.map +1 -1
- data/node_modules/preact/dist/preact.mjs +1 -1
- data/node_modules/preact/dist/preact.module.js +1 -1
- data/node_modules/preact/dist/preact.module.js.map +1 -1
- data/node_modules/preact/dist/preact.umd.js +1 -1
- data/node_modules/preact/dist/preact.umd.js.map +1 -1
- data/node_modules/preact/hooks/dist/hooks.js +1 -1
- data/node_modules/preact/hooks/dist/hooks.js.map +1 -1
- data/node_modules/preact/hooks/dist/hooks.mjs +1 -1
- data/node_modules/preact/hooks/dist/hooks.module.js +1 -1
- data/node_modules/preact/hooks/dist/hooks.module.js.map +1 -1
- data/node_modules/preact/hooks/dist/hooks.umd.js +1 -1
- data/node_modules/preact/hooks/dist/hooks.umd.js.map +1 -1
- data/node_modules/preact/hooks/package.json +33 -24
- data/node_modules/preact/hooks/src/index.js +43 -8
- data/node_modules/preact/hooks/src/internal.d.ts +3 -0
- data/node_modules/preact/jsx-runtime/package.json +9 -0
- data/node_modules/preact/package.json +3 -1
- data/node_modules/preact/src/diff/index.js +20 -6
- data/node_modules/preact/src/jsx.d.ts +1 -0
- data/node_modules/preact/test-utils/package.json +26 -17
- data/package.json +1 -1
- metadata +9 -8
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"hooks.module.js","sources":["../src/index.js"],"sourcesContent":["import { options } from 'preact';\n\n/** @type {number} */\nlet currentIndex;\n\n/** @type {import('./internal').Component} */\nlet currentComponent;\n\n/** @type {number} */\nlet currentHook = 0;\n\n/** @type {Array<import('./internal').Component>} */\nlet afterPaintEffects = [];\n\nlet oldBeforeDiff = options._diff;\nlet oldBeforeRender = options._render;\nlet oldAfterDiff = options.diffed;\nlet oldCommit = options._commit;\nlet oldBeforeUnmount = options.unmount;\n\nconst RAF_TIMEOUT = 100;\nlet prevRaf;\n\noptions._diff = vnode => {\n\tcurrentComponent = null;\n\tif (oldBeforeDiff) oldBeforeDiff(vnode);\n};\n\noptions._render = vnode => {\n\tif (oldBeforeRender) oldBeforeRender(vnode);\n\n\tcurrentComponent = vnode._component;\n\tcurrentIndex = 0;\n\n\tconst hooks = currentComponent.__hooks;\n\tif (hooks) {\n\t\thooks._pendingEffects.forEach(invokeCleanup);\n\t\thooks._pendingEffects.forEach(invokeEffect);\n\t\thooks._pendingEffects = [];\n\t}\n};\n\noptions.diffed = vnode => {\n\tif (oldAfterDiff) oldAfterDiff(vnode);\n\n\tconst c = vnode._component;\n\tif (c && c.__hooks && c.__hooks._pendingEffects.length) {\n\t\tafterPaint(afterPaintEffects.push(c));\n\t}\n\tcurrentComponent = null;\n};\n\noptions._commit = (vnode, commitQueue) => {\n\tcommitQueue.some(component => {\n\t\ttry {\n\t\t\tcomponent._renderCallbacks.forEach(invokeCleanup);\n\t\t\tcomponent._renderCallbacks = component._renderCallbacks.filter(cb =>\n\t\t\t\tcb._value ? invokeEffect(cb) : true\n\t\t\t);\n\t\t} catch (e) {\n\t\t\tcommitQueue.some(c => {\n\t\t\t\tif (c._renderCallbacks) c._renderCallbacks = [];\n\t\t\t});\n\t\t\tcommitQueue = [];\n\t\t\toptions._catchError(e, component._vnode);\n\t\t}\n\t});\n\n\tif (oldCommit) oldCommit(vnode, commitQueue);\n};\n\noptions.unmount = vnode => {\n\tif (oldBeforeUnmount) oldBeforeUnmount(vnode);\n\n\tconst c = vnode._component;\n\tif (c && c.__hooks) {\n\t\tlet hasErrored;\n\t\tc.__hooks._list.forEach(s => {\n\t\t\ttry {\n\t\t\t\tinvokeCleanup(s);\n\t\t\t} catch (e) {\n\t\t\t\thasErrored = e;\n\t\t\t}\n\t\t});\n\t\tif (hasErrored) options._catchError(hasErrored, c._vnode);\n\t}\n};\n\n/**\n * Get a hook's state from the currentComponent\n * @param {number} index The index of the hook to get\n * @param {number} type The index of the hook to get\n * @returns {any}\n */\nfunction getHookState(index, type) {\n\tif (options._hook) {\n\t\toptions._hook(currentComponent, index, currentHook || type);\n\t}\n\tcurrentHook = 0;\n\n\t// Largely inspired by:\n\t// * https://github.com/michael-klein/funcy.js/blob/f6be73468e6ec46b0ff5aa3cc4c9baf72a29025a/src/hooks/core_hooks.mjs\n\t// * https://github.com/michael-klein/funcy.js/blob/650beaa58c43c33a74820a3c98b3c7079cf2e333/src/renderer.mjs\n\t// Other implementations to look at:\n\t// * https://codesandbox.io/s/mnox05qp8\n\tconst hooks =\n\t\tcurrentComponent.__hooks ||\n\t\t(currentComponent.__hooks = {\n\t\t\t_list: [],\n\t\t\t_pendingEffects: []\n\t\t});\n\n\tif (index >= hooks._list.length) {\n\t\thooks._list.push({});\n\t}\n\treturn hooks._list[index];\n}\n\n/**\n * @param {import('./index').StateUpdater<any>} [initialState]\n */\nexport function useState(initialState) {\n\tcurrentHook = 1;\n\treturn useReducer(invokeOrReturn, initialState);\n}\n\n/**\n * @param {import('./index').Reducer<any, any>} reducer\n * @param {import('./index').StateUpdater<any>} initialState\n * @param {(initialState: any) => void} [init]\n * @returns {[ any, (state: any) => void ]}\n */\nexport function useReducer(reducer, initialState, init) {\n\t/** @type {import('./internal').ReducerHookState} */\n\tconst hookState = getHookState(currentIndex++, 2);\n\thookState._reducer = reducer;\n\tif (!hookState._component) {\n\t\thookState._value = [\n\t\t\t!init ? invokeOrReturn(undefined, initialState) : init(initialState),\n\n\t\t\taction => {\n\t\t\t\tconst nextValue = hookState._reducer(hookState._value[0], action);\n\t\t\t\tif (hookState._value[0] !== nextValue) {\n\t\t\t\t\thookState._value = [nextValue, hookState._value[1]];\n\t\t\t\t\thookState._component.setState({});\n\t\t\t\t}\n\t\t\t}\n\t\t];\n\n\t\thookState._component = currentComponent;\n\t}\n\n\treturn hookState._value;\n}\n\n/**\n * @param {import('./internal').Effect} callback\n * @param {any[]} args\n */\nexport function useEffect(callback, args) {\n\t/** @type {import('./internal').EffectHookState} */\n\tconst state = getHookState(currentIndex++, 3);\n\tif (!options._skipEffects && argsChanged(state._args, args)) {\n\t\tstate._value = callback;\n\t\tstate._args = args;\n\n\t\tcurrentComponent.__hooks._pendingEffects.push(state);\n\t}\n}\n\n/**\n * @param {import('./internal').Effect} callback\n * @param {any[]} args\n */\nexport function useLayoutEffect(callback, args) {\n\t/** @type {import('./internal').EffectHookState} */\n\tconst state = getHookState(currentIndex++, 4);\n\tif (!options._skipEffects && argsChanged(state._args, args)) {\n\t\tstate._value = callback;\n\t\tstate._args = args;\n\n\t\tcurrentComponent._renderCallbacks.push(state);\n\t}\n}\n\nexport function useRef(initialValue) {\n\tcurrentHook = 5;\n\treturn useMemo(() => ({ current: initialValue }), []);\n}\n\n/**\n * @param {object} ref\n * @param {() => object} createHandle\n * @param {any[]} args\n */\nexport function useImperativeHandle(ref, createHandle, args) {\n\tcurrentHook = 6;\n\tuseLayoutEffect(\n\t\t() => {\n\t\t\tif (typeof ref == 'function') {\n\t\t\t\tref(createHandle());\n\t\t\t\treturn () => ref(null);\n\t\t\t} else if (ref) {\n\t\t\t\tref.current = createHandle();\n\t\t\t\treturn () => ref.current = null;\n\t\t\t}\n\t\t},\n\t\targs == null ? args : args.concat(ref)\n\t);\n}\n\n/**\n * @param {() => any} factory\n * @param {any[]} args\n */\nexport function useMemo(factory, args) {\n\t/** @type {import('./internal').MemoHookState} */\n\tconst state = getHookState(currentIndex++, 7);\n\tif (argsChanged(state._args, args)) {\n\t\tstate._value = factory();\n\t\tstate._args = args;\n\t\tstate._factory = factory;\n\t}\n\n\treturn state._value;\n}\n\n/**\n * @param {() => void} callback\n * @param {any[]} args\n */\nexport function useCallback(callback, args) {\n\tcurrentHook = 8;\n\treturn useMemo(() => callback, args);\n}\n\n/**\n * @param {import('./internal').PreactContext} context\n */\nexport function useContext(context) {\n\tconst provider = currentComponent.context[context._id];\n\t// We could skip this call here, but than we'd not call\n\t// `options._hook`. We need to do that in order to make\n\t// the devtools aware of this hook.\n\t/** @type {import('./internal').ContextHookState} */\n\tconst state = getHookState(currentIndex++, 9);\n\t// The devtools needs access to the context object to\n\t// be able to pull of the default value when no provider\n\t// is present in the tree.\n\tstate._context = context;\n\tif (!provider) return context._defaultValue;\n\t// This is probably not safe to convert to \"!\"\n\tif (state._value == null) {\n\t\tstate._value = true;\n\t\tprovider.sub(currentComponent);\n\t}\n\treturn provider.props.value;\n}\n\n/**\n * Display a custom label for a custom hook for the devtools panel\n * @type {<T>(value: T, cb?: (value: T) => string | number) => void}\n */\nexport function useDebugValue(value, formatter) {\n\tif (options.useDebugValue) {\n\t\toptions.useDebugValue(formatter ? formatter(value) : value);\n\t}\n}\n\n/**\n * @param {(error: any) => void} cb\n */\nexport function useErrorBoundary(cb) {\n\t/** @type {import('./internal').ErrorBoundaryHookState} */\n\tconst state = getHookState(currentIndex++, 10);\n\tconst errState = useState();\n\tstate._value = cb;\n\tif (!currentComponent.componentDidCatch) {\n\t\tcurrentComponent.componentDidCatch = err => {\n\t\t\tif (state._value) state._value(err);\n\t\t\terrState[1](err);\n\t\t};\n\t}\n\treturn [\n\t\terrState[0],\n\t\t() => {\n\t\t\terrState[1](undefined);\n\t\t}\n\t];\n}\n\n/**\n * After paint effects consumer.\n */\nfunction flushAfterPaintEffects() {\n\tlet component;\n\twhile ((component = afterPaintEffects.shift())) {\n\t\tif (!component._parentDom) continue;\n\t\ttry {\n\t\t\tcomponent.__hooks._pendingEffects.forEach(invokeCleanup);\n\t\t\tcomponent.__hooks._pendingEffects.forEach(invokeEffect);\n\t\t\tcomponent.__hooks._pendingEffects = [];\n\t\t} catch (e) {\n\t\t\tcomponent.__hooks._pendingEffects = [];\n\t\t\toptions._catchError(e, component._vnode);\n\t\t}\n\t}\n}\n\nlet HAS_RAF = typeof requestAnimationFrame == 'function';\n\n/**\n * Schedule a callback to be invoked after the browser has a chance to paint a new frame.\n * Do this by combining requestAnimationFrame (rAF) + setTimeout to invoke a callback after\n * the next browser frame.\n *\n * Also, schedule a timeout in parallel to the the rAF to ensure the callback is invoked\n * even if RAF doesn't fire (for example if the browser tab is not visible)\n *\n * @param {() => void} callback\n */\nfunction afterNextFrame(callback) {\n\tconst done = () => {\n\t\tclearTimeout(timeout);\n\t\tif (HAS_RAF) cancelAnimationFrame(raf);\n\t\tsetTimeout(callback);\n\t};\n\tconst timeout = setTimeout(done, RAF_TIMEOUT);\n\n\tlet raf;\n\tif (HAS_RAF) {\n\t\traf = requestAnimationFrame(done);\n\t}\n}\n\n// Note: if someone used options.debounceRendering = requestAnimationFrame,\n// then effects will ALWAYS run on the NEXT frame instead of the current one, incurring a ~16ms delay.\n// Perhaps this is not such a big deal.\n/**\n * Schedule afterPaintEffects flush after the browser paints\n * @param {number} newQueueLength\n */\nfunction afterPaint(newQueueLength) {\n\tif (newQueueLength === 1 || prevRaf !== options.requestAnimationFrame) {\n\t\tprevRaf = options.requestAnimationFrame;\n\t\t(prevRaf || afterNextFrame)(flushAfterPaintEffects);\n\t}\n}\n\n/**\n * @param {import('./internal').EffectHookState} hook\n */\nfunction invokeCleanup(hook) {\n\t// A hook cleanup can introduce a call to render which creates a new root, this will call options.vnode\n\t// and move the currentComponent away.\n\tconst comp = currentComponent;\n\tlet cleanup = hook._cleanup;\n\tif (typeof cleanup == 'function') {\n\t\thook._cleanup = undefined;\n\t\tcleanup();\n\t}\n\tcurrentComponent = comp;\n}\n\n/**\n * Invoke a Hook's effect\n * @param {import('./internal').EffectHookState} hook\n */\nfunction invokeEffect(hook) {\n\t// A hook call can introduce a call to render which creates a new root, this will call options.vnode\n\t// and move the currentComponent away.\n\tconst comp = currentComponent;\n\thook._cleanup = hook._value();\n\tcurrentComponent = comp;\n}\n\n/**\n * @param {any[]} oldArgs\n * @param {any[]} newArgs\n */\nfunction argsChanged(oldArgs, newArgs) {\n\treturn (\n\t\t!oldArgs ||\n\t\toldArgs.length !== newArgs.length ||\n\t\tnewArgs.some((arg, index) => arg !== oldArgs[index])\n\t);\n}\n\nfunction invokeOrReturn(arg, f) {\n\treturn typeof f == 'function' ? f(arg) : f;\n}\n"],"names":["currentIndex","currentComponent","prevRaf","currentHook","afterPaintEffects","oldBeforeDiff","options","oldBeforeRender","oldAfterDiff","diffed","oldCommit","oldBeforeUnmount","unmount","getHookState","index","type","hooks","length","push","useState","initialState","useReducer","invokeOrReturn","reducer","init","hookState","_reducer","undefined","action","nextValue","setState","useEffect","callback","args","state","argsChanged","useLayoutEffect","useRef","initialValue","useMemo","current","useImperativeHandle","ref","createHandle","concat","factory","useCallback","useContext","context","provider","sub","props","value","useDebugValue","formatter","useErrorBoundary","cb","errState","componentDidCatch","err","flushAfterPaintEffects","component","shift","forEach","invokeCleanup","invokeEffect","e","vnode","c","requestAnimationFrame","raf","done","clearTimeout","timeout","HAS_RAF","cancelAnimationFrame","setTimeout","commitQueue","some","filter","hasErrored","s","hook","comp","cleanup","oldArgs","newArgs","arg","f"],"mappings":"iCAGA,IAAIA,EAGAC,EAeAC,EAZAC,EAAc,EAGdC,EAAoB,GAEpBC,EAAgBC,MAChBC,EAAkBD,MAClBE,EAAeF,EAAQG,OACvBC,EAAYJ,MACZK,EAAmBL,EAAQM,QA4E/B,SAASC,EAAaC,EAAOC,GACxBT,OACHA,MAAcL,EAAkBa,EAAOX,GAAeY,GAEvDZ,EAAc,MAORa,EACLf,QACCA,MAA2B,IACpB,OACU,YAGfa,GAASE,KAAYC,QACxBD,KAAYE,KAAK,IAEXF,KAAYF,GAMb,SAASK,EAASC,UACxBjB,EAAc,EACPkB,EAAWC,EAAgBF,GASnC,SAAgBC,EAAWE,EAASH,EAAcI,OAE3CC,EAAYZ,EAAab,IAAgB,UAC/CyB,EAAUC,EAAWH,EAChBE,QACJA,KAAmB,CACjBD,EAAiDA,EAAKJ,GAA/CE,OAAeK,EAAWP,GAElC,SAAAQ,OACOC,EAAYJ,EAAUC,EAASD,KAAiB,GAAIG,GACtDH,KAAiB,KAAOI,IAC3BJ,KAAmB,CAACI,EAAWJ,KAAiB,IAChDA,MAAqBK,SAAS,OAKjCL,MAAuBxB,GAGjBwB,KAOD,SAASM,EAAUC,EAAUC,OAE7BC,EAAQrB,EAAab,IAAgB,IACtCM,OAAwB6B,EAAYD,MAAaD,KACrDC,KAAeF,EACfE,MAAcD,EAEdhC,UAAyCiB,KAAKgB,IAQzC,SAASE,EAAgBJ,EAAUC,OAEnCC,EAAQrB,EAAab,IAAgB,IACtCM,OAAwB6B,EAAYD,MAAaD,KACrDC,KAAeF,EACfE,MAAcD,EAEdhC,MAAkCiB,KAAKgB,IAIlC,SAASG,EAAOC,UACtBnC,EAAc,EACPoC,EAAQ,iBAAO,CAAEC,QAASF,IAAiB,IAQnD,SAAgBG,EAAoBC,EAAKC,E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ASQ,EAAeiE,EAAKC,SACT,mBAALA,EAAkBA,EAAED,GAAOC"}
|
1
|
+
{"version":3,"file":"hooks.module.js","sources":["../src/index.js"],"sourcesContent":["import { options } from 'preact';\n\n/** @type {number} */\nlet currentIndex;\n\n/** @type {import('./internal').Component} */\nlet currentComponent;\n\n/** @type {import('./internal').Component} */\nlet previousComponent;\n\n/** @type {number} */\nlet currentHook = 0;\n\n/** @type {Array<import('./internal').Component>} */\nlet afterPaintEffects = [];\n\nlet oldBeforeDiff = options._diff;\nlet oldBeforeRender = options._render;\nlet oldAfterDiff = options.diffed;\nlet oldCommit = options._commit;\nlet oldBeforeUnmount = options.unmount;\n\nconst RAF_TIMEOUT = 100;\nlet prevRaf;\n\noptions._diff = vnode => {\n\tcurrentComponent = null;\n\tif (oldBeforeDiff) oldBeforeDiff(vnode);\n};\n\noptions._render = vnode => {\n\tif (oldBeforeRender) oldBeforeRender(vnode);\n\n\tcurrentComponent = vnode._component;\n\tcurrentIndex = 0;\n\n\tconst hooks = currentComponent.__hooks;\n\tif (hooks) {\n\t\tif (previousComponent === currentComponent) {\n\t\t\thooks._pendingEffects = [];\n\t\t\tcurrentComponent._renderCallbacks = [];\n\t\t\thooks._list.forEach(hookItem => {\n\t\t\t\thookItem._pendingValue = hookItem._pendingArgs = undefined;\n\t\t\t});\n\t\t} else {\n\t\t\thooks._list.forEach(hookItem => {\n\t\t\t\tif (hookItem._pendingArgs) {\n\t\t\t\t\thookItem._args = hookItem._pendingArgs;\n\t\t\t\t}\n\t\t\t\tif (hookItem._pendingValue) {\n\t\t\t\t\thookItem._value = hookItem._pendingValue;\n\t\t\t\t}\n\t\t\t\thookItem._pendingValue = hookItem._pendingArgs = undefined;\n\t\t\t});\n\t\t\thooks._pendingEffects.forEach(invokeCleanup);\n\t\t\thooks._pendingEffects.forEach(invokeEffect);\n\t\t\thooks._pendingEffects = [];\n\t\t}\n\t}\n\tpreviousComponent = currentComponent;\n};\n\noptions.diffed = vnode => {\n\tif (oldAfterDiff) oldAfterDiff(vnode);\n\n\tconst c = vnode._component;\n\tif (c && c.__hooks && c.__hooks._pendingEffects.length) {\n\t\tafterPaint(afterPaintEffects.push(c));\n\t}\n\tcurrentComponent = null;\n\tpreviousComponent = null;\n};\n\noptions._commit = (vnode, commitQueue) => {\n\tcommitQueue.some(component => {\n\t\ttry {\n\t\t\tif (component.__hooks) {\n\t\t\t\tcomponent.__hooks._list.forEach(hookItem => {\n\t\t\t\t\tif (hookItem._pendingArgs) {\n\t\t\t\t\t\thookItem._args = hookItem._pendingArgs;\n\t\t\t\t\t}\n\t\t\t\t\tif (hookItem._pendingValue) {\n\t\t\t\t\t\thookItem._value = hookItem._pendingValue;\n\t\t\t\t\t}\n\t\t\t\t\thookItem._pendingValue = hookItem._pendingArgs = undefined;\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tcomponent._renderCallbacks.forEach(invokeCleanup);\n\t\t\tcomponent._renderCallbacks = component._renderCallbacks.filter(cb =>\n\t\t\t\tcb._value ? invokeEffect(cb) : true\n\t\t\t);\n\t\t} catch (e) {\n\t\t\tcommitQueue.some(c => {\n\t\t\t\tif (c._renderCallbacks) c._renderCallbacks = [];\n\t\t\t});\n\t\t\tcommitQueue = [];\n\t\t\toptions._catchError(e, component._vnode);\n\t\t}\n\t});\n\n\tif (oldCommit) oldCommit(vnode, commitQueue);\n};\n\noptions.unmount = vnode => {\n\tif (oldBeforeUnmount) oldBeforeUnmount(vnode);\n\n\tconst c = vnode._component;\n\tif (c && c.__hooks) {\n\t\tlet hasErrored;\n\t\tc.__hooks._list.forEach(s => {\n\t\t\ttry {\n\t\t\t\tinvokeCleanup(s);\n\t\t\t} catch (e) {\n\t\t\t\thasErrored = e;\n\t\t\t}\n\t\t});\n\t\tif (hasErrored) options._catchError(hasErrored, c._vnode);\n\t}\n};\n\n/**\n * Get a hook's state from the currentComponent\n * @param {number} index The index of the hook to get\n * @param {number} type The index of the hook to get\n * @returns {any}\n */\nfunction getHookState(index, type) {\n\tif (options._hook) {\n\t\toptions._hook(currentComponent, index, currentHook || type);\n\t}\n\tcurrentHook = 0;\n\n\t// Largely inspired by:\n\t// * https://github.com/michael-klein/funcy.js/blob/f6be73468e6ec46b0ff5aa3cc4c9baf72a29025a/src/hooks/core_hooks.mjs\n\t// * https://github.com/michael-klein/funcy.js/blob/650beaa58c43c33a74820a3c98b3c7079cf2e333/src/renderer.mjs\n\t// Other implementations to look at:\n\t// * https://codesandbox.io/s/mnox05qp8\n\tconst hooks =\n\t\tcurrentComponent.__hooks ||\n\t\t(currentComponent.__hooks = {\n\t\t\t_list: [],\n\t\t\t_pendingEffects: []\n\t\t});\n\n\tif (index >= hooks._list.length) {\n\t\thooks._list.push({});\n\t}\n\treturn hooks._list[index];\n}\n\n/**\n * @param {import('./index').StateUpdater<any>} [initialState]\n */\nexport function useState(initialState) {\n\tcurrentHook = 1;\n\treturn useReducer(invokeOrReturn, initialState);\n}\n\n/**\n * @param {import('./index').Reducer<any, any>} reducer\n * @param {import('./index').StateUpdater<any>} initialState\n * @param {(initialState: any) => void} [init]\n * @returns {[ any, (state: any) => void ]}\n */\nexport function useReducer(reducer, initialState, init) {\n\t/** @type {import('./internal').ReducerHookState} */\n\tconst hookState = getHookState(currentIndex++, 2);\n\thookState._reducer = reducer;\n\tif (!hookState._component) {\n\t\thookState._value = [\n\t\t\t!init ? invokeOrReturn(undefined, initialState) : init(initialState),\n\n\t\t\taction => {\n\t\t\t\tconst nextValue = hookState._reducer(hookState._value[0], action);\n\t\t\t\tif (hookState._value[0] !== nextValue) {\n\t\t\t\t\thookState._value = [nextValue, hookState._value[1]];\n\t\t\t\t\thookState._component.setState({});\n\t\t\t\t}\n\t\t\t}\n\t\t];\n\n\t\thookState._component = currentComponent;\n\t}\n\n\treturn hookState._value;\n}\n\n/**\n * @param {import('./internal').Effect} callback\n * @param {any[]} args\n */\nexport function useEffect(callback, args) {\n\t/** @type {import('./internal').EffectHookState} */\n\tconst state = getHookState(currentIndex++, 3);\n\tif (!options._skipEffects && argsChanged(state._args, args)) {\n\t\tstate._value = callback;\n\t\tstate._pendingArgs = args;\n\n\t\tcurrentComponent.__hooks._pendingEffects.push(state);\n\t}\n}\n\n/**\n * @param {import('./internal').Effect} callback\n * @param {any[]} args\n */\nexport function useLayoutEffect(callback, args) {\n\t/** @type {import('./internal').EffectHookState} */\n\tconst state = getHookState(currentIndex++, 4);\n\tif (!options._skipEffects && argsChanged(state._args, args)) {\n\t\tstate._value = callback;\n\t\tstate._pendingArgs = args;\n\n\t\tcurrentComponent._renderCallbacks.push(state);\n\t}\n}\n\nexport function useRef(initialValue) {\n\tcurrentHook = 5;\n\treturn useMemo(() => ({ current: initialValue }), []);\n}\n\n/**\n * @param {object} ref\n * @param {() => object} createHandle\n * @param {any[]} args\n */\nexport function useImperativeHandle(ref, createHandle, args) {\n\tcurrentHook = 6;\n\tuseLayoutEffect(\n\t\t() => {\n\t\t\tif (typeof ref == 'function') {\n\t\t\t\tref(createHandle());\n\t\t\t\treturn () => ref(null);\n\t\t\t} else if (ref) {\n\t\t\t\tref.current = createHandle();\n\t\t\t\treturn () => (ref.current = null);\n\t\t\t}\n\t\t},\n\t\targs == null ? args : args.concat(ref)\n\t);\n}\n\n/**\n * @param {() => any} factory\n * @param {any[]} args\n */\nexport function useMemo(factory, args) {\n\t/** @type {import('./internal').MemoHookState} */\n\tconst state = getHookState(currentIndex++, 7);\n\tif (argsChanged(state._args, args)) {\n\t\tstate._pendingValue = factory();\n\t\tstate._pendingArgs = args;\n\t\tstate._factory = factory;\n\t\treturn state._pendingValue;\n\t}\n\n\treturn state._value;\n}\n\n/**\n * @param {() => void} callback\n * @param {any[]} args\n */\nexport function useCallback(callback, args) {\n\tcurrentHook = 8;\n\treturn useMemo(() => callback, args);\n}\n\n/**\n * @param {import('./internal').PreactContext} context\n */\nexport function useContext(context) {\n\tconst provider = currentComponent.context[context._id];\n\t// We could skip this call here, but than we'd not call\n\t// `options._hook`. We need to do that in order to make\n\t// the devtools aware of this hook.\n\t/** @type {import('./internal').ContextHookState} */\n\tconst state = getHookState(currentIndex++, 9);\n\t// The devtools needs access to the context object to\n\t// be able to pull of the default value when no provider\n\t// is present in the tree.\n\tstate._context = context;\n\tif (!provider) return context._defaultValue;\n\t// This is probably not safe to convert to \"!\"\n\tif (state._value == null) {\n\t\tstate._value = true;\n\t\tprovider.sub(currentComponent);\n\t}\n\treturn provider.props.value;\n}\n\n/**\n * Display a custom label for a custom hook for the devtools panel\n * @type {<T>(value: T, cb?: (value: T) => string | number) => void}\n */\nexport function useDebugValue(value, formatter) {\n\tif (options.useDebugValue) {\n\t\toptions.useDebugValue(formatter ? formatter(value) : value);\n\t}\n}\n\n/**\n * @param {(error: any) => void} cb\n */\nexport function useErrorBoundary(cb) {\n\t/** @type {import('./internal').ErrorBoundaryHookState} */\n\tconst state = getHookState(currentIndex++, 10);\n\tconst errState = useState();\n\tstate._value = cb;\n\tif (!currentComponent.componentDidCatch) {\n\t\tcurrentComponent.componentDidCatch = err => {\n\t\t\tif (state._value) state._value(err);\n\t\t\terrState[1](err);\n\t\t};\n\t}\n\treturn [\n\t\terrState[0],\n\t\t() => {\n\t\t\terrState[1](undefined);\n\t\t}\n\t];\n}\n\n/**\n * After paint effects consumer.\n */\nfunction flushAfterPaintEffects() {\n\tlet component;\n\twhile ((component = afterPaintEffects.shift())) {\n\t\tif (!component._parentDom) continue;\n\t\ttry {\n\t\t\tcomponent.__hooks._pendingEffects.forEach(invokeCleanup);\n\t\t\tcomponent.__hooks._pendingEffects.forEach(invokeEffect);\n\t\t\tcomponent.__hooks._pendingEffects = [];\n\t\t} catch (e) {\n\t\t\tcomponent.__hooks._pendingEffects = [];\n\t\t\toptions._catchError(e, component._vnode);\n\t\t}\n\t}\n}\n\nlet HAS_RAF = typeof requestAnimationFrame == 'function';\n\n/**\n * Schedule a callback to be invoked after the browser has a chance to paint a new frame.\n * Do this by combining requestAnimationFrame (rAF) + setTimeout to invoke a callback after\n * the next browser frame.\n *\n * Also, schedule a timeout in parallel to the the rAF to ensure the callback is invoked\n * even if RAF doesn't fire (for example if the browser tab is not visible)\n *\n * @param {() => void} callback\n */\nfunction afterNextFrame(callback) {\n\tconst done = () => {\n\t\tclearTimeout(timeout);\n\t\tif (HAS_RAF) cancelAnimationFrame(raf);\n\t\tsetTimeout(callback);\n\t};\n\tconst timeout = setTimeout(done, RAF_TIMEOUT);\n\n\tlet raf;\n\tif (HAS_RAF) {\n\t\traf = requestAnimationFrame(done);\n\t}\n}\n\n// Note: if someone used options.debounceRendering = requestAnimationFrame,\n// then effects will ALWAYS run on the NEXT frame instead of the current one, incurring a ~16ms delay.\n// Perhaps this is not such a big deal.\n/**\n * Schedule afterPaintEffects flush after the browser paints\n * @param {number} newQueueLength\n */\nfunction afterPaint(newQueueLength) {\n\tif (newQueueLength === 1 || prevRaf !== options.requestAnimationFrame) {\n\t\tprevRaf = options.requestAnimationFrame;\n\t\t(prevRaf || afterNextFrame)(flushAfterPaintEffects);\n\t}\n}\n\n/**\n * @param {import('./internal').EffectHookState} hook\n */\nfunction invokeCleanup(hook) {\n\t// A hook cleanup can introduce a call to render which creates a new root, this will call options.vnode\n\t// and move the currentComponent away.\n\tconst comp = currentComponent;\n\tlet cleanup = hook._cleanup;\n\tif (typeof cleanup == 'function') {\n\t\thook._cleanup = undefined;\n\t\tcleanup();\n\t}\n\tcurrentComponent = comp;\n}\n\n/**\n * Invoke a Hook's effect\n * @param {import('./internal').EffectHookState} hook\n */\nfunction invokeEffect(hook) {\n\t// A hook call can introduce a call to render which creates a new root, this will call options.vnode\n\t// and move the currentComponent away.\n\tconst comp = currentComponent;\n\thook._cleanup = hook._value();\n\tcurrentComponent = comp;\n}\n\n/**\n * @param {any[]} oldArgs\n * @param {any[]} newArgs\n */\nfunction argsChanged(oldArgs, newArgs) {\n\treturn (\n\t\t!oldArgs ||\n\t\toldArgs.length !== newArgs.length ||\n\t\tnewArgs.some((arg, index) => arg !== oldArgs[index])\n\t);\n}\n\nfunction invokeOrReturn(arg, f) {\n\treturn typeof f == 'function' ? f(arg) : f;\n}\n"],"names":["currentIndex","currentComponent","previousComponent","prevRaf","currentHook","afterPaintEffects","oldBeforeDiff","options","oldBeforeRender","oldAfterDiff","diffed","oldCommit","oldBeforeUnmount","unmount","getHookState","index","type","hooks","length","push","useState","initialState","useReducer","invokeOrReturn","reducer","init","hookState","_reducer","undefined","action","nextValue","setState","useEffect","callback","args","state","argsChanged","_pendingArgs","useLayoutEffect","useRef","initialValue","useMemo","current","useImperativeHandle","ref","createHandle","concat","factory","_pendingValue","useCallback","useContext","context","provider","sub","props","value","useDebugValue","formatter","useErrorBoundary","cb","errState","componentDidCatch","err","flushAfterPaintEffects","component","shift","forEach","invokeCleanup","invokeEffect","e","vnode","hookItem","c","requestAnimationFrame","raf","done","clearTimeout","timeout","HAS_RAF","cancelAnimationFrame","setTimeout","commitQueue","some","filter","hasErrored","s","hook","comp","cleanup","oldArgs","newArgs","arg","f"],"mappings":"iCAGA,IAAIA,EAGAC,EAGAC,EAeAC,EAZAC,EAAc,EAGdC,EAAoB,GAEpBC,EAAgBC,MAChBC,EAAkBD,MAClBE,EAAeF,EAAQG,OACvBC,EAAYJ,MACZK,EAAmBL,EAAQM,QA2G/B,SAASC,EAAaC,EAAOC,GACxBT,OACHA,MAAcN,EAAkBc,EAAOX,GAAeY,GAEvDZ,EAAc,MAORa,EACLhB,QACCA,MAA2B,IACpB,OACU,YAGfc,GAASE,KAAYC,QACxBD,KAAYE,KAAK,IAEXF,KAAYF,GAMb,SAASK,EAASC,UACxBjB,EAAc,EACPkB,EAAWC,EAAgBF,GASnC,SAAgBC,EAAWE,EAASH,EAAcI,OAE3CC,EAAYZ,EAAad,IAAgB,UAC/C0B,EAAUC,EAAWH,EAChBE,QACJA,KAAmB,CACjBD,EAAiDA,EAAKJ,GAA/CE,OAAeK,EAAWP,GAElC,SAAAQ,OACOC,EAAYJ,EAAUC,EAASD,KAAiB,GAAIG,GACtDH,KAAiB,KAAOI,IAC3BJ,KAAmB,CAACI,EAAW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EAAOW,GACzBA,EAAYC,KAAK,SAAAlB,OAEXA,OACHA,SAAwBE,QAAQ,SAAAK,GAC3BA,EAASlC,IACZkC,MAAiBA,EAASlC,GAEvBkC,EAASvB,IACZuB,KAAkBA,EAASvB,GAE5BuB,EAASvB,EAAgBuB,EAASlC,OAAeT,IAInDoC,MAA2BE,QAAQC,GACnCH,MAA6BA,MAA2BmB,OAAO,SAAAxB,UAC9DA,MAAYS,EAAaT,KAEzB,MAAOU,GACRY,EAAYC,KAAK,SAAAV,GACZA,QAAoBA,MAAqB,MAE9CS,EAAc,GACd1E,MAAoB8D,EAAGL,UAIrBrD,GAAWA,EAAU2D,EAAOW,IAGjC1E,EAAQM,QAAU,SAAAyD,GACb1D,GAAkBA,EAAiB0D,OAIlCc,EAFCZ,EAAIF,MACNE,GAAKA,QAERA,SAAgBN,QAAQ,SAAAmB,OAEtBlB,EAAckB,GACb,MAAOhB,GACRe,EAAaf,KAGXe,GAAY7E,MAAoB6E,EAAYZ,SAkOlD,IAAIM,EAA0C,mBAAzBL,sBA2CrB,SAASN,EAAcmB,OAGhBC,EAAOtF,EACTuF,EAAUF,MACQ,mBAAXE,IACVF,WAAgB1D,EAChB4D,KAEDvF,EAAmBsF,EAOpB,SAASnB,EAAakB,OAGfC,EAAOtF,EACbqF,MAAgBA,OAChBrF,EAAmBsF,EAOpB,SAASnD,EAAYqD,EAASC,UAE3BD,GACDA,EAAQvE,SAAWwE,EAAQxE,QAC3BwE,EAAQR,KAAK,SAACS,EAAK5E,UAAU4E,IAAQF,EAAQ1E,KAI/C,SAASQ,EAAeoE,EAAKC,SACT,mBAALA,EAAkBA,EAAED,GAAOC"}
|
@@ -1,2 +1,2 @@
|
|
1
|
-
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("preact")):"function"==typeof define&&define.amd?define(["exports","preact"],t):t(n.preactHooks={},n.preact)}(this,function(n,t){var u,
|
1
|
+
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("preact")):"function"==typeof define&&define.amd?define(["exports","preact"],t):t(n.preactHooks={},n.preact)}(this,function(n,t){var u,o,i,r,c=0,f=[],e=t.options.__b,a=t.options.__r,v=t.options.diffed,l=t.options.__c,d=t.options.unmount;function p(n,u){t.options.__h&&t.options.__h(o,n,c||u),c=0;var i=o.__H||(o.__H={__:[],__h:[]});return n>=i.__.length&&i.__.push({}),i.__[n]}function m(n){return c=1,y(T,n)}function y(n,t,i){var r=p(u++,2);return r.t=n,r.__c||(r.__=[i?i(t):T(void 0,t),function(n){var t=r.t(r.__[0],n);r.__[0]!==t&&(r.__=[t,r.__[1]],r.__c.setState({}))}],r.__c=o),r.__}function s(n,i){var r=p(u++,4);!t.options.__s&&F(r.__H,i)&&(r.__=n,r.u=i,o.__h.push(r))}function h(n,t){var o=p(u++,7);return F(o.__H,t)?(o.o=n(),o.u=t,o.__h=n,o.o):o.__}function _(){for(var n;n=f.shift();)if(n.__P)try{n.__H.__h.forEach(x),n.__H.__h.forEach(A),n.__H.__h=[]}catch(u){n.__H.__h=[],t.options.__e(u,n.__v)}}t.options.__b=function(n){o=null,e&&e(n)},t.options.__r=function(n){a&&a(n),u=0;var t=(o=n.__c).__H;t&&(i===o?(t.__h=[],o.__h=[],t.__.forEach(function(n){n.o=n.u=void 0})):(t.__.forEach(function(n){n.u&&(n.__H=n.u),n.o&&(n.__=n.o),n.o=n.u=void 0}),t.__h.forEach(x),t.__h.forEach(A),t.__h=[])),i=o},t.options.diffed=function(n){v&&v(n);var u=n.__c;u&&u.__H&&u.__H.__h.length&&(1!==f.push(u)&&r===t.options.requestAnimationFrame||((r=t.options.requestAnimationFrame)||function(n){var t,u=function(){clearTimeout(o),q&&cancelAnimationFrame(t),setTimeout(n)},o=setTimeout(u,100);q&&(t=requestAnimationFrame(u))})(_)),o=null,i=null},t.options.__c=function(n,u){u.some(function(n){try{n.__H&&n.__H.__.forEach(function(n){n.u&&(n.__H=n.u),n.o&&(n.__=n.o),n.o=n.u=void 0}),n.__h.forEach(x),n.__h=n.__h.filter(function(n){return!n.__||A(n)})}catch(o){u.some(function(n){n.__h&&(n.__h=[])}),u=[],t.options.__e(o,n.__v)}}),l&&l(n,u)},t.options.unmount=function(n){d&&d(n);var u,o=n.__c;o&&o.__H&&(o.__H.__.forEach(function(n){try{x(n)}catch(n){u=n}}),u&&t.options.__e(u,o.__v))};var q="function"==typeof requestAnimationFrame;function x(n){var t=o,u=n.__c;"function"==typeof u&&(n.__c=void 0,u()),o=t}function A(n){var t=o;n.__c=n.__(),o=t}function F(n,t){return!n||n.length!==t.length||t.some(function(t,u){return t!==n[u]})}function T(n,t){return"function"==typeof t?t(n):t}n.useState=m,n.useReducer=y,n.useEffect=function(n,i){var r=p(u++,3);!t.options.__s&&F(r.__H,i)&&(r.__=n,r.u=i,o.__H.__h.push(r))},n.useLayoutEffect=s,n.useRef=function(n){return c=5,h(function(){return{current:n}},[])},n.useImperativeHandle=function(n,t,u){c=6,s(function(){return"function"==typeof n?(n(t()),function(){return n(null)}):n?(n.current=t(),function(){return n.current=null}):void 0},null==u?u:u.concat(n))},n.useMemo=h,n.useCallback=function(n,t){return c=8,h(function(){return n},t)},n.useContext=function(n){var t=o.context[n.__c],i=p(u++,9);return i.c=n,t?(null==i.__&&(i.__=!0,t.sub(o)),t.props.value):n.__},n.useDebugValue=function(n,u){t.options.useDebugValue&&t.options.useDebugValue(u?u(n):n)},n.useErrorBoundary=function(n){var t=p(u++,10),i=m();return t.__=n,o.componentDidCatch||(o.componentDidCatch=function(n){t.__&&t.__(n),i[1](n)}),[i[0],function(){i[1](void 0)}]}});
|
2
2
|
//# sourceMappingURL=hooks.umd.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"hooks.umd.js","sources":["../src/index.js"],"sourcesContent":["import { options } from 'preact';\n\n/** @type {number} */\nlet currentIndex;\n\n/** @type {import('./internal').Component} */\nlet currentComponent;\n\n/** @type {number} */\nlet currentHook = 0;\n\n/** @type {Array<import('./internal').Component>} */\nlet afterPaintEffects = [];\n\nlet oldBeforeDiff = options._diff;\nlet oldBeforeRender = options._render;\nlet oldAfterDiff = options.diffed;\nlet oldCommit = options._commit;\nlet oldBeforeUnmount = options.unmount;\n\nconst RAF_TIMEOUT = 100;\nlet prevRaf;\n\noptions._diff = vnode => {\n\tcurrentComponent = null;\n\tif (oldBeforeDiff) oldBeforeDiff(vnode);\n};\n\noptions._render = vnode => {\n\tif (oldBeforeRender) oldBeforeRender(vnode);\n\n\tcurrentComponent = vnode._component;\n\tcurrentIndex = 0;\n\n\tconst hooks = currentComponent.__hooks;\n\tif (hooks) {\n\t\thooks._pendingEffects.forEach(invokeCleanup);\n\t\thooks._pendingEffects.forEach(invokeEffect);\n\t\thooks._pendingEffects = [];\n\t}\n};\n\noptions.diffed = vnode => {\n\tif (oldAfterDiff) oldAfterDiff(vnode);\n\n\tconst c = vnode._component;\n\tif (c && c.__hooks && c.__hooks._pendingEffects.length) {\n\t\tafterPaint(afterPaintEffects.push(c));\n\t}\n\tcurrentComponent = null;\n};\n\noptions._commit = (vnode, commitQueue) => {\n\tcommitQueue.some(component => {\n\t\ttry {\n\t\t\tcomponent._renderCallbacks.forEach(invokeCleanup);\n\t\t\tcomponent._renderCallbacks = component._renderCallbacks.filter(cb =>\n\t\t\t\tcb._value ? invokeEffect(cb) : true\n\t\t\t);\n\t\t} catch (e) {\n\t\t\tcommitQueue.some(c => {\n\t\t\t\tif (c._renderCallbacks) c._renderCallbacks = [];\n\t\t\t});\n\t\t\tcommitQueue = [];\n\t\t\toptions._catchError(e, component._vnode);\n\t\t}\n\t});\n\n\tif (oldCommit) oldCommit(vnode, commitQueue);\n};\n\noptions.unmount = vnode => {\n\tif (oldBeforeUnmount) oldBeforeUnmount(vnode);\n\n\tconst c = vnode._component;\n\tif (c && c.__hooks) {\n\t\tlet hasErrored;\n\t\tc.__hooks._list.forEach(s => {\n\t\t\ttry {\n\t\t\t\tinvokeCleanup(s);\n\t\t\t} catch (e) {\n\t\t\t\thasErrored = e;\n\t\t\t}\n\t\t});\n\t\tif (hasErrored) options._catchError(hasErrored, c._vnode);\n\t}\n};\n\n/**\n * Get a hook's state from the currentComponent\n * @param {number} index The index of the hook to get\n * @param {number} type The index of the hook to get\n * @returns {any}\n */\nfunction getHookState(index, type) {\n\tif (options._hook) {\n\t\toptions._hook(currentComponent, index, currentHook || type);\n\t}\n\tcurrentHook = 0;\n\n\t// Largely inspired by:\n\t// * https://github.com/michael-klein/funcy.js/blob/f6be73468e6ec46b0ff5aa3cc4c9baf72a29025a/src/hooks/core_hooks.mjs\n\t// * https://github.com/michael-klein/funcy.js/blob/650beaa58c43c33a74820a3c98b3c7079cf2e333/src/renderer.mjs\n\t// Other implementations to look at:\n\t// * https://codesandbox.io/s/mnox05qp8\n\tconst hooks =\n\t\tcurrentComponent.__hooks ||\n\t\t(currentComponent.__hooks = {\n\t\t\t_list: [],\n\t\t\t_pendingEffects: []\n\t\t});\n\n\tif (index >= hooks._list.length) {\n\t\thooks._list.push({});\n\t}\n\treturn hooks._list[index];\n}\n\n/**\n * @param {import('./index').StateUpdater<any>} [initialState]\n */\nexport function useState(initialState) {\n\tcurrentHook = 1;\n\treturn useReducer(invokeOrReturn, initialState);\n}\n\n/**\n * @param {import('./index').Reducer<any, any>} reducer\n * @param {import('./index').StateUpdater<any>} initialState\n * @param {(initialState: any) => void} [init]\n * @returns {[ any, (state: any) => void ]}\n */\nexport function useReducer(reducer, initialState, init) {\n\t/** @type {import('./internal').ReducerHookState} */\n\tconst hookState = getHookState(currentIndex++, 2);\n\thookState._reducer = reducer;\n\tif (!hookState._component) {\n\t\thookState._value = [\n\t\t\t!init ? invokeOrReturn(undefined, initialState) : init(initialState),\n\n\t\t\taction => {\n\t\t\t\tconst nextValue = hookState._reducer(hookState._value[0], action);\n\t\t\t\tif (hookState._value[0] !== nextValue) {\n\t\t\t\t\thookState._value = [nextValue, hookState._value[1]];\n\t\t\t\t\thookState._component.setState({});\n\t\t\t\t}\n\t\t\t}\n\t\t];\n\n\t\thookState._component = currentComponent;\n\t}\n\n\treturn hookState._value;\n}\n\n/**\n * @param {import('./internal').Effect} callback\n * @param {any[]} args\n */\nexport function useEffect(callback, args) {\n\t/** @type {import('./internal').EffectHookState} */\n\tconst state = getHookState(currentIndex++, 3);\n\tif (!options._skipEffects && argsChanged(state._args, args)) {\n\t\tstate._value = callback;\n\t\tstate._args = args;\n\n\t\tcurrentComponent.__hooks._pendingEffects.push(state);\n\t}\n}\n\n/**\n * @param {import('./internal').Effect} callback\n * @param {any[]} args\n */\nexport function useLayoutEffect(callback, args) {\n\t/** @type {import('./internal').EffectHookState} */\n\tconst state = getHookState(currentIndex++, 4);\n\tif (!options._skipEffects && argsChanged(state._args, args)) {\n\t\tstate._value = callback;\n\t\tstate._args = args;\n\n\t\tcurrentComponent._renderCallbacks.push(state);\n\t}\n}\n\nexport function useRef(initialValue) {\n\tcurrentHook = 5;\n\treturn useMemo(() => ({ current: initialValue }), []);\n}\n\n/**\n * @param {object} ref\n * @param {() => object} createHandle\n * @param {any[]} args\n */\nexport function useImperativeHandle(ref, createHandle, args) {\n\tcurrentHook = 6;\n\tuseLayoutEffect(\n\t\t() => {\n\t\t\tif (typeof ref == 'function') {\n\t\t\t\tref(createHandle());\n\t\t\t\treturn () => ref(null);\n\t\t\t} else if (ref) {\n\t\t\t\tref.current = createHandle();\n\t\t\t\treturn () => ref.current = null;\n\t\t\t}\n\t\t},\n\t\targs == null ? args : args.concat(ref)\n\t);\n}\n\n/**\n * @param {() => any} factory\n * @param {any[]} args\n */\nexport function useMemo(factory, args) {\n\t/** @type {import('./internal').MemoHookState} */\n\tconst state = getHookState(currentIndex++, 7);\n\tif (argsChanged(state._args, args)) {\n\t\tstate._value = factory();\n\t\tstate._args = args;\n\t\tstate._factory = factory;\n\t}\n\n\treturn state._value;\n}\n\n/**\n * @param {() => void} callback\n * @param {any[]} args\n */\nexport function useCallback(callback, args) {\n\tcurrentHook = 8;\n\treturn useMemo(() => callback, args);\n}\n\n/**\n * @param {import('./internal').PreactContext} context\n */\nexport function useContext(context) {\n\tconst provider = currentComponent.context[context._id];\n\t// We could skip this call here, but than we'd not call\n\t// `options._hook`. We need to do that in order to make\n\t// the devtools aware of this hook.\n\t/** @type {import('./internal').ContextHookState} */\n\tconst state = getHookState(currentIndex++, 9);\n\t// The devtools needs access to the context object to\n\t// be able to pull of the default value when no provider\n\t// is present in the tree.\n\tstate._context = context;\n\tif (!provider) return context._defaultValue;\n\t// This is probably not safe to convert to \"!\"\n\tif (state._value == null) {\n\t\tstate._value = true;\n\t\tprovider.sub(currentComponent);\n\t}\n\treturn provider.props.value;\n}\n\n/**\n * Display a custom label for a custom hook for the devtools panel\n * @type {<T>(value: T, cb?: (value: T) => string | number) => void}\n */\nexport function useDebugValue(value, formatter) {\n\tif (options.useDebugValue) {\n\t\toptions.useDebugValue(formatter ? formatter(value) : value);\n\t}\n}\n\n/**\n * @param {(error: any) => void} cb\n */\nexport function useErrorBoundary(cb) {\n\t/** @type {import('./internal').ErrorBoundaryHookState} */\n\tconst state = getHookState(currentIndex++, 10);\n\tconst errState = useState();\n\tstate._value = cb;\n\tif (!currentComponent.componentDidCatch) {\n\t\tcurrentComponent.componentDidCatch = err => {\n\t\t\tif (state._value) state._value(err);\n\t\t\terrState[1](err);\n\t\t};\n\t}\n\treturn [\n\t\terrState[0],\n\t\t() => {\n\t\t\terrState[1](undefined);\n\t\t}\n\t];\n}\n\n/**\n * After paint effects consumer.\n */\nfunction flushAfterPaintEffects() {\n\tlet component;\n\twhile ((component = afterPaintEffects.shift())) {\n\t\tif (!component._parentDom) continue;\n\t\ttry {\n\t\t\tcomponent.__hooks._pendingEffects.forEach(invokeCleanup);\n\t\t\tcomponent.__hooks._pendingEffects.forEach(invokeEffect);\n\t\t\tcomponent.__hooks._pendingEffects = [];\n\t\t} catch (e) {\n\t\t\tcomponent.__hooks._pendingEffects = [];\n\t\t\toptions._catchError(e, component._vnode);\n\t\t}\n\t}\n}\n\nlet HAS_RAF = typeof requestAnimationFrame == 'function';\n\n/**\n * Schedule a callback to be invoked after the browser has a chance to paint a new frame.\n * Do this by combining requestAnimationFrame (rAF) + setTimeout to invoke a callback after\n * the next browser frame.\n *\n * Also, schedule a timeout in parallel to the the rAF to ensure the callback is invoked\n * even if RAF doesn't fire (for example if the browser tab is not visible)\n *\n * @param {() => void} callback\n */\nfunction afterNextFrame(callback) {\n\tconst done = () => {\n\t\tclearTimeout(timeout);\n\t\tif (HAS_RAF) cancelAnimationFrame(raf);\n\t\tsetTimeout(callback);\n\t};\n\tconst timeout = setTimeout(done, RAF_TIMEOUT);\n\n\tlet raf;\n\tif (HAS_RAF) {\n\t\traf = requestAnimationFrame(done);\n\t}\n}\n\n// Note: if someone used options.debounceRendering = requestAnimationFrame,\n// then effects will ALWAYS run on the NEXT frame instead of the current one, incurring a ~16ms delay.\n// Perhaps this is not such a big deal.\n/**\n * Schedule afterPaintEffects flush after the browser paints\n * @param {number} newQueueLength\n */\nfunction afterPaint(newQueueLength) {\n\tif (newQueueLength === 1 || prevRaf !== options.requestAnimationFrame) {\n\t\tprevRaf = options.requestAnimationFrame;\n\t\t(prevRaf || afterNextFrame)(flushAfterPaintEffects);\n\t}\n}\n\n/**\n * @param {import('./internal').EffectHookState} hook\n */\nfunction invokeCleanup(hook) {\n\t// A hook cleanup can introduce a call to render which creates a new root, this will call options.vnode\n\t// and move the currentComponent away.\n\tconst comp = currentComponent;\n\tlet cleanup = hook._cleanup;\n\tif (typeof cleanup == 'function') {\n\t\thook._cleanup = undefined;\n\t\tcleanup();\n\t}\n\tcurrentComponent = comp;\n}\n\n/**\n * Invoke a Hook's effect\n * @param {import('./internal').EffectHookState} hook\n */\nfunction invokeEffect(hook) {\n\t// A hook call can introduce a call to render which creates a new root, this will call options.vnode\n\t// and move the currentComponent away.\n\tconst comp = currentComponent;\n\thook._cleanup = hook._value();\n\tcurrentComponent = comp;\n}\n\n/**\n * @param {any[]} oldArgs\n * @param {any[]} newArgs\n */\nfunction argsChanged(oldArgs, newArgs) {\n\treturn (\n\t\t!oldArgs ||\n\t\toldArgs.length !== newArgs.length ||\n\t\tnewArgs.some((arg, index) => arg !== oldArgs[index])\n\t);\n}\n\nfunction invokeOrReturn(arg, f) {\n\treturn typeof f == 'function' ? f(arg) : f;\n}\n"],"names":["currentIndex","currentComponent","prevRaf","currentHook","afterPaintEffects","oldBeforeDiff","options","oldBeforeRender","oldAfterDiff","diffed","oldCommit","oldBeforeUnmount","unmount","getHookState","index","type","hooks","length","push","useState","initialState","useReducer","invokeOrReturn","reducer","init","hookState","_reducer","undefined","action","nextValue","setState","useLayoutEffect","callback","args","state","argsChanged","useMemo","factory","flushAfterPaintEffects","component","shift","forEach","invokeCleanup","invokeEffect","e","vnode","c","requestAnimationFrame","raf","done","clearTimeout","timeout","HAS_RAF","cancelAnimationFrame","setTimeout","commitQueue","some","filter","cb","hasErrored","s","hook","comp","cleanup","oldArgs","newArgs","arg","f","initialValue","current","ref","createHandle","concat","context","provider","sub","props","value","formatter","useDebugValue","errState","componentDidCatch","err"],"mappings":"uNAGA,IAAIA,EAGAC,EAeAC,EAZAC,EAAc,EAGdC,EAAoB,GAEpBC,EAAgBC,cAChBC,EAAkBD,cAClBE,EAAeF,UAAQG,OACvBC,EAAYJ,cACZK,EAAmBL,UAAQM,QA4E/B,SAASC,EAAaC,EAAOC,GACxBT,eACHA,cAAcL,EAAkBa,EAAOX,GAAeY,GAEvDZ,EAAc,MAORa,EACLf,QACCA,MAA2B,IACpB,OACU,YAGfa,GAASE,KAAYC,QACxBD,KAAYE,KAAK,IAEXF,KAAYF,GAMb,SAASK,EAASC,UACxBjB,EAAc,EACPkB,EAAWC,EAAgBF,GAS5B,SAASC,EAAWE,EAASH,EAAcI,OAE3CC,EAAYZ,EAAab,IAAgB,UAC/CyB,EAAUC,EAAWH,EAChBE,QACJA,KAAmB,CACjBD,EAAiDA,EAAKJ,GAA/CE,OAAeK,EAAWP,GAElC,SAAAQ,OACOC,EAAYJ,EAAUC,EAASD,KAAiB,GAAIG,G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,GAAUC,iBAMzB,SAAoBwC,OACpBC,EAAWzE,EAAiBwE,QAAQA,OAKpCvC,EAAQrB,EAAab,IAAgB,UAI3CkC,IAAiBuC,EACZC,GAEe,MAAhBxC,OACHA,MAAe,EACfwC,EAASC,IAAI1E,IAEPyE,EAASE,MAAMC,OANAJ,sBAahB,SAAuBI,EAAOC,GAChCxE,UAAQyE,eACXzE,UAAQyE,cAAcD,EAAYA,EAAUD,GAASA,uBAOhD,SAA0BnB,OAE1BxB,EAAQrB,EAAab,IAAgB,IACrCgF,EAAW7D,WACjBe,KAAewB,EACVzD,EAAiBgF,oBACrBhF,EAAiBgF,kBAAoB,SAAAC,GAChChD,MAAcA,KAAagD,GAC/BF,EAAS,GAAGE,KAGP,CACNF,EAAS,GACT,WACCA,EAAS,QAAGrD"}
|
1
|
+
{"version":3,"file":"hooks.umd.js","sources":["../src/index.js"],"sourcesContent":["import { options } from 'preact';\n\n/** @type {number} */\nlet currentIndex;\n\n/** @type {import('./internal').Component} */\nlet currentComponent;\n\n/** @type {import('./internal').Component} */\nlet previousComponent;\n\n/** @type {number} */\nlet currentHook = 0;\n\n/** @type {Array<import('./internal').Component>} */\nlet afterPaintEffects = [];\n\nlet oldBeforeDiff = options._diff;\nlet oldBeforeRender = options._render;\nlet oldAfterDiff = options.diffed;\nlet oldCommit = options._commit;\nlet oldBeforeUnmount = options.unmount;\n\nconst RAF_TIMEOUT = 100;\nlet prevRaf;\n\noptions._diff = vnode => {\n\tcurrentComponent = null;\n\tif (oldBeforeDiff) oldBeforeDiff(vnode);\n};\n\noptions._render = vnode => {\n\tif (oldBeforeRender) oldBeforeRender(vnode);\n\n\tcurrentComponent = vnode._component;\n\tcurrentIndex = 0;\n\n\tconst hooks = currentComponent.__hooks;\n\tif (hooks) {\n\t\tif (previousComponent === currentComponent) {\n\t\t\thooks._pendingEffects = [];\n\t\t\tcurrentComponent._renderCallbacks = [];\n\t\t\thooks._list.forEach(hookItem => {\n\t\t\t\thookItem._pendingValue = hookItem._pendingArgs = undefined;\n\t\t\t});\n\t\t} else {\n\t\t\thooks._list.forEach(hookItem => {\n\t\t\t\tif (hookItem._pendingArgs) {\n\t\t\t\t\thookItem._args = hookItem._pendingArgs;\n\t\t\t\t}\n\t\t\t\tif (hookItem._pendingValue) {\n\t\t\t\t\thookItem._value = hookItem._pendingValue;\n\t\t\t\t}\n\t\t\t\thookItem._pendingValue = hookItem._pendingArgs = undefined;\n\t\t\t});\n\t\t\thooks._pendingEffects.forEach(invokeCleanup);\n\t\t\thooks._pendingEffects.forEach(invokeEffect);\n\t\t\thooks._pendingEffects = [];\n\t\t}\n\t}\n\tpreviousComponent = currentComponent;\n};\n\noptions.diffed = vnode => {\n\tif (oldAfterDiff) oldAfterDiff(vnode);\n\n\tconst c = vnode._component;\n\tif (c && c.__hooks && c.__hooks._pendingEffects.length) {\n\t\tafterPaint(afterPaintEffects.push(c));\n\t}\n\tcurrentComponent = null;\n\tpreviousComponent = null;\n};\n\noptions._commit = (vnode, commitQueue) => {\n\tcommitQueue.some(component => {\n\t\ttry {\n\t\t\tif (component.__hooks) {\n\t\t\t\tcomponent.__hooks._list.forEach(hookItem => {\n\t\t\t\t\tif (hookItem._pendingArgs) {\n\t\t\t\t\t\thookItem._args = hookItem._pendingArgs;\n\t\t\t\t\t}\n\t\t\t\t\tif (hookItem._pendingValue) {\n\t\t\t\t\t\thookItem._value = hookItem._pendingValue;\n\t\t\t\t\t}\n\t\t\t\t\thookItem._pendingValue = hookItem._pendingArgs = undefined;\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tcomponent._renderCallbacks.forEach(invokeCleanup);\n\t\t\tcomponent._renderCallbacks = component._renderCallbacks.filter(cb =>\n\t\t\t\tcb._value ? invokeEffect(cb) : true\n\t\t\t);\n\t\t} catch (e) {\n\t\t\tcommitQueue.some(c => {\n\t\t\t\tif (c._renderCallbacks) c._renderCallbacks = [];\n\t\t\t});\n\t\t\tcommitQueue = [];\n\t\t\toptions._catchError(e, component._vnode);\n\t\t}\n\t});\n\n\tif (oldCommit) oldCommit(vnode, commitQueue);\n};\n\noptions.unmount = vnode => {\n\tif (oldBeforeUnmount) oldBeforeUnmount(vnode);\n\n\tconst c = vnode._component;\n\tif (c && c.__hooks) {\n\t\tlet hasErrored;\n\t\tc.__hooks._list.forEach(s => {\n\t\t\ttry {\n\t\t\t\tinvokeCleanup(s);\n\t\t\t} catch (e) {\n\t\t\t\thasErrored = e;\n\t\t\t}\n\t\t});\n\t\tif (hasErrored) options._catchError(hasErrored, c._vnode);\n\t}\n};\n\n/**\n * Get a hook's state from the currentComponent\n * @param {number} index The index of the hook to get\n * @param {number} type The index of the hook to get\n * @returns {any}\n */\nfunction getHookState(index, type) {\n\tif (options._hook) {\n\t\toptions._hook(currentComponent, index, currentHook || type);\n\t}\n\tcurrentHook = 0;\n\n\t// Largely inspired by:\n\t// * https://github.com/michael-klein/funcy.js/blob/f6be73468e6ec46b0ff5aa3cc4c9baf72a29025a/src/hooks/core_hooks.mjs\n\t// * https://github.com/michael-klein/funcy.js/blob/650beaa58c43c33a74820a3c98b3c7079cf2e333/src/renderer.mjs\n\t// Other implementations to look at:\n\t// * https://codesandbox.io/s/mnox05qp8\n\tconst hooks =\n\t\tcurrentComponent.__hooks ||\n\t\t(currentComponent.__hooks = {\n\t\t\t_list: [],\n\t\t\t_pendingEffects: []\n\t\t});\n\n\tif (index >= hooks._list.length) {\n\t\thooks._list.push({});\n\t}\n\treturn hooks._list[index];\n}\n\n/**\n * @param {import('./index').StateUpdater<any>} [initialState]\n */\nexport function useState(initialState) {\n\tcurrentHook = 1;\n\treturn useReducer(invokeOrReturn, initialState);\n}\n\n/**\n * @param {import('./index').Reducer<any, any>} reducer\n * @param {import('./index').StateUpdater<any>} initialState\n * @param {(initialState: any) => void} [init]\n * @returns {[ any, (state: any) => void ]}\n */\nexport function useReducer(reducer, initialState, init) {\n\t/** @type {import('./internal').ReducerHookState} */\n\tconst hookState = getHookState(currentIndex++, 2);\n\thookState._reducer = reducer;\n\tif (!hookState._component) {\n\t\thookState._value = [\n\t\t\t!init ? invokeOrReturn(undefined, initialState) : init(initialState),\n\n\t\t\taction => {\n\t\t\t\tconst nextValue = hookState._reducer(hookState._value[0], action);\n\t\t\t\tif (hookState._value[0] !== nextValue) {\n\t\t\t\t\thookState._value = [nextValue, hookState._value[1]];\n\t\t\t\t\thookState._component.setState({});\n\t\t\t\t}\n\t\t\t}\n\t\t];\n\n\t\thookState._component = currentComponent;\n\t}\n\n\treturn hookState._value;\n}\n\n/**\n * @param {import('./internal').Effect} callback\n * @param {any[]} args\n */\nexport function useEffect(callback, args) {\n\t/** @type {import('./internal').EffectHookState} */\n\tconst state = getHookState(currentIndex++, 3);\n\tif (!options._skipEffects && argsChanged(state._args, args)) {\n\t\tstate._value = callback;\n\t\tstate._pendingArgs = args;\n\n\t\tcurrentComponent.__hooks._pendingEffects.push(state);\n\t}\n}\n\n/**\n * @param {import('./internal').Effect} callback\n * @param {any[]} args\n */\nexport function useLayoutEffect(callback, args) {\n\t/** @type {import('./internal').EffectHookState} */\n\tconst state = getHookState(currentIndex++, 4);\n\tif (!options._skipEffects && argsChanged(state._args, args)) {\n\t\tstate._value = callback;\n\t\tstate._pendingArgs = args;\n\n\t\tcurrentComponent._renderCallbacks.push(state);\n\t}\n}\n\nexport function useRef(initialValue) {\n\tcurrentHook = 5;\n\treturn useMemo(() => ({ current: initialValue }), []);\n}\n\n/**\n * @param {object} ref\n * @param {() => object} createHandle\n * @param {any[]} args\n */\nexport function useImperativeHandle(ref, createHandle, args) {\n\tcurrentHook = 6;\n\tuseLayoutEffect(\n\t\t() => {\n\t\t\tif (typeof ref == 'function') {\n\t\t\t\tref(createHandle());\n\t\t\t\treturn () => ref(null);\n\t\t\t} else if (ref) {\n\t\t\t\tref.current = createHandle();\n\t\t\t\treturn () => (ref.current = null);\n\t\t\t}\n\t\t},\n\t\targs == null ? args : args.concat(ref)\n\t);\n}\n\n/**\n * @param {() => any} factory\n * @param {any[]} args\n */\nexport function useMemo(factory, args) {\n\t/** @type {import('./internal').MemoHookState} */\n\tconst state = getHookState(currentIndex++, 7);\n\tif (argsChanged(state._args, args)) {\n\t\tstate._pendingValue = factory();\n\t\tstate._pendingArgs = args;\n\t\tstate._factory = factory;\n\t\treturn state._pendingValue;\n\t}\n\n\treturn state._value;\n}\n\n/**\n * @param {() => void} callback\n * @param {any[]} args\n */\nexport function useCallback(callback, args) {\n\tcurrentHook = 8;\n\treturn useMemo(() => callback, args);\n}\n\n/**\n * @param {import('./internal').PreactContext} context\n */\nexport function useContext(context) {\n\tconst provider = currentComponent.context[context._id];\n\t// We could skip this call here, but than we'd not call\n\t// `options._hook`. We need to do that in order to make\n\t// the devtools aware of this hook.\n\t/** @type {import('./internal').ContextHookState} */\n\tconst state = getHookState(currentIndex++, 9);\n\t// The devtools needs access to the context object to\n\t// be able to pull of the default value when no provider\n\t// is present in the tree.\n\tstate._context = context;\n\tif (!provider) return context._defaultValue;\n\t// This is probably not safe to convert to \"!\"\n\tif (state._value == null) {\n\t\tstate._value = true;\n\t\tprovider.sub(currentComponent);\n\t}\n\treturn provider.props.value;\n}\n\n/**\n * Display a custom label for a custom hook for the devtools panel\n * @type {<T>(value: T, cb?: (value: T) => string | number) => void}\n */\nexport function useDebugValue(value, formatter) {\n\tif (options.useDebugValue) {\n\t\toptions.useDebugValue(formatter ? formatter(value) : value);\n\t}\n}\n\n/**\n * @param {(error: any) => void} cb\n */\nexport function useErrorBoundary(cb) {\n\t/** @type {import('./internal').ErrorBoundaryHookState} */\n\tconst state = getHookState(currentIndex++, 10);\n\tconst errState = useState();\n\tstate._value = cb;\n\tif (!currentComponent.componentDidCatch) {\n\t\tcurrentComponent.componentDidCatch = err => {\n\t\t\tif (state._value) state._value(err);\n\t\t\terrState[1](err);\n\t\t};\n\t}\n\treturn [\n\t\terrState[0],\n\t\t() => {\n\t\t\terrState[1](undefined);\n\t\t}\n\t];\n}\n\n/**\n * After paint effects consumer.\n */\nfunction flushAfterPaintEffects() {\n\tlet component;\n\twhile ((component = afterPaintEffects.shift())) {\n\t\tif (!component._parentDom) continue;\n\t\ttry {\n\t\t\tcomponent.__hooks._pendingEffects.forEach(invokeCleanup);\n\t\t\tcomponent.__hooks._pendingEffects.forEach(invokeEffect);\n\t\t\tcomponent.__hooks._pendingEffects = [];\n\t\t} catch (e) {\n\t\t\tcomponent.__hooks._pendingEffects = [];\n\t\t\toptions._catchError(e, component._vnode);\n\t\t}\n\t}\n}\n\nlet HAS_RAF = typeof requestAnimationFrame == 'function';\n\n/**\n * Schedule a callback to be invoked after the browser has a chance to paint a new frame.\n * Do this by combining requestAnimationFrame (rAF) + setTimeout to invoke a callback after\n * the next browser frame.\n *\n * Also, schedule a timeout in parallel to the the rAF to ensure the callback is invoked\n * even if RAF doesn't fire (for example if the browser tab is not visible)\n *\n * @param {() => void} callback\n */\nfunction afterNextFrame(callback) {\n\tconst done = () => {\n\t\tclearTimeout(timeout);\n\t\tif (HAS_RAF) cancelAnimationFrame(raf);\n\t\tsetTimeout(callback);\n\t};\n\tconst timeout = setTimeout(done, RAF_TIMEOUT);\n\n\tlet raf;\n\tif (HAS_RAF) {\n\t\traf = requestAnimationFrame(done);\n\t}\n}\n\n// Note: if someone used options.debounceRendering = requestAnimationFrame,\n// then effects will ALWAYS run on the NEXT frame instead of the current one, incurring a ~16ms delay.\n// Perhaps this is not such a big deal.\n/**\n * Schedule afterPaintEffects flush after the browser paints\n * @param {number} newQueueLength\n */\nfunction afterPaint(newQueueLength) {\n\tif (newQueueLength === 1 || prevRaf !== options.requestAnimationFrame) {\n\t\tprevRaf = options.requestAnimationFrame;\n\t\t(prevRaf || afterNextFrame)(flushAfterPaintEffects);\n\t}\n}\n\n/**\n * @param {import('./internal').EffectHookState} hook\n */\nfunction invokeCleanup(hook) {\n\t// A hook cleanup can introduce a call to render which creates a new root, this will call options.vnode\n\t// and move the currentComponent away.\n\tconst comp = currentComponent;\n\tlet cleanup = hook._cleanup;\n\tif (typeof cleanup == 'function') {\n\t\thook._cleanup = undefined;\n\t\tcleanup();\n\t}\n\tcurrentComponent = comp;\n}\n\n/**\n * Invoke a Hook's effect\n * @param {import('./internal').EffectHookState} hook\n */\nfunction invokeEffect(hook) {\n\t// A hook call can introduce a call to render which creates a new root, this will call options.vnode\n\t// and move the currentComponent away.\n\tconst comp = currentComponent;\n\thook._cleanup = hook._value();\n\tcurrentComponent = comp;\n}\n\n/**\n * @param {any[]} oldArgs\n * @param {any[]} newArgs\n */\nfunction argsChanged(oldArgs, newArgs) {\n\treturn (\n\t\t!oldArgs ||\n\t\toldArgs.length !== newArgs.length ||\n\t\tnewArgs.some((arg, index) => arg !== oldArgs[index])\n\t);\n}\n\nfunction invokeOrReturn(arg, f) {\n\treturn typeof f == 'function' ? f(arg) : f;\n}\n"],"names":["currentIndex","currentComponent","previousComponent","prevRaf","currentHook","afterPaintEffects","oldBeforeDiff","options","oldBeforeRender","oldAfterDiff","diffed","oldCommit","oldBeforeUnmount","unmount","getHookState","index","type","hooks","length","push","useState","initialState","useReducer","invokeOrReturn","reducer","init","hookState","_reducer","undefined","action","nextValue","setState","useLayoutEffect","callback","args","state","argsChanged","_pendingArgs","useMemo","factory","_pendingValue","flushAfterPaintEffects","component","shift","forEach","invokeCleanup","invokeEffect","e","vnode","hookItem","c","requestAnimationFrame","raf","done","clearTimeout","timeout","HAS_RAF","cancelAnimationFrame","setTimeout","commitQueue","some","filter","cb","hasErrored","s","hook","comp","cleanup","oldArgs","newArgs","arg","f","initialValue","current","ref","createHandle","concat","context","provider","sub","props","value","formatter","useDebugValue","errState","componentDidCatch","err"],"mappings":"uNAGA,IAAIA,EAGAC,EAGAC,EAeAC,EAZAC,EAAc,EAGdC,EAAoB,GAEpBC,EAAgBC,cAChBC,EAAkBD,cAClBE,EAAeF,UAAQG,OACvBC,EAAYJ,cACZK,EAAmBL,UAAQM,QA2G/B,SAASC,EAAaC,EAAOC,GACxBT,eACHA,cAAcN,EAAkBc,EAAOX,GAAeY,GAEvDZ,EAAc,MAORa,EACLh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F,kBAAoB,SAAAC,GAChCnD,MAAcA,KAAamD,GAC/BF,EAAS,GAAGE,KAGP,CACNF,EAAS,GACT,WACCA,EAAS,QAAGxD"}
|
@@ -1,26 +1,35 @@
|
|
1
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
|
-
|
2
|
+
"name": "preact-hooks",
|
3
|
+
"amdName": "preactHooks",
|
4
|
+
"version": "0.1.0",
|
5
|
+
"private": true,
|
6
|
+
"description": "Hook addon for Preact",
|
7
|
+
"main": "dist/hooks.js",
|
8
|
+
"module": "dist/hooks.module.js",
|
9
|
+
"umd:main": "dist/hooks.umd.js",
|
10
|
+
"source": "src/index.js",
|
11
|
+
"license": "MIT",
|
12
|
+
"types": "src/index.d.ts",
|
13
|
+
"scripts": {
|
14
|
+
"build": "microbundle build --raw",
|
15
|
+
"dev": "microbundle watch --raw --format cjs",
|
16
|
+
"test": "npm-run-all build --parallel test:karma",
|
17
|
+
"test:karma": "karma start test/karma.conf.js --single-run",
|
18
|
+
"test:karma:watch": "karma start test/karma.conf.js --no-single-run"
|
19
|
+
},
|
20
|
+
"peerDependencies": {
|
21
|
+
"preact": "^10.0.0"
|
22
|
+
},
|
23
|
+
"mangle": {
|
24
|
+
"regex": "^_"
|
25
|
+
},
|
26
|
+
"exports": {
|
27
|
+
".": {
|
28
|
+
"types": "./src/index.d.ts",
|
29
|
+
"browser": "./dist/hooks.module.js",
|
30
|
+
"umd": "./dist/hooks.umd.js",
|
31
|
+
"import": "./dist/hooks.mjs",
|
32
|
+
"require": "./dist/hooks.js"
|
33
|
+
}
|
34
|
+
}
|
26
35
|
}
|
@@ -6,6 +6,9 @@ let currentIndex;
|
|
6
6
|
/** @type {import('./internal').Component} */
|
7
7
|
let currentComponent;
|
8
8
|
|
9
|
+
/** @type {import('./internal').Component} */
|
10
|
+
let previousComponent;
|
11
|
+
|
9
12
|
/** @type {number} */
|
10
13
|
let currentHook = 0;
|
11
14
|
|
@@ -34,10 +37,28 @@ options._render = vnode => {
|
|
34
37
|
|
35
38
|
const hooks = currentComponent.__hooks;
|
36
39
|
if (hooks) {
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
+
if (previousComponent === currentComponent) {
|
41
|
+
hooks._pendingEffects = [];
|
42
|
+
currentComponent._renderCallbacks = [];
|
43
|
+
hooks._list.forEach(hookItem => {
|
44
|
+
hookItem._pendingValue = hookItem._pendingArgs = undefined;
|
45
|
+
});
|
46
|
+
} else {
|
47
|
+
hooks._list.forEach(hookItem => {
|
48
|
+
if (hookItem._pendingArgs) {
|
49
|
+
hookItem._args = hookItem._pendingArgs;
|
50
|
+
}
|
51
|
+
if (hookItem._pendingValue) {
|
52
|
+
hookItem._value = hookItem._pendingValue;
|
53
|
+
}
|
54
|
+
hookItem._pendingValue = hookItem._pendingArgs = undefined;
|
55
|
+
});
|
56
|
+
hooks._pendingEffects.forEach(invokeCleanup);
|
57
|
+
hooks._pendingEffects.forEach(invokeEffect);
|
58
|
+
hooks._pendingEffects = [];
|
59
|
+
}
|
40
60
|
}
|
61
|
+
previousComponent = currentComponent;
|
41
62
|
};
|
42
63
|
|
43
64
|
options.diffed = vnode => {
|
@@ -48,11 +69,24 @@ options.diffed = vnode => {
|
|
48
69
|
afterPaint(afterPaintEffects.push(c));
|
49
70
|
}
|
50
71
|
currentComponent = null;
|
72
|
+
previousComponent = null;
|
51
73
|
};
|
52
74
|
|
53
75
|
options._commit = (vnode, commitQueue) => {
|
54
76
|
commitQueue.some(component => {
|
55
77
|
try {
|
78
|
+
if (component.__hooks) {
|
79
|
+
component.__hooks._list.forEach(hookItem => {
|
80
|
+
if (hookItem._pendingArgs) {
|
81
|
+
hookItem._args = hookItem._pendingArgs;
|
82
|
+
}
|
83
|
+
if (hookItem._pendingValue) {
|
84
|
+
hookItem._value = hookItem._pendingValue;
|
85
|
+
}
|
86
|
+
hookItem._pendingValue = hookItem._pendingArgs = undefined;
|
87
|
+
});
|
88
|
+
}
|
89
|
+
|
56
90
|
component._renderCallbacks.forEach(invokeCleanup);
|
57
91
|
component._renderCallbacks = component._renderCallbacks.filter(cb =>
|
58
92
|
cb._value ? invokeEffect(cb) : true
|
@@ -162,7 +196,7 @@ export function useEffect(callback, args) {
|
|
162
196
|
const state = getHookState(currentIndex++, 3);
|
163
197
|
if (!options._skipEffects && argsChanged(state._args, args)) {
|
164
198
|
state._value = callback;
|
165
|
-
state.
|
199
|
+
state._pendingArgs = args;
|
166
200
|
|
167
201
|
currentComponent.__hooks._pendingEffects.push(state);
|
168
202
|
}
|
@@ -177,7 +211,7 @@ export function useLayoutEffect(callback, args) {
|
|
177
211
|
const state = getHookState(currentIndex++, 4);
|
178
212
|
if (!options._skipEffects && argsChanged(state._args, args)) {
|
179
213
|
state._value = callback;
|
180
|
-
state.
|
214
|
+
state._pendingArgs = args;
|
181
215
|
|
182
216
|
currentComponent._renderCallbacks.push(state);
|
183
217
|
}
|
@@ -202,7 +236,7 @@ export function useImperativeHandle(ref, createHandle, args) {
|
|
202
236
|
return () => ref(null);
|
203
237
|
} else if (ref) {
|
204
238
|
ref.current = createHandle();
|
205
|
-
return () => ref.current = null;
|
239
|
+
return () => (ref.current = null);
|
206
240
|
}
|
207
241
|
},
|
208
242
|
args == null ? args : args.concat(ref)
|
@@ -217,9 +251,10 @@ export function useMemo(factory, args) {
|
|
217
251
|
/** @type {import('./internal').MemoHookState} */
|
218
252
|
const state = getHookState(currentIndex++, 7);
|
219
253
|
if (argsChanged(state._args, args)) {
|
220
|
-
state.
|
221
|
-
state.
|
254
|
+
state._pendingValue = factory();
|
255
|
+
state._pendingArgs = args;
|
222
256
|
state._factory = factory;
|
257
|
+
return state._pendingValue;
|
223
258
|
}
|
224
259
|
|
225
260
|
return state._value;
|
@@ -49,12 +49,15 @@ export type Cleanup = () => void;
|
|
49
49
|
export interface EffectHookState {
|
50
50
|
_value?: Effect;
|
51
51
|
_args?: any[];
|
52
|
+
_pendingArgs?: any[];
|
52
53
|
_cleanup?: Cleanup | void;
|
53
54
|
}
|
54
55
|
|
55
56
|
export interface MemoHookState {
|
56
57
|
_value?: any;
|
58
|
+
_pendingValue?: any;
|
57
59
|
_args?: any[];
|
60
|
+
_pendingArgs?: any[];
|
58
61
|
_factory?: () => any;
|
59
62
|
}
|
60
63
|
|
@@ -15,5 +15,14 @@
|
|
15
15
|
},
|
16
16
|
"mangle": {
|
17
17
|
"regex": "^_"
|
18
|
+
},
|
19
|
+
"exports": {
|
20
|
+
".": {
|
21
|
+
"types": "./src/index.d.ts",
|
22
|
+
"browser": "./dist/jsxRuntime.module.js",
|
23
|
+
"umd": "./dist/jsxRuntime.umd.js",
|
24
|
+
"import": "./dist/jsxRuntime.mjs",
|
25
|
+
"require": "./dist/jsxRuntime.js"
|
26
|
+
}
|
18
27
|
}
|
19
28
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "preact",
|
3
3
|
"amdName": "preact",
|
4
|
-
"version": "10.
|
4
|
+
"version": "10.8.1",
|
5
5
|
"private": false,
|
6
6
|
"description": "Fast 3kb React-compatible Virtual DOM library.",
|
7
7
|
"main": "dist/preact.js",
|
@@ -69,6 +69,7 @@
|
|
69
69
|
"require": "./compat/client.js"
|
70
70
|
},
|
71
71
|
"./compat/server": {
|
72
|
+
"browser": "./compat/server.browser.js",
|
72
73
|
"import": "./compat/server.mjs",
|
73
74
|
"require": "./compat/server.js"
|
74
75
|
},
|
@@ -202,6 +203,7 @@
|
|
202
203
|
"compat/src",
|
203
204
|
"compat/client.js",
|
204
205
|
"compat/client.mjs",
|
206
|
+
"compat/server.browser.js",
|
205
207
|
"compat/server.js",
|
206
208
|
"compat/server.mjs",
|
207
209
|
"compat/scheduler.js",
|
@@ -170,15 +170,29 @@ export function diff(
|
|
170
170
|
|
171
171
|
c.context = componentContext;
|
172
172
|
c.props = newProps;
|
173
|
-
c.state = c._nextState;
|
174
|
-
|
175
|
-
if ((tmp = options._render)) tmp(newVNode);
|
176
|
-
|
177
|
-
c._dirty = false;
|
178
173
|
c._vnode = newVNode;
|
179
174
|
c._parentDom = parentDom;
|
180
175
|
|
181
|
-
|
176
|
+
let renderHook = options._render,
|
177
|
+
count = 0;
|
178
|
+
if ('prototype' in newType && newType.prototype.render) {
|
179
|
+
c.state = c._nextState;
|
180
|
+
c._dirty = false;
|
181
|
+
|
182
|
+
if (renderHook) renderHook(newVNode);
|
183
|
+
|
184
|
+
tmp = c.render(c.props, c.state, c.context);
|
185
|
+
} else {
|
186
|
+
do {
|
187
|
+
c._dirty = false;
|
188
|
+
if (renderHook) renderHook(newVNode);
|
189
|
+
|
190
|
+
tmp = c.render(c.props, c.state, c.context);
|
191
|
+
|
192
|
+
// Handle setState called in render, see #2553
|
193
|
+
c.state = c._nextState;
|
194
|
+
} while (c._dirty && ++count < 25);
|
195
|
+
}
|
182
196
|
|
183
197
|
// Handle setState called in render, see #2553
|
184
198
|
c.state = c._nextState;
|
@@ -1006,6 +1006,7 @@ export namespace JSXInternal {
|
|
1006
1006
|
stop: SVGAttributes<SVGStopElement>;
|
1007
1007
|
symbol: SVGAttributes<SVGSymbolElement>;
|
1008
1008
|
text: SVGAttributes<SVGTextElement>;
|
1009
|
+
textPath: SVGAttributes<SVGTextPathElement>;
|
1009
1010
|
tspan: SVGAttributes<SVGTSpanElement>;
|
1010
1011
|
use: SVGAttributes<SVGUseElement>;
|
1011
1012
|
}
|
@@ -1,19 +1,28 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
2
|
+
"name": "test-utils",
|
3
|
+
"amdName": "preactTestUtils",
|
4
|
+
"version": "0.1.0",
|
5
|
+
"private": true,
|
6
|
+
"description": "Test-utils for Preact",
|
7
|
+
"main": "dist/testUtils.js",
|
8
|
+
"module": "dist/testUtils.module.js",
|
9
|
+
"umd:main": "dist/testUtils.umd.js",
|
10
|
+
"source": "src/index.js",
|
11
|
+
"license": "MIT",
|
12
|
+
"types": "src/index.d.ts",
|
13
|
+
"peerDependencies": {
|
14
|
+
"preact": "^10.0.0"
|
15
|
+
},
|
16
|
+
"mangle": {
|
17
|
+
"regex": "^_"
|
18
|
+
},
|
19
|
+
"exports": {
|
20
|
+
".": {
|
21
|
+
"types": "./src/index.d.ts",
|
22
|
+
"browser": "./dist/testUtils.module.js",
|
23
|
+
"umd": "./dist/testUtils.umd.js",
|
24
|
+
"import": "./dist/testUtils.mjs",
|
25
|
+
"require": "./dist/testUtils.js"
|
26
|
+
}
|
27
|
+
}
|
19
28
|
}
|
data/package.json
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: isomorfeus-preact
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 10.
|
4
|
+
version: 10.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Biedermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.1.
|
19
|
+
version: 1.1.10
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.1.
|
26
|
+
version: 1.1.10
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: oj
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 3.13.
|
33
|
+
version: 3.13.13
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 3.13.
|
40
|
+
version: 3.13.13
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: opal
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,14 +92,14 @@ dependencies:
|
|
92
92
|
requirements:
|
93
93
|
- - "~>"
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
version: 0.14.
|
95
|
+
version: 0.14.24
|
96
96
|
type: :runtime
|
97
97
|
prerelease: false
|
98
98
|
version_requirements: !ruby/object:Gem::Requirement
|
99
99
|
requirements:
|
100
100
|
- - "~>"
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version: 0.14.
|
102
|
+
version: 0.14.24
|
103
103
|
- !ruby/object:Gem::Dependency
|
104
104
|
name: isomorfeus-redux
|
105
105
|
requirement: !ruby/object:Gem::Requirement
|
@@ -307,6 +307,7 @@ files:
|
|
307
307
|
- node_modules/preact/compat/package.json
|
308
308
|
- node_modules/preact/compat/scheduler.js
|
309
309
|
- node_modules/preact/compat/scheduler.mjs
|
310
|
+
- node_modules/preact/compat/server.browser.js
|
310
311
|
- node_modules/preact/compat/server.js
|
311
312
|
- node_modules/preact/compat/server.mjs
|
312
313
|
- node_modules/preact/compat/src/Children.js
|