react-usectx 1.2.4 → 1.2.5
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/context.js +1 -1
- package/create-context.js +6 -4
- package/package.json +1 -1
- package/src/context.ts +1 -1
- package/src/create-context.ts +6 -4
- package/types.js +1 -0
package/context.js
CHANGED
|
@@ -23,7 +23,7 @@ export function getCtx(name) {
|
|
|
23
23
|
export function useCtx(name, initialState) {
|
|
24
24
|
const initializeFirst = updateCtx(name, initialState);
|
|
25
25
|
const api = getInstance(name);
|
|
26
|
-
return [getCtx(name), initializeFirst, api.undo, api.redo, api];
|
|
26
|
+
return [getCtx(name), initializeFirst, api.undo.bind(api), api.redo.bind(api), api];
|
|
27
27
|
}
|
|
28
28
|
const reducerCache = {};
|
|
29
29
|
export function useReducer(name, modiFier, useCache = false) {
|
package/create-context.js
CHANGED
|
@@ -33,7 +33,7 @@ export class CreateContext {
|
|
|
33
33
|
undo() {
|
|
34
34
|
const prev = this.#history[this.#activeVersion - 1];
|
|
35
35
|
if (prev) {
|
|
36
|
-
this.setState(prev, this.#history.indexOf(prev));
|
|
36
|
+
this.setState(prev, this.#history.indexOf(prev), true);
|
|
37
37
|
this.#needsReset = true;
|
|
38
38
|
this.#trigger();
|
|
39
39
|
}
|
|
@@ -45,7 +45,7 @@ export class CreateContext {
|
|
|
45
45
|
const nextIndex = this.#activeVersion + 1;
|
|
46
46
|
const next = this.#history[nextIndex];
|
|
47
47
|
if (next) {
|
|
48
|
-
this.setState(next, nextIndex);
|
|
48
|
+
this.setState(next, nextIndex, true);
|
|
49
49
|
this.#needsReset = true;
|
|
50
50
|
this.#trigger();
|
|
51
51
|
}
|
|
@@ -53,13 +53,15 @@ export class CreateContext {
|
|
|
53
53
|
hasRedo() {
|
|
54
54
|
return !!this.#history[this.#activeVersion + 1];
|
|
55
55
|
}
|
|
56
|
-
setState(state, _av) {
|
|
56
|
+
setState(state, _av, _skipPush) {
|
|
57
57
|
if (typeof _av === 'undefined') {
|
|
58
58
|
if (this.#history.length > 0 && (this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
59
59
|
this.#history.splice(this.#activeVersion + 1, this.#history.length);
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
-
|
|
62
|
+
if (!_skipPush) {
|
|
63
|
+
this.#history.push(state);
|
|
64
|
+
}
|
|
63
65
|
if (this.#history.length > this.settings.historySize) {
|
|
64
66
|
this.#history.shift();
|
|
65
67
|
}
|
package/package.json
CHANGED
package/src/context.ts
CHANGED
|
@@ -42,7 +42,7 @@ export function useCtx(
|
|
|
42
42
|
): [ReactNode, Function, Function, Function, CreateContext] {
|
|
43
43
|
const initializeFirst = updateCtx(name, initialState);
|
|
44
44
|
const api = getInstance(name);
|
|
45
|
-
return [getCtx(name), initializeFirst, api.undo, api.redo, api];
|
|
45
|
+
return [getCtx(name), initializeFirst, api.undo.bind(api), api.redo.bind(api), api];
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
const reducerCache: { [key: string]: WeakMap<any, any> } = {};
|
package/src/create-context.ts
CHANGED
|
@@ -49,7 +49,7 @@ export class CreateContext {
|
|
|
49
49
|
undo() {
|
|
50
50
|
const prev = this.#history[this.#activeVersion - 1];
|
|
51
51
|
if (prev) {
|
|
52
|
-
this.setState(prev, this.#history.indexOf(prev));
|
|
52
|
+
this.setState(prev, this.#history.indexOf(prev), true);
|
|
53
53
|
this.#needsReset = true;
|
|
54
54
|
this.#trigger()
|
|
55
55
|
}
|
|
@@ -64,7 +64,7 @@ export class CreateContext {
|
|
|
64
64
|
const nextIndex = this.#activeVersion + 1;
|
|
65
65
|
const next = this.#history[nextIndex];
|
|
66
66
|
if (next) {
|
|
67
|
-
this.setState(next, nextIndex);
|
|
67
|
+
this.setState(next, nextIndex, true);
|
|
68
68
|
this.#needsReset = true;
|
|
69
69
|
this.#trigger()
|
|
70
70
|
}
|
|
@@ -74,13 +74,15 @@ export class CreateContext {
|
|
|
74
74
|
return !!this.#history[this.#activeVersion + 1]
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
setState(state: state, _av?: number): void {
|
|
77
|
+
setState(state: state, _av?: number, _skipPush?: boolean): void {
|
|
78
78
|
if (typeof _av === 'undefined') {
|
|
79
79
|
if(this.#history.length > 0 && (this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
80
80
|
this.#history.splice(this.#activeVersion + 1, this.#history.length)
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
|
|
83
|
+
if(!_skipPush) {
|
|
84
|
+
this.#history.push(state);
|
|
85
|
+
}
|
|
84
86
|
if (this.#history.length > this.settings.historySize!) {
|
|
85
87
|
this.#history.shift();
|
|
86
88
|
}
|
package/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|