This commit is contained in:
Aaron Smith 2023-09-07 15:27:24 +01:00
parent dafdc0739b
commit 470d2b5407
10 changed files with 242 additions and 450 deletions

View file

@ -5,11 +5,11 @@ local throw = require(script.Parent.throw)
local flags = require(script.Parent.flags)
local graph = require(script.Parent.graph)
type Node<T> = graph.Node<T>
local create = graph.create
local init_scope = graph.init_scope
local create_node = graph.create_node
local open_scope = graph.open_scope
local close_scope = graph.close_scope
local add_child = graph.add_child
local capture = graph.capture
local capture_parents = graph.capture_parents
--[[
@ -67,21 +67,14 @@ function bind(instance: Instance, property: string, setter: (Instance) -> ())
end
end
local binding = create(instance)
init_scope(binding)
-- run setter to capture any nodes being depended on
local nodes = (capture(setter :: () -> unknown, instance))
close_scope()
local binding = create_node(instance)
binding.effect = setter
-- register the setter as a side-effect of each node
for _, node in next, nodes do
add_child(node, binding)
end
open_scope(binding.scope)
capture_parents(binding, setter :: () -> any, instance)
close_scope()
end
local function bind_property(instance: Instance, property: string, fn: () -> unknown)

View file

@ -1,34 +1,29 @@
if not game then script = require "test/relative-string" end
local graph = require(script.Parent.graph)
local create = graph.create
local capture = graph.capture
local create_node = graph.create_node
local capture_parents = graph.capture_parents
local add_child = graph.add_child
local set_effect = graph.set_effect
local update = graph.update
local track = graph.track
local init_scope = graph.init_scope
local get_scope = graph.get_scope
local open_scope = graph.open_scope
local close_scope = graph.close_scope
local function derive<T>(fn: () -> T): () -> T
local node = create((false :: any) :: T)
assert(get_scope())
init_scope(node)
local nodes, value = capture(fn)
close_scope()
for _, parent in next, nodes do
add_child(parent, node)
end
set_effect(node, function()
local node = create_node((false :: any) :: T)
node.effect = function()
node.cache = fn()
update(node)
end)
end
node.cache = value
open_scope(node.scope)
node.cache = capture_parents(node, fn)
close_scope()
return function()
track(node)

View file

@ -2,14 +2,18 @@ if not game then script = require "test/relative-string" end
local throw = require(script.Parent.throw)
local flags = require(script.Parent.flags)
local on_gc = require(script.Parent.on_gc)()
export type Scope = {
parent: Scope | false,
cleanups: { () -> () } | false,
[number]: Scope -- children
}
export type Node<T> = {
scope: Scope,
cache: T,
effect: (unknown) -> (),
parents: { Node<T> } | false,
children: { Node<T> } | false, -- weak values
cleanups: { () -> () } | false
effect: (T) -> (),
[number]: Node<T> -- children
}
-- flag used to detect when node reference capturing is active
@ -17,7 +21,7 @@ local reff = false
-- array of all nodes referenced since above flag was set
local refs = {} :: { Node<unknown> }
local scopes = { n = 0 } :: { [number]: Node<unknown>, n: number }
local scopes = { n = 0 } :: { [number]: Scope, n: number }
local WEAK_VALUES = { __mode = "v" }
local EVALUATION_ERR = "error while evaluating source:\n\n"
@ -50,14 +54,14 @@ local check_for_yield: <T...>(fn: (T...) -> unknown, T...) -> () do
end
end
local function get_scope(): Node<unknown>
local function get_scope(): Scope
return scopes[scopes.n]
end
local function open_scope(node: Node<unknown>)
local function open_scope(scope: Scope)
local n = scopes.n + 1
scopes.n = n
scopes[n] = node
scopes[n] = scope
end
local function close_scope()
@ -66,20 +70,20 @@ local function close_scope()
scopes[n] = nil
end
local function add_cleanup(node: Node<unknown>, cleanup: () -> ())
if node.cleanups then
table.insert(node.cleanups, cleanup)
local function add_cleanup(scope: Scope, cleanup: () -> ())
if scope.cleanups then
table.insert(scope.cleanups, cleanup)
else
node.cleanups = { cleanup }
scope.cleanups = { cleanup }
end
end
local function run_cleanups(node: { cleanups: { () -> () } | false})
if node.cleanups then
for _, fn in next, node.cleanups do
local function run_cleanups(scope: Scope)
if scope.cleanups then
for _, fn in next, scope.cleanups do
fn()
end
table.clear(node.cleanups)
table.clear(scope.cleanups)
end
end
@ -94,85 +98,38 @@ The weak key is passed as an argument to its side-effect callback.
]]
local function set_effect<T>(node: Node<unknown>, fn: () -> ())
node.effect = fn
end
local function run_effect(node: Node<unknown>)
local function run_effect<T>(node: Node<T>)
node.effect(node.cache)
end
-- links two nodes as parent-child
local function add_child(parent: Node<unknown>, child: Node<unknown>)
if parent.children then
table.insert(parent.children, child)
else
parent.children = { child }
setmetatable(parent.children :: any, {})
end
local function add_child(parent: Node<any>, child: Node<any>)
table.insert(parent, child)
end
local function add_children(parent: Node<unknown>, children: { Node<unknown> })
if parent.children then
local function add_children(parent: Node<any>, children: { Node<any> })
for _, child in next, children do
table.insert(parent.children, child)
end
else
parent.children = table.clone(children)
table.insert(parent, child)
end
end
local function add_parent(child: Node<unknown>, parent: Node<unknown>)
child.parents = { parent }
end
local function rec(node: { effect: any, children: { Node<unknown> }, cleanups: { () -> () }})
run_cleanups(node)
node.effect = function() assert(false) end
if node.children then
for _, child in node.children do
rec(child)
local function destroy(scope: Scope)
run_cleanups(scope)
for _, child in ipairs(scope) do
destroy(child)
end
end
end
local function destroy(node: Node<unknown>)
if node.parents then
for _, parent in node.parents do
parent.children[table.find(parent.children, node)] = nil -- todo: can iter invalidation occur here?
end
end
rec(node)
end
local function init_scope(node: Node<unknown>)
local parent = scopes[scopes.n]
if parent then
add_child(parent, node)
add_parent(node, parent)
end
open_scope(node)
end
-- runs node effects, recalculates descendants and runs descendant effects
local function update(node: Node<unknown>)
if node.children then
for _, child in node.children do
open_scope(child)
run_cleanups(child)
local function update<T>(node: Node<T>)
for _, child in ipairs(node) do
local scope = child.scope
assert(scope)
open_scope(scope :: Scope)
run_cleanups(scope :: Scope)
run_effect(child)
update(child)
close_scope()
end
end
end
-- links two nodes as parent-child with a function to compute a new value for child
local function link<T>(parent: Node<unknown>, child: Node<T>, derive: () -> T)
child.effect = function()
child.cache = derive()
end
add_child(parent, child)
end
-- detect what nodes were referenced in the given callback and returns them in an array
@ -197,53 +154,51 @@ local function capture<T, U>(fn: (U?) -> T, arg: U?): ({ Node<unknown> }, T)
return refs, result :: T
end
-- captures and links any detected nodes
local function capture_and_link<T>(child: Node<T>, derive: () -> T): T
local nodes, value = capture(derive, nil)
local function capture_parents<T, U>(child: Node<T>, fn: (U?) -> T, arg: U?): T
local refs, result = capture(fn, arg)
child.effect = function()
child.cache = derive()
end
for _, parent: Node<unknown> in next, nodes do
for _, parent in next, refs do
add_child(parent, child)
end
return value :: T
return result
end
local function track(node: Node<unknown>)
if reff then table.insert(refs, node) end
local function track<T>(node: Node<T>)
if reff then table.insert(refs, node :: Node<any>) end
end
local function create<T>(value: T): Node<T>
local function create_scope(): Scope
return {
parent = get_scope() or false,
cleanups = false
}
end
local function create_node<T>(value: T): Node<T>
local node = {
scope = create_scope(),
cache = value,
effect = function() end,
parents = false :: false,
children = false :: false,
cleanups = false :: false
}
return node
end
return table.freeze {
init_scope = init_scope,
open_scope = open_scope,
close_scope = close_scope,
get_scope = get_scope,
add_cleanup = add_cleanup,
destroy = destroy,
run_cleanups = run_cleanups,
set_effect = set_effect,
track = track,
update = update,
link = link,
add_parent = add_parent,
add_child = add_child,
add_children = add_children,
capture = capture,
capture_and_link = capture_and_link,
create = create :: (<T>(value: T) -> (Node<T>, () -> T)) & (<T>() -> (Node<T>, () -> T)),
capture_parents = capture_parents,
create_node = create_node,
create_scope = create_scope,
refs = refs
}

