react-usectx 1.2.1 → 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/README.md +74 -1
- package/context.js +5 -6
- package/create-context.js +45 -3
- package/package.json +1 -1
- package/src/context.ts +6 -5
- package/src/create-context.ts +55 -3
- package/types.js +1 -0
- /package/{babel.config.js → babel.config.cjs} +0 -0
package/README.md
CHANGED
|
@@ -79,6 +79,79 @@ useGlobalState(myContextID, { name: "John" });
|
|
|
79
79
|
|
|
80
80
|
The value you want the state to be initially. It can be a value of any type.
|
|
81
81
|
|
|
82
|
+
|
|
83
|
+
### API Methods
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
const api = stateApi(myContextID);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Manualy control the flow of the data.
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
### subscribe
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
api.subscribe(data => {
|
|
96
|
+
console.log(data)
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Subscribe for changes
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
### unsubscribe
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
const subscription = data => {
|
|
107
|
+
console.log(data)
|
|
108
|
+
}
|
|
109
|
+
api.unsubscribe(subscription);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
unsubscribe from specific subscription
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
### commit
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
api.commit({
|
|
119
|
+
my: "data"
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Sets data, and triggers all subscriptions and react hooks.
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
### setState
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
api.setState({
|
|
130
|
+
my: "data"
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Sets data 'silently', without triggering anything.
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
### undo
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
api.undo();
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Sets state to previous (if any), and triggers all subscriptions and react hooks.
|
|
144
|
+
|
|
145
|
+
### redo
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
api.redo();
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Sets data to next state if undo was called previously, and triggers all subscriptions and react hooks.
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
82
155
|
## Example
|
|
83
156
|
|
|
84
157
|
```js
|
|
@@ -102,7 +175,7 @@ function Page() {
|
|
|
102
175
|
}
|
|
103
176
|
|
|
104
177
|
function Title() {
|
|
105
|
-
const title: string =
|
|
178
|
+
const title: string = getGlobalState("title");
|
|
106
179
|
return (
|
|
107
180
|
<h1>
|
|
108
181
|
Component: <br />
|
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) {
|
|
@@ -51,3 +47,6 @@ export function useReducer(name, modiFier, useCache = false) {
|
|
|
51
47
|
};
|
|
52
48
|
return useSyncExternalStore(subscription, getState, getState);
|
|
53
49
|
}
|
|
50
|
+
export function stateApi(name) {
|
|
51
|
+
return getInstance(name);
|
|
52
|
+
}
|
package/create-context.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
export class CreateContext {
|
|
2
|
-
constructor(initialState) {
|
|
2
|
+
constructor(initialState, options = {}) {
|
|
3
|
+
const defaults = {
|
|
4
|
+
historySize: 999
|
|
5
|
+
};
|
|
6
|
+
this.settings = Object.assign({}, defaults, options);
|
|
3
7
|
if (initialState !== undefined) {
|
|
4
8
|
this.setState(initialState);
|
|
5
9
|
}
|
|
6
10
|
}
|
|
11
|
+
settings;
|
|
7
12
|
#eventHandlers = [];
|
|
8
13
|
#state = null;
|
|
14
|
+
#history = [];
|
|
15
|
+
#activeVersion = -1;
|
|
16
|
+
#needsReset = false;
|
|
9
17
|
subscribe(handler) {
|
|
10
18
|
this.#eventHandlers.push(handler);
|
|
11
19
|
}
|
|
@@ -17,9 +25,43 @@ export class CreateContext {
|
|
|
17
25
|
}
|
|
18
26
|
commit(state) {
|
|
19
27
|
this.setState(state);
|
|
20
|
-
this.#
|
|
28
|
+
this.#trigger();
|
|
21
29
|
}
|
|
22
|
-
|
|
30
|
+
#trigger() {
|
|
31
|
+
this.#eventHandlers.forEach((callback) => callback.call(undefined, this.getState()));
|
|
32
|
+
}
|
|
33
|
+
undo() {
|
|
34
|
+
const prev = this.#history[this.#activeVersion - 1];
|
|
35
|
+
if (prev) {
|
|
36
|
+
this.setState(prev, this.#history.indexOf(prev));
|
|
37
|
+
this.#needsReset = true;
|
|
38
|
+
this.#trigger();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
redo() {
|
|
42
|
+
const next = this.#history[this.#activeVersion + 1];
|
|
43
|
+
if (next) {
|
|
44
|
+
this.setState(next, this.#history.indexOf(next));
|
|
45
|
+
this.#needsReset = true;
|
|
46
|
+
this.#trigger();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
setState(state, _av) {
|
|
50
|
+
if (typeof _av === 'undefined') {
|
|
51
|
+
if ((this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
52
|
+
this.#history.splice(this.#activeVersion + 1, this.#history.length);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
this.#history.push(state);
|
|
56
|
+
if (this.#history.length > this.settings.historySize) {
|
|
57
|
+
this.#history.shift();
|
|
58
|
+
}
|
|
59
|
+
if (typeof _av === 'number') {
|
|
60
|
+
this.#activeVersion = _av;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.#activeVersion = this.#history.length - 1;
|
|
64
|
+
}
|
|
23
65
|
this.#state = state;
|
|
24
66
|
}
|
|
25
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> } = {};
|
|
@@ -75,3 +72,7 @@ export function useReducer<Snapshot>(
|
|
|
75
72
|
};
|
|
76
73
|
return useSyncExternalStore(subscription, getState, getState);
|
|
77
74
|
}
|
|
75
|
+
|
|
76
|
+
export function stateApi(name: string): InstanceType<typeof CreateContext> {
|
|
77
|
+
return getInstance(name)
|
|
78
|
+
}
|
package/src/create-context.ts
CHANGED
|
@@ -1,16 +1,31 @@
|
|
|
1
1
|
import { eventHandlers, state } from "./types";
|
|
2
2
|
|
|
3
|
+
|
|
4
|
+
export interface CreateContextOptions {
|
|
5
|
+
historySize?: number
|
|
6
|
+
}
|
|
7
|
+
|
|
3
8
|
export class CreateContext {
|
|
4
|
-
constructor(initialState?: state) {
|
|
9
|
+
constructor(initialState?: state, options: CreateContextOptions = {}) {
|
|
10
|
+
const defaults: CreateContextOptions = {
|
|
11
|
+
historySize: 999
|
|
12
|
+
}
|
|
13
|
+
this.settings = Object.assign({}, defaults, options)
|
|
5
14
|
if (initialState !== undefined) {
|
|
6
15
|
this.setState(initialState);
|
|
7
16
|
}
|
|
8
17
|
}
|
|
9
18
|
|
|
19
|
+
settings: CreateContextOptions;
|
|
20
|
+
|
|
10
21
|
#eventHandlers: eventHandlers = [];
|
|
11
22
|
|
|
12
23
|
#state: state = null;
|
|
13
24
|
|
|
25
|
+
#history: any[] = [];
|
|
26
|
+
#activeVersion:number = -1;
|
|
27
|
+
#needsReset: boolean = false;
|
|
28
|
+
|
|
14
29
|
subscribe(handler: Function): void {
|
|
15
30
|
this.#eventHandlers.push(handler);
|
|
16
31
|
}
|
|
@@ -24,10 +39,47 @@ export class CreateContext {
|
|
|
24
39
|
|
|
25
40
|
commit(state: state): void {
|
|
26
41
|
this.setState(state);
|
|
27
|
-
this.#
|
|
42
|
+
this.#trigger();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#trigger(): void {
|
|
46
|
+
this.#eventHandlers.forEach((callback) => callback.call(undefined, this.getState()));
|
|
28
47
|
}
|
|
29
48
|
|
|
30
|
-
|
|
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
|
+
}
|
|
58
|
+
redo() {
|
|
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
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setState(state: state, _av?: number): void {
|
|
68
|
+
if (typeof _av === 'undefined') {
|
|
69
|
+
if((this.#history.length - 1) > this.#activeVersion && this.#needsReset) {
|
|
70
|
+
this.#history.splice(this.#activeVersion + 1, this.#history.length)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
this.#history.push(state);
|
|
74
|
+
if (this.#history.length > this.settings.historySize!) {
|
|
75
|
+
this.#history.shift();
|
|
76
|
+
}
|
|
77
|
+
if (typeof _av === 'number') {
|
|
78
|
+
this.#activeVersion = _av;
|
|
79
|
+
} else {
|
|
80
|
+
this.#activeVersion = this.#history.length - 1;
|
|
81
|
+
}
|
|
82
|
+
|
|
31
83
|
this.#state = state;
|
|
32
84
|
}
|
|
33
85
|
|
package/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
File without changes
|