Skip to content

Commit 95810bb

Browse files
feat(selector): support selector for useStore (#167)
* fix(type): fix useStore type issue * feat(selector): support selector for useStore * test(middleware): update middlewares' config's testcases
1 parent 434a93e commit 95810bb

8 files changed

Lines changed: 59 additions & 18 deletions

File tree

__test__/dubugger.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// @ts-ignore
22
console.group = undefined
33
/// <reference path="./index.d.ts" />
4-
import { Model } from '../src'
4+
import { Model, middlewares } from '../src'
55
import { Counter } from '.'
66
import { renderHook } from '@testing-library/react-hooks'
77

88
describe('PubSub', () => {
99
test('run callback when specific action run', async () => {
10+
middlewares.config.logger.enable = true
1011
let actions: any
1112
let count = 0
1213
const { useStore, subscribe } = Model({ Counter })
@@ -20,5 +21,6 @@ describe('PubSub', () => {
2021
await actions.increment()
2122
await actions.increment()
2223
expect(count).toBe(13)
24+
middlewares.config.logger.enable = false
2325
})
2426
})

__test__/middlewares/devToolsListener.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
send: () => {}
55
}
66
import { renderHook } from '@testing-library/react-hooks'
7-
import { Model } from '../../src'
7+
import { Model, middlewares } from '../../src'
88
import { Counter } from '..'
99

10+
middlewares.config.devtools.enable = true
11+
1012
describe('withDevTools', () => {
1113
test("won't break the behavior without DevTools", async () => {
1214
let state: any

__test__/middlewares/tryCatch.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path="../index.d.ts" />
22
process.env.NODE_ENV = 'production'
3-
import { Model } from '../../src'
3+
import { Model, middlewares } from '../../src'
44
import { ErrorCounter } from '..'
55
import { renderHook } from '@testing-library/react-hooks'
66

@@ -17,4 +17,18 @@ describe('tryCatch', () => {
1717
})
1818
expect(errNum).toBe(0)
1919
})
20+
21+
test("throw actions' error when turn off tryCatch middleware", async () => {
22+
let actions: any
23+
let errNum = 0
24+
middlewares.config.tryCatch.enable = false
25+
const { useStore } = Model({ ErrorCounter })
26+
renderHook(() => {
27+
;[, actions] = useStore('ErrorCounter')
28+
})
29+
await actions.increment().catch(() => {
30+
errNum += 1
31+
})
32+
expect(errNum).toBe(1)
33+
})
2034
})

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "react-model",
3-
"version": "4.0.0-rc.1",
3+
"version": "4.0.0-rc.6",
44
"description": "The State management library for React",
55
"main": "./dist/react-model.js",
6+
"module": "./dist/react-model.esm.js",
67
"umd:main": "./dist/react-model.umd.js",
78
"types": "./src/index",
89
"scripts": {
9-
"build": "microbundle --sourcemap false --jsx React.createElement",
10+
"build": "microbundle --sourcemap false --jsx React.createElement --output dist --tsconfig ./tsconfig.json",
1011
"commit": "git-cz",
1112
"lint-ts": "tslint -c tslint.json 'src/**/*.ts'",
1213
"lint-md": "remark .",
@@ -67,5 +68,13 @@
6768
"commitizen": {
6869
"path": "./node_modules/cz-conventional-changelog"
6970
}
70-
}
71+
},
72+
"browserslist": [
73+
"edge 17",
74+
"firefox 70",
75+
"chrome 48",
76+
"safari 12.1",
77+
"android 4.0",
78+
"samsung 9.2"
79+
]
7180
}

src/helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ const consumerActions = (
4343
modelContext: { modelName: string }
4444
) => {
4545
const ret: any = {}
46-
Object.entries<Action>(actions).forEach(([key, action]) => {
47-
ret[key] = consumerAction(action, modelContext)
46+
Object.keys(actions).forEach((key) => {
47+
// @ts-ignore
48+
ret[key] = consumerAction(actions[key], modelContext)
4849
})
4950
return ret
5051
}

src/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// Remember to remove types: ./src/index when run `yarn build` command
12
/// <reference path="./index.d.ts" />
2-
import produce from 'immer'
3+
import produce, { enableES5 } from 'immer'
4+
enableES5()
35
import * as React from 'react'
46
import { PureComponent, useEffect, useState, useRef } from 'react'
57
import Global from './global'
@@ -84,7 +86,8 @@ function Model<M extends Models, MT extends ModelType, E>(
8486
})
8587
}
8688
extContext && (Global.Context['__global'] = extContext)
87-
Object.entries(models).forEach(([name, model]) => {
89+
Object.keys(models).forEach((name) => {
90+
const model = models[name]
8891
if (model.__ERROR__) {
8992
// Fallback State and Actions when model schema is invalid
9093
console.error(name + " model's schema is invalid")
@@ -193,11 +196,11 @@ const getActions = (
193196
baseContext: Partial<Context> = { type: 'outer' }
194197
) => {
195198
const updaters: any = {}
196-
Object.entries(Global.Actions[modelName]).forEach(
197-
([key, action]) =>
199+
Object.keys(Global.Actions[modelName]).forEach(
200+
(key) =>
198201
(updaters[key] = async (params: any, middlewareConfig?: any) => {
199202
const context: InnerContext = {
200-
action,
203+
action: Global.Actions[modelName][key],
201204
actionName: key,
202205
consumerActions,
203206
middlewareConfig,

src/middlewares.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { getCache, setPartialState, timeout, shallowEqual } from './helper'
33

44
const config: MiddlewareConfig = {
55
logger: {
6-
enable: process.env.NODE_ENV !== 'production'
6+
enable: false
77
},
88
devtools: {
9-
enable: process.env.NODE_ENV !== 'production'
9+
enable: false
1010
},
1111
tryCatch: {
12-
enable: process.env.NODE_ENV === 'production'
12+
enable: true
1313
}
1414
}
1515

@@ -59,7 +59,17 @@ const getNewStateWithCache = (maxTime: number = 5000): Middleware => async (
5959
}
6060

6161
const setNewState: Middleware = async (context, restMiddlewares) => {
62-
const { modelName, newState, next } = context
62+
const { modelName, newState, next, Global } = context
63+
if (Global.Setter.functionSetter[modelName]) {
64+
Object.keys(Global.Setter.functionSetter[modelName]).map((key) => {
65+
const setter = Global.Setter.functionSetter[modelName][key]
66+
if (setter) {
67+
if (setter.selector && !setter.selectorRef) {
68+
setter.selectorRef = setter.selector(Global.State[modelName])
69+
}
70+
}
71+
})
72+
}
6373
if (newState) {
6474
setPartialState(modelName, newState)
6575
await next(restMiddlewares)

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
"strict": true
1717
},
1818
"include": ["./src/*"],
19-
"exclude": ["node_modules", "./dist/*"]
19+
"exclude": ["node_modules", "dist"]
2020
}

0 commit comments

Comments
 (0)