use-good-hooks 1.0.37 → 1.0.39

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/README.md CHANGED
@@ -215,7 +215,7 @@ const TextEditor = () => {
215
215
  canUndo,
216
216
  history,
217
217
  redo,
218
- setState,
218
+ set,
219
219
  state,
220
220
  undo
221
221
  } = useHistoryState('', { maxCapacity: 10 });
@@ -224,7 +224,7 @@ const TextEditor = () => {
224
224
  <div>
225
225
  <textarea
226
226
  value={state}
227
- onChange={(e) => setState(e.target.value)}
227
+ onChange={(e) => set(e.target.value)}
228
228
  rows={4}
229
229
  cols={50}
230
230
  />
@@ -262,7 +262,6 @@ const TextEditor = () => {
262
262
  - `paused`: Boolean indicating if the history is paused
263
263
  - `redo`: Function to move to the next state (redo)
264
264
  - `set`: Function to update the state and record history
265
- - `setDirect`: Function to update the state without recording history
266
265
  - `state`: The current state value
267
266
  - `undo`: Function to move to the previous state (undo)
268
267
 
@@ -1,164 +1,155 @@
1
- import { useRef as M, useReducer as T, useCallback as d } from "react";
2
- import H from "lodash/cloneDeep";
3
- import J from "lodash/isNil";
4
- import E from "lodash/size";
5
- import k from "./use-debounce-fn.js";
6
- const q = {
1
+ import { useRef as k, useReducer as I, useCallback as l, useEffect as K } from "react";
2
+ import Q from "lodash/cloneDeep";
3
+ import W from "lodash/isNil";
4
+ import o from "lodash/size";
5
+ import X from "./use-debounce-fn.js";
6
+ const Y = {
7
7
  future: [],
8
8
  past: [],
9
9
  present: null
10
- }, l = (e, r) => r ? e : H(e), v = (e, r, y) => y ? e === r : JSON.stringify(e) === JSON.stringify(r), z = ({
11
- action: e,
12
- immutable: r,
13
- maxCapacity: y,
14
- onChange: t,
15
- state: f
16
- }) => {
17
- const { future: o, past: n, paused: S, present: p } = f;
18
- if (e.type === "CLEAR") {
19
- const s = {
20
- future: [],
21
- past: [],
22
- paused: S,
23
- present: l(e.initialState, r)
24
- };
25
- return t == null || t({
26
- action: e.type,
27
- state: s.present
28
- }), s;
29
- } else {
30
- if (e.type === "PAUSE")
31
- return {
32
- ...f,
33
- paused: !0
34
- };
35
- if (e.type === "REDO") {
36
- if (E(o) === 0)
37
- return f;
38
- const s = o[0], c = {
39
- future: o.slice(1),
40
- past: [...n, l(p, r)],
41
- paused: S,
42
- present: l(s, r)
43
- };
44
- return t == null || t({
45
- action: e.type,
46
- state: c.present
47
- }), c;
48
- } else {
49
- if (e.type === "RESUME")
50
- return {
51
- ...f,
52
- paused: !1
53
- };
54
- if (e.type === "REPLACE") {
55
- const { newPresent: s } = e, u = {
56
- ...f,
10
+ }, c = (f, p) => p ? f : Q(f), Z = (f, p, S) => S ? f === p : JSON.stringify(f) === JSON.stringify(p), ee = (f, p) => {
11
+ const { maxCapacity: S, debounceMs: D, debounceSettings: q, onChange: U, immutable: n } = p || {}, b = k(f), e = k(U), [E, i] = I(
12
+ (t, r) => {
13
+ var L, x, F, M, T, H;
14
+ const { future: w, past: y, paused: d, present: m } = t;
15
+ if (r.type === "CLEAR") {
16
+ const u = {
57
17
  future: [],
58
18
  past: [],
59
- present: s
19
+ paused: d,
20
+ present: c(r.initialState, n)
60
21
  };
61
- return t == null || t({
62
- action: e.type,
22
+ return (L = e.current) == null || L.call(e, {
23
+ action: r.type,
63
24
  state: u.present
64
25
  }), u;
65
- } else if (e.type === "SET") {
66
- const { newPresent: s } = e;
67
- if (v(s, p, r))
68
- return f;
69
- if (S) {
70
- const w = {
71
- ...f,
72
- present: l(s, r)
26
+ } else {
27
+ if (r.type === "PAUSE")
28
+ return {
29
+ ...t,
30
+ paused: !0
31
+ };
32
+ if (r.type === "REDO") {
33
+ if (o(w) === 0)
34
+ return t;
35
+ const u = w[0], a = {
36
+ future: w.slice(1),
37
+ past: [...y, c(m, n)],
38
+ paused: d,
39
+ present: c(u, n)
73
40
  };
74
- return t == null || t({
75
- action: e.type,
76
- state: w.present
77
- }), w;
41
+ return (x = e.current) == null || x.call(e, {
42
+ action: r.type,
43
+ state: a.present
44
+ }), a;
45
+ } else {
46
+ if (r.type === "RESUME")
47
+ return {
48
+ ...t,
49
+ paused: !1
50
+ };
51
+ if (r.type === "REPLACE") {
52
+ const { newPresent: u } = r, s = {
53
+ ...t,
54
+ future: [],
55
+ past: [],
56
+ present: u
57
+ };
58
+ return (F = e.current) == null || F.call(e, {
59
+ action: r.type,
60
+ state: s.present
61
+ }), s;
62
+ } else if (r.type === "SET") {
63
+ const { newPresent: u } = r;
64
+ if (Z(u, m, n))
65
+ return t;
66
+ if (d) {
67
+ const J = {
68
+ ...t,
69
+ present: c(u, n)
70
+ };
71
+ return (M = e.current) == null || M.call(e, {
72
+ action: r.type,
73
+ state: J.present
74
+ }), J;
75
+ }
76
+ let s = [...y];
77
+ m !== null && (s = [...s, c(m, n)]), !W(S) && S > 0 && o(s) > S && (s = s.slice(o(s) - S));
78
+ const a = {
79
+ future: [],
80
+ past: s,
81
+ paused: d,
82
+ present: c(u, n)
83
+ };
84
+ return (T = e.current) == null || T.call(e, {
85
+ action: r.type,
86
+ state: a.present
87
+ }), a;
88
+ } else if (r.type === "UNDO") {
89
+ if (o(y) === 0)
90
+ return t;
91
+ const u = y[o(y) - 1], s = y.slice(0, o(y) - 1), a = {
92
+ future: [c(m, n), ...w],
93
+ past: s,
94
+ paused: d,
95
+ present: c(u, n)
96
+ };
97
+ return (H = e.current) == null || H.call(e, {
98
+ action: r.type,
99
+ state: a.present
100
+ }), a;
101
+ } else
102
+ throw new Error("Unsupported action type");
78
103
  }
79
- let u = [...n];
80
- p !== null && (u = [...u, l(p, r)]), !J(y) && y > 0 && E(u) > y && (u = u.slice(E(u) - y));
81
- const c = {
82
- future: [],
83
- past: u,
84
- paused: S,
85
- present: l(s, r)
86
- };
87
- return t == null || t({
88
- action: e.type,
89
- state: c.present
90
- }), c;
91
- } else if (e.type === "UNDO") {
92
- if (E(n) === 0)
93
- return f;
94
- const s = n[E(n) - 1], u = n.slice(0, E(n) - 1), c = {
95
- future: [l(p, r), ...o],
96
- past: u,
97
- paused: S,
98
- present: l(s, r)
99
- };
100
- return t == null || t({
101
- action: e.type,
102
- state: c.present
103
- }), c;
104
- } else
105
- throw new Error("Unsupported action type");
106
- }
107
- }
108
- }, G = (e, r) => {
109
- const { maxCapacity: y, debounceMs: t, debounceSettings: f, onChange: o, immutable: n } = r || {}, S = M(e), [p, s] = T(
110
- (i, F) => z({
111
- action: F,
112
- immutable: n,
113
- maxCapacity: y,
114
- onChange: o,
115
- state: i
116
- }),
104
+ }
105
+ },
117
106
  {
118
- ...q,
119
- paused: (r == null ? void 0 : r.paused) ?? !1,
120
- present: l(S.current, n)
107
+ ...Y,
108
+ paused: (p == null ? void 0 : p.paused) ?? !1,
109
+ present: c(b.current, n)
121
110
  }
122
- ), u = E(p.future) !== 0, c = E(p.past) !== 0, w = d(() => s({
123
- initialState: S.current,
111
+ ), P = o(E.future) !== 0, A = o(E.past) !== 0, v = l(() => i({
112
+ initialState: b.current,
124
113
  type: "CLEAR"
125
- }), []), U = d(() => {
126
- s({ type: "PAUSE" });
127
- }, []), P = d(() => {
128
- u && s({ type: "REDO" });
129
- }, [u]), A = d((i) => {
130
- s({ type: "REPLACE", newPresent: i });
131
- }, []), O = d(() => {
132
- s({ type: "RESUME" });
133
- }, []), D = k(
134
- (i) => s({ type: "SET", newPresent: i }),
135
- t,
136
- f
137
- ), R = d((i) => s({ type: "SET", newPresent: i }), []), N = d(
138
- (i) => {
139
- t ? D(i) : R(i);
114
+ }), []), z = l(() => {
115
+ i({ type: "PAUSE" });
116
+ }, []), R = l(() => {
117
+ P && i({ type: "REDO" });
118
+ }, [P]), V = l((t) => {
119
+ i({ type: "REPLACE", newPresent: t });
120
+ }, []), j = l(() => {
121
+ i({ type: "RESUME" });
122
+ }, []), N = X(
123
+ (t) => i({ type: "SET", newPresent: t }),
124
+ D,
125
+ q
126
+ ), O = l((t) => i({ type: "SET", newPresent: t }), []), B = l(
127
+ (t) => {
128
+ D ? N(t) : O(t);
140
129
  },
141
- [t, D, R]
142
- ), L = d(() => {
143
- c && s({ type: "UNDO" });
144
- }, [c]);
145
- return {
146
- canRedo: u,
147
- canUndo: c,
148
- clear: w,
149
- future: p.future,
150
- past: p.past,
151
- pause: U,
152
- paused: p.paused,
153
- redo: P,
154
- replace: A,
155
- resume: O,
156
- set: N,
157
- setDirect: R,
158
- state: p.present,
159
- undo: L
130
+ [D, N, O]
131
+ ), G = l(() => {
132
+ A && i({ type: "UNDO" });
133
+ }, [A]);
134
+ return K(() => {
135
+ e.current = U;
136
+ }, [U]), {
137
+ canRedo: P,
138
+ canUndo: A,
139
+ clear: v,
140
+ future: E.future,
141
+ past: E.past,
142
+ pause: z,
143
+ paused: E.paused,
144
+ redo: R,
145
+ replace: V,
146
+ resume: j,
147
+ set: B,
148
+ setDirect: O,
149
+ state: E.present,
150
+ undo: G
160
151
  };
161
152
  };
162
153
  export {
163
- G as default
154
+ ee as default
164
155
  };
package/package.json CHANGED
@@ -50,5 +50,5 @@
50
50
  "test:coverage": "vitest --coverage --run",
51
51
  "test:watch": "vitest"
52
52
  },
53
- "version": "1.0.37"
53
+ "version": "1.0.39"
54
54
  }