use-good-hooks 1.0.30 → 1.0.31

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
@@ -218,7 +218,7 @@ const TextEditor = () => {
218
218
  setState,
219
219
  state,
220
220
  undo
221
- } = useHistoryState('', { capacity: 10 });
221
+ } = useHistoryState('', { maxCapacity: 10 });
222
222
 
223
223
  return (
224
224
  <div>
@@ -5,8 +5,12 @@ export { DebounceSettings }
5
5
  declare type HistoryAction<T> = {
6
6
  type: 'CLEAR';
7
7
  initialPresent: T;
8
+ } | {
9
+ type: 'PAUSE';
8
10
  } | {
9
11
  type: 'REDO';
12
+ } | {
13
+ type: 'RESUME';
10
14
  } | {
11
15
  type: 'SET';
12
16
  newPresent: T;
@@ -22,12 +26,14 @@ declare type HistoryOnChange<T> = ({ action, state }: {
22
26
  declare type HistoryState<T> = {
23
27
  future: T[];
24
28
  past: T[];
29
+ paused: boolean;
25
30
  present: T | null;
26
31
  };
27
32
 
28
33
  declare type UseHistoryOptionsState<T> = {
29
34
  debounceSettings?: DebounceSettings;
30
35
  debounceTime?: number;
36
+ immutable?: boolean;
31
37
  maxCapacity?: number;
32
38
  onChange?: HistoryOnChange<T>;
33
39
  };
@@ -40,7 +46,10 @@ declare const useHistoryState: <T>(initialPresent: T, options?: UseHistoryOption
40
46
  past: T[];
41
47
  future: T[];
42
48
  };
49
+ pause: () => void;
50
+ paused: boolean;
43
51
  redo: () => void;
52
+ resume: () => void;
44
53
  set: (newPresent: T) => void;
45
54
  setDirect: (newPresent: T) => void;
46
55
  state: T;
@@ -1,114 +1,148 @@
1
- import { useRef as P, useReducer as b, useCallback as d } from "react";
2
- import c from "lodash/cloneDeep";
3
- import N from "lodash/isNil";
4
- import f from "lodash/size";
5
- import T from "./use-debounce-fn.js";
6
- const U = {
1
+ import { useRef as F, useReducer as H, useCallback as S } from "react";
2
+ import J from "lodash/cloneDeep";
3
+ import L from "lodash/isNil";
4
+ import d from "lodash/size";
5
+ import M from "./use-debounce-fn.js";
6
+ const k = {
7
+ future: [],
7
8
  past: [],
8
- present: null,
9
- future: []
10
- }, A = ({
11
- action: r,
12
- maxCapacity: y,
13
- onChange: s,
14
- state: n
9
+ paused: !1,
10
+ present: null
11
+ }, i = (e, r) => r ? e : J(e), q = (e, r, o) => o ? e === r : JSON.stringify(e) === JSON.stringify(r), v = ({
12
+ action: e,
13
+ immutable: r,
14
+ maxCapacity: o,
15
+ onChange: t,
16
+ state: f
15
17
  }) => {
16
- const { past: p, present: o, future: l } = n;
17
- if (r.type === "UNDO") {
18
- if (f(p) === 0)
19
- return n;
20
- const e = p[f(p) - 1], u = {
21
- past: p.slice(0, f(p) - 1),
22
- present: c(e),
23
- future: [c(o), ...l]
18
+ const { future: E, past: c, paused: l, present: p } = f;
19
+ if (e.type === "PAUSE")
20
+ return {
21
+ ...f,
22
+ paused: !0
24
23
  };
25
- return s == null || s({
26
- action: r.type,
27
- state: u.present
28
- }), u;
29
- } else if (r.type === "REDO") {
30
- if (f(l) === 0)
31
- return n;
32
- const e = l[0], t = l.slice(1), u = {
33
- past: [...p, c(o)],
34
- present: c(e),
35
- future: t
24
+ if (e.type === "RESUME")
25
+ return {
26
+ ...f,
27
+ paused: !1
36
28
  };
37
- return s == null || s({
38
- action: r.type,
39
- state: u.present
40
- }), u;
41
- } else if (r.type === "SET") {
42
- const { newPresent: e } = r;
43
- if (JSON.stringify(e) === JSON.stringify(o))
44
- return n;
45
- let t = [...p];
46
- o !== null && (t = [...t, c(o)]), !N(y) && y > 0 && f(t) > y && (t = t.slice(f(t) - y));
47
- const u = {
48
- past: t,
49
- present: c(e),
50
- future: []
29
+ if (e.type === "UNDO") {
30
+ if (d(c) === 0)
31
+ return f;
32
+ const s = c[d(c) - 1], u = c.slice(0, d(c) - 1), n = {
33
+ future: [i(p, r), ...E],
34
+ past: u,
35
+ paused: l,
36
+ present: i(s, r)
51
37
  };
52
- return s == null || s({
53
- action: r.type,
54
- state: u.present
55
- }), u;
56
- } else if (r.type === "CLEAR") {
57
- const e = {
38
+ return t == null || t({
39
+ action: e.type,
40
+ state: n.present
41
+ }), n;
42
+ } else if (e.type === "REDO") {
43
+ if (d(E) === 0)
44
+ return f;
45
+ const s = E[0], n = {
46
+ future: E.slice(1),
47
+ past: [...c, i(p, r)],
48
+ paused: l,
49
+ present: i(s, r)
50
+ };
51
+ return t == null || t({
52
+ action: e.type,
53
+ state: n.present
54
+ }), n;
55
+ } else if (e.type === "SET") {
56
+ const { newPresent: s } = e;
57
+ if (q(s, p, r))
58
+ return f;
59
+ if (l) {
60
+ const w = {
61
+ ...f,
62
+ present: i(s, r)
63
+ };
64
+ return t == null || t({
65
+ action: e.type,
66
+ state: w.present
67
+ }), w;
68
+ }
69
+ let u = [...c];
70
+ p !== null && (u = [...u, i(p, r)]), !L(o) && o > 0 && d(u) > o && (u = u.slice(d(u) - o));
71
+ const n = {
72
+ future: [],
73
+ past: u,
74
+ paused: l,
75
+ present: i(s, r)
76
+ };
77
+ return t == null || t({
78
+ action: e.type,
79
+ state: n.present
80
+ }), n;
81
+ } else if (e.type === "CLEAR") {
82
+ const s = {
83
+ future: [],
58
84
  past: [],
59
- present: c(r.initialPresent),
60
- future: []
85
+ paused: l,
86
+ present: i(e.initialPresent, r)
61
87
  };
62
- return s == null || s({
63
- action: r.type,
64
- state: e.present
65
- }), e;
88
+ return t == null || t({
89
+ action: e.type,
90
+ state: s.present
91
+ }), s;
66
92
  } else
67
93
  throw new Error("Unsupported action type");
68
- }, v = (r, y) => {
69
- const { maxCapacity: s, debounceTime: n, debounceSettings: p, onChange: o } = y || {}, l = P(r), [e, t] = b(
70
- (i, O) => A({
71
- action: O,
72
- maxCapacity: s,
73
- onChange: o,
74
- state: i
94
+ }, B = (e, r) => {
95
+ const { maxCapacity: o, debounceTime: t, debounceSettings: f, onChange: E, immutable: c } = r || {}, l = F(e), [p, s] = H(
96
+ (y, T) => v({
97
+ action: T,
98
+ immutable: c,
99
+ maxCapacity: o,
100
+ onChange: E,
101
+ state: y
75
102
  }),
76
103
  {
77
- ...U,
78
- present: c(l.current)
104
+ ...k,
105
+ present: i(l.current, c)
79
106
  }
80
- ), u = f(e.future) !== 0, S = f(e.past) !== 0, R = d(() => t({
107
+ ), u = d(p.future) !== 0, n = d(p.past) !== 0, w = S(() => s({
81
108
  initialPresent: l.current,
82
109
  type: "CLEAR"
83
- }), []), D = d(() => {
84
- u && t({ type: "REDO" });
85
- }, [u]), m = T(
86
- (i) => t({ type: "SET", newPresent: i }),
87
- n,
88
- p
89
- ), w = d((i) => t({ type: "SET", newPresent: i }), []), E = d(() => {
90
- S && t({ type: "UNDO" });
91
- }, [S]), a = d(
92
- (i) => {
93
- n ? m(i) : w(i);
110
+ }), []), U = S(() => {
111
+ s({ type: "PAUSE" });
112
+ }, []), P = S(() => {
113
+ s({ type: "RESUME" });
114
+ }, []), O = S(() => {
115
+ u && s({ type: "REDO" });
116
+ }, [u]), D = M(
117
+ (y) => s({ type: "SET", newPresent: y }),
118
+ t,
119
+ f
120
+ ), R = S((y) => s({ type: "SET", newPresent: y }), []), N = S(() => {
121
+ n && s({ type: "UNDO" });
122
+ }, [n]), A = S(
123
+ (y) => {
124
+ t ? D(y) : R(y);
94
125
  },
95
- [n, m, w]
126
+ [t, D, R]
96
127
  );
97
128
  return {
98
129
  canRedo: u,
99
- canUndo: S,
100
- clear: R,
130
+ canUndo: n,
131
+ clear: w,
101
132
  history: {
102
- past: e.past,
103
- future: e.future
133
+ past: p.past,
134
+ future: p.future
104
135
  },
105
- redo: D,
106
- set: a,
107
- setDirect: w,
108
- state: e.present,
109
- undo: E
136
+ pause: U,
137
+ paused: p.paused,
138
+ redo: O,
139
+ resume: P,
140
+ set: A,
141
+ setDirect: R,
142
+ state: p.present,
143
+ undo: N
110
144
  };
111
145
  };
112
146
  export {
113
- v as default
147
+ B as default
114
148
  };
package/package.json CHANGED
@@ -45,9 +45,10 @@
45
45
  "scripts": {
46
46
  "build": "yarn lint && vite build --mode export",
47
47
  "lint": "prettier --write . && eslint .",
48
- "npm:publish": "yarn test --run && yarn build && yarn version --patch --no-git-tag-version && yarn publish --non-interactive",
49
- "test": "vitest",
50
- "test:coverage": "vitest --coverage"
48
+ "npm:publish": "yarn test && yarn build && yarn version --patch --no-git-tag-version && yarn publish --non-interactive",
49
+ "test": "vitest --run",
50
+ "test:coverage": "vitest --coverage --run",
51
+ "test:watch": "vitest"
51
52
  },
52
- "version": "1.0.30"
53
+ "version": "1.0.31"
53
54
  }