Skip to content

Commit cea3d06

Browse files
committed
fix(devtool): add devtool support for createStore
1 parent ecb69f8 commit cea3d06

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

__test__/middlewares/devToolsListener.spec.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
connect: () => {},
44
send: () => {}
55
}
6-
import { renderHook } from '@testing-library/react-hooks'
7-
import { Model, middlewares } from '../../src'
6+
import { renderHook, act } from '@testing-library/react-hooks'
7+
import { Model, middlewares, createStore, useModel } from '../../src'
88
import { Counter } from '..'
99

1010
middlewares.config.devtools.enable = true
@@ -21,4 +21,31 @@ describe('withDevTools', () => {
2121
await actions.increment(3)
2222
expect(state.count).toBe(3)
2323
})
24+
25+
test('support createStore', () => {
26+
const { useStore } = createStore(() => {
27+
const [count, setCount] = useModel(1)
28+
return { count, setCount }
29+
})
30+
let renderTimes = 0
31+
const { result } = renderHook(() => {
32+
const { count, setCount } = useStore()
33+
renderTimes += 1
34+
return { renderTimes, count, setCount }
35+
})
36+
37+
act(() => {
38+
expect(result.current.renderTimes).toEqual(1)
39+
expect(result.current.count).toBe(1)
40+
})
41+
42+
act(() => {
43+
result.current.setCount(5)
44+
})
45+
46+
act(() => {
47+
expect(renderTimes).toEqual(2)
48+
expect(result.current.count).toBe(5)
49+
})
50+
})
2451
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-model",
3-
"version": "4.2.0-rc.2",
3+
"version": "4.2.1-rc.0",
44
"description": "The State management library for React",
55
"main": "./dist/react-model.js",
66
"module": "./dist/react-model.esm.js",

src/index.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,6 @@ function Model<M extends Models, MT extends ModelType, E>(
264264
actions[n] = getActions(name)
265265
})
266266

267-
Global.withDevTools =
268-
typeof window !== 'undefined' &&
269-
(window as any).__REDUX_DEVTOOLS_EXTENSION__
270-
if (Global.withDevTools && middlewares.config.devtools.enable) {
271-
Global.devTools = (window as any).__REDUX_DEVTOOLS_EXTENSION__
272-
Global.devTools.connect()
273-
}
274267
return {
275268
actions,
276269
getActions: (name: string) => getActions(prefix + name),

src/middlewares.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,27 @@ const consoleDebugger: Middleware = async (context, restMiddlewares) => {
168168
const devToolsListener: Middleware = async (context, restMiddlewares) => {
169169
const { Global } = context
170170
const ret = await context.next(restMiddlewares)
171+
Global.withDevTools =
172+
typeof window !== 'undefined' &&
173+
(window as any).__REDUX_DEVTOOLS_EXTENSION__
174+
if (
175+
Global.withDevTools &&
176+
middlewares.config.devtools.enable &&
177+
!Global.devTools
178+
) {
179+
Global.devTools = (window as any).__REDUX_DEVTOOLS_EXTENSION__
180+
Global.devTools.connect()
181+
}
171182
if (Global.withDevTools && config.devtools.enable) {
183+
const actionName =
184+
context.type === 'u' && context.disableSelectorUpdate
185+
? `store[${context.modelName}].update`
186+
: `${context.modelName}_${context.actionName}`
172187
Global.devTools.send(
173-
`${context.modelName}_${context.actionName}`,
174-
Global.State
188+
actionName,
189+
Global.mutableState[context.modelName],
190+
undefined,
191+
context.modelName
175192
)
176193
}
177194
return ret

0 commit comments

Comments
 (0)