UNPKG

@undermuz/use-form

Version:
79 lines (77 loc) 2.83 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/utils/useReducer.ts var useReducer_exports = {}; __export(useReducer_exports, { useReducer: () => useReducer }); module.exports = __toCommonJS(useReducer_exports); var import_react = require("react"); function compose(...fns) { if (fns.length === 0) return (arg) => arg; if (fns.length === 1) return fns[0]; return fns.reduce( (a, b) => (...args) => a(b(...args)) ); } var useReducer = (reducer, initialState, middlewares = [], options) => { const initialStateRef = (0, import_react.useRef)(null); if (initialStateRef.current === null) { initialStateRef.current = { ...initialState }; if (options == null ? void 0 : options.debug) console.log("[useReducer][initialState]", initialStateRef.current); } const draftState = (0, import_react.useRef)(initialState); const [state, setState] = (0, import_react.useState)(() => { return draftState.current; }); const enhancedDispatch = (0, import_react.useMemo)(() => { const store2 = { getState: () => draftState.current, dispatch: (...args) => _enhancedDispatch(...args) }; const dispatch = (action) => { draftState.current = reducer(draftState.current, action); setState(draftState.current); return action; }; const chain = middlewares.map((middleware) => middleware(store2)); const _enhancedDispatch = compose(...chain)(dispatch); return _enhancedDispatch; }, []); const store = (0, import_react.useMemo)(() => { return { getState: () => draftState.current, dispatch: (...args) => enhancedDispatch(...args) }; }, []); const reset = (0, import_react.useCallback)(() => { draftState.current = initialStateRef.current; if (options == null ? void 0 : options.debug) console.log("[useReducer][Reset]", initialStateRef.current); setState(draftState.current); }, []); return [state, enhancedDispatch, { store, reset }]; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useReducer });