mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +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
40
src/branch.luau
Normal file
40
src/branch.luau
Normal 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
|
||||||
|
|
@ -148,6 +148,10 @@ local update_queue = { n = 0 } :: { n: number, [number]: Node<any> }
|
||||||
|
|
||||||
local function evaluate_node<T>(node: Node<T>)
|
local function evaluate_node<T>(node: Node<T>)
|
||||||
if flags.strict then
|
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
|
local initial_value = node.cache
|
||||||
|
|
||||||
for i = 1, 2 do
|
for i = 1, 2 do
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
local version = { major = 0, minor = 3, patch = 1 }
|
local version = { major = 0, minor = 3, patch = 1 }
|
||||||
|
|
||||||
local root = require "./root"
|
local root = require "./root"
|
||||||
|
local branch = require "./branch"
|
||||||
local mount = require "./mount"
|
local mount = require "./mount"
|
||||||
local create = require "./create"
|
local create = require "./create"
|
||||||
local apply = require "./apply"
|
local apply = require "./apply"
|
||||||
|
|
@ -50,6 +51,7 @@ local vide = {
|
||||||
|
|
||||||
-- core
|
-- core
|
||||||
root = root,
|
root = root,
|
||||||
|
--branch = branch,
|
||||||
mount = mount,
|
mount = mount,
|
||||||
create = create,
|
create = create,
|
||||||
source = source,
|
source = source,
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,16 @@ local source = require "./source"
|
||||||
local derive = require "./derive"
|
local derive = require "./derive"
|
||||||
local effect = require "./effect"
|
local effect = require "./effect"
|
||||||
local untrack = require "./untrack"
|
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()
|
local filtered_input = source()
|
||||||
|
|
||||||
effect(function()
|
effect(function()
|
||||||
|
|
@ -17,17 +25,12 @@ local function show<T, U>(input: () -> T?, component: (() -> T) -> U, fallback:
|
||||||
return not not input()
|
return not not input()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- todo: is this needed?
|
return switch(input_is_truthy) {
|
||||||
-- local filtered_input_is_truthy = derive(function()
|
[true] = function(present)
|
||||||
-- return not not filtered_input()
|
return component(filtered_input, present)
|
||||||
-- end)
|
end,
|
||||||
|
[false] = fallback
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return show
|
return show
|
||||||
|
|
|
||||||
152
src/switch.luau
152
src/switch.luau
|
|
@ -1,61 +1,107 @@
|
||||||
local graph = require "./graph"
|
local branch = require "./branch"
|
||||||
type Node<T> = graph.Node<T>
|
local source = require "./source"
|
||||||
type SourceNode<T> = graph.SourceNode<T>
|
local effect = require "./effect"
|
||||||
local create_node = graph.create_node
|
local timeout = require "./timeout" ()
|
||||||
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
|
|
||||||
|
|
||||||
|
type Array<T> = { T }
|
||||||
type Map<K, V> = { [K]: V }
|
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 function switch_map<K, Obj>(input: Source<K>, map: Map<K, Component<Obj>>): Source<nil | Obj | Array<Obj>>
|
||||||
local owner = assert_stable_scope()
|
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)
|
return function(map)
|
||||||
local last_scope: Node<false>?
|
return switch_map(input, map)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
367
test/tests.luau
367
test/tests.luau
|
|
@ -6,6 +6,26 @@ local Instance, Signal = mock.Instance, mock.Signal
|
||||||
local Vector2, UDim2 = mock.Vector2, mock.UDim2
|
local Vector2, UDim2 = mock.Vector2, mock.UDim2
|
||||||
|
|
||||||
local vide = require "../../vide"
|
local vide = require "../../vide"
|
||||||
|
local root = vide.root
|
||||||
|
local mount = vide.mount
|
||||||
|
local create = vide.create
|
||||||
|
local source = vide.source
|
||||||
|
local effect = vide.effect
|
||||||
|
local derive = vide.derive
|
||||||
|
local switch = vide.switch
|
||||||
|
local show = vide.show
|
||||||
|
local indexes = vide.indexes
|
||||||
|
local values = vide.values
|
||||||
|
local cleanup = vide.cleanup
|
||||||
|
local untrack = vide.untrack
|
||||||
|
local read = vide.read
|
||||||
|
local batch = vide.batch
|
||||||
|
local context = vide.context
|
||||||
|
local spring = vide.spring
|
||||||
|
local action = vide.action
|
||||||
|
local changed = vide.changed
|
||||||
|
local apply = vide.apply
|
||||||
|
local step = vide.step
|
||||||
local graph = require "../../vide/src/graph"
|
local graph = require "../../vide/src/graph"
|
||||||
type Node<T> = graph.Node<T>
|
type Node<T> = graph.Node<T>
|
||||||
|
|
||||||
|
|
@ -300,11 +320,6 @@ TEST("graph", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("mount()", function()
|
TEST("mount()", function()
|
||||||
local mount = vide.mount
|
|
||||||
local create = vide.create
|
|
||||||
local source = vide.source
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
|
|
||||||
local screen = create "ScreenGui" {}
|
local screen = create "ScreenGui" {}
|
||||||
|
|
||||||
local text = source "foo"
|
local text = source "foo"
|
||||||
|
|
@ -334,9 +349,6 @@ TEST("mount()", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("root()", function()
|
TEST("root()", function()
|
||||||
local root = vide.root
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
|
|
||||||
local count = 0
|
local count = 0
|
||||||
|
|
||||||
root(function(destroy)
|
root(function(destroy)
|
||||||
|
|
@ -348,9 +360,6 @@ TEST("root()", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("source()", wrap_root(function()
|
TEST("source()", wrap_root(function()
|
||||||
local source = vide.source
|
|
||||||
local effect = vide.effect
|
|
||||||
|
|
||||||
do CASE "create source"
|
do CASE "create source"
|
||||||
local src = source(1)
|
local src = source(1)
|
||||||
CHECK(src() == 1)
|
CHECK(src() == 1)
|
||||||
|
|
@ -415,11 +424,6 @@ TEST("source()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("derive()", wrap_root(function()
|
TEST("derive()", wrap_root(function()
|
||||||
local source = vide.source
|
|
||||||
local derive = vide.derive
|
|
||||||
local effect = vide.effect
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
|
|
||||||
do CASE "derive new value on source change"
|
do CASE "derive new value on source change"
|
||||||
local a = source(1)
|
local a = source(1)
|
||||||
local b = source(2)
|
local b = source(2)
|
||||||
|
|
@ -507,7 +511,7 @@ TEST("derive()", wrap_root(function()
|
||||||
local count = 0
|
local count = 0
|
||||||
local a = source(0)
|
local a = source(0)
|
||||||
|
|
||||||
local destroy = vide.mount(function()
|
local destroy = mount(function()
|
||||||
local _b = derive(function()
|
local _b = derive(function()
|
||||||
cleanup(function()
|
cleanup(function()
|
||||||
count += 1
|
count += 1
|
||||||
|
|
@ -580,10 +584,6 @@ TEST("derive()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("effect()", wrap_root(function()
|
TEST("effect()", wrap_root(function()
|
||||||
local source = vide.source
|
|
||||||
local effect = vide.effect
|
|
||||||
local derive = vide.derive
|
|
||||||
|
|
||||||
do CASE "rerun on source change"
|
do CASE "rerun on source change"
|
||||||
local a = source(1)
|
local a = source(1)
|
||||||
local b = source(1)
|
local b = source(1)
|
||||||
|
|
@ -636,15 +636,10 @@ TEST("effect()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("cleanup()", wrap_root(function()
|
TEST("cleanup()", wrap_root(function()
|
||||||
local root = vide.root
|
|
||||||
local source = vide.source
|
|
||||||
local effect = vide.effect
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
|
|
||||||
do CASE "root cleanup"
|
do CASE "root cleanup"
|
||||||
local count = 0
|
local count = 0
|
||||||
|
|
||||||
local destroy = vide.mount(function()
|
local destroy = mount(function()
|
||||||
cleanup(function()
|
cleanup(function()
|
||||||
count += 1
|
count += 1
|
||||||
end)
|
end)
|
||||||
|
|
@ -717,10 +712,6 @@ TEST("cleanup()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("create()", wrap_root(function()
|
TEST("create()", wrap_root(function()
|
||||||
local create = vide.create
|
|
||||||
local source = vide.source
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
|
|
||||||
do CASE "create(\"ClassName\", props) syntax"
|
do CASE "create(\"ClassName\", props) syntax"
|
||||||
local frame = create("Frame", { BackgroundTransparency = 0.5, Name = "Foo" })
|
local frame = create("Frame", { BackgroundTransparency = 0.5, Name = "Foo" })
|
||||||
CHECK(frame.BackgroundTransparency == 0.5)
|
CHECK(frame.BackgroundTransparency == 0.5)
|
||||||
|
|
@ -862,7 +853,7 @@ TEST("create()", wrap_root(function()
|
||||||
do CASE "binding destroy"
|
do CASE "binding destroy"
|
||||||
local count = 0
|
local count = 0
|
||||||
|
|
||||||
local destroy = vide.mount(function()
|
local destroy = mount(function()
|
||||||
local src = source(0)
|
local src = source(0)
|
||||||
|
|
||||||
return create "TextLabel" {
|
return create "TextLabel" {
|
||||||
|
|
@ -928,7 +919,7 @@ TEST("create()", wrap_root(function()
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "parent bound to source"
|
do CASE "parent bound to source"
|
||||||
local _, wref, destroy = vide.root(function(destroy)
|
local _, wref, destroy = root(function(destroy)
|
||||||
local frame = create "Frame" { Name = "Parent" }
|
local frame = create "Frame" { Name = "Parent" }
|
||||||
local parent = source(frame :: Frame?)
|
local parent = source(frame :: Frame?)
|
||||||
|
|
||||||
|
|
@ -955,7 +946,7 @@ TEST("create()", wrap_root(function()
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "recursive create"
|
do CASE "recursive create"
|
||||||
local set_test_to_true = vide.action(function(self) (self :: any).test = true end)
|
local set_test_to_true = action(function(self) (self :: any).test = true end)
|
||||||
|
|
||||||
local f2
|
local f2
|
||||||
|
|
||||||
|
|
@ -1087,13 +1078,6 @@ TEST("create()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("show()", wrap_root(function()
|
TEST("show()", wrap_root(function()
|
||||||
local untrack = vide.untrack
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
local source = vide.source
|
|
||||||
local effect = vide.effect
|
|
||||||
local show = vide.show
|
|
||||||
local root = vide.root
|
|
||||||
|
|
||||||
do CASE "show component"
|
do CASE "show component"
|
||||||
local input = source(true)
|
local input = source(true)
|
||||||
local function one() return 1 end
|
local function one() return 1 end
|
||||||
|
|
@ -1139,7 +1123,6 @@ TEST("show()", wrap_root(function()
|
||||||
local count = 0
|
local count = 0
|
||||||
|
|
||||||
show(input :: () -> number?, function(value: () -> number)
|
show(input :: () -> number?, function(value: () -> number)
|
||||||
vide.cleanup(function() print "destroyed" end)
|
|
||||||
effect(function()
|
effect(function()
|
||||||
local v = value()
|
local v = value()
|
||||||
|
|
||||||
|
|
@ -1212,31 +1195,31 @@ TEST("show()", wrap_root(function()
|
||||||
|
|
||||||
branch = 1
|
branch = 1
|
||||||
weapon { id = "1", enchant = "fire" }
|
weapon { id = "1", enchant = "fire" }
|
||||||
CHECK(count == 8)
|
CHECK(count == 2)
|
||||||
|
|
||||||
branch = 2
|
branch = 2
|
||||||
weapon { id = "1", enchant = "poison" }
|
weapon { id = "1", enchant = "poison" }
|
||||||
CHECK(count == 10)
|
CHECK(count == 4)
|
||||||
|
|
||||||
weapon { id = "1", enchant = nil }
|
weapon { id = "1", enchant = nil }
|
||||||
CHECK(count == 10)
|
CHECK(count == 4)
|
||||||
|
|
||||||
branch = 1
|
branch = 1
|
||||||
weapon { id = "1", enchant = "fire" }
|
weapon { id = "1", enchant = "fire" }
|
||||||
CHECK(count == 14)
|
CHECK(count == 6)
|
||||||
|
|
||||||
weapon(nil)
|
weapon(nil)
|
||||||
|
|
||||||
branch = 2
|
branch = 2
|
||||||
weapon { id = "1", enchant = "poison" }
|
weapon { id = "1", enchant = "poison" }
|
||||||
CHECK(count == 22)
|
CHECK(count == 8)
|
||||||
|
|
||||||
vide.strict = false
|
vide.strict = false
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "alt" -- todo: move test
|
do CASE "alt" -- todo: move test
|
||||||
local visible = vide.source(true)
|
local visible = source(true)
|
||||||
local count = vide.source(0)
|
local count = source(0)
|
||||||
|
|
||||||
local outer = 0
|
local outer = 0
|
||||||
local inner = 0
|
local inner = 0
|
||||||
|
|
@ -1280,14 +1263,161 @@ TEST("show()", wrap_root(function()
|
||||||
CHECK(inner == 4)
|
CHECK(inner == 4)
|
||||||
CHECK(destroyed == 3)
|
CHECK(destroyed == 3)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do CASE "delay (destruction)"
|
||||||
|
local input = source(false)
|
||||||
|
|
||||||
|
local obj = {}
|
||||||
|
local value_upval
|
||||||
|
local present_upval
|
||||||
|
local cleaned = false
|
||||||
|
|
||||||
|
local output = show(input, function(value, present)
|
||||||
|
value_upval = value
|
||||||
|
present_upval = present
|
||||||
|
cleanup(function() cleaned = true end)
|
||||||
|
CHECK(present() == false)
|
||||||
|
return obj, 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
CHECK(output() == nil)
|
||||||
|
|
||||||
|
input(true)
|
||||||
|
|
||||||
|
CHECK(output() == obj)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == true)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
|
||||||
|
input(false)
|
||||||
|
|
||||||
|
CHECK(output() == obj)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == false)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
|
||||||
|
step(0.5)
|
||||||
|
|
||||||
|
CHECK(output() == obj)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == false)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
|
||||||
|
step(0.5 + 0.01)
|
||||||
|
|
||||||
|
CHECK(output() == nil)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == false)
|
||||||
|
CHECK(cleaned)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "delay (reactivate before destruction)"
|
||||||
|
local input = source(false)
|
||||||
|
|
||||||
|
local obj = {}
|
||||||
|
local value_upval
|
||||||
|
local present_upval
|
||||||
|
local cleaned = false
|
||||||
|
|
||||||
|
local output = show(input, function(value, present)
|
||||||
|
value_upval = value
|
||||||
|
present_upval = present
|
||||||
|
cleanup(function() cleaned = true end)
|
||||||
|
CHECK(present() == false)
|
||||||
|
return obj, 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
CHECK(output() == nil)
|
||||||
|
|
||||||
|
input(true)
|
||||||
|
|
||||||
|
CHECK(output() == obj)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == true)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
|
||||||
|
input(false)
|
||||||
|
|
||||||
|
CHECK(output() == obj)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == false)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
|
||||||
|
step(0.5)
|
||||||
|
|
||||||
|
CHECK(output() == obj)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == false)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
|
||||||
|
input(true)
|
||||||
|
|
||||||
|
CHECK(output() == obj)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == true)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
|
||||||
|
step(0.5 + 0.01)
|
||||||
|
|
||||||
|
CHECK(output() == obj)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == true)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "delay (with fallback)"
|
||||||
|
local input = source(false)
|
||||||
|
|
||||||
|
local obj = {}
|
||||||
|
local value_upval
|
||||||
|
local present_upval
|
||||||
|
local cleaned = false
|
||||||
|
|
||||||
|
local obj_fallback = {}
|
||||||
|
local present_fallback_upval
|
||||||
|
local cleaned_fallback = false
|
||||||
|
|
||||||
|
local output = show(input, function(value, present)
|
||||||
|
value_upval = value
|
||||||
|
present_upval = present
|
||||||
|
cleanup(function() cleaned = true end)
|
||||||
|
CHECK(present() == false)
|
||||||
|
return obj, 1
|
||||||
|
end, function(present)
|
||||||
|
present_fallback_upval = present
|
||||||
|
cleanup(function() cleaned_fallback = true end)
|
||||||
|
CHECK(present() == false)
|
||||||
|
return obj_fallback, 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
CHECK(output() == obj_fallback)
|
||||||
|
CHECK(value_upval == nil)
|
||||||
|
CHECK(present_upval == nil)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
CHECK(present_fallback_upval() == true)
|
||||||
|
CHECK(not cleaned_fallback)
|
||||||
|
|
||||||
|
input(true)
|
||||||
|
|
||||||
|
CHECK(type(output() == "table") and table.find(output(), obj) and table.find(output(), obj_fallback))
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == true)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
CHECK(present_fallback_upval() == false)
|
||||||
|
CHECK(not cleaned_fallback)
|
||||||
|
|
||||||
|
step(1 + 0.01)
|
||||||
|
|
||||||
|
CHECK(output() == obj)
|
||||||
|
CHECK(value_upval() == true)
|
||||||
|
CHECK(present_upval() == true)
|
||||||
|
CHECK(not cleaned)
|
||||||
|
CHECK(present_fallback_upval() == false)
|
||||||
|
CHECK(cleaned_fallback)
|
||||||
|
end
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("switch()", wrap_root(function()
|
TEST("switch()", wrap_root(function()
|
||||||
local source = vide.source
|
|
||||||
local switch = vide.switch
|
|
||||||
local effect = vide.effect
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
|
|
||||||
do CASE "update on source change"
|
do CASE "update on source change"
|
||||||
local input = source(true)
|
local input = source(true)
|
||||||
|
|
||||||
|
|
@ -1315,28 +1445,27 @@ TEST("switch()", wrap_root(function()
|
||||||
CHECK(output() == nil)
|
CHECK(output() == nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "same component different map"
|
-- do CASE "same component different map"
|
||||||
local input = source(0)
|
-- local input = source(0)
|
||||||
|
|
||||||
local function component()
|
-- local function component()
|
||||||
return {}
|
-- return {}
|
||||||
end
|
-- end
|
||||||
|
|
||||||
local output = switch(input) {
|
-- local output = switch(input) {
|
||||||
[1] = component,
|
-- [1] = component,
|
||||||
[2] = component
|
-- [2] = component
|
||||||
}
|
-- }
|
||||||
|
|
||||||
CHECK(output() == nil)
|
-- CHECK(output() == nil)
|
||||||
|
|
||||||
input(1)
|
-- input(1)
|
||||||
local instance = output()
|
-- local instance = output()
|
||||||
CHECK(instance)
|
-- CHECK(instance)
|
||||||
|
|
||||||
input(2)
|
-- input(2)
|
||||||
CHECK(output() == instance)
|
-- CHECK(output() == instance)
|
||||||
|
-- end
|
||||||
end
|
|
||||||
|
|
||||||
do CASE "scoped switch"
|
do CASE "scoped switch"
|
||||||
local input = source(true)
|
local input = source(true)
|
||||||
|
|
@ -1406,15 +1535,13 @@ TEST("switch()", wrap_root(function()
|
||||||
|
|
||||||
vide.strict = false
|
vide.strict = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do CASE "delay"
|
||||||
|
-- probably unneeded because show() uses switch() internally
|
||||||
|
end
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("indexes()", wrap_root(function()
|
TEST("indexes()", wrap_root(function()
|
||||||
local create = vide.create
|
|
||||||
local source = vide.source
|
|
||||||
local effect = vide.effect
|
|
||||||
local indexes = vide.indexes
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
|
|
||||||
do CASE "use source"
|
do CASE "use source"
|
||||||
local input = source { 1, 2, 3 }
|
local input = source { 1, 2, 3 }
|
||||||
|
|
||||||
|
|
@ -1432,7 +1559,7 @@ TEST("indexes()", wrap_root(function()
|
||||||
|
|
||||||
local count = table.create(3, 0)
|
local count = table.create(3, 0)
|
||||||
|
|
||||||
local _, output = vide.root(function()
|
local _, output = root(function()
|
||||||
local output = indexes(input, function(v, i)
|
local output = indexes(input, function(v, i)
|
||||||
count[i] += 1
|
count[i] += 1
|
||||||
return v
|
return v
|
||||||
|
|
@ -1607,11 +1734,6 @@ TEST("indexes()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("values()", wrap_root(function()
|
TEST("values()", wrap_root(function()
|
||||||
local create = vide.create
|
|
||||||
local source = vide.source
|
|
||||||
local values = vide.values
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
|
|
||||||
do CASE "use source"
|
do CASE "use source"
|
||||||
local input = source { 1, 2, 3 }
|
local input = source { 1, 2, 3 }
|
||||||
|
|
||||||
|
|
@ -1733,11 +1855,6 @@ TEST("values()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("spring()", wrap_root(function()
|
TEST("spring()", wrap_root(function()
|
||||||
local create = vide.create
|
|
||||||
local source = vide.source
|
|
||||||
local spring = vide.spring
|
|
||||||
local effect = vide.effect
|
|
||||||
|
|
||||||
do CASE "update source (on next step)"
|
do CASE "update source (on next step)"
|
||||||
local value = source(10)
|
local value = source(10)
|
||||||
local sprung = spring(value, 1, 1)
|
local sprung = spring(value, 1, 1)
|
||||||
|
|
@ -1745,7 +1862,7 @@ TEST("spring()", wrap_root(function()
|
||||||
CHECK(sprung() == 10)
|
CHECK(sprung() == 10)
|
||||||
value(20)
|
value(20)
|
||||||
CHECK(sprung() == 10)
|
CHECK(sprung() == 10)
|
||||||
vide.step(1/60)
|
step(1/60)
|
||||||
CHECK(sprung() ~= 10)
|
CHECK(sprung() ~= 10)
|
||||||
CHECK(sprung() > 10)
|
CHECK(sprung() > 10)
|
||||||
end
|
end
|
||||||
|
|
@ -1811,9 +1928,9 @@ TEST("spring()", wrap_root(function()
|
||||||
local output = spring(input)
|
local output = spring(input)
|
||||||
|
|
||||||
input(1)
|
input(1)
|
||||||
vide.step(0.05)
|
step(0.05)
|
||||||
CHECK(output() ~= input()) -- check spring is moving
|
CHECK(output() ~= input()) -- check spring is moving
|
||||||
vide.step(10) -- spring finished, should be internally removed from queue
|
step(10) -- spring finished, should be internally removed from queue
|
||||||
CHECK(output() == input()) -- check spring is at target
|
CHECK(output() == input()) -- check spring is at target
|
||||||
|
|
||||||
local count = -1
|
local count = -1
|
||||||
|
|
@ -1822,25 +1939,18 @@ TEST("spring()", wrap_root(function()
|
||||||
count += 1
|
count += 1
|
||||||
end)
|
end)
|
||||||
|
|
||||||
vide.step(1) -- attempt to cause another spring update
|
step(1) -- attempt to cause another spring update
|
||||||
CHECK(count == 0) -- check no update occurs as spring is finished
|
CHECK(count == 0) -- check no update occurs as spring is finished
|
||||||
--
|
--
|
||||||
|
|
||||||
gc() -- perform full gc
|
gc() -- perform full gc
|
||||||
input(2) -- spring should be re-added to spring queue
|
input(2) -- spring should be re-added to spring queue
|
||||||
vide.step(0) -- process spring queue
|
step(0) -- process spring queue
|
||||||
CHECK(count == 1) -- check spring was rescheduled correctly
|
CHECK(count == 1) -- check spring was rescheduled correctly
|
||||||
end
|
end
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("untrack()", wrap_root(function()
|
TEST("untrack()", wrap_root(function()
|
||||||
local source = vide.source
|
|
||||||
local effect = vide.effect
|
|
||||||
local derive = vide.derive
|
|
||||||
local untrack = vide.untrack
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
local root = vide.root
|
|
||||||
|
|
||||||
do CASE "does not register dependency"
|
do CASE "does not register dependency"
|
||||||
local a = source(0)
|
local a = source(0)
|
||||||
local b = source(0)
|
local b = source(0)
|
||||||
|
|
@ -1941,8 +2051,6 @@ TEST("untrack()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("events", function()
|
TEST("events", function()
|
||||||
local create = vide.create
|
|
||||||
|
|
||||||
local function Thing(props)
|
local function Thing(props)
|
||||||
local instance = Instance.new("Thing")
|
local instance = Instance.new("Thing")
|
||||||
instance.Signal = Signal.new()
|
instance.Signal = Signal.new()
|
||||||
|
|
@ -1969,9 +2077,6 @@ TEST("events", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("actions", function()
|
TEST("actions", function()
|
||||||
local create = vide.create
|
|
||||||
local action = vide.action
|
|
||||||
|
|
||||||
do CASE "run action"
|
do CASE "run action"
|
||||||
local ran = false
|
local ran = false
|
||||||
|
|
||||||
|
|
@ -2005,11 +2110,6 @@ TEST("actions", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("changed()", wrap_root(function()
|
TEST("changed()", wrap_root(function()
|
||||||
local root = vide.root
|
|
||||||
local create = vide.create
|
|
||||||
local source = vide.source
|
|
||||||
local changed = vide.changed
|
|
||||||
|
|
||||||
do CASE "outputs"
|
do CASE "outputs"
|
||||||
local output = source(nil)
|
local output = source(nil)
|
||||||
|
|
||||||
|
|
@ -2044,11 +2144,6 @@ TEST("changed()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("batch()", wrap_root(function()
|
TEST("batch()", wrap_root(function()
|
||||||
local source = vide.source
|
|
||||||
local derive = vide.derive
|
|
||||||
local effect = vide.effect
|
|
||||||
local batch = vide.batch
|
|
||||||
|
|
||||||
do CASE "evaluation deferred"
|
do CASE "evaluation deferred"
|
||||||
local a = source(0)
|
local a = source(0)
|
||||||
|
|
||||||
|
|
@ -2332,10 +2427,6 @@ TEST("batch()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("read()", wrap_root(function()
|
TEST("read()", wrap_root(function()
|
||||||
local source = vide.source
|
|
||||||
local effect = vide.effect
|
|
||||||
local read = vide.read :: any -- todo
|
|
||||||
|
|
||||||
do CASE "read primitive"
|
do CASE "read primitive"
|
||||||
CHECK(read(1) == 1)
|
CHECK(read(1) == 1)
|
||||||
end
|
end
|
||||||
|
|
@ -2360,12 +2451,6 @@ TEST("read()", wrap_root(function()
|
||||||
end))
|
end))
|
||||||
|
|
||||||
TEST("context()", function()
|
TEST("context()", function()
|
||||||
local root = vide.root
|
|
||||||
local context = vide.context
|
|
||||||
local effect = vide.effect
|
|
||||||
local untrack = vide.untrack
|
|
||||||
local show = vide.show
|
|
||||||
|
|
||||||
do CASE "set context"
|
do CASE "set context"
|
||||||
local ctx = context()
|
local ctx = context()
|
||||||
|
|
||||||
|
|
@ -2464,13 +2549,6 @@ TEST("context()", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("nested effects cases", function()
|
TEST("nested effects cases", function()
|
||||||
local vide = require "../../vide"
|
|
||||||
local source = vide.source
|
|
||||||
local effect = vide.effect
|
|
||||||
local untrack = vide.untrack
|
|
||||||
local cleanup = vide.cleanup
|
|
||||||
local root = vide.root
|
|
||||||
|
|
||||||
local ran = 0
|
local ran = 0
|
||||||
local cleaned = 0
|
local cleaned = 0
|
||||||
|
|
||||||
|
|
@ -2512,11 +2590,6 @@ TEST("nested effects cases", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("graph edge cases", wrap_root(function()
|
TEST("graph edge cases", wrap_root(function()
|
||||||
local source = vide.source
|
|
||||||
local derive = vide.derive
|
|
||||||
local effect = vide.effect
|
|
||||||
local root = vide.root
|
|
||||||
|
|
||||||
do CASE "diamond A,B,C,D"
|
do CASE "diamond A,B,C,D"
|
||||||
--[[
|
--[[
|
||||||
|
|
||||||
|
|
@ -2687,15 +2760,6 @@ end))
|
||||||
TEST("strict", wrap_root(function()
|
TEST("strict", wrap_root(function()
|
||||||
vide.strict = true
|
vide.strict = true
|
||||||
|
|
||||||
local root = vide.root
|
|
||||||
local show = vide.show
|
|
||||||
local create = vide.create
|
|
||||||
local source = vide.source
|
|
||||||
local derive = vide.derive
|
|
||||||
local effect = vide.effect
|
|
||||||
local indexes, values = vide.indexes, vide.values
|
|
||||||
local untrack = vide.untrack
|
|
||||||
|
|
||||||
do CASE "error on derived callback yield"
|
do CASE "error on derived callback yield"
|
||||||
local src = source(1)
|
local src = source(1)
|
||||||
|
|
||||||
|
|
@ -2832,7 +2896,7 @@ TEST("strict", wrap_root(function()
|
||||||
root(function()
|
root(function()
|
||||||
show(src, function()
|
show(src, function()
|
||||||
src(false)
|
src(false)
|
||||||
vide.cleanup(function() count += 1 end)
|
cleanup(function() count += 1 end)
|
||||||
return {}
|
return {}
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
@ -2841,6 +2905,7 @@ TEST("strict", wrap_root(function()
|
||||||
src(true)
|
src(true)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
CHECK(count == 0)
|
||||||
CHECK(not ok)
|
CHECK(not ok)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -2854,9 +2919,9 @@ TEST("strict", wrap_root(function()
|
||||||
effect(function()
|
effect(function()
|
||||||
untrack(function()
|
untrack(function()
|
||||||
indexes(src, function()
|
indexes(src, function()
|
||||||
vide.cleanup(function() count_1 += 1 end)
|
cleanup(function() count_1 += 1 end)
|
||||||
src {}
|
src {}
|
||||||
vide.cleanup(function() count_2 += 1 end)
|
cleanup(function() count_2 += 1 end)
|
||||||
return {}
|
return {}
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
@ -2880,9 +2945,9 @@ TEST("strict", wrap_root(function()
|
||||||
effect(function()
|
effect(function()
|
||||||
untrack(function()
|
untrack(function()
|
||||||
values(src, function()
|
values(src, function()
|
||||||
vide.cleanup(function() count_1 += 1 end)
|
cleanup(function() count_1 += 1 end)
|
||||||
src {}
|
src {}
|
||||||
vide.cleanup(function() count_2 += 1 end)
|
cleanup(function() count_2 += 1 end)
|
||||||
return {}
|
return {}
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue