Skip to content

Commit 2026a1c

Browse files
committed
Update loggerMiddleware to no longer require passing in an output function
1 parent 0237d00 commit 2026a1c

File tree

3 files changed

+65
-48
lines changed

3 files changed

+65
-48
lines changed

lib/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ return {
88
Store = Store,
99
createReducer = createReducer,
1010
combineReducers = combineReducers,
11-
loggerMiddleware = loggerMiddleware,
11+
loggerMiddleware = loggerMiddleware.middleware,
1212
thunkMiddleware = thunkMiddleware,
1313
}

lib/loggerMiddleware.lua

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,54 @@
1-
local indentStr = " "
2-
3-
local function prettyPrint(t, indent)
4-
indent = indent or 1
5-
local outputBuffer = {
6-
"{\n",
7-
}
8-
9-
for key, value in pairs(t) do
10-
local strKey = tostring(key)
11-
12-
table.insert(outputBuffer, indentStr:rep(indent + 1))
13-
table.insert(outputBuffer, strKey)
14-
table.insert(outputBuffer, " = ")
15-
16-
if typeof(value) == "table" then
17-
table.insert(outputBuffer, prettyPrint(value, indent + 1))
18-
table.insert(outputBuffer, "\n")
19-
else
20-
table.insert(outputBuffer, tostring(value))
21-
table.insert(outputBuffer, "; (")
22-
table.insert(outputBuffer, typeof(value))
23-
table.insert(outputBuffer, ")\n")
1+
local indent = " "
2+
3+
local function prettyPrint(value, indentLevel)
4+
indentLevel = indentLevel or 0
5+
local output = {}
6+
7+
if typeof(value) == "table" then
8+
table.insert(output, "{\n")
9+
10+
for key, value in pairs(value) do
11+
table.insert(output, indent:rep(indentLevel + 1))
12+
table.insert(output, tostring(key))
13+
table.insert(output, " = ")
14+
15+
table.insert(output, prettyPrint(value, indentLevel + 1))
16+
table.insert(output, "\n")
2417
end
25-
end
2618

27-
table.insert(outputBuffer, indentStr:rep(indent))
28-
table.insert(outputBuffer, "}")
19+
table.insert(output, indent:rep(indentLevel))
20+
table.insert(output, "}")
21+
elseif typeof(value) == "string" then
22+
table.insert(output, string.format("%q", value))
23+
table.insert(output, " (string)")
24+
else
25+
table.insert(output, tostring(value))
26+
table.insert(output, " (")
27+
table.insert(output, typeof(value))
28+
table.insert(output, ")")
29+
end
2930

30-
return table.concat(outputBuffer, "")
31+
return table.concat(output, "")
3132
end
3233

33-
local function loggerMiddleware(outputFunction)
34-
outputFunction = outputFunction or print
34+
-- We want to be able to override outputFunction in tests, so the shape of this
35+
-- module is kind of unconventional.
36+
--
37+
-- We fix it this weird shape in init.lua.
38+
local loggerMiddleware = {
39+
outputFunction = print,
40+
}
3541

36-
return function(next)
37-
return function(store, action)
38-
local result = next(store, action)
42+
function loggerMiddleware.middleware(next)
43+
return function(store, action)
44+
local result = next(store, action)
3945

40-
outputFunction(("Action dispatched: %s\nState changed to: %s"):format(
41-
prettyPrint(action),
42-
prettyPrint(store:getState()))
43-
)
46+
loggerMiddleware.outputFunction(("Action dispatched: %s\nState changed to: %s"):format(
47+
prettyPrint(action),
48+
prettyPrint(store:getState())
49+
))
4450

45-
return result
46-
end
51+
return result
4752
end
4853
end
4954

lib/loggerMiddleware.spec.lua

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,38 @@ return function()
44

55
it("should print whenever an action is dispatched", function()
66
local outputCount = 0
7+
local outputMessage
78

89
local function reducer(state, action)
910
return state
1011
end
1112

12-
local function outputFunction()
13+
local store = Store.new(reducer, {
14+
fooValue = 12345,
15+
barValue = {
16+
bazValue = "hiBaz",
17+
},
18+
}, { loggerMiddleware.middleware })
19+
20+
loggerMiddleware.outputFunction = function(message)
1321
outputCount = outputCount + 1
22+
outputMessage = message
1423
end
1524

16-
local logger = loggerMiddleware(outputFunction)
17-
18-
local store = Store.new(reducer, {
19-
value = 0,
20-
otherValue = {},
21-
}, { logger })
22-
2325
store:dispatch({
24-
type = "test",
26+
type = "testActionType",
2527
})
2628

29+
print(outputMessage)
30+
2731
expect(outputCount).to.equal(1)
32+
expect(outputMessage:find("testActionType")).to.be.ok()
33+
expect(outputMessage:find("fooValue")).to.be.ok()
34+
expect(outputMessage:find("12345")).to.be.ok()
35+
expect(outputMessage:find("barValue")).to.be.ok()
36+
expect(outputMessage:find("bazValue")).to.be.ok()
37+
expect(outputMessage:find("hiBaz")).to.be.ok()
38+
39+
loggerMiddleware.outputFunction = print
2840
end)
2941
end

0 commit comments

Comments
 (0)