View file

@ -5,6 +5,7 @@
if not game then script = require "test/relative-string" end
local root = require(script.root)
local create = require(script.create)
local apply = require(script.apply)
local source = require(script.source)
@ -48,6 +49,7 @@ end)
local vide = {
-- core
root = root,
create = create,
source = source,
watch = watch,

View file

@ -5,18 +5,19 @@ if not game then script = require "test/relative-string" end
local throw = require(script.Parent.throw)
local flags = require(script.Parent.flags)
local graph = require(script.Parent.graph)
type Scope = graph.Scope
type Node<T> = graph.Node<T>
local create = graph.create
local set = graph.set
local create_node = graph.create_node
local create_scope = graph.create_scope
local track = graph.track
local update = graph.update
local capture = graph.capture
local run_cleanups = graph.run_cleanups
local set_child = graph.set_child
local open_new_scope = graph.open_new_scope
local capture_parents = graph.capture_parents
local add_child = graph.add_child
local get_scope = graph.get_scope
local open_scope = graph.open_scope
local close_scope = graph.close_scope
local link = graph.link
local destroy_tree = graph.destroy_tree
local destroy = graph.destroy
type Map<K, V> = { [K]: V }
@ -31,15 +32,19 @@ end
-- todo: optimize output array
local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI, K) -> VO): () -> { VO }
assert(get_scope())
local root = create_scope()
local input_cache = {} :: Map<K, VI>
local output_cache = {} :: Map<K, VO>
local input_nodes = {} :: Map<K, Node<VI>>
local remove_queue = {} :: { K }
local output_array = {} :: { VO }
local scopes = {} :: Map<K, Node<unknown>>
local scopes = {} :: Map<K, Scope>
local function recompute(data)
local function update_children(data)
-- queue removed values
for i in next, input_cache do
if data[i] == nil then
@ -49,8 +54,7 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
-- remove queued values
for _, i in next, remove_queue do
destroy_tree(scopes[i])
destroy(scopes[i])
input_cache[i] = nil
output_cache[i] = nil
@ -60,29 +64,38 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
table.clear(remove_queue)
open_scope(root)
-- process new or changed values
for i, v in next, data do
local cv = input_cache[i]
if cv ~= v then
if cv == nil then
local scope = create(false)
local scope = create_scope()
scopes[i] = scope
open_new_scope(scope)
open_scope(scope)
local node, get_value = create(v)
local node = create_node(v)
input_nodes[i] = node
output_cache[i] = transform(get_value, i)
input_cache[i] = v
output_cache[i] = transform(function()
track(node)
return node.cache
end, i)
close_scope()
else
set(input_nodes[i], v)
input_nodes[i].cache = v
update(input_nodes[i])
input_cache[i] = v
end
end
end
close_scope()
-- output elements
table.clear(output_array)
for _, v in next, output_cache do
@ -93,34 +106,27 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
return output_array
end
local output, read_output_value = create(nil :: any)
local scope = create(false)
local function derive()
local _ = scope
return recompute(input())
local output = create_node(false :: any)
output.effect = function()
update_children(input())
end
local nodes, value = capture(input)
local value = capture_parents(output, input)
for _, node in next, nodes do
link(node, output, derive)
output.cache = update_children(value)
return function()
track(output)
return output.cache
end
output.cache = recompute(value)
local scope_parent = get_scope()
set_child(scope_parent, scope)
return read_output_value
end
-- todo: optimize output array
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO): () -> { VO }
assert(get_scope())
local root = create_scope()
local cur_input_cache_up = {} :: Map<VI, K>
local new_input_cache_up = {} :: Map<VI, K>
@ -128,9 +134,9 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
local input_nodes = {} :: Map<VI, Node<K>>
local output_array = {} :: { VO }
local cleanups = {} :: Map<VI, { () -> () }>
local scopes = {} :: Map<VI, Scope>
local function recompute(data: Map<K, VI>)
local function update_children(data: Map<K, VI>)
local cur_input_cache, new_input_cache = cur_input_cache_up, new_input_cache_up
if flags.strict then
@ -143,6 +149,8 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
end
end
open_scope(root)
-- process data
for i, v in next, data do
new_input_cache[v] = i
@ -150,30 +158,37 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
local cv = cur_input_cache[v]
if cv == nil then
manual_cleanup_mode(transform)
local scope = create_scope()
scopes[v] = scope
local node, get_value = create(i)
open_scope(scope)
local node = create_node(i)
input_nodes[v] = node
output_cache[v] = transform(v, get_value)
output_cache[v] = transform(v, function()
track(node)
return node.cache
end)
cleanups[v] = manual_cleanup_mode(nil)
close_scope()
else
if cv ~= i then
set(input_nodes[v], i)
input_nodes[v].cache = i
update(input_nodes[v])
end
cur_input_cache[v] = nil
end
end
close_scope()
-- remove old values
for v in next, cur_input_cache do
for _, callback in next, cleanups[v] do
callback() -- todo: pcall
end
destroy(scopes[v])
output_cache[v] = nil
input_nodes[v] = nil
cleanups[v] = nil
scopes[v] = nil
end
-- update buffer cache
@ -187,33 +202,24 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
table.insert(output_array, v)
end
check_primitives(output_array)
return output_array
end
local output, read_output_value = create(nil :: any)
local function derive()
return recompute(input())
local output = create_node(false :: any)
output.effect = function()
update_children(input())
end
local nodes, value = capture(input)
local value = capture_parents(output, input)
for _, node in next, nodes do
link(node, output, derive)
output.cache = update_children(value)
return function()
track(output)
return output.cache
end
check_primitives(output_array)
output.cache = recompute(value)
cleanup_ref(tostring(output), output, function()
for _, callbacks in next, cleanups do
for _, callback in next, callbacks do
callback() -- todo: pcall
end
end
end)
return read_output_value
end
return function() return indexes, values end

