Created
April 10, 2018 23:20
-
-
Save LPGhatguy/28dfad42d1897e0b3d9e7283f013193d to your computer and use it in GitHub Desktop.
Copying the Reselect API in ~40 lines of code. Does not allocate on call. Untested.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function applySelector(selector, ...) | |
if typeof(selector) == "string" then | |
return (...)[selector] | |
else | |
return selector(...) | |
end | |
end | |
local function listShallowEqual(a, b, len) | |
for i = 1, len do | |
if a[i] ~= b[i] then | |
return false | |
end | |
end | |
return true | |
end | |
local function createSelector(inputSelectors, fn) | |
local lastValues = {} | |
local values = {} | |
local result = nil | |
local numInputSelectors = #inputSelectors | |
return function(...) | |
for i = 1, numInputSelectors do | |
values[i] = applySelector(inputSelectors[i], ...) | |
end | |
if listShallowEqual(values, lastValues, numInputSelectors) then | |
return result | |
end | |
result = fn(unpack(values)) | |
values, lastValues = lastValues, values | |
return result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment