react-usectx 1.2.2 → 1.2.3
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 +2 -6
- package/create-context.js +14 -4
- package/package.json +1 -1
- package/src/context.ts +2 -5
- package/src/create-context.ts +22 -12
- package/types.js +1 -0
package/context.js
CHANGED
|
@@ -6,11 +6,6 @@ const getInstance = (name, initialState) => {
|
|
|
6
6
|
if (!cache[name]) {
|
|
7
7
|
cache[name] = new CreateContext(initialState);
|
|
8
8
|
}
|
|
9
|
-
else {
|
|
10
|
-
if (typeof initialState !== "undefined") {
|
|
11
|
-
cache[name].commit(initialState);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
9
|
return cache[name];
|
|
15
10
|
};
|
|
16
11
|
export function updateCtx(name, initialState) {
|
|
@@ -26,7 +21,8 @@ export function getCtx(name) {
|
|
|
26
21
|
return useSyncExternalStore(subscription, context.getState.bind(context), context.getState.bind(context));
|
|
27
22
|
}
|
|
28
23
|
export function useCtx(name, initialState) {
|
|
29
|
-
|
|
24
|
+
const initializeFirst = updateCtx(name, initialState);
|
|
25
|
+
return [getCtx(name), initializeFirst];
|
|
30
26
|
}
|
|
31
27
|
const reducerCache = {};
|
|
32
28
|
export function useReducer(name, modiFier, useCache = false) {
|
package/create-context.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export class CreateContext {
|
|
2
2
|
constructor(initialState, options = {}) {
|
|
3
3
|
const defaults = {
|
|
4
|
-
historySize:
|
|
4
|
+
historySize: 999
|
|
5
5
|
};
|
|
6
6
|
this.settings = Object.assign({}, defaults, options);
|
|
7
7
|
if (initialState !== undefined) {
|
|
@@ -13,6 +13,7 @@ export class CreateContext {
|
|
|
13
13
|
#state = null;
|
|
14
14
|
#history = [];
|
|
15
15
|
#activeVersion = -1;
|
|
16
|
+
#needsReset = false;
|
|
16
17
|
subscribe(handler) {
|
|
17
18
|
this.#eventHandlers.push(handler);
|
|
18
19
|
}
|
|
@@ -24,26 +25,32 @@ export class CreateContext {
|
|
|
24
25
|
}
|
|
25
26
|
commit(state) {
|
|
26
27
|
this.setState(state);
|
|
27
|
-
this.#
|
|
28
|
+
this.#trigger();
|
|
29
|
+
}
|
|
30
|
+
#trigger() {
|
|
31
|
+
this.#eventHandlers.forEach((callback) => callback.call(undefined, this.getState()));
|
|
28
32
|
}
|
|
29
33
|
undo() {
|
|
30
34
|
const prev = this.#history[this.#activeVersion - 1];
|
|
31
35
|
if (prev) {
|
|
32
36
|
this.setState(prev, this.#history.indexOf(prev));
|
|
37
|
+
this.#needsReset = true;
|
|
38
|
+
this.#trigger();
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
41
|
redo() {
|
|
36
42
|
const next = this.#history[this.#activeVersion + 1];
|
|
37
43
|
if (next) {
|
|
38
44
|
this.setState(next, this.#history.indexOf(next));
|
|
45
|
+
this.#needsReset = true;
|
|
46
|
+
this.#trigger();
|
|
39
47
|
}
|
|
40
48
|
}
|
|
41
49
|
setState(state, _av) {
|
|
42
50
|
if (typeof _av === 'undefined') {
|
|
43
|
-
if ((this.#history.length - 1) > this.#activeVersion) {
|
|
51
|
+
if ((this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
44
52
|
this.#history.splice(this.#activeVersion + 1, this.#history.length);
|
|
45
53
|
}
|
|
46
|
-
this.#activeVersion = this.#history.length - 1;
|
|
47
54
|
}
|
|
48
55
|
this.#history.push(state);
|
|
49
56
|
if (this.#history.length > this.settings.historySize) {
|
|
@@ -52,6 +59,9 @@ export class CreateContext {
|
|
|
52
59
|
if (typeof _av === 'number') {
|
|
53
60
|
this.#activeVersion = _av;
|
|
54
61
|
}
|
|
62
|
+
else {
|
|
63
|
+
this.#activeVersion = this.#history.length - 1;
|
|
64
|
+
}
|
|
55
65
|
this.#state = state;
|
|
56
66
|
}
|
|
57
67
|
getState() {
|
package/package.json
CHANGED
package/src/context.ts
CHANGED
|
@@ -14,10 +14,6 @@ const getInstance = (
|
|
|
14
14
|
): InstanceType<typeof CreateContext> => {
|
|
15
15
|
if (!cache[name]) {
|
|
16
16
|
cache[name] = new CreateContext(initialState);
|
|
17
|
-
} else {
|
|
18
|
-
if (typeof initialState !== "undefined") {
|
|
19
|
-
cache[name].commit(initialState);
|
|
20
|
-
}
|
|
21
17
|
}
|
|
22
18
|
return cache[name];
|
|
23
19
|
};
|
|
@@ -44,7 +40,8 @@ export function useCtx(
|
|
|
44
40
|
name: string,
|
|
45
41
|
initialState?: state
|
|
46
42
|
): [ReactNode, Function] {
|
|
47
|
-
|
|
43
|
+
const initializeFirst = updateCtx(name, initialState)
|
|
44
|
+
return [getCtx(name), initializeFirst];
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
const reducerCache: { [key: string]: WeakMap<any, any> } = {};
|
package/src/create-context.ts
CHANGED
|
@@ -24,6 +24,7 @@ export class CreateContext {
|
|
|
24
24
|
|
|
25
25
|
#history: any[] = [];
|
|
26
26
|
#activeVersion:number = -1;
|
|
27
|
+
#needsReset: boolean = false;
|
|
27
28
|
|
|
28
29
|
subscribe(handler: Function): void {
|
|
29
30
|
this.#eventHandlers.push(handler);
|
|
@@ -38,29 +39,36 @@ export class CreateContext {
|
|
|
38
39
|
|
|
39
40
|
commit(state: state): void {
|
|
40
41
|
this.setState(state);
|
|
41
|
-
this.#
|
|
42
|
+
this.#trigger();
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if(prev) {
|
|
47
|
-
this.setState(prev, this.#history.indexOf(prev))
|
|
48
|
-
}
|
|
45
|
+
#trigger(): void {
|
|
46
|
+
this.#eventHandlers.forEach((callback) => callback.call(undefined, this.getState()));
|
|
49
47
|
}
|
|
50
48
|
|
|
49
|
+
undo() {
|
|
50
|
+
const prev = this.#history[this.#activeVersion - 1];
|
|
51
|
+
if (prev) {
|
|
52
|
+
this.setState(prev, this.#history.indexOf(prev));
|
|
53
|
+
this.#needsReset = true;
|
|
54
|
+
this.#trigger()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
}
|
|
51
58
|
redo() {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
59
|
+
const next = this.#history[this.#activeVersion + 1];
|
|
60
|
+
if (next) {
|
|
61
|
+
this.setState(next, this.#history.indexOf(next));
|
|
62
|
+
this.#needsReset = true;
|
|
63
|
+
this.#trigger()
|
|
64
|
+
}
|
|
56
65
|
}
|
|
57
66
|
|
|
58
67
|
setState(state: state, _av?: number): void {
|
|
59
68
|
if (typeof _av === 'undefined') {
|
|
60
|
-
if((this.#history.length - 1) > this.#activeVersion) {
|
|
69
|
+
if((this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
61
70
|
this.#history.splice(this.#activeVersion + 1, this.#history.length)
|
|
62
71
|
}
|
|
63
|
-
this.#activeVersion = this.#history.length - 1;
|
|
64
72
|
}
|
|
65
73
|
this.#history.push(state);
|
|
66
74
|
if (this.#history.length > this.settings.historySize!) {
|
|
@@ -68,6 +76,8 @@ export class CreateContext {
|
|
|
68
76
|
}
|
|
69
77
|
if (typeof _av === 'number') {
|
|
70
78
|
this.#activeVersion = _av;
|
|
79
|
+
} else {
|
|
80
|
+
this.#activeVersion = this.#history.length - 1;
|
|
71
81
|
}
|
|
72
82
|
|
|
73
83
|
this.#state = state;
|
package/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|