mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Cleanup scope functions
This commit is contained in:
parent
4b4db9602e
commit
ce24f8ade9
4 changed files with 134 additions and 105 deletions
|
|
@ -13,24 +13,26 @@ local function values<K, V, Obj>(
|
|||
component: (V, Source<K>, Source<boolean>) -> (Obj, number?)
|
||||
): Source<Array<Obj>>
|
||||
local update_count = 0
|
||||
local caches = {} :: Map<V, {
|
||||
destroy_scope: () -> (),
|
||||
present: (boolean?) -> boolean,
|
||||
local scopes = {} :: Map<V, {
|
||||
destroy: () -> (),
|
||||
object: Obj,
|
||||
index: K?,
|
||||
index_source: (K?) -> K,
|
||||
object: Obj,
|
||||
delay: number,
|
||||
timeout: { cancel: boolean }?,
|
||||
count: number,
|
||||
delay: number,
|
||||
present: (boolean?) -> boolean,
|
||||
timeout: { cancel: boolean }?,
|
||||
}>
|
||||
|
||||
local output = source({} :: Array<Obj>)
|
||||
local function update_output()
|
||||
local array = table.create(16)
|
||||
for _, cache in caches do
|
||||
table.insert(array, cache.object)
|
||||
local objects = table.create(4)
|
||||
|
||||
for _, scope in scopes do
|
||||
table.insert(objects, scope.object)
|
||||
end
|
||||
output(array)
|
||||
|
||||
output(objects)
|
||||
end
|
||||
|
||||
effect(function()
|
||||
|
|
@ -41,7 +43,7 @@ local function values<K, V, Obj>(
|
|||
|
||||
local children_need_update = false -- set to true if a scope is created or destroyed
|
||||
|
||||
if flags.strict then
|
||||
if flags.strict then -- check for duplicate values
|
||||
local map = {}
|
||||
for _, v in data do
|
||||
if map[v] then
|
||||
|
|
@ -51,11 +53,11 @@ local function values<K, V, Obj>(
|
|||
end
|
||||
end
|
||||
|
||||
-- process data
|
||||
-- create or update scopes
|
||||
for i, v in data do
|
||||
local cache = caches[v]
|
||||
local scope = scopes[v]
|
||||
|
||||
if cache == nil then -- create new scope and create component
|
||||
if scope == nil then -- create new scope and create component
|
||||
local index_source = source(i)
|
||||
local present = source(false)
|
||||
|
||||
|
|
@ -70,46 +72,47 @@ local function values<K, V, Obj>(
|
|||
|
||||
children_need_update = true
|
||||
|
||||
caches[v] = {
|
||||
count = count,
|
||||
scopes[v] = {
|
||||
destroy = destroy,
|
||||
object = object,
|
||||
index = i,
|
||||
destroy_scope = destroy,
|
||||
index_source = index_source,
|
||||
present = present,
|
||||
count = count,
|
||||
delay = delay or 0,
|
||||
object = object
|
||||
present = present,
|
||||
timeout = nil,
|
||||
}
|
||||
else -- update source
|
||||
cache.count = count
|
||||
else -- update scope
|
||||
scope.count = count
|
||||
|
||||
if cache.index ~= i then
|
||||
if cache.timeout then
|
||||
cache.timeout.cancel = true
|
||||
cache.timeout = nil
|
||||
cache.present(true)
|
||||
if scope.index ~= i then
|
||||
if scope.timeout then -- value is in input table again; cancel destruction
|
||||
scope.timeout.cancel = true
|
||||
scope.timeout = nil
|
||||
scope.present(true)
|
||||
end
|
||||
|
||||
cache.index = i
|
||||
cache.index_source(i)
|
||||
scope.index = i
|
||||
scope.index_source(i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- remove old values
|
||||
for v, cache in caches do
|
||||
if cache.count < count then -- if count is not latest then value is no longer in the input table
|
||||
cache.present(false)
|
||||
-- destroy scopes
|
||||
for v, scope in scopes do
|
||||
if scope.count < count then -- if count is not latest then value is no longer in the input table
|
||||
scope.present(false)
|
||||
|
||||
if cache.delay == 0 then
|
||||
cache.destroy_scope()
|
||||
caches[v] = nil
|
||||
if scope.delay == 0 then
|
||||
scope.destroy()
|
||||
scopes[v] = nil
|
||||
children_need_update = true
|
||||
else
|
||||
cache.index = nil
|
||||
if cache.timeout == nil then
|
||||
cache.timeout = timeout(cache.delay, function() -- todo: avoid redundant updates (e.g. values() input is cleared)
|
||||
cache.destroy_scope()
|
||||
caches[v] = nil
|
||||
scope.index = nil -- set to nil for the `scope.index ~= i` check
|
||||
if scope.timeout == nil then
|
||||
scope.timeout = timeout(scope.delay, function() -- todo: possible redundant updates
|
||||
scope.destroy()
|
||||
scopes[v] = nil
|
||||
update_output()
|
||||
end)
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue