@chhsiao1981/use-thunk 12.0.0 → 14.0.0

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.
Files changed (101) hide show
  1. package/README.md +211 -122
  2. package/{types → dist}/action/ActionOrThunk.d.ts +1 -1
  3. package/{types → dist}/action/index.d.ts +2 -2
  4. package/dist/action/thunk.d.ts +5 -0
  5. package/dist/defaultThunks/index.d.ts +10 -0
  6. package/dist/defaultThunks/init/index.d.ts +9 -0
  7. package/{types → dist/defaultThunks}/init/initCore.d.ts +2 -2
  8. package/{types → dist/defaultThunks}/remove.d.ts +2 -2
  9. package/{types → dist/defaultThunks}/setDefaultID.d.ts +2 -2
  10. package/{types → dist/defaultThunks}/update.d.ts +2 -2
  11. package/dist/defaultThunks/upsert.d.ts +5 -0
  12. package/dist/dispatch.d.ts +3 -3
  13. package/dist/get.d.ts +3 -0
  14. package/dist/index.d.ts +11 -14
  15. package/dist/index.js +173 -0
  16. package/dist/index.umd.cjs +1 -0
  17. package/dist/reducer/createReducer.d.ts +3 -0
  18. package/dist/reducer/defaultReduceMap.d.ts +2 -0
  19. package/{types/reducer.d.ts → dist/reducer/index.d.ts} +4 -2
  20. package/dist/reducer/reduceMap.d.ts +5 -0
  21. package/{types/createThunk.d.ts → dist/registerThunk.d.ts} +1 -1
  22. package/dist/set/index.d.ts +5 -0
  23. package/dist/set/setMap.d.ts +16 -0
  24. package/dist/states/index.d.ts +10 -0
  25. package/{types/stateTypes.d.ts → dist/states/types.d.ts} +5 -4
  26. package/dist/thunkContext/ThunkContext.d.ts +7 -0
  27. package/dist/thunkContext/index.d.ts +6 -0
  28. package/{types → dist/thunkContext}/thunkContextMap.d.ts +2 -2
  29. package/{types/thunkContextTypes.d.ts → dist/thunkContext/types.d.ts} +1 -1
  30. package/dist/thunkModule/defaultDoModule.d.ts +5 -0
  31. package/dist/thunkModule/index.d.ts +13 -0
  32. package/dist/useThunk/index.d.ts +9 -0
  33. package/{types → dist/useThunk}/useThunkReducer.d.ts +3 -3
  34. package/dist/utils/deepCopy.d.ts +2 -0
  35. package/dist/utils/genID.d.ts +1 -0
  36. package/dist/utils/index.d.ts +3 -0
  37. package/package.json +35 -30
  38. package/dist/ThunkContext.d.ts +0 -7
  39. package/dist/action.d.ts +0 -11
  40. package/dist/createReducer.d.ts +0 -4
  41. package/dist/createThunk.d.ts +0 -4
  42. package/dist/dispatchFuncMap.d.ts +0 -18
  43. package/dist/genUUID.d.ts +0 -1
  44. package/dist/init.d.ts +0 -16
  45. package/dist/reduceMap.d.ts +0 -6
  46. package/dist/reducer.d.ts +0 -5
  47. package/dist/remove.d.ts +0 -5
  48. package/dist/setDefaultID.d.ts +0 -5
  49. package/dist/stateTypes.d.ts +0 -16
  50. package/dist/states.d.ts +0 -9
  51. package/dist/thunk.d.ts +0 -15
  52. package/dist/thunkContextMap.d.ts +0 -15
  53. package/dist/thunkContextTypes.d.ts +0 -8
  54. package/dist/thunkModuleFuncMap.d.ts +0 -7
  55. package/dist/update.d.ts +0 -6
  56. package/dist/useThunk.d.ts +0 -12
  57. package/dist/useThunkReducer.d.ts +0 -17
  58. package/src/ThunkContext.tsx +0 -46
  59. package/src/action/ActionOrThunk.ts +0 -5
  60. package/src/action/baseAction.ts +0 -6
  61. package/src/action/index.ts +0 -16
  62. package/src/action/thunk.ts +0 -9
  63. package/src/createReducer.ts +0 -24
  64. package/src/createThunk.ts +0 -24
  65. package/src/genUUID.ts +0 -38
  66. package/src/get.ts +0 -3
  67. package/src/index.ts +0 -42
  68. package/src/init/index.ts +0 -26
  69. package/src/init/initCore.ts +0 -31
  70. package/src/reduceMap.ts +0 -22
  71. package/src/reducer.ts +0 -9
  72. package/src/remove.ts +0 -35
  73. package/src/set.ts +0 -7
  74. package/src/setDefaultID.ts +0 -17
  75. package/src/setMap.ts +0 -65
  76. package/src/stateTypes.ts +0 -22
  77. package/src/states.ts +0 -50
  78. package/src/thunkContextMap.ts +0 -22
  79. package/src/thunkContextTypes.ts +0 -8
  80. package/src/thunkModule/defaultThunkModuleFuncMap.ts +0 -15
  81. package/src/thunkModule/index.ts +0 -20
  82. package/src/update.ts +0 -26
  83. package/src/useThunk.ts +0 -50
  84. package/src/useThunkReducer.ts +0 -70
  85. package/types/ThunkContext.d.ts +0 -7
  86. package/types/action/thunk.d.ts +0 -3
  87. package/types/createReducer.d.ts +0 -4
  88. package/types/dispatch.d.ts +0 -4
  89. package/types/dispatchFuncMap.d.ts +0 -17
  90. package/types/genUUID.d.ts +0 -1
  91. package/types/get.d.ts +0 -2
  92. package/types/index.d.ts +0 -16
  93. package/types/init/index.d.ts +0 -7
  94. package/types/reduceMap.d.ts +0 -6
  95. package/types/set.d.ts +0 -4
  96. package/types/setMap.d.ts +0 -17
  97. package/types/states.d.ts +0 -9
  98. package/types/thunkModule/defaultThunkModuleFuncMap.d.ts +0 -5
  99. package/types/thunkModule/index.d.ts +0 -15
  100. package/types/useThunk.d.ts +0 -9
  101. /package/{types → dist}/action/baseAction.d.ts +0 -0
