Skip to content

Commit

Permalink
Merge af37c57 into 38ef143
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaranthineCodices committed Mar 5, 2018
2 parents 38ef143 + af37c57 commit 2249409
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/Store.lua
Expand Up @@ -23,8 +23,9 @@ Store.__index = Store
Reducers do not mutate the state object, so the original state is still
valid.
]]
function Store.new(reducer, initialState)
function Store.new(reducer, initialState, middlewares)
assert(typeof(reducer) == "function", "Bad argument #1 to Store.new, expected function.")
assert(middlewares == nil or typeof(middlewares) == "table", "Bad argument #3 to Store.new, expected nil or table.")

local self = {}

Expand All @@ -46,6 +47,15 @@ function Store.new(reducer, initialState)
end)
table.insert(self._connections, connection)

if middlewares then
local dispatch = Store.dispatch
for _, middleware in ipairs(middlewares) do
dispatch = middleware(dispatch)
end

self.dispatch = dispatch
end

return self
end

Expand Down
36 changes: 36 additions & 0 deletions lib/Store.spec.lua
Expand Up @@ -24,6 +24,42 @@ return function()
store:destruct()
end)

it("should instantiate with a reducer, initial state, and middlewares", function()
local store = Store.new(function(state, action)
return state
end, "initial state", {})

expect(store).to.be.ok()
expect(store:getState()).to.equal("initial state")

store:destruct()
end)

it("should modify the dispatch method when middlewares are passed", function()
local middlewareInvokeCount = 0

local function reducer(state, action)
return state
end

local function testMiddleware(next)
return function(store, action)
middlewareInvokeCount = middlewareInvokeCount + 1
next(store, action)
end
end

local store = Store.new(reducer, "initial state", { testMiddleware })

store:dispatch({
type = "test"
})

expect(middlewareInvokeCount).to.equal(1)

store:destruct()
end)

it("should send an initial action with a 'type' field", function()
local lastAction
local callCount = 0
Expand Down
2 changes: 2 additions & 0 deletions lib/init.lua
@@ -1,9 +1,11 @@
local Store = require(script.Store)
local createReducer = require(script.createReducer)
local combineReducers = require(script.combineReducers)
local loggerMiddleware = require(script.loggerMiddleware)

return {
Store = Store,
createReducer = createReducer,
combineReducers = combineReducers,
loggerMiddleware = loggerMiddleware,
}
41 changes: 41 additions & 0 deletions lib/loggerMiddleware.lua
@@ -0,0 +1,41 @@
local indentStr = " "

local function prettyPrint(t, indent)
indent = indent or 1
local outputBuffer = {
"{\n"
}

for key, value in pairs(t) do
local strKey = tostring(key)

table.insert(outputBuffer, indentStr:rep(indent + 1))
table.insert(outputBuffer, strKey)
table.insert(outputBuffer, " = ")

if type(value) == "table" then
table.insert(outputBuffer, prettyPrint(value, indent + 1))
table.insert(outputBuffer, "\n")
else
table.insert(outputBuffer, tostring(value))
table.insert(outputBuffer, "; (")
table.insert(outputBuffer, typeof(value))
table.insert(outputBuffer, ")\n")
end
end

table.insert(outputBuffer, indentStr:rep(indent))
table.insert(outputBuffer, "}")

return table.concat(outputBuffer, "")
end

local function loggerPlugin(next)
return function(store, action)
print("Action dispatched:", prettyPrint(action))
next(store, action)
print("State changed to:", prettyPrint(store:getState()))
end
end

return loggerPlugin

0 comments on commit 2249409

Please sign in to comment.