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

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