Skip to content

Commit f61afdd

Browse files
committed
fix(usemodel): accept falsy value
1 parent 8f7f137 commit f61afdd

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

__test__/lane/lane.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,42 @@ describe('lane model', () => {
2929
})
3030
})
3131

32+
test('false value can be accepted', async () => {
33+
const { useStore } = createStore(() => {
34+
const [count, setCount] = useModel(true)
35+
return { count, setCount }
36+
})
37+
38+
let renderTimes = 0
39+
const { result, rerender } = renderHook(() => {
40+
const { count, setCount } = useStore()
41+
renderTimes += 1
42+
return { renderTimes, count, setCount }
43+
})
44+
await act(async () => {
45+
expect(renderTimes).toEqual(1)
46+
expect(result.current.count).toBe(true)
47+
})
48+
49+
await act(async () => {
50+
await result.current.setCount(false)
51+
})
52+
53+
await act(() => {
54+
expect(renderTimes).toEqual(2)
55+
expect(result.current.count).toBe(false)
56+
})
57+
58+
await act(() => {
59+
rerender()
60+
})
61+
62+
await act(() => {
63+
expect(renderTimes).toEqual(3)
64+
expect(result.current.count).toBe(false)
65+
})
66+
})
67+
3268
test('multiple models', async () => {
3369
const { useStore } = createStore(() => {
3470
const [count, setCount] = useModel(1)

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ interface BaseContext<S = {}, P = any> {
9696

9797
interface InnerContext<S = {}> extends BaseContext<S> {
9898
// Actions with function type context will always invoke current component's reload.
99-
type?: 'function' | 'outer' | 'class'
99+
type?: 'function' | 'outer' | 'class' | 'useModel'
100100
__hash?: string
101101
}
102102

src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function useModel<S>(state: S): [S, (state: S) => void] {
2727
const storeId = Global.currentStoreId
2828
const index = Global.mutableState[storeId].count
2929
Global.mutableState[storeId].count += 1
30-
if (!Global.mutableState[storeId][index]) {
30+
if (!Global.mutableState[storeId].hasOwnProperty(index)) {
3131
Global.mutableState[storeId][index] = state
3232
}
3333

@@ -44,7 +44,7 @@ function useModel<S>(state: S): [S, (state: S) => void] {
4444
modelName: '__' + storeId,
4545
newState: {},
4646
params: undefined,
47-
type: 'outer'
47+
type: 'useModel'
4848
}
4949
Global.mutableState[storeId][index] = state
5050
return await applyMiddlewares(actionMiddlewares, context)

src/middlewares.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ const getNewStateWithCache = (maxTime: number = 5000): Middleware => async (
5959
}
6060

6161
const setNewState: Middleware = async (context, restMiddlewares) => {
62-
const { modelName, newState, next, Global, disableSelectorUpdate } = context
62+
const {
63+
modelName,
64+
newState,
65+
next,
66+
Global,
67+
disableSelectorUpdate,
68+
type
69+
} = context
6370
if (Global.Setter.functionSetter[modelName] && !disableSelectorUpdate) {
6471
Object.keys(Global.Setter.functionSetter[modelName]).map((key) => {
6572
const setter = Global.Setter.functionSetter[modelName][key]
@@ -70,8 +77,8 @@ const setNewState: Middleware = async (context, restMiddlewares) => {
7077
}
7178
})
7279
}
73-
if (newState) {
74-
setPartialState(modelName, newState)
80+
if (newState || type === 'useModel') {
81+
setPartialState(modelName, newState || {})
7582
return await next(restMiddlewares)
7683
}
7784
}

0 commit comments

Comments
 (0)