use-good-hooks 1.0.33 → 1.0.35
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 +2 -2
- package/dist/use-debounce.d.ts +1 -1
- package/dist/use-debounce.js +2 -2
- package/dist/use-history-state.d.ts +5 -1
- package/dist/use-history-state.js +128 -110
- package/dist/use-temporary-state.d.ts +1 -1
- package/dist/use-temporary-state.js +4 -4
- package/dist/use-throttle.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -244,7 +244,7 @@ const TextEditor = () => {
|
|
|
244
244
|
- `initialState`: The initial state value
|
|
245
245
|
- `options`: (Optional) Configuration options:
|
|
246
246
|
- `debounceSettings`: Debounce settings object (default: { leading: true, trailing: false })
|
|
247
|
-
- `
|
|
247
|
+
- `debounceMs`: Time in milliseconds to debounce the state changes (default: 0)
|
|
248
248
|
- `immutable`: Boolean indicating if the state should be treated as immutable (default: false)
|
|
249
249
|
- `maxCapacity`: Maximum number of history entries to keep (default: 10)
|
|
250
250
|
- `onChange`: Function to call when the state changes
|
|
@@ -516,7 +516,7 @@ const [state, setState] = useTemporaryState('initial', 1000);
|
|
|
516
516
|
#### Parameters
|
|
517
517
|
|
|
518
518
|
- `initialState`: The initial state value
|
|
519
|
-
- `
|
|
519
|
+
- `ms`: The timeout duration in milliseconds (default: 3000)
|
|
520
520
|
|
|
521
521
|
#### Returns
|
|
522
522
|
|
package/dist/use-debounce.d.ts
CHANGED
package/dist/use-debounce.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useState as s, useRef as d, useEffect as n } from "react";
|
|
2
2
|
import f from "lodash/debounce";
|
|
3
|
-
const b = 300,
|
|
3
|
+
const b = 300, p = (e, o = b) => {
|
|
4
4
|
const [u, r] = s(e), c = d(
|
|
5
5
|
f((t) => {
|
|
6
6
|
r(t);
|
|
@@ -16,5 +16,5 @@ const b = 300, D = (e, o = b) => {
|
|
|
16
16
|
}, []), u;
|
|
17
17
|
};
|
|
18
18
|
export {
|
|
19
|
-
|
|
19
|
+
p as default
|
|
20
20
|
};
|
|
@@ -9,6 +9,9 @@ declare type HistoryAction<T> = {
|
|
|
9
9
|
type: 'PAUSE';
|
|
10
10
|
} | {
|
|
11
11
|
type: 'REDO';
|
|
12
|
+
} | {
|
|
13
|
+
type: 'REPLACE';
|
|
14
|
+
newPresent: T;
|
|
12
15
|
} | {
|
|
13
16
|
type: 'RESUME';
|
|
14
17
|
} | {
|
|
@@ -32,7 +35,7 @@ declare type HistoryState<T> = {
|
|
|
32
35
|
|
|
33
36
|
declare type UseHistoryOptionsState<T> = {
|
|
34
37
|
debounceSettings?: DebounceSettings;
|
|
35
|
-
|
|
38
|
+
debounceMs?: number;
|
|
36
39
|
immutable?: boolean;
|
|
37
40
|
maxCapacity?: number;
|
|
38
41
|
onChange?: HistoryOnChange<T>;
|
|
@@ -48,6 +51,7 @@ declare const useHistoryState: <T>(initialState: T, options?: UseHistoryOptionsS
|
|
|
48
51
|
pause: () => void;
|
|
49
52
|
paused: boolean;
|
|
50
53
|
redo: () => void;
|
|
54
|
+
replace: (newPresent: T) => void;
|
|
51
55
|
resume: () => void;
|
|
52
56
|
set: (newPresent: T) => void;
|
|
53
57
|
setDirect: (newPresent: T) => void;
|
|
@@ -1,130 +1,147 @@
|
|
|
1
|
-
import { useRef as
|
|
1
|
+
import { useRef as M, useReducer as T, useCallback as d } from "react";
|
|
2
2
|
import H from "lodash/cloneDeep";
|
|
3
3
|
import J from "lodash/isNil";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
const
|
|
4
|
+
import E from "lodash/size";
|
|
5
|
+
import k from "./use-debounce-fn.js";
|
|
6
|
+
const q = {
|
|
7
7
|
future: [],
|
|
8
8
|
past: [],
|
|
9
9
|
present: null
|
|
10
|
-
},
|
|
10
|
+
}, l = (e, r) => r ? e : H(e), v = (e, r, y) => y ? e === r : JSON.stringify(e) === JSON.stringify(r), z = ({
|
|
11
11
|
action: e,
|
|
12
|
-
immutable:
|
|
13
|
-
maxCapacity:
|
|
14
|
-
onChange:
|
|
12
|
+
immutable: r,
|
|
13
|
+
maxCapacity: y,
|
|
14
|
+
onChange: t,
|
|
15
15
|
state: f
|
|
16
16
|
}) => {
|
|
17
|
-
const { future:
|
|
18
|
-
if (e.type === "
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
...f,
|
|
26
|
-
paused: !1
|
|
27
|
-
};
|
|
28
|
-
if (e.type === "UNDO") {
|
|
29
|
-
if (d(n) === 0)
|
|
30
|
-
return f;
|
|
31
|
-
const r = n[d(n) - 1], u = n.slice(0, d(n) - 1), c = {
|
|
32
|
-
future: [i(p, t), ...E],
|
|
33
|
-
past: u,
|
|
34
|
-
paused: y,
|
|
35
|
-
present: i(r, t)
|
|
36
|
-
};
|
|
37
|
-
return s == null || s({
|
|
38
|
-
action: e.type,
|
|
39
|
-
state: c.present
|
|
40
|
-
}), c;
|
|
41
|
-
} else if (e.type === "REDO") {
|
|
42
|
-
if (d(E) === 0)
|
|
43
|
-
return f;
|
|
44
|
-
const r = E[0], c = {
|
|
45
|
-
future: E.slice(1),
|
|
46
|
-
past: [...n, i(p, t)],
|
|
47
|
-
paused: y,
|
|
48
|
-
present: i(r, t)
|
|
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)
|
|
49
24
|
};
|
|
50
|
-
return
|
|
25
|
+
return t == null || t({
|
|
51
26
|
action: e.type,
|
|
52
|
-
state:
|
|
53
|
-
}),
|
|
54
|
-
} else
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return f;
|
|
58
|
-
if (y) {
|
|
59
|
-
const w = {
|
|
27
|
+
state: s.present
|
|
28
|
+
}), s;
|
|
29
|
+
} else {
|
|
30
|
+
if (e.type === "PAUSE")
|
|
31
|
+
return {
|
|
60
32
|
...f,
|
|
61
|
-
|
|
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)
|
|
62
43
|
};
|
|
63
|
-
return
|
|
44
|
+
return t == null || t({
|
|
64
45
|
action: e.type,
|
|
65
|
-
state:
|
|
66
|
-
}),
|
|
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,
|
|
57
|
+
future: [],
|
|
58
|
+
past: [],
|
|
59
|
+
present: s
|
|
60
|
+
};
|
|
61
|
+
return t == null || t({
|
|
62
|
+
action: e.type,
|
|
63
|
+
state: u.present
|
|
64
|
+
}), 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)
|
|
73
|
+
};
|
|
74
|
+
return t == null || t({
|
|
75
|
+
action: e.type,
|
|
76
|
+
state: w.present
|
|
77
|
+
}), w;
|
|
78
|
+
}
|
|
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");
|
|
67
106
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
paused: y,
|
|
74
|
-
present: i(r, t)
|
|
75
|
-
};
|
|
76
|
-
return s == null || s({
|
|
77
|
-
action: e.type,
|
|
78
|
-
state: c.present
|
|
79
|
-
}), c;
|
|
80
|
-
} else if (e.type === "CLEAR") {
|
|
81
|
-
const r = {
|
|
82
|
-
future: [],
|
|
83
|
-
past: [],
|
|
84
|
-
paused: y,
|
|
85
|
-
present: i(e.initialState, t)
|
|
86
|
-
};
|
|
87
|
-
return s == null || s({
|
|
88
|
-
action: e.type,
|
|
89
|
-
state: r.present
|
|
90
|
-
}), r;
|
|
91
|
-
} else
|
|
92
|
-
throw new Error("Unsupported action type");
|
|
93
|
-
}, B = (e, t) => {
|
|
94
|
-
const { maxCapacity: l, debounceTime: s, debounceSettings: f, onChange: E, immutable: n } = t || {}, y = T(e), [p, r] = F(
|
|
95
|
-
(S, P) => q({
|
|
96
|
-
action: P,
|
|
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,
|
|
97
112
|
immutable: n,
|
|
98
|
-
maxCapacity:
|
|
99
|
-
onChange:
|
|
100
|
-
state:
|
|
113
|
+
maxCapacity: y,
|
|
114
|
+
onChange: o,
|
|
115
|
+
state: i
|
|
101
116
|
}),
|
|
102
117
|
{
|
|
103
|
-
...
|
|
104
|
-
paused: (
|
|
105
|
-
present:
|
|
118
|
+
...q,
|
|
119
|
+
paused: (r == null ? void 0 : r.paused) ?? !1,
|
|
120
|
+
present: l(S.current, n)
|
|
106
121
|
}
|
|
107
|
-
), u =
|
|
108
|
-
initialState:
|
|
122
|
+
), u = E(p.future) !== 0, c = E(p.past) !== 0, w = d(() => s({
|
|
123
|
+
initialState: S.current,
|
|
109
124
|
type: "CLEAR"
|
|
110
|
-
}), []), U =
|
|
111
|
-
|
|
112
|
-
}, []),
|
|
113
|
-
|
|
114
|
-
}, []),
|
|
115
|
-
|
|
116
|
-
}, [
|
|
117
|
-
(
|
|
118
|
-
|
|
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,
|
|
119
136
|
f
|
|
120
|
-
), R =
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
(S) => {
|
|
124
|
-
s ? D(S) : R(S);
|
|
137
|
+
), R = d((i) => s({ type: "SET", newPresent: i }), []), N = d(
|
|
138
|
+
(i) => {
|
|
139
|
+
t ? D(i) : R(i);
|
|
125
140
|
},
|
|
126
|
-
[
|
|
127
|
-
)
|
|
141
|
+
[t, D, R]
|
|
142
|
+
), L = d(() => {
|
|
143
|
+
c && s({ type: "UNDO" });
|
|
144
|
+
}, [c]);
|
|
128
145
|
return {
|
|
129
146
|
canRedo: u,
|
|
130
147
|
canUndo: c,
|
|
@@ -133,14 +150,15 @@ const M = {
|
|
|
133
150
|
past: p.past,
|
|
134
151
|
pause: U,
|
|
135
152
|
paused: p.paused,
|
|
136
|
-
redo:
|
|
153
|
+
redo: P,
|
|
154
|
+
replace: A,
|
|
137
155
|
resume: O,
|
|
138
|
-
set:
|
|
156
|
+
set: N,
|
|
139
157
|
setDirect: R,
|
|
140
158
|
state: p.present,
|
|
141
|
-
undo:
|
|
159
|
+
undo: L
|
|
142
160
|
};
|
|
143
161
|
};
|
|
144
162
|
export {
|
|
145
|
-
|
|
163
|
+
G as default
|
|
146
164
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Dispatch } from 'react';
|
|
2
2
|
import { SetStateAction } from 'react';
|
|
3
3
|
|
|
4
|
-
declare const useTemporaryState: <T>(initialState: T,
|
|
4
|
+
declare const useTemporaryState: <T>(initialState: T, ms: number) => readonly [T, Dispatch<SetStateAction<T>>];
|
|
5
5
|
export default useTemporaryState;
|
|
6
6
|
|
|
7
7
|
export { }
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { useRef as
|
|
1
|
+
import { useRef as s, useState as c, useEffect as n } from "react";
|
|
2
2
|
const m = (e, r) => {
|
|
3
|
-
const t =
|
|
3
|
+
const t = s(null), [u, o] = c(e);
|
|
4
4
|
return n(() => (clearTimeout(t.current), t.current = setTimeout(() => {
|
|
5
|
-
|
|
5
|
+
o(e);
|
|
6
6
|
}, r), () => {
|
|
7
7
|
clearTimeout(t.current);
|
|
8
|
-
}), [e, u, r]), [u,
|
|
8
|
+
}), [e, u, r]), [u, o];
|
|
9
9
|
};
|
|
10
10
|
export {
|
|
11
11
|
m as default
|
package/dist/use-throttle.d.ts
CHANGED
package/package.json
CHANGED