View file

@ -5,33 +5,25 @@ local throw = require(script.Parent.throw)
local on_gc = require(script.Parent.on_gc)()
local graph = require(script.Parent.graph)
type Node<T> = graph.Node<T>
local create = graph.create
local init_scope = graph.init_scope
local create_scope = graph.create_scope
local open_scope = graph.open_scope
local close_scope = graph.close_scope
local get_scope = graph.get_scope
local destroy = graph.destroy
local refs = {} :: { [Node<unknown>]: unknown }
setmetatable(refs :: any, { __mode = "v" })
local function root<T>(fn: () -> T): T
local function root<T>(fn: () -> T): (T, () -> ())
assert(not get_scope())
local scope = create_scope()
local node = create(nil) -- todo: lifetime with return vaue from fn
init_scope(node)
open_scope(scope)
local v = fn()
close_scope()
refs[node] = v
on_gc(v, function()
destroy(node)
end)
return v
return v, function()
destroy(scope)
end
end
return root

View file

@ -2,14 +2,17 @@ if not game then script = require "test/relative-string" end
local graph = require(script.Parent.graph)
type Node<T> = graph.Node<T>
local create = graph.create
local create_node = graph.create_node
local get_scope = graph.get_scope
local track = graph.track
local update = graph.update
export type Source<T> = (() -> T) & ((T) -> T)
local function source<T>(initial_value: T): Source<T>
local node = create(initial_value)
assert(get_scope())
local node = create_node(initial_value)
return function(...): T
if select("#", ...) == 0 then -- no args were given

