use-good-hooks 1.0.33 → 1.0.34

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.
@@ -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
  } | {
@@ -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 T, useReducer as F, useCallback as o } from "react";
2
- import H from "lodash/cloneDeep";
3
- import J from "lodash/isNil";
4
- import d from "lodash/size";
5
- import L from "./use-debounce-fn.js";
6
- const M = {
1
+ import { useRef as F, useReducer as H, useCallback as d } from "react";
2
+ import J from "lodash/cloneDeep";
3
+ import M from "lodash/isNil";
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
- }, i = (e, t) => t ? e : H(e), k = (e, t, l) => l ? e === t : JSON.stringify(e) === JSON.stringify(t), q = ({
10
+ }, l = (e, r) => r ? e : J(e), v = (e, r, y) => y ? e === r : JSON.stringify(e) === JSON.stringify(r), z = ({
11
11
  action: e,
12
- immutable: t,
13
- maxCapacity: l,
14
- onChange: s,
12
+ immutable: r,
13
+ maxCapacity: y,
14
+ onChange: t,
15
15
  state: f
16
16
  }) => {
17
- const { future: E, past: n, paused: y, present: p } = f;
18
- if (e.type === "PAUSE")
19
- return {
20
- ...f,
21
- paused: !0
22
- };
23
- if (e.type === "RESUME")
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 s == null || s({
25
+ return t == null || t({
51
26
  action: e.type,
52
- state: c.present
53
- }), c;
54
- } else if (e.type === "SET") {
55
- const { newPresent: r } = e;
56
- if (k(r, p, t))
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
- present: i(r, t)
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 s == null || s({
44
+ return t == null || t({
64
45
  action: e.type,
65
- state: w.present
66
- }), w;
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)]), !M(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
- let u = [...n];
69
- p !== null && (u = [...u, i(p, t)]), !J(l) && l > 0 && d(u) > l && (u = u.slice(d(u) - l));
70
- const c = {
71
- future: [],
72
- past: u,
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, debounceTime: t, debounceSettings: f, onChange: o, immutable: n } = r || {}, S = F(e), [p, s] = H(
110
+ (i, T) => z({
111
+ action: T,
97
112
  immutable: n,
98
- maxCapacity: l,
99
- onChange: E,
100
- state: S
113
+ maxCapacity: y,
114
+ onChange: o,
115
+ state: i
101
116
  }),
102
117
  {
103
- ...M,
104
- paused: (t == null ? void 0 : t.paused) ?? !1,
105
- present: i(y.current, n)
118
+ ...q,
119
+ paused: (r == null ? void 0 : r.paused) ?? !1,
120
+ present: l(S.current, n)
106
121
  }
107
- ), u = d(p.future) !== 0, c = d(p.past) !== 0, w = o(() => r({
108
- initialState: y.current,
122
+ ), u = E(p.future) !== 0, c = E(p.past) !== 0, w = d(() => s({
123
+ initialState: S.current,
109
124
  type: "CLEAR"
110
- }), []), U = o(() => {
111
- r({ type: "PAUSE" });
112
- }, []), O = o(() => {
113
- r({ type: "RESUME" });
114
- }, []), N = o(() => {
115
- u && r({ type: "REDO" });
116
- }, [u]), D = L(
117
- (S) => r({ type: "SET", newPresent: S }),
118
- s,
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 = o((S) => r({ type: "SET", newPresent: S }), []), a = o(() => {
121
- c && r({ type: "UNDO" });
122
- }, [c]), A = o(
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
- [s, D, R]
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: N,
153
+ redo: P,
154
+ replace: A,
137
155
  resume: O,
138
- set: A,
156
+ set: N,
139
157
  setDirect: R,
140
158
  state: p.present,
141
- undo: a
159
+ undo: L
142
160
  };
143
161
  };
144
162
  export {
145
- B as default
163
+ G as default
146
164
  };
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.33"
53
+ "version": "1.0.34"
54
54
  }