1 | import { useState as _useState, useReducer as _useReducer, useRef as _useRef, useEffect as _useEffect, useImperativeHandle as _useImperativeHandle, useLayoutEffect as _useLayoutEffect, useCallback as _useCallback, useMemo as _useMemo, useContext as _useContext, useDebugValue as _useDebugValue, useErrorBoundary as _useErrorBoundary, } from "preact/hooks";
|
2 | import { HydrateContext } from "./hydrate.js";
|
3 | const hooks = new WeakMap();
|
4 | const warn = (name, hook, result) => {
|
5 | console.error(new Error(`A non-hydrated component attempted to invoke "${name}".\n\nWrap this component in the "withHydrate" HOC (exported by "microsite/hydrate") to enable interactivity at compile time.`));
|
6 | hooks.set(hook, result);
|
7 | return result;
|
8 | };
|
9 | function wrapHook(name, hook) {
|
10 | function wrappedHook(...args) {
|
11 | const hydrated = _useContext(HydrateContext);
|
12 | if (!hydrated) {
|
13 | if (hooks.has(hook))
|
14 | return hooks.get(hook);
|
15 | return warn(name, hook, hook.apply(null, args));
|
16 | }
|
17 | return hook.apply(null, args);
|
18 | }
|
19 | Object.defineProperty(wrappedHook, "name", {
|
20 | value: name,
|
21 | configurable: true,
|
22 | });
|
23 | return wrappedHook;
|
24 | }
|
25 | export const useState = wrapHook("useState", _useState);
|
26 | export const useReducer = wrapHook("useReducer", _useReducer);
|
27 | export const useRef = wrapHook("useRef", _useRef);
|
28 | export const useEffect = wrapHook("useEffect", _useEffect);
|
29 | export const useImperativeHandle = wrapHook("useImperativeHandle", _useImperativeHandle);
|
30 | export const useLayoutEffect = wrapHook("useLayoutEffect", _useLayoutEffect);
|
31 | export const useCallback = wrapHook("useCallback", _useCallback);
|
32 | export const useMemo = wrapHook("useMemo", _useMemo);
|
33 | export const useContext = wrapHook("useContext", _useContext);
|
34 | export const useDebugValue = wrapHook("useDebugValue", _useDebugValue);
|
35 | export const useErrorBoundary = wrapHook("useErrorBoundary", _useErrorBoundary);
|