mirror of
https://github.com/centau/vide.git
synced 2026-10-21 11:44:07 +00:00
Implement delays for show() and switch()
This commit is contained in:
parent
2518e292ed
commit
5262ef711c
6 changed files with 376 additions and 216 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue