@chhsiao1981/use-thunk 14.0.0 → 14.0.3

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.
@@ -2,7 +2,8 @@ import type { ModuleState, State } from '../states';
2
2
  import type { ActionOrThunk } from './ActionOrThunk';
3
3
  import type BaseAction from './baseAction';
4
4
  import type { Thunk } from './thunk';
5
- export type { Thunk, ActionOrThunk, BaseAction };
5
+ export type { ActionOrThunk, BaseAction, Thunk };
6
6
  export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>;
7
+ export type BaseActionFunc = (...params: any[]) => BaseAction;
7
8
  export type ThunkFunc<S extends State> = (...params: any[]) => Thunk<S>;
8
9
  export type getModuleState<S extends State> = () => ModuleState<S>;
package/dist/index.js CHANGED
@@ -96,7 +96,7 @@ var o = (e) => {
96
96
  context: s,
97
97
  refModuleState: o
98
98
  }, T.theList = Object.keys(T.theMap).sort(), console.info("registerThunk: done:", n);
99
- }, O = (e) => e.defaultID ?? "", k = (e, t) => {
99
+ }, O = (e, t = !1) => e.defaultID ?? (t ? c() : ""), k = (e, t) => {
100
100
  let n = t || O(e);
101
101
  return n && e.nodes[n] || null;
102
102
  }, A = (e, t) => {
@@ -105,7 +105,7 @@ var o = (e) => {
105
105
  let r = e.nodes[n];
106
106
  return r ? r.state : null;
107
107
  }, j = (e, t) => {
108
- let n = t || O(e) || c(), r = A(e, n);
108
+ let n = t || O(e, !0), r = A(e, n);
109
109
  if (r) return r;
110
110
  let i = o(e.defaultState), a = {
111
111
  id: n,
@@ -113,7 +113,7 @@ var o = (e) => {
113
113
  };
114
114
  return e.nodes[n] = a, e.isInitDefaultID || e.defaultID || (e.defaultID = n, e.isInitDefaultID = !0), i;
115
115
  }, M = (e, t) => {
116
- let [n, r] = e, i = t || O(n);
116
+ let [n, r] = e, i = t || O(n, !0);
117
117
  return [
118
118
  j(n, i),
119
119
  r,
@@ -1 +1 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("react"),require("react/jsx-runtime")):typeof define==`function`&&define.amd?define([`exports`,`react`,`react/jsx-runtime`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e[`@chhsiao1981/use-thunk`]={},e.React,e.ReactJSXRuntime))})(this,function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var r=e=>{if(typeof e!=`object`||!e)return e;if(e instanceof Date)return new Date(e.getTime());if(Array.isArray(e))return e.map(e=>r(e));let t={};for(let n in e)e.hasOwnProperty(n)&&(t[n]=r(e[n]));return t},i=1,a=()=>(i+=1,`${i}`),o=`@chhsiao1981/use-thunk/SET_DEFAULT_ID`,s=e=>({myID:e,type:o}),c=(e,t)=>{let{myID:n}=t;return Object.assign({},e,{defaultID:n,isInitDefaultID:!0})},l=`@chhsiao1981/use-thunk/INIT`,u=(e,t)=>({myID:e,type:l,state:t}),d=(e,t)=>{let{myID:n,state:r}=t,i={id:n,state:r},a=Object.assign({},e.nodes,{[n]:i});return Object.assign({},e,{nodes:a})},f=e=>(t,n,r,i,o)=>{let c=e.myID??a(),{state:l}=e;t(u(c,l));let{defaultID:d}=o();d||t(s(c))},p=`@chhsiao1981/use-thunk/UPDATE`,m=(e,t)=>({myID:e,type:p,data:t}),h=(e,t)=>{let{myID:n,data:r}=t,i=e.nodes[n];if(!i)return e;let a=Object.assign({},i.state,r),o=Object.assign({},i,{state:a}),s=Object.assign({},e.nodes,{[n]:o});return Object.assign({},e,{nodes:s})},g=`@chhsiao1981/use-thunk/REMOVE`,_=e=>({myID:e,type:g}),v=(e,t)=>{let{myID:n}=t;if(!e.nodes[n])return e;let r=Object.keys(e.nodes).filter(e=>e!==n).reduce((t,n)=>(t[n]=e.nodes[n],t),{}),i=Object.assign({},e,{nodes:r});return i.defaultID===n&&(i.defaultID=null),i},y=`@chhsiao1981/use-thunk/UPSERT`,b=(e,t)=>({myID:e,type:y,data:t}),x=(e,t)=>{let{myID:n,data:i}=t,a=e.nodes[n]??{id:n,state:r(e.defaultState)},o=Object.assign({},a.state,i),s=Object.assign({},a,{state:o}),c=Object.assign({},e.nodes,{[n]:s});return Object.assign({},e,{nodes:c})},S={theMap:{},theList:[]},C=e=>{let{modules:r,children:i}=e,a=r||S.theList;if(a.length===0)return i;let o=a[0],{context:s,refModuleState:c}=S.theMap[o],[l,u]=(0,t.useState)(c.current);c.current=l;let d=(0,t.useMemo)(()=>({refModuleState:c,setModuleState:u}),[l]),f=a.length===1?i:C({modules:a.slice(1),children:i});return(0,n.jsx)(s.Provider,{value:d,children:f})},w=e=>{let{name:n,defaultState:r}=e;if(S.theMap[n]){console.warn(`registerThunk: already init:`,n);return}let i={name:n,nodes:{},defaultState:r},a=()=>{},o={current:i},s=(0,t.createContext)({refModuleState:o,setModuleState:a});S.theMap[n]={context:s,refModuleState:o},S.theList=Object.keys(S.theMap).sort(),console.info(`registerThunk: done:`,n)},T=e=>e.defaultID??``,E=(e,t)=>{let n=t||T(e);return n&&e.nodes[n]||null},D=(e,t)=>{let n=t||T(e);if(!n)return null;let r=e.nodes[n];return r?r.state:null},O=(e,t)=>{let n=t||T(e)||a(),i=D(e,n);if(i)return i;let o=r(e.defaultState),s={id:n,state:o};return e.nodes[n]=s,e.isInitDefaultID||e.defaultID||(e.defaultID=n,e.isInitDefaultID=!0),o},k=(e,t)=>{let[n,r]=e,i=t||T(n);return[O(n,i),r,i]},A={[l]:d,[p]:h,[g]:v,[y]:x,[o]:c},j=()=>(e,t)=>t&&A[t.type]?A[t.type](e,t):e,M={init:f,update:m,remove:_},N=(e,t,n)=>(Object.keys(e).filter(t=>typeof e[t]==`function`).reduce((n,r)=>{if(n[r])return n;let i=e[r];return n[r]=(...e)=>t(i(...e)),n},n),Object.keys(M).reduce((e,n)=>{if(e[n])return e;let r=M[n];return e[n]=(...e)=>t(r(...e)),e},n),n),P=(e,n)=>{let{context:r}=S.theMap[n],{refModuleState:i,setModuleState:a}=(0,t.useContext)(r),o=(0,t.useCallback)(()=>i.current,[i]),s=(0,t.useCallback)(e=>{i.current=e,a(e)},[i,a]),c=(0,t.useCallback)(e=>D(o(),e),[o]),l=(0,t.useCallback)(e=>O(o(),e),[o]),u=(0,t.useCallback)(t=>e(o(),t),[e,o]),d=(0,t.useCallback)(e=>{if(typeof e==`function`){e(f,l,c,d,o);return}s(u(e))},[o,s,u]),f=(0,t.useCallback)((e,t)=>{if(typeof e==`string`){if(!t)return;s(u(b(e,t)));return}d(e)},[o,s,u]);return[i.current,f]},F={};e.ThunkContext=C,e.genID=a,e.getDefaultID=T,e.getNodeOrNull=E,e.getState=k,e.getStateByModule=O,e.getStateOrNullByModule=D,e.init=f,e.registerThunk=w,e.remove=_,e.setDefaultID=s,e.update=m,e.upsert=b,e.useThunk=e=>{let{name:n}=e,r=!F[n];r&&(F[n]={});let i=F[n],[a,o]=P((0,t.useMemo)(()=>j(),[]),n),s=(0,t.useMemo)(()=>[a,i],[a,i]);return r&&N(e,o,i),s}});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("react"),require("react/jsx-runtime")):typeof define==`function`&&define.amd?define([`exports`,`react`,`react/jsx-runtime`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e[`@chhsiao1981/use-thunk`]={},e.React,e.ReactJSXRuntime))})(this,function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var r=e=>{if(typeof e!=`object`||!e)return e;if(e instanceof Date)return new Date(e.getTime());if(Array.isArray(e))return e.map(e=>r(e));let t={};for(let n in e)e.hasOwnProperty(n)&&(t[n]=r(e[n]));return t},i=1,a=()=>(i+=1,`${i}`),o=`@chhsiao1981/use-thunk/SET_DEFAULT_ID`,s=e=>({myID:e,type:o}),c=(e,t)=>{let{myID:n}=t;return Object.assign({},e,{defaultID:n,isInitDefaultID:!0})},l=`@chhsiao1981/use-thunk/INIT`,u=(e,t)=>({myID:e,type:l,state:t}),d=(e,t)=>{let{myID:n,state:r}=t,i={id:n,state:r},a=Object.assign({},e.nodes,{[n]:i});return Object.assign({},e,{nodes:a})},f=e=>(t,n,r,i,o)=>{let c=e.myID??a(),{state:l}=e;t(u(c,l));let{defaultID:d}=o();d||t(s(c))},p=`@chhsiao1981/use-thunk/UPDATE`,m=(e,t)=>({myID:e,type:p,data:t}),h=(e,t)=>{let{myID:n,data:r}=t,i=e.nodes[n];if(!i)return e;let a=Object.assign({},i.state,r),o=Object.assign({},i,{state:a}),s=Object.assign({},e.nodes,{[n]:o});return Object.assign({},e,{nodes:s})},g=`@chhsiao1981/use-thunk/REMOVE`,_=e=>({myID:e,type:g}),v=(e,t)=>{let{myID:n}=t;if(!e.nodes[n])return e;let r=Object.keys(e.nodes).filter(e=>e!==n).reduce((t,n)=>(t[n]=e.nodes[n],t),{}),i=Object.assign({},e,{nodes:r});return i.defaultID===n&&(i.defaultID=null),i},y=`@chhsiao1981/use-thunk/UPSERT`,b=(e,t)=>({myID:e,type:y,data:t}),x=(e,t)=>{let{myID:n,data:i}=t,a=e.nodes[n]??{id:n,state:r(e.defaultState)},o=Object.assign({},a.state,i),s=Object.assign({},a,{state:o}),c=Object.assign({},e.nodes,{[n]:s});return Object.assign({},e,{nodes:c})},S={theMap:{},theList:[]},C=e=>{let{modules:r,children:i}=e,a=r||S.theList;if(a.length===0)return i;let o=a[0],{context:s,refModuleState:c}=S.theMap[o],[l,u]=(0,t.useState)(c.current);c.current=l;let d=(0,t.useMemo)(()=>({refModuleState:c,setModuleState:u}),[l]),f=a.length===1?i:C({modules:a.slice(1),children:i});return(0,n.jsx)(s.Provider,{value:d,children:f})},w=e=>{let{name:n,defaultState:r}=e;if(S.theMap[n]){console.warn(`registerThunk: already init:`,n);return}let i={name:n,nodes:{},defaultState:r},a=()=>{},o={current:i},s=(0,t.createContext)({refModuleState:o,setModuleState:a});S.theMap[n]={context:s,refModuleState:o},S.theList=Object.keys(S.theMap).sort(),console.info(`registerThunk: done:`,n)},T=(e,t=!1)=>e.defaultID??(t?a():``),E=(e,t)=>{let n=t||T(e);return n&&e.nodes[n]||null},D=(e,t)=>{let n=t||T(e);if(!n)return null;let r=e.nodes[n];return r?r.state:null},O=(e,t)=>{let n=t||T(e,!0),i=D(e,n);if(i)return i;let a=r(e.defaultState),o={id:n,state:a};return e.nodes[n]=o,e.isInitDefaultID||e.defaultID||(e.defaultID=n,e.isInitDefaultID=!0),a},k=(e,t)=>{let[n,r]=e,i=t||T(n,!0);return[O(n,i),r,i]},A={[l]:d,[p]:h,[g]:v,[y]:x,[o]:c},j=()=>(e,t)=>t&&A[t.type]?A[t.type](e,t):e,M={init:f,update:m,remove:_},N=(e,t,n)=>(Object.keys(e).filter(t=>typeof e[t]==`function`).reduce((n,r)=>{if(n[r])return n;let i=e[r];return n[r]=(...e)=>t(i(...e)),n},n),Object.keys(M).reduce((e,n)=>{if(e[n])return e;let r=M[n];return e[n]=(...e)=>t(r(...e)),e},n),n),P=(e,n)=>{let{context:r}=S.theMap[n],{refModuleState:i,setModuleState:a}=(0,t.useContext)(r),o=(0,t.useCallback)(()=>i.current,[i]),s=(0,t.useCallback)(e=>{i.current=e,a(e)},[i,a]),c=(0,t.useCallback)(e=>D(o(),e),[o]),l=(0,t.useCallback)(e=>O(o(),e),[o]),u=(0,t.useCallback)(t=>e(o(),t),[e,o]),d=(0,t.useCallback)(e=>{if(typeof e==`function`){e(f,l,c,d,o);return}s(u(e))},[o,s,u]),f=(0,t.useCallback)((e,t)=>{if(typeof e==`string`){if(!t)return;s(u(b(e,t)));return}d(e)},[o,s,u]);return[i.current,f]},F={};e.ThunkContext=C,e.genID=a,e.getDefaultID=T,e.getNodeOrNull=E,e.getState=k,e.getStateByModule=O,e.getStateOrNullByModule=D,e.init=f,e.registerThunk=w,e.remove=_,e.setDefaultID=s,e.update=m,e.upsert=b,e.useThunk=e=>{let{name:n}=e,r=!F[n];r&&(F[n]={});let i=F[n],[a,o]=P((0,t.useMemo)(()=>j(),[]),n),s=(0,t.useMemo)(()=>[a,i],[a,i]);return r&&N(e,o,i),s}});
@@ -2,8 +2,8 @@ import type { setMap } from '../set';
2
2
  import type { doModule } from '../thunkModule';
