-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathloggerMiddleware.lua
55 lines (44 loc) · 1.34 KB
/
loggerMiddleware.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
local indent = " "
local function prettyPrint(value, indentLevel)
indentLevel = indentLevel or 0
local output = {}
if typeof(value) == "table" then
table.insert(output, "{\n")
for key, value in pairs(value) do
table.insert(output, indent:rep(indentLevel + 1))
table.insert(output, tostring(key))
table.insert(output, " = ")
table.insert(output, prettyPrint(value, indentLevel + 1))
table.insert(output, "\n")
end
table.insert(output, indent:rep(indentLevel))
table.insert(output, "}")
elseif typeof(value) == "string" then
table.insert(output, string.format("%q", value))
table.insert(output, " (string)")
else
table.insert(output, tostring(value))
table.insert(output, " (")
table.insert(output, typeof(value))
table.insert(output, ")")
end
return table.concat(output, "")
end
-- We want to be able to override outputFunction in tests, so the shape of this
-- module is kind of unconventional.
--
-- We fix it this weird shape in init.lua.
local loggerMiddleware = {
outputFunction = print,
}
function loggerMiddleware.middleware(nextDispatch, store)
return function(action)
local result = nextDispatch(action)
loggerMiddleware.outputFunction(("Action dispatched: %s\nState changed to: %s"):format(
prettyPrint(action),
prettyPrint(store:getState())
))
return result
end
end
return loggerMiddleware