package/dist/index.js ADDED
@@ -0,0 +1,173 @@
1
+ import { createContext as e, useCallback as t, useContext as n, useMemo as r, useState as i } from "react";
2
+ import { jsx as a } from "react/jsx-runtime";
3
+ //#region src/utils/deepCopy.ts
4
+ var o = (e) => {
5
+ if (typeof e != "object" || !e) return e;
6
+ if (e instanceof Date) return new Date(e.getTime());
7
+ if (Array.isArray(e)) return e.map((e) => o(e));
8
+ let t = {};
9
+ for (let n in e) e.hasOwnProperty(n) && (t[n] = o(e[n]));
10
+ return t;
11
+ }, s = 1, c = () => (s += 1, `${s}`), l = "@chhsiao1981/use-thunk/SET_DEFAULT_ID", u = (e) => ({
12
+ myID: e,
13
+ type: l
14
+ }), d = (e, t) => {
15
+ let { myID: n } = t;
16
+ return Object.assign({}, e, {
17
+ defaultID: n,
18
+ isInitDefaultID: !0
19
+ });
20
+ }, f = "@chhsiao1981/use-thunk/INIT", p = (e, t) => ({
21
+ myID: e,
22
+ type: f,
23
+ state: t
24
+ }), m = (e, t) => {
25
+ let { myID: n, state: r } = t, i = {
26
+ id: n,
27
+ state: r
28
+ }, a = Object.assign({}, e.nodes, { [n]: i });
29
+ return Object.assign({}, e, { nodes: a });
30
+ }, h = (e) => (t, n, r, i, a) => {
31
+ let o = e.myID ?? c(), { state: s } = e;
32
+ t(p(o, s));
33
+ let { defaultID: l } = a();
34
+ l || t(u(o));
35
+ }, g = "@chhsiao1981/use-thunk/UPDATE", _ = (e, t) => ({
36
+ myID: e,
37
+ type: g,
38
+ data: t
39
+ }), v = (e, t) => {
40
+ let { myID: n, data: r } = t, i = e.nodes[n];
41
+ if (!i) return e;
42
+ let a = Object.assign({}, i.state, r), o = Object.assign({}, i, { state: a }), s = Object.assign({}, e.nodes, { [n]: o });
43
+ return Object.assign({}, e, { nodes: s });
44
+ }, y = "@chhsiao1981/use-thunk/REMOVE", b = (e) => ({
45
+ myID: e,
46
+ type: y
47
+ }), x = (e, t) => {
48
+ let { myID: n } = t;
49
+ if (!e.nodes[n]) return e;
50
+ 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 });
51
+ return i.defaultID === n && (i.defaultID = null), i;
52
+ }, S = "@chhsiao1981/use-thunk/UPSERT", C = (e, t) => ({
53
+ myID: e,
54
+ type: S,
55
+ data: t
56
+ }), w = (e, t) => {
57
+ let { myID: n, data: r } = t, i = e.nodes[n] ?? {
58
+ id: n,
59
+ state: o(e.defaultState)
60
+ }, a = Object.assign({}, i.state, r), s = Object.assign({}, i, { state: a }), c = Object.assign({}, e.nodes, { [n]: s });
61
+ return Object.assign({}, e, { nodes: c });
62
+ }, T = {
63
+ theMap: {},
64
+ theList: []
65
+ }, E = (e) => {
66
+ let { modules: t, children: n } = e, o = t || T.theList;
67
+ if (o.length === 0) return n;
68
+ let s = o[0], { context: c, refModuleState: l } = T.theMap[s], [u, d] = i(l.current);
69
+ l.current = u;
70
+ let f = r(() => ({
71
+ refModuleState: l,
72
+ setModuleState: d
73
+ }), [u]), p = o.length === 1 ? n : E({
74
+ modules: o.slice(1),
75
+ children: n
76
+ });
77
+ return /* @__PURE__ */ a(c.Provider, {
78
+ value: f,
79
+ children: p
80
+ });
81
+ }, D = (t) => {
82
+ let { name: n, defaultState: r } = t;
83
+ if (T.theMap[n]) {
84
+ console.warn("registerThunk: already init:", n);
85
+ return;
86
+ }
87
+ let i = {
88
+ name: n,
89
+ nodes: {},
90
+ defaultState: r
91
+ }, a = () => {}, o = { current: i }, s = e({
92
+ refModuleState: o,
93
+ setModuleState: a
94
+ });
95
+ T.theMap[n] = {
96
+ context: s,
97
+ refModuleState: o
98
+ }, T.theList = Object.keys(T.theMap).sort(), console.info("registerThunk: done:", n);
99
+ }, O = (e) => e.defaultID ?? "", k = (e, t) => {
100
+ let n = t || O(e);
101
+ return n && e.nodes[n] || null;
102
+ }, A = (e, t) => {
103
+ let n = t || O(e);
104
+ if (!n) return null;
105
+ let r = e.nodes[n];
106
+ return r ? r.state : null;
107
+ }, j = (e, t) => {
108
+ let n = t || O(e) || c(), r = A(e, n);
109
+ if (r) return r;
110
+ let i = o(e.defaultState), a = {
111
+ id: n,
112
+ state: i
113
+ };
114
+ return e.nodes[n] = a, e.isInitDefaultID || e.defaultID || (e.defaultID = n, e.isInitDefaultID = !0), i;
115
+ }, M = (e, t) => {
116
+ let [n, r] = e, i = t || O(n);
117
+ return [
118
+ j(n, i),
119
+ r,
120
+ i
121
+ ];
122
+ }, N = {
123
+ [f]: m,
124
+ [g]: v,
125
+ [y]: x,
126
+ [S]: w,
127
+ [l]: d
128
+ }, P = () => (e, t) => t && N[t.type] ? N[t.type](e, t) : e, F = {
129
+ init: h,
130
+ update: _,
131
+ remove: b
132
+ }, I = (e, t, n) => (Object.keys(e).filter((t) => typeof e[t] == "function").reduce((n, r) => {
133
+ if (n[r]) return n;
134
+ let i = e[r];
135
+ return n[r] = (...e) => t(i(...e)), n;
136
+ }, n), Object.keys(F).reduce((e, n) => {
137
+ if (e[n]) return e;
138
+ let r = F[n];
139
+ return e[n] = (...e) => t(r(...e)), e;
140
+ }, n), n), L = (e, r) => {
141
+ let { context: i } = T.theMap[r], { refModuleState: a, setModuleState: o } = n(i), s = t(() => a.current, [a]), c = t((e) => {
142
+ a.current = e, o(e);
143
+ }, [a, o]), l = t((e) => A(s(), e), [s]), u = t((e) => j(s(), e), [s]), d = t((t) => e(s(), t), [e, s]), f = t((e) => {
144
+ if (typeof e == "function") {
145
+ e(p, u, l, f, s);
146
+ return;
147
+ }
148
+ c(d(e));
149
+ }, [
150
+ s,
151
+ c,
152
+ d
153
+ ]), p = t((e, t) => {
154
+ if (typeof e == "string") {
155
+ if (!t) return;
156
+ c(d(C(e, t)));
157
+ return;
158
+ }
159
+ f(e);
160
+ }, [
161
+ s,
162
+ c,
163
+ d
164
+ ]);
165
+ return [a.current, p];
166
+ }, R = {}, z = (e) => {
167
+ let { name: t } = e, n = !R[t];
168
+ n && (R[t] = {});
169
+ let i = R[t], [a, o] = L(r(() => P(), []), t), s = r(() => [a, i], [a, i]);
170
+ return n && I(e, o, i), s;
171
+ };
172
+ //#endregion
173
+ export { E as ThunkContext, c as genID, O as getDefaultID, k as getNodeOrNull, M as getState, j as getStateByModule, A as getStateOrNullByModule, h as init, D as registerThunk, b as remove, u as setDefaultID, _ as update, C as upsert, z as useThunk };
@@ -0,0 +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}});
@@ -0,0 +1,3 @@
1
+ import type { State } from '../states';
2
+ import type { Reducer } from './index';
3
+ export declare const createReducer: <S extends State>() => Reducer<S>;
@@ -0,0 +1,2 @@
1
+ import type { ReduceMap } from './reduceMap';
2
+ export declare const DEFAULT_REDUCE_MAP: ReduceMap<any>;
@@ -1,5 +1,7 @@
1
1
  import type { Reducer as rReducer } from 'react';
2
- import type BaseAction from './action/baseAction';
3
- import type { ModuleState, State } from './stateTypes';
2
+ import type { BaseAction } from '../action';
3
+ import type { ModuleState, State } from '../states';
4
+ import { createReducer } from './createReducer';
5
+ export { createReducer };
4
6
  export type Reducer<S extends State> = rReducer<ModuleState<S>, BaseAction>;
5
7
  export type ReduceFunc<S extends State> = (state: ModuleState<S>, action: BaseAction) => ModuleState<S>;
@@ -0,0 +1,5 @@
1
+ import type { State } from '../states';
2
+ import type { ReduceFunc } from './index';
3
+ export interface ReduceMap<S extends State> {
4
+ [type: string]: ReduceFunc<S>;
5
+ }
@@ -1,4 +1,4 @@
1
- import type { State } from './stateTypes';
1
+ import type { State } from './states';
2
2
  import type { ThunkModule } from './thunkModule';
3
3
  declare const _default: <S extends State>(theDo: ThunkModule<S>) => void;
4
4
  export default _default;
@@ -0,0 +1,5 @@
1
+ import type { ActionOrThunk } from '../action';
2
+ import type { State } from '../states';
3
+ import { constructSetMap, type DefaultSetMap, type setMap, type setMapByModuleMap } from './setMap';
4
+ export { constructSetMap, type DefaultSetMap, type setMap, type setMapByModuleMap };
5
+ export type set<S extends State> = (actionOrID: ActionOrThunk<S> | string, data?: Partial<S>) => void;
@@ -0,0 +1,16 @@
1
+ import type { set } from '../set';
2
+ import type { State } from '../states';
3
+ import type { doModule, ThunkModule } from '../thunkModule';
4
+ import { type defaultDoModule } from '../thunkModule';
5
+ type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
6
+ export type setMap<S extends State, T extends doModule<S>> = {
7
+ [action in keyof T]: VoidReturnType<T[action]>;
8
+ } & Omit<DefaultSetMap, keyof T>;
9
+ export type DefaultSetMap = {
10
+ [action in keyof defaultDoModule]: VoidReturnType<defaultDoModule[action]>;
11
+ };
12
+ export interface setMapByModuleMap<S extends State, T extends doModule<S>> {
13
+ [moduleMap: string]: setMap<S, T>;
14
+ }
15
+ export declare const constructSetMap: <S extends State, T extends doModule<S>>(theDo: ThunkModule<S>, set: set<S>, setMap: setMap<S, T>) => setMap<S, T>;
16
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { setMap } from '../set';
2
+ import type { doModule } from '../thunkModule';
3
+ import type { UseThunk } from '../useThunk';
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;
7
+ export declare const getNodeOrNull: <S extends State>(moduleState: ModuleState<S>, myID?: string) => Readonly<NodeState<S> | null>;
8
+ export declare const getStateOrNullByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => Readonly<S | null>;
9
+ export declare const getStateByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => Readonly<S>;
10
+ export declare const getState: <S extends State, R extends doModule<S>>(theUseThunk: UseThunk<S, R>, myID?: string) => [Readonly<S>, setMap<S, R>, string];
@@ -5,12 +5,13 @@ export type NodeState<S extends State> = {
5
5
  id: string;
6
6
  state: S;
7
7
  };
8
- export type NodeStateMap<S extends State> = {
9
- [key: string]: NodeState<S>;
10
- };
11
8
  export type ModuleState<S extends State> = {
12
9
  name: string;
13
- defaultID?: string | null;
14
10
  nodes: NodeStateMap<S>;
15
11
  defaultState: S;
12
+ defaultID?: string | null;
13
+ isInitDefaultID?: boolean;
14
+ };
15
+ export type NodeStateMap<S extends State> = {
16
+ [key: string]: NodeState<S>;
16
17
  };
@@ -0,0 +1,7 @@
1
+ import { type JSX, type ReactNode } from 'react';
2
+ type Props = {
3
+ modules?: string[];
4
+ children?: ReactNode;
5
+ };
6
+ declare const ThunkContext: (props: Props) => JSX.Element;
7
+ export default ThunkContext;
@@ -0,0 +1,6 @@
1
+ import type { Context } from './types';
2
+ export type { Context };
3
+ import { THUNK_CONTEXT_MAP, type ThunkContextMap } from './thunkContextMap';
4
+ export { THUNK_CONTEXT_MAP, type ThunkContextMap };
5
+ import ThunkContext from './ThunkContext';
6
+ export { ThunkContext };
@@ -1,6 +1,6 @@
1
1
  import type { Context as rContext } from 'react';