View file

@ -24,10 +24,11 @@ Unsupported datatypes:
local throw = require(script.Parent.throw)
local graph = require(script.Parent.graph)
type Node<T> = graph.Node<T>
local create = graph.create
local set = graph.set
local set_child = graph.set_child
local create_node = graph.create_node
local update = graph.update
local capture = graph.capture
local add_child = graph.add_child
local track = graph.track
local UPDATE_RATE = 120
local TOLERANCE = 0.0001
@ -146,7 +147,6 @@ setmetatable(springs, { __mode = "v" })
local function spring<T>(source: () -> T, period: number?, damping_ratio: number?): () -> T
local inputs, initial_value = capture(source)
local output, output_get = create(initial_value)
local vtype = typeof(initial_value)
@ -177,23 +177,25 @@ local function spring<T>(source: () -> T, period: number?, damping_ratio: number
source_value = initial_value,
}
-- reschedule spring for simulation on input update
local function input_updated()
local output = create_node(initial_value)
local updater = create_node(false)
updater.effect = function()
local v = source()
data.x1_123, data.x1_456 = type_to_vec6[typeof(v)](v)
data.source_value = v
springs[data] = output -- todo: investigate why insertion is not O(1) at ~20k springs
end
-- unused field, use so output prevents gc of inputs
output.effect = input_updated
-- register above function as side-effect for all inputs
for _, input in next, inputs do
set_child(input, output)
add_child(input, updater)
end
return output_get, data
return function()
track(output)
return output.cache
end
end
local function step_springs(dt: number)
@ -251,10 +253,11 @@ local function update_spring_sources()
if (v_123 + v_456 + dx_123 + dx_456).Magnitude < TOLERANCE then
-- close enough to target, unshedule spring and set value to target
table.insert(remove_queue, data)
set(output, data.source_value)
output.cache = data.source_value
else
set(output, vec6_to_type[typeof(data.source_value)](x0_123, x0_456))
output.cache = vec6_to_type[typeof(data.source_value)](x0_123, x0_456)
end
update(output)
end
for _, data in next, remove_queue do

