Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for store changes before firing.changed #53

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Store.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local RunService = game:GetService("RunService")

local didChange = require(script.Parent.didChange)
local Signal = require(script.Parent.Signal)
local NoYield = require(script.Parent.NoYield)

Expand Down Expand Up @@ -86,8 +87,11 @@ function Store:dispatch(action)
error("action does not have a type field", 2)
end

self._state = self._reducer(self._state, action)
self._mutatedSinceFlush = true
local newState = self._reducer(self._state, action)
if didChange(self._state, newState) then
self._mutatedSinceFlush = true
end
self._state = newState
else
error(("actions of type %q are not permitted"):format(typeof(action)), 2)
end
Expand Down
35 changes: 33 additions & 2 deletions src/Store.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,13 @@ return function()

describe("flush", function()
it("should not fire a changed event if there were no dispatches", function()
local store = Store.new(function()
end)
local store = Store.new(function(state, action)
if action.type == 'increment' then
return state + 1
end

return state
end, 0)

local count = 0
store.changed:connect(function()
Expand All @@ -338,5 +343,31 @@ return function()

store:destruct()
end)

it("should not fire a changed event if the state did not change", function()
local store = Store.new(function(state, action)
return {
state = "non-changed state"
}
end, {
state = "non-changed state"
})

local count = 0
store.changed:connect(function()
print("store changed")
count = count + 1
end)

store:dispatch({
type = ""
})

store:flush()

expect(count).to.equal(0)

store:destruct()
end)
end)
end
51 changes: 51 additions & 0 deletions src/didChange.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--[[
A function that can be used to determine if a new value is equal to the old
value, recursively
]]

local didChange

didChange = function(original, new)
-- if both original and new aren't tables, compare
if not (type(original) == 'table' and type(new) == 'table') then
return original ~= new
end

-- compare new to old
for key, value in pairs(new) do
-- check for changed keys
if not (type(original[key]) == 'table' and type(value) == 'table') then
if original[key] ~= value then
return true
end
end

-- repeat for children
if type(value) == 'table' then
if didChange(original[key], value) then
return true
end
end
end

-- compare old to new
for key, value in pairs(original) do
-- check for changed keys
if not (type(value) == 'table' and type(new[key]) == 'table') then
if value ~= new[key] then
return true
end
end

-- repeat for children
if type(value) == 'table' then
if didChange(value, new[key]) then
return true
end
end
end

return false
end

return didChange
95 changes: 95 additions & 0 deletions src/didChange.spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
return function()
local didChange = require(script.Parent.didChange)

it("should return true for changed non-table to non-table values", function()
local original = "hello"
local new = "world"

local isDifferent = didChange(original, new)
expect(isDifferent).to.equal(true)
end)

it("should return false for not-changed non-table to non-table values", function()
local original = "hello world"
local new = "hello world"

local isDifferent = didChange(original, new)
expect(isDifferent).to.equal(false)
end)

it("should return true for changed table to table values", function()
-- check same key change
local original = {
key = "hello"
}
local new = {
key = "world"
}

local isDifferent = didChange(original, new)
expect(isDifferent).to.equal(true)

-- check new key added
original = {
key = "hello"
}
new = {
key = "hello",
otherKey = "world"
}

isDifferent = didChange(original, new)
expect(isDifferent).to.equal(true)

-- check key removed
original = {
key = "hello"
}
new = {}

isDifferent = didChange(original, new)
expect(isDifferent).to.equal(true)

-- check recursive
original = {
data = {
key = "hello"
}
}
new = {
data = {
key = "world"
}
}

isDifferent = didChange(original, new)
expect(isDifferent).to.equal(true)
end)

it("should return false for not changed table to table values", function()
local original = {
key = "hello world"
}
local new = {
key = "hello world"
}

local isDifferent = didChange(original, new)
expect(isDifferent).to.equal(false)

-- check recursive
original = {
data = {
key = "hello world"
}
}
new = {
data = {
key = "hello world"
}
}

isDifferent = didChange(original, new)
expect(isDifferent).to.equal(false)
end)
end