Cleanup scope functions

This commit is contained in:
centauri 2025-09-01 21:59:35 +01:00
parent 4b4db9602e
commit ce24f8ade9
4 changed files with 134 additions and 105 deletions

View file

@ -13,24 +13,26 @@ local function indexes<K, V, Obj>(
component: (Source<V>, K, Source<boolean>) -> (Obj, number?) component: (Source<V>, K, Source<boolean>) -> (Obj, number?)
): Source<Array<Obj>> ): Source<Array<Obj>>
local update_count = 0 local update_count = 0
local caches = {} :: Map<K, { local scopes = {} :: Map<K, {
destroy_scope: () -> (), destroy: () -> (),
present: (boolean?) -> boolean, object: Obj,
value: V?, value: V?,
value_source: (V?) -> V, value_source: (V?) -> V,
object: Obj,
delay: number,
timeout: { cancel: boolean }?,
count: number, count: number,
delay: number,
present: (boolean?) -> boolean,
timeout: { cancel: boolean }?,
}> }>
local output = source({} :: Array<Obj>) local output = source({} :: Array<Obj>)
local function update_output() local function update_output()
local array = table.create(#caches) local objects = table.create(4)
for _, cache in caches do
table.insert(array, cache.object) for _, scope in scopes do
table.insert(objects, scope.object)
end end
output(array)
output(objects)
end end
effect(function() effect(function()
@ -41,11 +43,11 @@ local function indexes<K, V, Obj>(
local children_need_update = false -- set to true if a scope is created or destroyed local children_need_update = false -- set to true if a scope is created or destroyed
-- process data -- create or update scopes
for i, v in data do for i, v in data do
local cache = caches[i] local scope = scopes[i]
if cache == nil then -- create new scope and create component if scope == nil then -- create new scope and create component
local value_source = source(v) local value_source = source(v)
local present = source(false) local present = source(false)
@ -60,46 +62,47 @@ local function indexes<K, V, Obj>(
children_need_update = true children_need_update = true
caches[i] = { scopes[i] = {
count = count, destroy = destroy,
object = object,
value = v, value = v,
destroy_scope = destroy,
value_source = value_source, value_source = value_source,
present = present, count = count,
delay = delay or 0, delay = delay or 0,
object = object present = present,
timeout = nil,
} }
else -- update source else -- update scope
cache.count = count scope.count = count
if cache.value ~= v then if scope.value ~= v then
if cache.timeout then if scope.timeout then -- index is in input table again; cancel destruction
cache.timeout.cancel = true scope.timeout.cancel = true
cache.timeout = nil scope.timeout = nil
cache.present(true) scope.present(true)
end end
cache.value = v scope.value = v
cache.value_source(v) scope.value_source(v)
end end
end end
end end
-- remove old indexes -- destroy scopes
for i, cache in caches do for i, scope in scopes do
if cache.count < count then -- if count is not latest then value is no longer in the input table if scope.count < count then -- if count is not latest then index is no longer in the input table
cache.present(false) scope.present(false)
if cache.delay == 0 then if scope.delay == 0 then
cache.destroy_scope() scope.destroy()
caches[i] = nil scopes[i] = nil
children_need_update = true children_need_update = true
else else
cache.value = nil scope.value = nil -- set to nil for the `scope.value ~= v` check
if cache.timeout == nil then if scope.timeout == nil then
cache.timeout = timeout(cache.delay, function() -- todo: avoid redundant updates (e.g. indexes() input is cleared) scope.timeout = timeout(scope.delay, function() -- todo: possible redundant updates
cache.destroy_scope() scope.destroy()
caches[i] = nil scopes[i] = nil
update_output() update_output()
end) end)
end end

View file

@ -6,26 +6,26 @@ local timeout = require "./timeout" ()
type Array<T> = { T } type Array<T> = { T }
type Map<K, V> = { [K]: V } type Map<K, V> = { [K]: V }
type Source<T> = () -> T type Source<T> = () -> T
type Component<T> = (Source<boolean>) -> T type Component<T> = (Source<boolean>) -> (T, number?)
local function switch_map<K, Obj>( local function switch_map<K, Obj>(
input: Source<K>, input: Source<K>,
map: Map<K, Component<Obj>> map: Map<K, Component<Obj>>
): Source<nil | Obj | Array<Obj>> ): Source<nil | Obj | Array<Obj>>
local output = source(nil :: nil | Obj | Array<Obj>) local scopes = {} :: Map<K, {
destroy: () -> (),
local caches = {} :: Map<K, {
destroy_scope: () -> (),
present: (boolean?) -> boolean,
object: Obj, object: Obj,
delay: number, delay: number,
present: (boolean?) -> boolean,
timeout: { cancel: boolean }? timeout: { cancel: boolean }?
}> }>
local output = source(nil :: nil | Obj | Array<Obj>)
local function update_output() local function update_output()
local objects = {} local objects = {}
for _, cache in caches do
table.insert(objects, cache.object) for _, scope in scopes do
table.insert(objects, scope.object)
end end
output( output(
@ -38,33 +38,36 @@ local function switch_map<K, Obj>(
effect(function() effect(function()
local key: K? = input() local key: K? = input()
for k, cache in caches do -- destroy (or queue destroy) all scopes not associated with the input key
for k, scope in scopes do
if k == key then continue end if k == key then continue end
cache.present(false)
if cache.delay == 0 then scope.present(false)
cache.destroy_scope()
caches[k] = nil if scope.delay == 0 then
scope.destroy()
scopes[k] = nil
else else
if cache.timeout == nil then if scope.timeout == nil then
cache.timeout = timeout(cache.delay, function() scope.timeout = timeout(scope.delay, function()
cache.destroy_scope() scope.destroy()
caches[k] = nil scopes[k] = nil
update_output() update_output()
end) end)
end end
end end
end end
-- create new scope or abort destruction of existing scope if key exists
if key ~= nil then if key ~= nil then
local cache = caches[key] local scope = scopes[key]
if cache then if scope then
cache.present(true) scope.present(true)
if cache.timeout then if scope.timeout then
cache.timeout.cancel = true scope.timeout.cancel = true
cache.timeout = nil scope.timeout = nil
end end
else else
local component = map[key] local component = map[key]
@ -85,11 +88,11 @@ local function switch_map<K, Obj>(
present(true) present(true)
caches[key] = { scopes[key] = {
destroy_scope = destroy, destroy = destroy,
present = present,
object = object, object = object,
delay = delay or 0, delay = delay or 0,
present = present,
timeout = nil timeout = nil
} }
end end

View file

@ -13,24 +13,26 @@ local function values<K, V, Obj>(
component: (V, Source<K>, Source<boolean>) -> (Obj, number?) component: (V, Source<K>, Source<boolean>) -> (Obj, number?)
): Source<Array<Obj>> ): Source<Array<Obj>>
local update_count = 0 local update_count = 0
local caches = {} :: Map<V, { local scopes = {} :: Map<V, {
destroy_scope: () -> (), destroy: () -> (),
present: (boolean?) -> boolean, object: Obj,
index: K?, index: K?,
index_source: (K?) -> K, index_source: (K?) -> K,
object: Obj,
delay: number,
timeout: { cancel: boolean }?,
count: number, count: number,
delay: number,
present: (boolean?) -> boolean,
timeout: { cancel: boolean }?,
}> }>
local output = source({} :: Array<Obj>) local output = source({} :: Array<Obj>)
local function update_output() local function update_output()
local array = table.create(16) local objects = table.create(4)
for _, cache in caches do
table.insert(array, cache.object) for _, scope in scopes do
table.insert(objects, scope.object)
end end
output(array)
output(objects)
end end
effect(function() 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 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 = {} local map = {}
for _, v in data do for _, v in data do
if map[v] then if map[v] then
@ -51,11 +53,11 @@ local function values<K, V, Obj>(
end end
end end
-- process data -- create or update scopes
for i, v in data do 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 index_source = source(i)
local present = source(false) local present = source(false)
@ -70,46 +72,47 @@ local function values<K, V, Obj>(
children_need_update = true children_need_update = true
caches[v] = { scopes[v] = {
count = count, destroy = destroy,
object = object,
index = i, index = i,
destroy_scope = destroy,
index_source = index_source, index_source = index_source,
present = present, count = count,
delay = delay or 0, delay = delay or 0,
object = object present = present,
timeout = nil,
} }
else -- update source else -- update scope
cache.count = count scope.count = count
if cache.index ~= i then if scope.index ~= i then
if cache.timeout then if scope.timeout then -- value is in input table again; cancel destruction
cache.timeout.cancel = true scope.timeout.cancel = true
cache.timeout = nil scope.timeout = nil
cache.present(true) scope.present(true)
end end
cache.index = i scope.index = i
cache.index_source(i) scope.index_source(i)
end end
end end
end end
-- remove old values -- destroy scopes
for v, cache in caches do for v, scope in scopes do
if cache.count < count then -- if count is not latest then value is no longer in the input table if scope.count < count then -- if count is not latest then value is no longer in the input table
cache.present(false) scope.present(false)
if cache.delay == 0 then if scope.delay == 0 then
cache.destroy_scope() scope.destroy()
caches[v] = nil scopes[v] = nil
children_need_update = true children_need_update = true
else else
cache.index = nil scope.index = nil -- set to nil for the `scope.index ~= i` check
if cache.timeout == nil then if scope.timeout == nil then
cache.timeout = timeout(cache.delay, function() -- todo: avoid redundant updates (e.g. values() input is cleared) scope.timeout = timeout(scope.delay, function() -- todo: possible redundant updates
cache.destroy_scope() scope.destroy()
caches[v] = nil scopes[v] = nil
update_output() update_output()
end) end)
end end

View file

@ -2004,6 +2004,26 @@ TEST("values()", wrap_root(function()
CHECK(cleaned_counts[3] == nil) CHECK(cleaned_counts[3] == nil)
end end
end end
do CASE "delayed destruction deferred"
local input = source {}
local output = values(input, function()
return {}, 1
end)
local count = 0
effect(function() output(); count += 1 end)
input { 1, 2, 3 }
CHECK(count == 2)
input {}
CHECK(count == 2)
step(1 + 0.01)
--CHECK(count == 3)
CHECK(count == 5)
end
end)) end))
TEST("spring()", wrap_root(function() TEST("spring()", wrap_root(function()