View file

@ -1,36 +1,23 @@
if not game then script = require "test/relative-string" end
local graph = require(script.Parent.graph)
local create = graph.create
local add_parent = graph.add_parent
local add_child = graph.add_child
local capture = graph.capture
local init_scope = graph.init_scope
local create_node = graph.create_node
local capture_parents = graph.capture_parents
local get_scope = graph.get_scope
local open_scope = graph.open_scope
local close_scope = graph.close_scope
local destroy = graph.destroy
local function watch(effect: () -> ()): () -> ()
local node = create(false)
init_scope(node)
local nodes = capture(effect :: () -> nil)
close_scope()
local function watch(effect: () -> ())
assert(get_scope())
local node = create_node(false)
node.effect = effect
for _, parent in next, nodes do
add_parent(node, parent)
add_child(parent, node)
end
open_scope(node.scope)
capture_parents(node, effect :: () -> any)
local function unwatch()
destroy(node)
end
return unwatch
close_scope()
end
return watch

View file

@ -18,22 +18,29 @@ local function weak<T>(t: T & {}): T
return t
end
local function wrap_root(fn: () -> ())
return function()
local _, destroy = vide.root(fn :: any)
destroy()
end
end
TEST("graph", function()
local graph = require "src/graph"
local create = graph.create
local create_node = graph.create_node
local track = graph.track
local capture = graph.capture
local update = graph.update
local add_child = graph.add_child
do CASE "node creation"
local node = create(1)
local node = create_node(1)
CHECK(node.cache == 1)
end
do CASE "capture nodes"
local node1 = create(nil)
local node2 = create(nil)
local node1 = create_node(nil)
local node2 = create_node(nil)
local captured = capture(function()
track(node1)
track(node2)
@ -44,8 +51,8 @@ TEST("graph", function()
end
do CASE "linking nodes"
local parent = create(1)
local child = create(0)
local parent = create_node(1)
local child = create_node(0)
add_child(parent, child)
@ -61,13 +68,13 @@ TEST("graph", function()
-- todo: further tests
do CASE "nodes garbage collection"
local wref = weak { create(1) }
local wref = weak { create_node(1) }
gc()
CHECK(not wref[1])
end
end)
TEST("source()", function()
TEST("source()", wrap_root(function()
local source = vide.source
local watch = vide.watch
@ -132,9 +139,9 @@ TEST("source()", function()
src(b)
CHECK(count == 1)
end
end)
end))
TEST("derive()", function()
TEST("derive()", wrap_root(function()
local source = vide.source
local derive = vide.derive
@ -182,9 +189,9 @@ TEST("derive()", function()
gc()
CHECK(wref[1])
end
end)
end))
TEST("watch()", function()
TEST("watch()", wrap_root(function()
local source = vide.source
local watch = vide.watch
local cleanup = vide.cleanup
@ -207,27 +214,13 @@ TEST("watch()", function()
CHECK(count == 3)
end
do CASE "stop watch"
local a = source(1)
local count = 0
local unwatch = watch(function()
a()
count += 1
end)
unwatch()
a(2)
CHECK(count == 1)
end
do CASE "side-effect cleanup"
local state = source(1)
local effect_count = 0
local cleanup_count = 0
local unwatch = watch(function()
watch(function()
state()
effect_count += 1
cleanup(function() cleanup_count += 1 end)
@ -238,14 +231,6 @@ TEST("watch()", function()
state(2)
CHECK(effect_count == 2)
CHECK(cleanup_count == 1)
unwatch()
unwatch = nil :: any
gc()
vide.step(0)
CHECK(effect_count == 2)
CHECK(cleanup_count == 2)
end
do CASE "garbage collection"
@ -270,28 +255,6 @@ TEST("watch()", function()
CHECK(wref[1])
end
do -- watcher can gc if stopped
local state = source(1)
local wref, unwatch
do
local effect = factory(state)
unwatch = watch(effect)
wref = weak { effect }
end
gc()
CHECK(wref[1])
unwatch()
unwatch = nil :: any -- unwatch holds ref to effect
gc()
CHECK(not wref[1])
end
do -- state can gc with watcher
local wref
@ -306,9 +269,9 @@ TEST("watch()", function()
CHECK(not wref[1])
end
end
end)
end))
TEST("cleanup()", function()
TEST("cleanup()", wrap_root(function()
local source = vide.source
local watch = vide.watch
local cleanup = vide.cleanup
@ -319,7 +282,7 @@ TEST("cleanup()", function()
local watched = 0
local cleaned = 0
local stop = watch(function()
watch(function()
state()
watched += 1
cleanup(function()
@ -334,67 +297,6 @@ TEST("cleanup()", function()
CHECK(watched == 2)
CHECK(cleaned == 1)
stop()
do -- vide detects by iterating through and checking for gc'd refs
stop = nil :: any
gc()
vide.step(0)
end
CHECK(watched == 2)
CHECK(cleaned == 2)
end
do CASE "scoped"
local function setup()
local state = source(1)
local obj = { cleaned = 0 }
local _stop = watch(function()
state()
cleanup(function()
obj.cleaned += 1
end)
end)
return state, obj
end
local stateA, objA = setup()
local stateB, objB = setup()
CHECK(objA.cleaned == 0)
CHECK(objB.cleaned == 0)
stateA(2)
CHECK(objA.cleaned == 1)
CHECK(objB.cleaned == 0)
stateB(2)
CHECK(objA.cleaned == 1)
CHECK(objB.cleaned == 1)
do
stateA = nil :: any
gc()
vide.step(0)
end
CHECK(objA.cleaned == 2)
CHECK(objB.cleaned == 1)
do
stateB = nil :: any
gc()
vide.step(0)
end
CHECK(objA.cleaned == 2)
CHECK(objB.cleaned == 2)
end
do CASE "multiple cleanup"
@ -414,17 +316,9 @@ TEST("cleanup()", function()
state(3)
CHECK(testkit.seq(queue, { 1, 2, 1, 2 }))
end
end))
do CASE "no scope"
local ok = pcall(function()
cleanup(function() end)
end)
CHECK(not ok)
end
end)
TEST("create()", function()
TEST("create()", wrap_root(function()
local create = vide.create
local source = vide.source
@ -534,6 +428,7 @@ TEST("create()", function()
end
do CASE "binding garbage collection"
--[[
do -- instance should gc when unparented
local state = source("Hi")
@ -546,7 +441,9 @@ TEST("create()", function()
gc()
CHECK(not wref[1])
end
]]
--[[
do -- instance should not gc when parented
local state = source("Hi")
@ -573,7 +470,9 @@ TEST("create()", function()
gc()
CHECK(not wref[1])
end
]]
--[[
do -- instance does not allow gc of state
local label
local wref
@ -591,6 +490,7 @@ TEST("create()", function()
CHECK(wref[2])
CHECK(wref[1])
end
]]
do -- state and instance should gc once both exit scope
local wref
@ -610,6 +510,7 @@ TEST("create()", function()
CHECK(not wref.box)
end
--[[
do -- binding should gc despite state still existing after instance is gc
local state = source("Hi")
@ -622,7 +523,7 @@ TEST("create()", function()
Text = state,
}
local binding = assert(node.children)[1]
local binding = assert(node[1])
wref = weak {
instance = instance,
@ -636,6 +537,7 @@ TEST("create()", function()
CHECK(not wref.instance)
CHECK(not wref.binding)
end
]]
end
do CASE "bind same state to multiple instance properties"
@ -733,11 +635,9 @@ TEST("create()", function()
gc()
CHECK(wref.data and wref.proxy)
end
end)
end))
-- todo: gc and cleanup call check for removed element
TEST("indexes()", function()
TEST("indexes()", wrap_root(function()
local create = vide.create
local source = vide.source
local indexes = vide.indexes
@ -839,21 +739,8 @@ TEST("indexes()", function()
local input = source { 1, 2, 3 }
local count = table.create(3, 0)
local unrelated_count = 0
local unrelated = (function()
return function()
cleanup(function()
unrelated_count += 1
end)
end
end)()
local output = indexes(input, function(v, i)
-- check that overriden cleanup scopes don't affect cleanup calls
-- in other function scopes
unrelated()
cleanup(function()
count[i] += 1
end)
@ -866,20 +753,10 @@ TEST("indexes()", function()
CHECK(count[1] == 0)
CHECK(count[2] == 0)
CHECK(count[3] == 0)
CHECK(unrelated_count == 2)
output = nil :: any
gc()
vide.step(0)
CHECK(count[1] == 1)
CHECK(count[2] == 1)
CHECK(count[3] == 1)
CHECK(unrelated_count == 2)
end
end)
end))
TEST("values()", function()
TEST("values()", wrap_root(function()
local create = vide.create
local source = vide.source
local values = vide.values
@ -967,21 +844,8 @@ TEST("values()", function()
local input = source { 1, 2, 3 }
local count = table.create(3, 0)
local unrelated_count = 0
local unrelated = (function()
return function()
cleanup(function()
unrelated_count += 1
end)
end
end)()
local output = values(input, function(v, i)
-- check that overriden cleanup scopes don't affect cleanup calls
-- in other function scopes
unrelated()
cleanup(function()
count[i()] += 1
end)
@ -994,20 +858,10 @@ TEST("values()", function()
CHECK(count[1] == 0)
CHECK(count[2] == 0)
CHECK(count[3] == 0)
CHECK(unrelated_count == 2)
output = nil :: any
gc()
vide.step(0)
CHECK(count[1] == 1)
CHECK(count[2] == 1)
CHECK(count[3] == 1)
CHECK(unrelated_count == 2)
end
end)
end))
TEST("spring()", function()
TEST("spring()", wrap_root(function()
local create = vide.create
local source = vide.source
local spring = vide.spring
@ -1025,6 +879,7 @@ TEST("spring()", function()
end
do CASE "garbage collection"
--[[
do -- `output` should not allow gc of `input`
local input = source(10)
local _output = spring(input)
@ -1035,6 +890,7 @@ TEST("spring()", function()
gc()
CHECK(wref[1])
end
]]
do -- `input` should allow gc of `output`
local input = source(10)
@ -1105,9 +961,9 @@ TEST("spring()", function()
vide.step(0) -- process spring queue
CHECK(count == 1) -- check spring was rescheduled correctly
end
end)
end))
TEST("untrack()", function()
TEST("untrack()", wrap_root(function()
local source = vide.source
local watch = vide.watch
local untrack = vide.untrack
@ -1157,7 +1013,7 @@ TEST("untrack()", function()
b(1)
CHECK(count == 1)
end
end)
end))
TEST("events", function()
local create = vide.create