You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The fifth argument to [`Store.new`](../api-reference.md#storenew) takes a devtools object that you can optionally provide. A devtools object has only two requirements: `devtools.__className` must be `"Devtools"` and `devtools:_hookIntoStore(store)` must be a valid function call. Beyond that, your devtools can be anything you need it to be.
2
+
3
+
Devtools can be very useful during development in gathering performance data, providing introspection, debugging, etc. We leave the devtools implementation up to the user in order to support any and all use cases, such as store modification in unit testing, live state inspection plugins, and whatever else you come up with.
4
+
5
+
A simple example of a devtools that profiles and logs:
6
+
7
+
```Lua
8
+
localDevtools= {}
9
+
Devtools.__className="Devtools"
10
+
Devtools.__index=Devtools
11
+
12
+
-- Creates a new Devtools object
13
+
functionDevtools.new()
14
+
localself=setmetatable({
15
+
_events=table.create(100),
16
+
_eventsIndex=0,
17
+
}, Devtools)
18
+
19
+
returnself
20
+
end
21
+
22
+
-- Overwrites the store's reducer and flushHandler with wrapped versions that contain logging and profiling
23
+
functionDevtools:_hookIntoStore(store)
24
+
self._store=store
25
+
self._source=store._source
26
+
27
+
self._originalReducer=store._reducer
28
+
store._reducer=function(state: any, action: any): any
29
+
localstartClock=os.clock()
30
+
localresult=self._originalReducer(state, action)
31
+
localstopClock=os.clock()
32
+
33
+
self:_addEvent("Reduce", {
34
+
name=action.typeortostring(action),
35
+
elapsedMs= (stopClock-startClock) *1000,
36
+
action=action,
37
+
state=result,
38
+
})
39
+
returnresult
40
+
end
41
+
42
+
self._originalFlushHandler=store._flushHandler
43
+
store._flushHandler=function(...)
44
+
localstartClock=os.clock()
45
+
self._originalFlushHandler(...)
46
+
localstopClock=os.clock()
47
+
48
+
self:_addEvent("Flush", {
49
+
name="@@FLUSH",
50
+
elapsedMs= (stopClock-startClock) *1000,
51
+
listeners=table.clone(store.changed._listeners),
52
+
})
53
+
end
54
+
end
55
+
56
+
-- Adds an event to the log
57
+
-- Automatically adds event.timestamp and event.source
58
+
functionDevtools:_addEvent(eventType: "Reduce" | "Flush", props: { [any]: any })
Copy file name to clipboardExpand all lines: docs/api-reference.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ The Store class is the core piece of Rodux. It is the state container that you c
5
5
6
6
### Store.new
7
7
```
8
-
Store.new(reducer, [initialState, [middlewares, [errorReporter]]]) -> Store
8
+
Store.new(reducer, [initialState, [middlewares, [errorReporter, [devtools]]]]) -> Store
9
9
```
10
10
11
11
Creates and returns a new Store.
@@ -14,6 +14,7 @@ Creates and returns a new Store.
14
14
*`initialState` is the store's initial state. This should be used to load a saved state from storage.
15
15
*`middlewares` is a list of [middleware functions](#middleware) to apply each time an action is dispatched to the store.
16
16
*`errorReporter` is a [error reporter object](advanced/error-reporters.md) that allows custom handling of errors that occur during different phases of the store's updates
17
+
*`devtools` is a [custom object](advanced/devtools.md) that you can provide in order to profile, log, or control the store for testing and debugging purposes
17
18
18
19
The store will automatically dispatch an initialization action with a `type` of `@@INIT`.
0 commit comments