extra-react-store 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 BlackGlory <woshenmedoubuzhidao@blackglory.me>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # extra-react-store
2
+ ## Install
3
+ ```sh
4
+ npm install --save extra-react-store
5
+ # or
6
+ yarn add extra-react-store
7
+ ```
8
+
9
+ ## Usage
10
+ ```tsx
11
+ import { useMemo } from 'react'
12
+ import { createStoreContext, Store, useSelector, useUpdater } from 'extra-react-store'
13
+
14
+ interface IState {
15
+ count: number
16
+ }
17
+
18
+ const context = createStoreContext<IState>()
19
+
20
+ function App() {
21
+ const store = useMemo<Store<IState>>(() => new Store({ count: 0 }), [])
22
+
23
+ return (
24
+ <context.Provider value={store}>
25
+ <Viewer />
26
+ <Controller />
27
+ </context.Provider>
28
+ )
29
+ }
30
+
31
+ function Viewer() {
32
+ const count = useSelector(context, state => state.count)
33
+
34
+ return (
35
+ <span>{count}</span>
36
+ )
37
+ }
38
+
39
+ function Controller() {
40
+ const update = useUpdater<IState>(context)
41
+
42
+ return (
43
+ <button onClick={() => update(increment)}>Increment</button>
44
+ )
45
+ }
46
+
47
+ function increment(state: IState): void {
48
+ state.count++
49
+ }
50
+ ```
51
+
52
+ ## API
53
+ ```ts
54
+ type StoreContext<State> = React.Context<IStore<State>>
55
+
56
+ interface IStore<State> {
57
+ getState(): State
58
+ setState(newState: State): void
59
+ subscribe(fn: (state: State) => void): () => void
60
+ }
61
+ ```
62
+
63
+ ### Store
64
+ ```ts
65
+ class Store<State> implements IStore<State> {
66
+ constructor(initialState: State)
67
+ }
68
+ ```
69
+
70
+ ### createStoreContext
71
+ ```ts
72
+ function createStoreContext<State>(): StoreContext<State>
73
+ ```
74
+
75
+ ### useSelector
76
+ ```ts
77
+ function useSelector<State, Value>(
78
+ context: StoreContext<State>
79
+ , selector: (state: State) => Value
80
+ ): Value
81
+ ```
82
+
83
+ ### useUpdater
84
+ ```ts
85
+ import { Updater } from 'use-immer'
86
+
87
+ function useUpdater<State>(context: StoreContext<State>): Updater<State>
88
+ ```
@@ -0,0 +1,2 @@
1
+ import { StoreContext } from './types.js';
2
+ export declare function createStoreContext<State>(): StoreContext<State>;
@@ -0,0 +1,6 @@
1
+ import { createContext } from 'react';
2
+ export function createStoreContext() {
3
+ const context = createContext({});
4
+ return context;
5
+ }
6
+ //# sourceMappingURL=create-store-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-store-context.js","sourceRoot":"","sources":["../src/create-store-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAGrC,MAAM,UAAU,kBAAkB;IAChC,MAAM,OAAO,GAAG,aAAa,CAAgB,EAAmB,CAAC,CAAA;IACjE,OAAO,OAAO,CAAA;AAChB,CAAC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './types.js';
2
+ export * from './store.js';
3
+ export * from './create-store-context.js';
4
+ export * from './use-selector.js';
5
+ export * from './use-updater.js';
package/lib/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from './types.js';
2
+ export * from './store.js';
3
+ export * from './create-store-context.js';
4
+ export * from './use-selector.js';
5
+ export * from './use-updater.js';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,2BAA2B,CAAA;AACzC,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA"}
package/lib/store.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { IStore } from './types.js';
2
+ export declare class Store<State> implements IStore<State> {
3
+ private state;
4
+ private emitter;
5
+ constructor(initialState: State);
6
+ getState(): State;
7
+ setState(newState: State): void;
8
+ subscribe(fn: (state: State) => void): () => void;
9
+ }
package/lib/store.js ADDED
@@ -0,0 +1,18 @@
1
+ import { Emitter } from '@blackglory/structures';
2
+ export class Store {
3
+ constructor(initialState) {
4
+ this.emitter = new Emitter();
5
+ this.state = initialState;
6
+ }
7
+ getState() {
8
+ return this.state;
9
+ }
10
+ setState(newState) {
11
+ this.state = newState;
12
+ this.emitter.emit('stateChanged', newState);
13
+ }
14
+ subscribe(fn) {
15
+ return this.emitter.on('stateChanged', fn);
16
+ }
17
+ }
18
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAGhD,MAAM,OAAO,KAAK;IAIhB,YAAY,YAAmB;QAFvB,YAAO,GAAG,IAAI,OAAO,EAAoC,CAAA;QAG/D,IAAI,CAAC,KAAK,GAAG,YAAY,CAAA;IAC3B,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;IAED,QAAQ,CAAC,QAAe;QACtB,IAAI,CAAC,KAAK,GAAG,QAAQ,CAAA;QACrB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAA;IAC7C,CAAC;IAED,SAAS,CAAC,EAA0B;QAClC,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,cAAc,EAAE,EAAE,CAAC,CAAA;IAC5C,CAAC;CACF"}
package/lib/types.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /// <reference types="react" />
2
+ export type StoreContext<State> = React.Context<IStore<State>>;
3
+ export interface IStore<State> {
4
+ getState(): State;
5
+ setState(newState: State): void;
6
+ subscribe(fn: (state: State) => void): () => void;
7
+ }
package/lib/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ import { StoreContext } from './types.js';
2
+ export declare function useSelector<State, Value>(context: StoreContext<State>, selector: (state: State) => Value): Value;
@@ -0,0 +1,13 @@
1
+ import { useState, useContext, useEffect } from 'react';
2
+ export function useSelector(context, selector) {
3
+ const store = useContext(context);
4
+ const [value, setValue] = useState(() => selector(store.getState()));
5
+ useEffect(() => {
6
+ return store.subscribe(state => {
7
+ const newValue = selector(state);
8
+ setValue(newValue);
9
+ });
10
+ }, [store, value]);
11
+ return value;
12
+ }
13
+ //# sourceMappingURL=use-selector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-selector.js","sourceRoot":"","sources":["../src/use-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAGvD,MAAM,UAAU,WAAW,CACzB,OAA4B,EAC5B,QAAiC;IAEjC,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IACjC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAA;IAEpE,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE;YAC7B,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;YAGhC,QAAQ,CAAC,QAAQ,CAAC,CAAA;QACpB,CAAC,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;IAElB,OAAO,KAAK,CAAA;AACd,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { Updater } from 'use-immer';
2
+ import { StoreContext } from './types.js';
3
+ export declare function useUpdater<State>(context: StoreContext<State>): Updater<State>;
@@ -0,0 +1,22 @@
1
+ import { useCallback, useContext } from 'react';
2
+ import { produce } from 'immer';
3
+ import { go, isFunction } from '@blackglory/prelude';
4
+ export function useUpdater(context) {
5
+ const store = useContext(context);
6
+ const updater = useCallback(newStateOrFn => {
7
+ const state = go(() => {
8
+ if (isFunction(newStateOrFn)) {
9
+ const fn = newStateOrFn;
10
+ const newState = produce(store.getState(), fn);
11
+ return newState;
12
+ }
13
+ else {
14
+ const newState = newStateOrFn;
15
+ return newState;
16
+ }
17
+ });
18
+ store.setState(state);
19
+ }, [store]);
20
+ return updater;
21
+ }
22
+ //# sourceMappingURL=use-updater.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-updater.js","sourceRoot":"","sources":["../src/use-updater.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAE/C,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC/B,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAGpD,MAAM,UAAU,UAAU,CAAQ,OAA4B;IAC5D,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IAEjC,MAAM,OAAO,GAAmB,WAAW,CAAC,YAAY,CAAC,EAAE;QACzD,MAAM,KAAK,GAAG,EAAE,CAAC,GAAG,EAAE;YACpB,IAAI,UAAU,CAAC,YAAY,CAAC,EAAE;gBAC5B,MAAM,EAAE,GAAG,YAAY,CAAA;gBACvB,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAA;gBAC9C,OAAO,QAAQ,CAAA;aAChB;iBAAM;gBACL,MAAM,QAAQ,GAAG,YAAY,CAAA;gBAC7B,OAAO,QAAQ,CAAA;aAChB;QACH,CAAC,CAAC,CAAA;QACF,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;IACvB,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAA;IAEX,OAAO,OAAO,CAAA;AAChB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "extra-react-store",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "keywords": [],
6
+ "files": [
7
+ "lib",
8
+ "src"
9
+ ],
10
+ "type": "module",
11
+ "main": "lib/index.js",
12
+ "types": "lib/index.d.ts",
13
+ "sideEffects": false,
14
+ "engines": {
15
+ "node": ">=16"
16
+ },
17
+ "repository": "git@github.com:BlackGlory/extra-react-store.git",
18
+ "author": "BlackGlory <woshenmedoubuzhidao@blackglory.me>",
19
+ "license": "MIT",
20
+ "scripts": {
21
+ "prepare": "ts-patch install -s",
22
+ "lint": "eslint --ext .js,.jsx,.ts,.tsx --quiet src __tests__",
23
+ "test": "vitest --run",
24
+ "prepublishOnly": "run-s prepare clean build",
25
+ "clean": "rimraf lib",
26
+ "build": "tsc --project tsconfig.build.json --outDir lib",
27
+ "release": "standard-version"
28
+ },
29
+ "husky": {
30
+ "hooks": {
31
+ "pre-commit": "run-s prepare lint build test",
32
+ "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
33
+ }
34
+ },
35
+ "devDependencies": {
36
+ "@commitlint/cli": "^17.6.5",
37
+ "@commitlint/config-conventional": "^17.6.5",
38
+ "@testing-library/react": "^14.0.0",
39
+ "@types/react": "^18.2.11",
40
+ "@typescript-eslint/eslint-plugin": "^5.59.9",
41
+ "@typescript-eslint/parser": "^5.59.9",
42
+ "@vitejs/plugin-react": "^4.0.3",
43
+ "eslint": "^8.42.0",
44
+ "eslint-plugin-react": "^7.32.2",
45
+ "eslint-plugin-react-hooks": "^4.6.0",
46
+ "extra-react-hooks": "^0.6.3",
47
+ "husky": "4",
48
+ "immer": "^10.0.2",
49
+ "jsdom": "^22.1.0",
50
+ "npm-run-all": "^4.1.5",
51
+ "react": "^18.2.0",
52
+ "react-dom": "^18.2.0",
53
+ "rimraf": "^4.3.0",
54
+ "standard-version": "^9.3.2",
55
+ "ts-patch": "^2.1.0",
56
+ "tslib": "^2.5.3",
57
+ "typescript": "4.9.5",
58
+ "typescript-transform-paths": "^3.4.6",
59
+ "vite": "^4.4.3",
60
+ "vite-tsconfig-paths": "^4.2.0",
61
+ "vitest": "^0.33.0"
62
+ },
63
+ "dependencies": {
64
+ "@blackglory/prelude": "^0.3.3",
65
+ "@blackglory/structures": "^0.13.4",
66
+ "use-immer": "^0.9.0"
67
+ },
68
+ "peerDependencies": {
69
+ "immer": "^10.0.2",
70
+ "react": "^18"
71
+ }
72
+ }
@@ -0,0 +1,7 @@
1
+ import { createContext } from 'react'
2
+ import { IStore, StoreContext } from './types.js'
3
+
4
+ export function createStoreContext<State>(): StoreContext<State> {
5
+ const context = createContext<IStore<State>>({} as IStore<State>)
6
+ return context
7
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './types.js'
2
+ export * from './store.js'
3
+ export * from './create-store-context.js'
4
+ export * from './use-selector.js'
5
+ export * from './use-updater.js'
package/src/store.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { Emitter } from '@blackglory/structures'
2
+ import { IStore } from './types.js'
3
+
4
+ export class Store<State> implements IStore<State> {
5
+ private state: State
6
+ private emitter = new Emitter<{ stateChanged: [state: State] }>()
7
+
8
+ constructor(initialState: State) {
9
+ this.state = initialState
10
+ }
11
+
12
+ getState(): State {
13
+ return this.state
14
+ }
15
+
16
+ setState(newState: State): void {
17
+ this.state = newState
18
+ this.emitter.emit('stateChanged', newState)
19
+ }
20
+
21
+ subscribe(fn: (state: State) => void): () => void {
22
+ return this.emitter.on('stateChanged', fn)
23
+ }
24
+ }
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ export type StoreContext<State> = React.Context<IStore<State>>
2
+
3
+ export interface IStore<State> {
4
+ getState(): State
5
+ setState(newState: State): void
6
+ subscribe(fn: (state: State) => void): () => void
7
+ }
@@ -0,0 +1,21 @@
1
+ import { useState, useContext, useEffect } from 'react'
2
+ import { StoreContext } from './types.js'
3
+
4
+ export function useSelector<State, Value>(
5
+ context: StoreContext<State>
6
+ , selector: (state: State) => Value
7
+ ): Value {
8
+ const store = useContext(context)
9
+ const [value, setValue] = useState(() => selector(store.getState()))
10
+
11
+ useEffect(() => {
12
+ return store.subscribe(state => {
13
+ const newValue = selector(state)
14
+
15
+ // 若新旧状态引用相等, 不会发生重新渲染.
16
+ setValue(newValue)
17
+ })
18
+ }, [store, value])
19
+
20
+ return value
21
+ }
@@ -0,0 +1,25 @@
1
+ import { useCallback, useContext } from 'react'
2
+ import { Updater } from 'use-immer'
3
+ import { produce } from 'immer'
4
+ import { go, isFunction } from '@blackglory/prelude'
5
+ import { StoreContext } from './types.js'
6
+
7
+ export function useUpdater<State>(context: StoreContext<State>): Updater<State> {
8
+ const store = useContext(context)
9
+
10
+ const updater: Updater<State> = useCallback(newStateOrFn => {
11
+ const state = go(() => {
12
+ if (isFunction(newStateOrFn)) {
13
+ const fn = newStateOrFn
14
+ const newState = produce(store.getState(), fn)
15
+ return newState
16
+ } else {
17
+ const newState = newStateOrFn
18
+ return newState
19
+ }
20
+ })
21
+ store.setState(state)
22
+ }, [store])
23
+
24
+ return updater
25
+ }