react-usectx 1.0.8 → 1.1.0
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 +8 -0
- package/context.js +12 -7
- package/create-context.js +5 -0
- package/package.json +1 -1
- package/src/context.ts +18 -7
- package/src/create-context.ts +7 -9
- package/src/types.ts +9 -0
package/README.md
CHANGED
|
@@ -71,6 +71,14 @@ When any of the provided functions—useCtx, updateCtx, or getCtx—are invoked,
|
|
|
71
71
|
|
|
72
72
|
This mechanism enables global state sharing across components. For example, calling getCtx("example-1") in one component will return the same state reference when called with "example-1" in any other component within the same document. This allows for seamless state synchronization across different parts of your application without the need for explicit context providers.
|
|
73
73
|
|
|
74
|
+
### initialState
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
useCtx(myContextID, { name: "John" });
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The value you want the state to be initially. It can be a value of any type.
|
|
81
|
+
|
|
74
82
|
## Example
|
|
75
83
|
|
|
76
84
|
```js
|
package/context.js
CHANGED
|
@@ -2,14 +2,19 @@
|
|
|
2
2
|
import { useSyncExternalStore } from "react";
|
|
3
3
|
import { CreateContext } from "./create-context";
|
|
4
4
|
const cache = {};
|
|
5
|
-
const getInstance = (name) => {
|
|
5
|
+
const getInstance = (name, initialState) => {
|
|
6
6
|
if (!cache[name]) {
|
|
7
|
-
cache[name] = new CreateContext();
|
|
7
|
+
cache[name] = new CreateContext(initialState);
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
if (typeof initialState !== "undefined") {
|
|
11
|
+
cache[name].commit(initialState);
|
|
12
|
+
}
|
|
8
13
|
}
|
|
9
14
|
return cache[name];
|
|
10
15
|
};
|
|
11
|
-
export function updateCtx(name) {
|
|
12
|
-
const ctx = getInstance(name);
|
|
16
|
+
export function updateCtx(name, initialState) {
|
|
17
|
+
const ctx = getInstance(name, initialState);
|
|
13
18
|
return ctx.commit.bind(ctx);
|
|
14
19
|
}
|
|
15
20
|
export function getCtx(name) {
|
|
@@ -20,11 +25,11 @@ export function getCtx(name) {
|
|
|
20
25
|
};
|
|
21
26
|
return useSyncExternalStore(subscription, context.getState.bind(context), context.getState.bind(context));
|
|
22
27
|
}
|
|
23
|
-
export function useCtx(name) {
|
|
24
|
-
return [getCtx(name), updateCtx(name)];
|
|
28
|
+
export function useCtx(name, initialState) {
|
|
29
|
+
return [getCtx(name), updateCtx(name, initialState)];
|
|
25
30
|
}
|
|
26
31
|
const reducerCache = {};
|
|
27
|
-
export function useReducer(name, modiFier, useCache =
|
|
32
|
+
export function useReducer(name, modiFier, useCache = false) {
|
|
28
33
|
if (!reducerCache[name]) {
|
|
29
34
|
reducerCache[name] = new WeakMap();
|
|
30
35
|
}
|
package/create-context.js
CHANGED
package/package.json
CHANGED
package/src/context.ts
CHANGED
|
@@ -2,20 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
import { ReactNode, useSyncExternalStore } from "react";
|
|
4
4
|
import { CreateContext } from "./create-context";
|
|
5
|
+
import { state } from "./types";
|
|
5
6
|
|
|
6
7
|
type cache = { [key: string]: InstanceType<typeof CreateContext> };
|
|
7
8
|
|
|
8
9
|
const cache: cache = {};
|
|
9
10
|
|
|
10
|
-
const getInstance = (
|
|
11
|
+
const getInstance = (
|
|
12
|
+
name: string,
|
|
13
|
+
initialState?: state
|
|
14
|
+
): InstanceType<typeof CreateContext> => {
|
|
11
15
|
if (!cache[name]) {
|
|
12
|
-
cache[name] = new CreateContext();
|
|
16
|
+
cache[name] = new CreateContext(initialState);
|
|
17
|
+
} else {
|
|
18
|
+
if (typeof initialState !== "undefined") {
|
|
19
|
+
cache[name].commit(initialState);
|
|
20
|
+
}
|
|
13
21
|
}
|
|
14
22
|
return cache[name];
|
|
15
23
|
};
|
|
16
24
|
|
|
17
|
-
export function updateCtx(name: string): Function {
|
|
18
|
-
const ctx = getInstance(name);
|
|
25
|
+
export function updateCtx(name: string, initialState?: state): Function {
|
|
26
|
+
const ctx = getInstance(name, initialState);
|
|
19
27
|
return ctx.commit.bind(ctx);
|
|
20
28
|
}
|
|
21
29
|
|
|
@@ -32,8 +40,11 @@ export function getCtx<Snapshot>(name: string): Snapshot {
|
|
|
32
40
|
);
|
|
33
41
|
}
|
|
34
42
|
|
|
35
|
-
export function useCtx(
|
|
36
|
-
|
|
43
|
+
export function useCtx(
|
|
44
|
+
name: string,
|
|
45
|
+
initialState?: state
|
|
46
|
+
): [ReactNode, Function] {
|
|
47
|
+
return [getCtx(name), updateCtx(name, initialState)];
|
|
37
48
|
}
|
|
38
49
|
|
|
39
50
|
const reducerCache: { [key: string]: WeakMap<any, any> } = {};
|
|
@@ -41,7 +52,7 @@ const reducerCache: { [key: string]: WeakMap<any, any> } = {};
|
|
|
41
52
|
export function useReducer<Snapshot>(
|
|
42
53
|
name: string,
|
|
43
54
|
modiFier: (data: any) => Snapshot,
|
|
44
|
-
useCache: boolean =
|
|
55
|
+
useCache: boolean = false
|
|
45
56
|
): Snapshot {
|
|
46
57
|
if (!reducerCache[name]) {
|
|
47
58
|
reducerCache[name] = new WeakMap();
|
package/src/create-context.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
type state =
|
|
4
|
-
| { [key: string]: any }
|
|
5
|
-
| Array<any>
|
|
6
|
-
| string
|
|
7
|
-
| number
|
|
8
|
-
| boolean
|
|
9
|
-
| null;
|
|
1
|
+
import { eventHandlers, state } from "./types";
|
|
10
2
|
|
|
11
3
|
export class CreateContext {
|
|
4
|
+
constructor(initialState?: state) {
|
|
5
|
+
if (initialState !== undefined) {
|
|
6
|
+
this.setState(initialState);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
12
10
|
#eventHandlers: eventHandlers = [];
|
|
13
11
|
|
|
14
12
|
#state: state = null;
|