2
- import type { ModuleState } from './stateTypes';
3
- import type { Context } from './thunkContextTypes';
2
+ import type { ModuleState } from '../states';
3
+ import type { Context } from './types';
4
4
  export type ThunkContextMap = {
5
5
  theMap: {
6
6
  [moduleName: string]: {
@@ -1,5 +1,5 @@
1
1
  import type { Dispatch, SetStateAction } from 'react';
2
- import type { ModuleState, State } from './stateTypes';
2
+ import type { ModuleState, State } from '../states';
3
3
  export type Context<S extends State> = {
4
4
  refModuleState: {
5
5
  current: ModuleState<S>;
@@ -0,0 +1,5 @@
1
+ import type { ActionFunc } from '../action';
2
+ export declare const DEFAULT_DO_MODULE: defaultDoModule;
3
+ export type defaultDoModule = {
4
+ [action: string]: ActionFunc<any>;
5
+ };
@@ -0,0 +1,13 @@
1
+ import type { ThunkFunc } from '../action';
2
+ import type { State } from '../states';
3
+ import { DEFAULT_DO_MODULE, type defaultDoModule } from './defaultDoModule';
4
+ export { DEFAULT_DO_MODULE, type defaultDoModule };
5
+ export interface doModule<S extends State> {
6
+ [action: string]: ThunkFunc<S>;
7
+ }
8
+ export type ThunkModule<S extends State> = {
9
+ name: string;
10
+ defaultState: S;
11
+ [action: string]: ThunkFunc<S> | string | S;
12
+ };
13
+ export type toDoModule<T extends ThunkModule<any>> = Omit<T, 'name' | 'defaultState'>;
@@ -0,0 +1,9 @@
1
+ import { type setMap } from '../set';
2
+ import type { ModuleState, State } from '../states';
3
+ import type { doModule, ThunkModule } from '../thunkModule';
4
+ export type UseThunk<S extends State, R extends doModule<S>> = [Readonly<ModuleState<S>>, setMap<S, R>];
5
+ /**********
6
+ * useThunk
7
+ **********/
8
+ declare const _default: <S extends State, R extends doModule<S>>(theDo: ThunkModule<S>) => UseThunk<S, R>;
9
+ export default _default;
@@ -1,6 +1,6 @@
1
- import type { Reducer } from './reducer';
2
- import type { set } from './set';
3
- import type { ModuleState, State } from './stateTypes';
1
+ import type { Reducer } from '../reducer';
2
+ import type { set } from '../set';
3
+ import { type ModuleState, type State } from '../states';
4
4
  /**
5
5
  * useThunkReducer
6
6
  *
@@ -0,0 +1,2 @@
1
+ declare const deepCopy: (obj: any) => any;
2
+ export default deepCopy;
@@ -0,0 +1 @@
1
+ export declare const genID: () => string;
@@ -0,0 +1,3 @@
1
+ import deepCopy from './deepCopy';
2
+ import { genID } from './genID';
3
+ export { deepCopy, genID };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chhsiao1981/use-thunk",
3
- "version": "12.0.0",
3
+ "version": "14.0.0",
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",
@@ -8,12 +8,19 @@
8
8
  "type": "git",
9
9
  "url": "https://github.com/chhsiao1981/use-thunk"
10
10
  },
11
- "main": "src/index.ts",
12
- "types": "types/index.d.ts",
11
+ "main": "dist/index.umd.cjs",
12
+ "module": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js",
18
+ "require": "./dist/index.umd.cjs",
19
+ "default": "./dist/index.umd.cjs"
20
+ }
21
+ },
13
22
  "files": [
14
- "src",
15
- "dist",
16
- "types"
23
+ "dist"
17
24
  ],
18
25
  "author": {
19
26
  "name": "Chuan-Heng Hsiao",
@@ -24,40 +31,38 @@
24
31
  "useThunk"
25
32
  ],
26
33
  "scripts": {
27
- "clean:lib": "rimraf ./dist/* ./types/*",
28
34
  "compile:lib": "vite build",
29
- "build:lib": "npm run clean:lib && npm run types && npm run compile:lib",
30
- "build": "npm run build:lib",
35
+ "compile:types": "tsc -b",
36
+ "build": "npm run compile:lib && npm run compile:types",
37
+
31
38
  "lint": "biome lint --error-on-warnings",
32
39
  "test": "vitest run",
33
- "types": "tsc -b",
34
- "coverage": "vitest run --coverage --reporter=junit --outputFile=test-report.junit.xml",
35
- "tsd": "npx -p typescript tsc src/*.ts --declaration --allowJs --emitDeclarationOnly --strict --jsx react-jsx --outDir types"
40
+ "coverage": "vitest run --coverage --reporter=junit --outputFile=test-report.junit.xml"
36
41
  },
37
42
  "license": "MIT",
38
43
  "devDependencies": {
39
- "@codecov/vite-plugin": "^1.9.1",
40
- "@eslint/js": "^9.39.4",
41
- "@types/node": "^25.5.0",
42
- "@types/react": ">=18.3.1",
44
+ "@babel/core": "^7.29.0",
45
+ "@biomejs/biome": "^2.5.1",
46
+ "@rolldown/plugin-babel": "^0.2.3",
47
+ "@types/babel__core": "^7.20.5",
48
+ "@types/node": "^25.9.3",
43
49
  "@types/react-dom": ">=18.3.1",
44
- "@vitejs/plugin-react-swc": "^4.3.0",
45
- "@vitest/coverage-v8": "^4.1.0",
46
- "eslint": "^9.39.4",
47
- "eslint-plugin-react-hooks": "^7.0.1",
48
- "eslint-plugin-react-refresh": "^0.5.2",
49
- "globals": "^17.4.0",
50
- "happy-dom": "^20.8.4",
51
- "jiti": "^2.6.1",
50
+ "@vitejs/plugin-react": "^6.0.1",
51
+ "@vitest/coverage-v8": "^4.1.8",
52
+ "babel-plugin-react-compiler": "^1.0.0",
53
+ "globals": "^17.6.0",
54
+ "happy-dom": "^20.10.2",
55
+ "jiti": "^2.7.0",
52
56
  "react-dom": ">=18.3.1",
53
- "rimraf": "^6.1.3",
54
57
  "typescript": "~5.9.3",
55
- "typescript-eslint": "^8.57.1",
56
- "vite": "^6.4.1",
57
- "vitest": "^4.1.0"
58
+ "vite": "^8.0.16",
59
+ "vitest": "^4.1.8"
60
+ },
61
+ "dependencies": {
62
+ "react-compiler-runtime": "^1.0.0"
58
63
  },
59
64
  "peerDependencies": {
60
- "react": ">=18.3.1",
61
- "uuid": "^14.0.0"
65
+ "@types/react": ">=18.3.1",
66
+ "react": ">=18.3.1"
62
67
  }
63
68
  }
@@ -1,7 +0,0 @@
1
- import { type ReactNode } from 'react';
2
- type Props = {
3
- classes?: string[];
4
- children?: ReactNode;
5
- };
6
- declare const ThunkContext: (props: Props) => ReactNode;
7
- export default ThunkContext;
package/dist/action.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import type { ClassState, State } from './stateTypes';
2
- import type { ActionOrThunk as rActionOrThunk, Thunk as rThunk } from './useThunkReducer';
3
- export interface BaseAction {
4
- myID: string;
5
- type: string;
6
- [key: string]: unknown;
7
- }
8
- export type Thunk<S extends State> = rThunk<S, BaseAction>;
9
- export type ActionOrThunk<S extends State> = rActionOrThunk<S, BaseAction>;
10
- export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>;
11
- export type GetClassState<S extends State> = () => ClassState<S>;
@@ -1,4 +0,0 @@
1
- import { type ReduceMap } from './reduceMap';
2
- import type { Reducer } from './reducer';
3
- import type { State } from './stateTypes';
4
- export declare const createReducer: <S extends State>(reduceMap?: ReduceMap<S>) => Reducer<S>;
@@ -1,4 +0,0 @@
1
- import type { State } from './stateTypes';
2
- import type { ThunkModule } from './thunk';
3
- export default function createThunk<S extends State>(theDo: ThunkModule<S>): void;
4
- export declare function registerThunk<S extends State>(theDo: ThunkModule<S>): void;
@@ -1,18 +0,0 @@
1
- import type { BaseAction } from './action';
2
- import type { State } from './stateTypes';
3
- import type { ThunkModule, ThunkModuleFunc } from './thunk';
4
- import { type DefaultThunkModuleFuncMap } from './thunkModuleFuncMap';
5
- import type { Thunk as rThunk } from './useThunkReducer';
6
- type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
7
- export type DispatchFuncMap<S extends State, T extends ThunkModuleFunc<S>> = {
8
- [action in keyof T]: VoidReturnType<T[action]>;
9
- } & Omit<DefaultDispatchFuncMap, keyof T>;
10
- export type DefaultDispatchFuncMap = {
11
- [action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>;
12
- };
13
- export interface DispatchFuncMapByClassMap<S extends State, T extends ThunkModuleFunc<S>> {
14
- [className: string]: DispatchFuncMap<S, T>;
15
- }
16
- export declare const DISPATCH_FUNC_MAP_BY_CLASS_MAP: DispatchFuncMapByClassMap<any, any>;
17
- export declare const constructDispatchMap: <S extends State, T extends ThunkModuleFunc<S>, A extends BaseAction>(theDo: ThunkModule<S>, dispatch: (action: A | rThunk<S, A>) => void, dispatchMap: DispatchFuncMap<S, T>) => DispatchFuncMap<S, T>;
18
- export {};
package/dist/genUUID.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const genUUID: (myuuidv4?: () => string) => string;
package/dist/init.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { BaseAction, Thunk } from './action';
2
- import type { ClassState, State } from './stateTypes';
3
- export interface InitParams<S extends State> {
4
- myID?: string;
5
- state: S;
6
- }
7
- export declare const init: <S extends State>(params: InitParams<S>, myuuidv4?: () => string) => Thunk<S>;
8
- interface InitAction<S extends State> extends BaseAction {
9
- parentID?: string;
10
- doParent?: DispatchFuncMap;
11
- parentClass?: string;
12
- state: S;
13
- }
14
- export declare const INIT = "@chhsiao1981/use-thunk/INIT";
15
- export declare const reduceInit: <S extends State>(classState: ClassState<S>, action: InitAction<S>) => ClassState<S>;
16
- export {};
@@ -1,6 +0,0 @@
1
- import type { ReduceFunc } from './reducer';
2
- import type { State } from './stateTypes';
3
- export interface ReduceMap<S extends State> {
4
- [type: string]: ReduceFunc<S>;
5
- }
6
- export declare const DEFAULT_REDUCE_MAP: <S extends State>() => ReduceMap<S>;
package/dist/reducer.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import type { Reducer as rReducer } from 'react';
2
- import type { BaseAction } from './action';
3
- import type { ClassState, State } from './stateTypes';
4
- export type Reducer<S extends State> = rReducer<ClassState<S>, BaseAction>;
5
- export type ReduceFunc<S extends State> = (state: ClassState<S>, action: BaseAction) => ClassState<S>;
package/dist/remove.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import type { BaseAction, Thunk } from './action';
2
- import type { ClassState, State } from './stateTypes';
3
- export declare const remove: <S extends State>(myID: string) => Thunk<S>;
4
- export declare const REMOVE = "@chhsiao1981/use-thunk/REMOVE";
5
- export declare const reduceRemove: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
@@ -1,5 +0,0 @@
1
- import type { BaseAction } from './action';
2
- import type { ClassState, State } from './stateTypes';
3
- export declare const SET_DEFAULT_ID = "@chhsiao1981/use-thunk/SET_DEFAULT_ID";
4
- export declare const setDefaultID: (myID: string) => BaseAction;
5
- export declare const reduceSetDefaultID: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
@@ -1,16 +0,0 @@
1
- export interface State {
2
- [key: string]: unknown;
3
- }
4
- export type NodeState<S extends State> = {
5
- id: string;
6
- state: S;
7
- };
8
- export type NodeStateMap<S extends State> = {
9
- [key: string]: NodeState<S>;
10
- };
11
- export type ClassState<S extends State> = {
12
- myClass: string;
13
- defaultID?: string | null;
14
- nodes: NodeStateMap<S>;
15
- defaultState: S;
16
- };
package/dist/states.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import type { DispatchFuncMap } from './dispatchFuncMap';
2
- import type { ClassState, NodeState, State } from './stateTypes';
3
- import type { ThunkModuleFunc } from './thunk';
4
- import type { UseThunk } from './useThunk';
5
- export declare const getDefaultID: <S extends State>(classState: ClassState<S>) => string;
6
- export declare const getNode: <S extends State>(classState: ClassState<S>, myID?: string) => NodeState<S> | null;
7
- export declare const getState: <S extends State>(classState: ClassState<S>, myID?: string) => S | null;
8
- export declare const mustGetState: <S extends State>(classState: ClassState<S>, myID?: string) => S;
9
- export declare const mustGetStateByThunk: <S extends State, R extends ThunkModuleFunc<S>>(theUseThunk: UseThunk<S, R>, myID?: string) => [S, DispatchFuncMap<S, R>, string];
package/dist/thunk.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import type { ActionFunc } from './action';
2
- import type { Reducer } from './reducer';
3
- import type { State } from './stateTypes';
4
- export interface ThunkModuleBase<S extends State> {
5
- [idx: string]: ActionFunc<S> | string | Reducer<S> | S | undefined;
6
- }
7
- export interface ThunkModuleFunc<S extends State> extends ThunkModuleBase<S> {
8
- [action: string]: ActionFunc<S>;
9
- }
10
- export type ThunkModule<S extends State> = {
11
- myClass: string;
12
- default?: Reducer<S>;
13
- defaultState: S;
14
- } & ThunkModuleBase<S>;
15
- export type ThunkModuleToFunc<T> = Omit<T, 'myClass' | 'default' | 'defaultState'>;
@@ -1,15 +0,0 @@
1
- import type { Context as rContext } from 'react';
2
- import type { ClassState } from './stateTypes';
3
- import type { Context } from './thunkContextTypes';
4
- export type ThunkContextMap = {
5
- theMap: {
6
- [classname: string]: {
7
- context: rContext<Context<any>>;
8
- refClassState: {
9
- current: ClassState<any>;
10
- };
11
- };
12
- };
13
- theList: string[];
14
- };
15
- export declare const THUNK_CONTEXT_MAP: ThunkContextMap;
@@ -1,8 +0,0 @@
1
- import type { Dispatch, SetStateAction } from 'react';
2
- import type { ClassState, State } from './stateTypes';
3
- export type Context<S extends State> = {
4
- refClassState: {
5
- current: ClassState<S>;
6
- };
7
- setClassState: Dispatch<SetStateAction<ClassState<S>>>;
8
- };