3
3
  import type { UseThunk } from '../useThunk';
4
4
  import type { ModuleState, NodeState, NodeStateMap, State } from './types';
5
- export type { ModuleState, NodeState, State, NodeStateMap };
6
- export declare const getDefaultID: <S extends State>(moduleState: ModuleState<S>) => string;
5
+ export type { ModuleState, NodeState, NodeStateMap, State };
6
+ export declare const getDefaultID: <S extends State>(moduleState: ModuleState<S>, isMust?: boolean) => string;
7
7
  export declare const getNodeOrNull: <S extends State>(moduleState: ModuleState<S>, myID?: string) => Readonly<NodeState<S> | null>;
8
8
  export declare const getStateOrNullByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => Readonly<S | null>;
9
9
  export declare const getStateByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => Readonly<S>;
@@ -1,13 +1,13 @@
1
- import type { ThunkFunc } from '../action';
1
+ import type { BaseActionFunc, ThunkFunc } from '../action';
2
2
  import type { State } from '../states';
3
3
  import { DEFAULT_DO_MODULE, type defaultDoModule } from './defaultDoModule';
4
4
  export { DEFAULT_DO_MODULE, type defaultDoModule };
5
5
  export interface doModule<S extends State> {
6
- [action: string]: ThunkFunc<S>;
6
+ [action: string]: ThunkFunc<S> | BaseActionFunc;
7
7
  }
8
8
  export type ThunkModule<S extends State> = {
9
9
  name: string;
10
10
  defaultState: S;
11
- [action: string]: ThunkFunc<S> | string | S;
11
+ [action: string]: ThunkFunc<S> | string | S | BaseActionFunc;
12
12
  };
13
13
  export type toDoModule<T extends ThunkModule<any>> = Omit<T, 'name' | 'defaultState'>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chhsiao1981/use-thunk",
3
- "version": "14.0.0",
3
+ "version": "14.0.3",
4
4
  "type": "module",
5
5
  "description": "A framework easily using useThunk to manage the data-state.",
6
6
  "homepage": "https://github.com/chhsiao1981/use-thunk",
@@ -42,7 +42,7 @@
42
42
  "license": "MIT",
43
43
  "devDependencies": {
44
44
  "@babel/core": "^7.29.0",
45
- "@biomejs/biome": "^2.5.1",
45
+ "@biomejs/biome": "2.3.1",
46
46
  "@rolldown/plugin-babel": "^0.2.3",
47
47
  "@types/babel__core": "^7.20.5",
48
48
  "@types/node": "^25.9.3",