expect(a[3]).to.equal(b[3])
end)
it(“should take values from a starting index when provided”, function()
local a = {1, 2, 3, 4}
local b = Functional.Take(a, 2, 2)
expect(#b).to.equal(2)
expect(b[1]).to.equal(2)
expect(b[2]).to.equal(3)
end)
it(“should not take past the end of a list when the starting index is provided”, function()
local a = {1, 2, 3, 4}
local b = Functional.Take(a, 3, 3)
expect(#b).to.equal(2)
expect(b[1]).to.equal(3)
expect(b[2]).to.equal(4)
end)
end)
describe(“Find”, function()
it(“should return index of matched item”, function()
local a = {“foo”, “bar”, “garply”}
local b = Functional.Find(a, “bar”)
expect(b).to.equal(2)
end)
it(“should find the first example in the case of duplicates”, function()
local a = {“foo”, “bar”, “garply”, “bar”}
local b = Functional.Find(a, “bar”)
expect(b).to.equal(2)
end)
it(“should return nil if item is not found”, function()
local a = {“foo”, “bar”, “garply”}
local b = Functional.Find(a, “fleebledegoop”)
expect(b).to.equal(nil)
end)
end)
end ise does
not have the correct status.
]]
function PerformFetch.ClearOutstandingPromiseStatus()
batchPromises = {}
end
local function singleFetchKeymapper(item)
— Single fetch keys are used directly
return item
end
–[[
Get the fetching status for a given status key. Defaults to
RetrievalStatus.NotStarted for missing keys.
]]
function PerformFetch.GetStatus(state, fetchStatusKey)
assert(typeof(state) == “table”)
assert(typeof(fetchStatusKey) == “string”)
assert(#fetchStatusKey > 0)
return state.FetchingStatus[fetchStatusKey] or RetrievalStatus.NotStarted
end
–[[
Perform a fetch operation for a single resource.
Args:
fetchStatusKey – String key for the fetching status to index the Rodux store.
fetchFunctor – Functor to call when a fetch needs to be performed for fetchStatusKey.
Returns:
A Promise that resolves or rejects in accordance with the result of fetchFunctor, or the
promise for the original fetch if one is already ongoing.
Usage:
In your main thunk, wrap your inner store function with this thunk, like this:
return function(arg1, arg2)
return PerformFetch.Single(“mykey”, function(store)
return doYourLogicHere() — Must return a Promise!!!
end)
end
Please note that in order for single fetches to integrate well with batch fetches,
your promise must NEVER resolve or reject with multiple arguments! Wrap your results
in a table instead.
]]
function PerformFetch.Single(fetchStatusKey, fetchFunctor)
assert(typeof(fetchStatusKey) == “string”)
assert(typeof(fetchFunctor) == “function”)
assert(#fetchStatusKey > 0)
return function(store)
— Call batch API to handle the individual fetch
return PerformFetch.Batch({ fetchStatusKey }, singleFetchKeymapper, function(batchStore, itemsToFetch)
assert(#itemsToFetch == 1)
local functorPromise = fetchFunctor(batchStore)
assert(Promise.is(functorPromise))
return functorPromise:andThen(function(…)
assert(#{…} <= 1)
return Promise.resolve({ [fetchStatusKey] = Result.new(true, (...)) })
end, function(...)
assert(#{...} <= 1)
return Promise.resolve({ [fetchStatusKey] = Result.new(false, (...)) })
end)
end)(store):andThen(function(batchResults)
local success, value = batchResults[fetchStatusKey]:unwrap()
if success then
return Promise.resolve(value)
else
return Promise.reject(value)
end
end)
end
end
--[[
Perform a fetch operation for multiple resources at once (batching).
Args:--[[
Provides functions for manipulating immutable data structures.
]]
local Immutable = {}
--[[
Merges dictionary-like tables together.
]]
function Immutable.JoinDictionaries(...)
local result = {}
for i = 1, select("#", ...) do
local dictionary = select(i, ...)
for key, value in pairs(dictionary) do
result[key] = value
end
end
return result
end
--[[
Joins any number of lists together into a new list
]]
function Immutable.JoinLists(...)
local new = {}
for listKey = 1, select("#", ...) do
local list = select(listKey, ...)
local len = #new
for itemKey = 1, #list do
new[len + itemKey] = list[itemKey]
end
end
return new
end
--[[
Creates a new copy of the dictionary and sets a value inside it.
]]
function Immutable.Set(dictionary, key, value)
local new = {}
for key, value in pairs(dic