Implement delays for show() and switch()

This commit is contained in:
centauri 2025-08-29 17:44:02 +01:00
parent 2518e292ed
commit 5262ef711c
6 changed files with 376 additions and 216 deletions

40
src/branch.luau Normal file
View file

@ -0,0 +1,40 @@
local graph = require "./graph"
type Node<T> = graph.Node<T>
local create_node = graph.create_node
local push_scope = graph.push_scope
local pop_scope = graph.pop_scope
local destroy = graph.destroy
local get_scope = graph.get_scope
local function branch<T>(fn: () -> T): (() -> (), T)
local current = get_scope()
if not current then
error(`cannot use branch() outside a stable or reactive scope`, 0)
end
local parent = current.owner
if not parent or parent.effect then
error(`current scope is not owned by a stable scope`, 0)
end
local node = create_node(parent, false, false)
local destroy = function()
destroy(node)
end
push_scope(node)
local ok, result = xpcall(fn, debug.traceback)
pop_scope()
if not ok then
destroy()
error(`error while running branch():\n\n{result}`, 0)
end
return destroy, result
end
return branch

View file

@ -148,6 +148,10 @@ local update_queue = { n = 0 } :: { n: number, [number]: Node<any> }
local function evaluate_node<T>(node: Node<T>)
if flags.strict then
if table.find(scopes, node) then
error("a scope, that should rerun due to the update of a source, is already active", 0)
end
local initial_value = node.cache
for i = 1, 2 do

View file

@ -1,6 +1,7 @@
local version = { major = 0, minor = 3, patch = 1 }
local root = require "./root"
local branch = require "./branch"
local mount = require "./mount"
local create = require "./create"
local apply = require "./apply"
@ -50,6 +51,7 @@ local vide = {
-- core
root = root,
--branch = branch,
mount = mount,
create = create,
source = source,

View file

@ -2,8 +2,16 @@ local source = require "./source"
local derive = require "./derive"
local effect = require "./effect"
local untrack = require "./untrack"
local switch = require "./switch"
local function show<T, U>(input: () -> T?, component: (() -> T) -> U, fallback: (() -> U)?): () -> U?
type Array<T> = { T }
type Source<T> = () -> T
local function show<T, Obj>(
input: Source<T?>,
component: (Source<T>, Source<boolean>) -> (Obj, number?),
fallback: ((Source<boolean>) -> (Obj, number?))?
): Source<nil | Obj | Array<Obj>>
local filtered_input = source()
effect(function()
@ -17,17 +25,12 @@ local function show<T, U>(input: () -> T?, component: (() -> T) -> U, fallback:
return not not input()
end)
-- todo: is this needed?
-- local filtered_input_is_truthy = derive(function()
-- return not not filtered_input()
-- end)
return derive(function()
return
if input_is_truthy() then untrack(function() return component(filtered_input :: () -> T) end)
elseif fallback then untrack(fallback)
else nil
end)
return switch(input_is_truthy) {
[true] = function(present)
return component(filtered_input, present)
end,
[false] = fallback
}
end
return show

View file

@ -1,61 +1,107 @@
local graph = require "./graph"
type Node<T> = graph.Node<T>
type SourceNode<T> = graph.SourceNode<T>
local create_node = graph.create_node
local evaluate_node = graph.evaluate_node
local push_scope_as_child_of = graph.push_scope_as_child_of
local destroy = graph.destroy
local assert_stable_scope = graph.assert_stable_scope
local push_scope = graph.push_scope
local pop_scope = graph.pop_scope
local branch = require "./branch"
local source = require "./source"
local effect = require "./effect"
local timeout = require "./timeout" ()
type Array<T> = { T }
type Map<K, V> = { [K]: V }
type Source<T> = () -> T
type Component<T> = (Source<boolean>) -> T
local function switch<T, U>(source: () -> T): (map: Map<T, ((() -> U)?)>) -> () -> U?
local owner = assert_stable_scope()
local function switch_map<K, Obj>(input: Source<K>, map: Map<K, Component<Obj>>): Source<nil | Obj | Array<Obj>>
local output = source(nil :: nil | Obj | Array<Obj>)
local caches = {} :: Map<K, {
destroy_scope: () -> (),
present: (boolean?) -> boolean,
object: Obj,
delay: number,
timeout: { cancel: boolean }?
}>
local function update_output()
local objects = {}
for _, cache in caches do
table.insert(objects, cache.object)
end
output(
if objects[2] then objects
elseif objects[1] then objects[1]
else nil
)
end
effect(function()
local key: K? = input()
for k, cache in caches do
if k == key then continue end
cache.present(false)
if cache.delay == 0 then
cache.destroy_scope()
caches[k] = nil
else
if cache.timeout == nil then
cache.timeout = timeout(cache.delay, function()
cache.destroy_scope()
caches[k] = nil
update_output()
end)
end
end
end
if key ~= nil then
local cache = caches[key]
if cache then
cache.present(true)
if cache.timeout then
cache.timeout.cancel = true
cache.timeout = nil
end
else
local component = map[key]
if component ~= nil then
if type(component) ~= "function" then
error("map must map a value to a function", 0)
end
local present = source(false)
local delay = nil :: number?
local destroy, object = branch(function()
local object, t = component(present)
delay = t
return object
end)
present(true)
caches[key] = {
destroy_scope = destroy,
present = present,
object = object,
delay = delay or 0,
timeout = nil
}
end
end
end
update_output()
end)
return output
end
local function switch<K, Obj>(input: Source<K>): (map: Map<K, Component<Obj>>) -> Source<nil | Obj | Array<Obj>>
return function(map)
local last_scope: Node<false>?
local last_component: (() -> U)?
local function update(cached): U?
local component = map[source()]
if component == last_component then return cached end
last_component = component
if last_scope then
destroy(last_scope :: Node<any>)
last_scope = nil
end
if component == nil then return nil end
if type(component) ~= "function" then
error "map must map a value to a function"
end
local new_scope = create_node(owner, false, false)
last_scope = new_scope :: Node<any>
push_scope(new_scope)
local ok, result = xpcall(component, debug.traceback)
pop_scope()
if not ok then error(result, 0) end
return result
end
local node = create_node(owner, update, nil)
evaluate_node(node)
return function()
push_scope_as_child_of(node)
return node.cache
end
return switch_map(input, map)
end
end