what-core 0.6.6 → 0.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +106 -0
- package/dist/index.js.map +2 -2
- package/dist/index.min.js +6 -6
- package/dist/index.min.js.map +2 -2
- package/hooks.d.ts +26 -0
- package/index.d.ts +3 -3
- package/package.json +3 -1
- package/src/index.js +7 -3
package/dist/index.js
CHANGED
|
@@ -2983,6 +2983,16 @@ function getHook(ctx) {
|
|
|
2983
2983
|
const index = ctx.hookIndex++;
|
|
2984
2984
|
return { index, exists: index < ctx.hooks.length };
|
|
2985
2985
|
}
|
|
2986
|
+
function useState(initial) {
|
|
2987
|
+
const ctx = getCtx("useState");
|
|
2988
|
+
const { index, exists } = getHook(ctx);
|
|
2989
|
+
if (!exists) {
|
|
2990
|
+
const s2 = signal(typeof initial === "function" ? initial() : initial);
|
|
2991
|
+
ctx.hooks[index] = s2;
|
|
2992
|
+
}
|
|
2993
|
+
const s = ctx.hooks[index];
|
|
2994
|
+
return [s, s.set];
|
|
2995
|
+
}
|
|
2986
2996
|
function useSignal(initial) {
|
|
2987
2997
|
const ctx = getCtx("useSignal");
|
|
2988
2998
|
const { index, exists } = getHook(ctx);
|
|
@@ -2999,6 +3009,97 @@ function useComputed(fn) {
|
|
|
2999
3009
|
}
|
|
3000
3010
|
return ctx.hooks[index];
|
|
3001
3011
|
}
|
|
3012
|
+
function useEffect(fn, deps) {
|
|
3013
|
+
const ctx = getCtx("useEffect");
|
|
3014
|
+
const { index, exists } = getHook(ctx);
|
|
3015
|
+
if (!exists) {
|
|
3016
|
+
ctx.hooks[index] = { cleanup: null, dispose: null };
|
|
3017
|
+
}
|
|
3018
|
+
if (__DEV__ && Array.isArray(deps) && deps.length > 0) {
|
|
3019
|
+
for (let i = 0; i < deps.length; i++) {
|
|
3020
|
+
const dep = deps[i];
|
|
3021
|
+
if (dep != null && typeof dep !== "function") {
|
|
3022
|
+
console.warn(
|
|
3023
|
+
`[what] useEffect dep at index ${i} is not a function. Did you mean to pass a signal? Use count instead of count()`
|
|
3024
|
+
);
|
|
3025
|
+
}
|
|
3026
|
+
}
|
|
3027
|
+
}
|
|
3028
|
+
const hook = ctx.hooks[index];
|
|
3029
|
+
if (hook.dispose) return;
|
|
3030
|
+
if (deps === void 0) {
|
|
3031
|
+
queueMicrotask(() => {
|
|
3032
|
+
if (ctx.disposed) return;
|
|
3033
|
+
hook.dispose = effect(() => {
|
|
3034
|
+
if (hook.cleanup) {
|
|
3035
|
+
try {
|
|
3036
|
+
hook.cleanup();
|
|
3037
|
+
} catch (e) {
|
|
3038
|
+
}
|
|
3039
|
+
hook.cleanup = null;
|
|
3040
|
+
}
|
|
3041
|
+
const result = fn();
|
|
3042
|
+
if (typeof result === "function") hook.cleanup = result;
|
|
3043
|
+
});
|
|
3044
|
+
ctx.effects = ctx.effects || [];
|
|
3045
|
+
ctx.effects.push(hook.dispose);
|
|
3046
|
+
});
|
|
3047
|
+
} else if (deps.length === 0) {
|
|
3048
|
+
queueMicrotask(() => {
|
|
3049
|
+
if (ctx.disposed) return;
|
|
3050
|
+
const result = fn();
|
|
3051
|
+
if (typeof result === "function") hook.cleanup = result;
|
|
3052
|
+
});
|
|
3053
|
+
hook.dispose = true;
|
|
3054
|
+
} else {
|
|
3055
|
+
queueMicrotask(() => {
|
|
3056
|
+
if (ctx.disposed) return;
|
|
3057
|
+
hook.dispose = effect(() => {
|
|
3058
|
+
for (let i = 0; i < deps.length; i++) {
|
|
3059
|
+
const dep = deps[i];
|
|
3060
|
+
if (typeof dep === "function" && dep._signal) {
|
|
3061
|
+
dep();
|
|
3062
|
+
}
|
|
3063
|
+
}
|
|
3064
|
+
if (hook.cleanup) {
|
|
3065
|
+
try {
|
|
3066
|
+
hook.cleanup();
|
|
3067
|
+
} catch (e) {
|
|
3068
|
+
}
|
|
3069
|
+
hook.cleanup = null;
|
|
3070
|
+
}
|
|
3071
|
+
const result = untrack(() => fn());
|
|
3072
|
+
if (typeof result === "function") hook.cleanup = result;
|
|
3073
|
+
});
|
|
3074
|
+
ctx.effects = ctx.effects || [];
|
|
3075
|
+
ctx.effects.push(hook.dispose);
|
|
3076
|
+
});
|
|
3077
|
+
}
|
|
3078
|
+
}
|
|
3079
|
+
function useMemo(fn, deps) {
|
|
3080
|
+
const ctx = getCtx("useMemo");
|
|
3081
|
+
const { index, exists } = getHook(ctx);
|
|
3082
|
+
if (!exists) {
|
|
3083
|
+
ctx.hooks[index] = { computed: computed(fn) };
|
|
3084
|
+
}
|
|
3085
|
+
return ctx.hooks[index].computed;
|
|
3086
|
+
}
|
|
3087
|
+
function useCallback(fn, deps) {
|
|
3088
|
+
const ctx = getCtx("useCallback");
|
|
3089
|
+
const { index, exists } = getHook(ctx);
|
|
3090
|
+
if (!exists) {
|
|
3091
|
+
ctx.hooks[index] = { callback: fn };
|
|
3092
|
+
}
|
|
3093
|
+
return ctx.hooks[index].callback;
|
|
3094
|
+
}
|
|
3095
|
+
function useRef(initial) {
|
|
3096
|
+
const ctx = getCtx("useRef");
|
|
3097
|
+
const { index, exists } = getHook(ctx);
|
|
3098
|
+
if (!exists) {
|
|
3099
|
+
ctx.hooks[index] = { current: initial };
|
|
3100
|
+
}
|
|
3101
|
+
return ctx.hooks[index];
|
|
3102
|
+
}
|
|
3002
3103
|
function useContext(context) {
|
|
3003
3104
|
let ctx = getCurrentComponent();
|
|
3004
3105
|
if (__DEV__ && !ctx) {
|
|
@@ -5972,10 +6073,12 @@ export {
|
|
|
5972
6073
|
useAriaChecked,
|
|
5973
6074
|
useAriaExpanded,
|
|
5974
6075
|
useAriaSelected,
|
|
6076
|
+
useCallback,
|
|
5975
6077
|
useClickOutside,
|
|
5976
6078
|
useComputed,
|
|
5977
6079
|
useContext,
|
|
5978
6080
|
useDescribedBy,
|
|
6081
|
+
useEffect,
|
|
5979
6082
|
useFetch,
|
|
5980
6083
|
useField,
|
|
5981
6084
|
useFocus,
|
|
@@ -5989,13 +6092,16 @@ export {
|
|
|
5989
6092
|
useLabelledBy,
|
|
5990
6093
|
useLocalStorage,
|
|
5991
6094
|
useMediaQuery,
|
|
6095
|
+
useMemo,
|
|
5992
6096
|
useQuery,
|
|
5993
6097
|
useReducer,
|
|
6098
|
+
useRef,
|
|
5994
6099
|
useRovingTabIndex,
|
|
5995
6100
|
useSWR,
|
|
5996
6101
|
useScheduledEffect,
|
|
5997
6102
|
useSignal,
|
|
5998
6103
|
useSkeleton,
|
|
6104
|
+
useState,
|
|
5999
6105
|
useTransition,
|
|
6000
6106
|
validateImports,
|
|
6001
6107
|
yupResolver,
|