Skip to content

Commit afb432b

Browse files
committed
feat(useStore): Add depAction argument.
1 parent 91cdad0 commit afb432b

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-model · ![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg) [![npm version](https://img.shields.io/npm/v/react-model.svg?style=flat)](https://www.npmjs.com/package/react-model) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js) [![install size](https://packagephobia.now.sh/badge?p=react-model)](https://packagephobia.now.sh/result?p=react-model) ![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)
1+
# react-model · ![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg) [![npm version](https://img.shields.io/npm/v/react-model.svg?style=flat)](https://www.npmjs.com/package/react-model) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js) [![downloads](https://img.shields.io/npm/dt/react-model.svg)](https://www.npmjs.com/package/react-model) ![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)
22

33
The State management library for React
44

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-model",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "The State management library for React",
55
"main": "./dist/react-model.js",
66
"types": "./src/index",
@@ -36,4 +36,4 @@
3636
"url": "https://github.com/byte-fe/react-model/issues"
3737
},
3838
"homepage": "https://github.com/byte-fe/react-model#readme"
39-
}
39+
}

src/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const Model = <M extends Models>(models: M, initialModels?: M) => {
3535
return { useStore, getState, getInitialState } as {
3636
useStore: <K extends keyof M>(
3737
name: K,
38-
models?: M
38+
depActions?: (keyof Get<M[K], 'actions'>)[]
3939
) => [Get<M[K], 'state'>, getConsumerActionsType<Get<M[K], 'actions'>>]
4040
getState: <K extends keyof M>(modelName: K) => Readonly<Get<M[K], 'state'>>
4141
getInitialState: typeof getInitialState
@@ -46,13 +46,13 @@ const getState = (modelName: keyof typeof Global.State) => {
4646
return (Global.State as any)[modelName].state
4747
}
4848

49-
const useStore = (modelName: string) => {
49+
const useStore = (modelName: string, depActions?: string[]) => {
5050
const setState = useState(Global.State[modelName].state)[1]
5151
Global.uid += 1
5252
const _hash = '' + Global.uid
5353
if (!Global.Setter.functionSetter[modelName])
5454
Global.Setter.functionSetter[modelName] = []
55-
Global.Setter.functionSetter[modelName][_hash] = { setState }
55+
Global.Setter.functionSetter[modelName][_hash] = { setState, depActions }
5656
useEffect(() => {
5757
return function cleanup() {
5858
delete Global.Setter.functionSetter[modelName][_hash]

src/middlewares.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,16 @@ const devToolsListener: Middleware = async (context, restMiddlewares) => {
6565
}
6666

6767
const communicator: Middleware<{}> = async (context, restMiddlewares) => {
68-
const { modelName, next } = context
68+
const { modelName, next, actionName } = context
6969
Global.Setter.classSetter && Global.Setter.classSetter(Global.State)
70-
Object.keys(Global.Setter.functionSetter[modelName]).map(
71-
key =>
72-
Global.Setter.functionSetter[modelName][key] &&
73-
Global.Setter.functionSetter[modelName][key].setState(
74-
Global.State[modelName].state
75-
)
76-
)
70+
Object.keys(Global.Setter.functionSetter[modelName]).map(key => {
71+
const setter = Global.Setter.functionSetter[modelName][key]
72+
if (setter) {
73+
if (!setter.depActions || setter.depActions.indexOf(actionName) !== -1) {
74+
setter.setState(Global.State[modelName].state)
75+
}
76+
}
77+
})
7778
await next(restMiddlewares)
7879
}
7980

0 commit comments

Comments
 (0)