@hefang/react-store 0.0.1
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 +15 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +61 -0
- package/package.json +22 -0
- package/src/index.ts +91 -0
- package/tsconfig.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @hefang/store
|
|
2
|
+
|
|
3
|
+
To install dependencies:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
To run:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun run src/index.ts
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This project was created using `bun init` in bun v1.3.11. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface CreateStoreOptions {
|
|
2
|
+
deepCompareFunc: (a: unknown, b: unknown) => boolean;
|
|
3
|
+
}
|
|
4
|
+
export declare function createStore<V extends Record<string, unknown>>(defaultValue?: V, options?: Partial<CreateStoreOptions>): readonly [() => V, (newValues: Partial<V> | ((prev: V) => Partial<V>), { immediately, deepCompare, }?: {
|
|
5
|
+
immediately?: boolean;
|
|
6
|
+
deepCompare?: boolean;
|
|
7
|
+
}) => void, () => V];
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
export function createStore(defaultValue, options) {
|
|
3
|
+
const opt = {
|
|
4
|
+
deepCompareFunc: Object.is,
|
|
5
|
+
...options,
|
|
6
|
+
};
|
|
7
|
+
const store = {
|
|
8
|
+
queues: 0,
|
|
9
|
+
queueTimer: 0,
|
|
10
|
+
value: { ...defaultValue },
|
|
11
|
+
listeners: new Set(),
|
|
12
|
+
};
|
|
13
|
+
function subscribe(onStoreChange) {
|
|
14
|
+
store.listeners.add(onStoreChange);
|
|
15
|
+
return () => {
|
|
16
|
+
store.listeners.delete(onStoreChange);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function getSnapshot() {
|
|
20
|
+
return store.value;
|
|
21
|
+
}
|
|
22
|
+
function getServerSnapshot() {
|
|
23
|
+
return store.value;
|
|
24
|
+
}
|
|
25
|
+
function emitChanges() {
|
|
26
|
+
store.queues = 0;
|
|
27
|
+
store.listeners.forEach((listener) => listener());
|
|
28
|
+
if (store.queueTimer) {
|
|
29
|
+
window.clearTimeout(store.queueTimer);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function setStore(newValues, { immediately, deepCompare, } = {}) {
|
|
33
|
+
const values = typeof newValues === "function" ? newValues(store.value) : newValues;
|
|
34
|
+
if (deepCompare) {
|
|
35
|
+
Object.keys(values).forEach((key) => {
|
|
36
|
+
if (!opt.deepCompareFunc(values[key], store.value[key])) {
|
|
37
|
+
store.value = { ...store.value, [key]: values[key] };
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
store.value = { ...store.value, ...values };
|
|
43
|
+
}
|
|
44
|
+
if (immediately) {
|
|
45
|
+
emitChanges();
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
if (!store.queues) {
|
|
49
|
+
queueMicrotask(emitChanges);
|
|
50
|
+
}
|
|
51
|
+
store.queues++;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function useStore() {
|
|
55
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
56
|
+
}
|
|
57
|
+
function getStore() {
|
|
58
|
+
return store.value;
|
|
59
|
+
}
|
|
60
|
+
return [useStore, setStore, getStore];
|
|
61
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hefang/react-store",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"require": "./dist/index.js",
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"default": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "bun tsc --noEmit false",
|
|
13
|
+
"prepack": "bun run build"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/react": "^18"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"typescript": "^5",
|
|
20
|
+
"react": "^18"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
|
|
3
|
+
interface Store<V extends Record<string, unknown>> {
|
|
4
|
+
value: V;
|
|
5
|
+
queueTimer: number;
|
|
6
|
+
listeners: Set<() => void>;
|
|
7
|
+
queues: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface CreateStoreOptions {
|
|
11
|
+
deepCompareFunc: (a: unknown, b: unknown) => boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createStore<V extends Record<string, unknown>>(
|
|
15
|
+
defaultValue?: V,
|
|
16
|
+
options?: Partial<CreateStoreOptions>,
|
|
17
|
+
) {
|
|
18
|
+
const opt: CreateStoreOptions = {
|
|
19
|
+
deepCompareFunc: Object.is,
|
|
20
|
+
...options,
|
|
21
|
+
};
|
|
22
|
+
const store: Store<V> = {
|
|
23
|
+
queues: 0,
|
|
24
|
+
queueTimer: 0,
|
|
25
|
+
value: { ...defaultValue } as V,
|
|
26
|
+
listeners: new Set<() => void>(),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function subscribe(onStoreChange: () => void) {
|
|
30
|
+
store.listeners.add(onStoreChange);
|
|
31
|
+
return () => {
|
|
32
|
+
store.listeners.delete(onStoreChange);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getSnapshot() {
|
|
37
|
+
return store.value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getServerSnapshot() {
|
|
41
|
+
return store.value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function emitChanges() {
|
|
45
|
+
store.queues = 0;
|
|
46
|
+
store.listeners.forEach((listener) => listener());
|
|
47
|
+
if (store.queueTimer) {
|
|
48
|
+
window.clearTimeout(store.queueTimer);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function setStore(
|
|
53
|
+
newValues: Partial<V> | ((prev: V) => Partial<V>),
|
|
54
|
+
{
|
|
55
|
+
immediately,
|
|
56
|
+
deepCompare,
|
|
57
|
+
}: { immediately?: boolean; deepCompare?: boolean } = {},
|
|
58
|
+
) {
|
|
59
|
+
const values =
|
|
60
|
+
typeof newValues === "function" ? newValues(store.value) : newValues;
|
|
61
|
+
|
|
62
|
+
if (deepCompare) {
|
|
63
|
+
Object.keys(values).forEach((key) => {
|
|
64
|
+
if (!opt.deepCompareFunc(values[key], store.value[key])) {
|
|
65
|
+
store.value = { ...store.value, [key]: values[key] };
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
} else {
|
|
69
|
+
store.value = { ...store.value, ...values };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (immediately) {
|
|
73
|
+
emitChanges();
|
|
74
|
+
} else {
|
|
75
|
+
if (!store.queues) {
|
|
76
|
+
queueMicrotask(emitChanges);
|
|
77
|
+
}
|
|
78
|
+
store.queues++;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function useStore() {
|
|
83
|
+
return useSyncExternalStore<V>(subscribe, getSnapshot, getServerSnapshot);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function getStore() {
|
|
87
|
+
return store.value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return [useStore, setStore, getStore] as const;
|
|
91
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Environment setup & latest features
|
|
4
|
+
"lib": ["ESNext", "DOM"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"module": "Preserve",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
|
|
11
|
+
// Bundler mode
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"allowImportingTsExtensions": false,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"noEmit": true,
|
|
16
|
+
|
|
17
|
+
// Best practices
|
|
18
|
+
"strict": true,
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"noFallthroughCasesInSwitch": true,
|
|
21
|
+
"noUncheckedIndexedAccess": true,
|
|
22
|
+
"noImplicitOverride": true,
|
|
23
|
+
|
|
24
|
+
// Some stricter flags (disabled by default)
|
|
25
|
+
"noUnusedLocals": false,
|
|
26
|
+
"noUnusedParameters": false,
|
|
27
|
+
"noPropertyAccessFromIndexSignature": false,
|
|
28
|
+
"declaration": true,
|
|
29
|
+
"outDir": "dist"
|
|
30
|
+
}
|
|
31
|
+
}
|