-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.luau
54 lines (40 loc) · 1.23 KB
/
init.luau
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local caches = require(script.cache)
local defaultEquals = require(script.Parent.defaultEquals)
local getOptions = require(script.getOptions)
local types = require(script.types)
export type MemoizeOptions = getOptions.MemoizeOptions
type EqualityFn = types.EqualityFn
--[=[
@within RoduxUtils
Memoizes a function. This is the default memoization function for
`createSelector`.
@param fn -- The function you want to memoize
@param equalityFnOrOptions
]=]
local function defaultMemoize<Args...>(
fn: (Args...) -> any?,
equalityFnOrOptions: (EqualityFn | MemoizeOptions)?
)
local options = getOptions(equalityFnOrOptions)
local comparator =
caches.createCacheKeyComparator(options.equalityCheck or defaultEquals)
local cache = caches.getCache(options.maxSize or 1, comparator)
return function(...: Args...)
local arguments = table.pack(...)
local value = cache:get(arguments)
if value == caches.None then
value = fn(...)
if options.resultEqualityCheck then
local entries = cache:getEntries()
for _, entry in entries do
if options.resultEqualityCheck(entry.value, value) then
value = entry.value
end
end
end
cache:put(arguments, value)
end
return value
end
end
return defaultMemoize