mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
This commit is contained in:
parent
dafdc0739b
commit
470d2b5407
10 changed files with 242 additions and 450 deletions
|
|
@ -5,11 +5,11 @@ local throw = require(script.Parent.throw)
|
||||||
local flags = require(script.Parent.flags)
|
local flags = require(script.Parent.flags)
|
||||||
local graph = require(script.Parent.graph)
|
local graph = require(script.Parent.graph)
|
||||||
type Node<T> = graph.Node<T>
|
type Node<T> = graph.Node<T>
|
||||||
local create = graph.create
|
local create_node = graph.create_node
|
||||||
local init_scope = graph.init_scope
|
local open_scope = graph.open_scope
|
||||||
local close_scope = graph.close_scope
|
local close_scope = graph.close_scope
|
||||||
local add_child = graph.add_child
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
local binding = create(instance)
|
local binding = create_node(instance)
|
||||||
|
|
||||||
init_scope(binding)
|
|
||||||
|
|
||||||
-- run setter to capture any nodes being depended on
|
|
||||||
local nodes = (capture(setter :: () -> unknown, instance))
|
|
||||||
|
|
||||||
close_scope()
|
|
||||||
|
|
||||||
binding.effect = setter
|
binding.effect = setter
|
||||||
|
|
||||||
-- register the setter as a side-effect of each node
|
open_scope(binding.scope)
|
||||||
for _, node in next, nodes do
|
|
||||||
add_child(node, binding)
|
capture_parents(binding, setter :: () -> any, instance)
|
||||||
end
|
|
||||||
|
close_scope()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function bind_property(instance: Instance, property: string, fn: () -> unknown)
|
local function bind_property(instance: Instance, property: string, fn: () -> unknown)
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,29 @@
|
||||||
if not game then script = require "test/relative-string" end
|
if not game then script = require "test/relative-string" end
|
||||||
|
|
||||||
local graph = require(script.Parent.graph)
|
local graph = require(script.Parent.graph)
|
||||||
local create = graph.create
|
local create_node = graph.create_node
|
||||||
local capture = graph.capture
|
local capture_parents = graph.capture_parents
|
||||||
local add_child = graph.add_child
|
local add_child = graph.add_child
|
||||||
local set_effect = graph.set_effect
|
|
||||||
local update = graph.update
|
local update = graph.update
|
||||||
local track = graph.track
|
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 close_scope = graph.close_scope
|
||||||
|
|
||||||
local function derive<T>(fn: () -> T): () -> T
|
local function derive<T>(fn: () -> T): () -> T
|
||||||
local node = create((false :: any) :: T)
|
assert(get_scope())
|
||||||
|
|
||||||
init_scope(node)
|
local node = create_node((false :: any) :: T)
|
||||||
|
node.effect = function()
|
||||||
local nodes, value = capture(fn)
|
|
||||||
|
|
||||||
close_scope()
|
|
||||||
|
|
||||||
for _, parent in next, nodes do
|
|
||||||
add_child(parent, node)
|
|
||||||
end
|
|
||||||
|
|
||||||
set_effect(node, function()
|
|
||||||
node.cache = fn()
|
node.cache = fn()
|
||||||
update(node)
|
update(node)
|
||||||
end)
|
end
|
||||||
|
|
||||||
node.cache = value
|
open_scope(node.scope)
|
||||||
|
|
||||||
|
node.cache = capture_parents(node, fn)
|
||||||
|
|
||||||
|
close_scope()
|
||||||
|
|
||||||
return function()
|
return function()
|
||||||
track(node)
|
track(node)
|
||||||
|
|
|
||||||
165
src/graph.luau
165
src/graph.luau
|
|
@ -2,14 +2,18 @@ if not game then script = require "test/relative-string" end
|
||||||
|
|
||||||
local throw = require(script.Parent.throw)
|
local throw = require(script.Parent.throw)
|
||||||
local flags = require(script.Parent.flags)
|
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> = {
|
export type Node<T> = {
|
||||||
|
scope: Scope,
|
||||||
cache: T,
|
cache: T,
|
||||||
effect: (unknown) -> (),
|
effect: (T) -> (),
|
||||||
parents: { Node<T> } | false,
|
[number]: Node<T> -- children
|
||||||
children: { Node<T> } | false, -- weak values
|
|
||||||
cleanups: { () -> () } | false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
-- flag used to detect when node reference capturing is active
|
-- 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
|
-- array of all nodes referenced since above flag was set
|
||||||
local refs = {} :: { Node<unknown> }
|
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 WEAK_VALUES = { __mode = "v" }
|
||||||
local EVALUATION_ERR = "error while evaluating source:\n\n"
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_scope(): Node<unknown>
|
local function get_scope(): Scope
|
||||||
return scopes[scopes.n]
|
return scopes[scopes.n]
|
||||||
end
|
end
|
||||||
|
|
||||||
local function open_scope(node: Node<unknown>)
|
local function open_scope(scope: Scope)
|
||||||
local n = scopes.n + 1
|
local n = scopes.n + 1
|
||||||
scopes.n = n
|
scopes.n = n
|
||||||
scopes[n] = node
|
scopes[n] = scope
|
||||||
end
|
end
|
||||||
|
|
||||||
local function close_scope()
|
local function close_scope()
|
||||||
|
|
@ -66,20 +70,20 @@ local function close_scope()
|
||||||
scopes[n] = nil
|
scopes[n] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function add_cleanup(node: Node<unknown>, cleanup: () -> ())
|
local function add_cleanup(scope: Scope, cleanup: () -> ())
|
||||||
if node.cleanups then
|
if scope.cleanups then
|
||||||
table.insert(node.cleanups, cleanup)
|
table.insert(scope.cleanups, cleanup)
|
||||||
else
|
else
|
||||||
node.cleanups = { cleanup }
|
scope.cleanups = { cleanup }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function run_cleanups(node: { cleanups: { () -> () } | false})
|
local function run_cleanups(scope: Scope)
|
||||||
if node.cleanups then
|
if scope.cleanups then
|
||||||
for _, fn in next, node.cleanups do
|
for _, fn in next, scope.cleanups do
|
||||||
fn()
|
fn()
|
||||||
end
|
end
|
||||||
table.clear(node.cleanups)
|
table.clear(scope.cleanups)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -94,87 +98,40 @@ The weak key is passed as an argument to its side-effect callback.
|
||||||
|
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local function set_effect<T>(node: Node<unknown>, fn: () -> ())
|
local function run_effect<T>(node: Node<T>)
|
||||||
node.effect = fn
|
|
||||||
end
|
|
||||||
|
|
||||||
local function run_effect(node: Node<unknown>)
|
|
||||||
node.effect(node.cache)
|
node.effect(node.cache)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- links two nodes as parent-child
|
local function add_child(parent: Node<any>, child: Node<any>)
|
||||||
local function add_child(parent: Node<unknown>, child: Node<unknown>)
|
table.insert(parent, child)
|
||||||
if parent.children then
|
end
|
||||||
table.insert(parent.children, child)
|
|
||||||
else
|
local function add_children(parent: Node<any>, children: { Node<any> })
|
||||||
parent.children = { child }
|
for _, child in next, children do
|
||||||
setmetatable(parent.children :: any, {})
|
table.insert(parent, child)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function add_children(parent: Node<unknown>, children: { Node<unknown> })
|
local function destroy(scope: Scope)
|
||||||
if parent.children then
|
run_cleanups(scope)
|
||||||
for _, child in next, children do
|
for _, child in ipairs(scope) do
|
||||||
table.insert(parent.children, child)
|
destroy(child)
|
||||||
end
|
|
||||||
else
|
|
||||||
parent.children = table.clone(children)
|
|
||||||
end
|
end
|
||||||
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)
|
|
||||||
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
|
-- runs node effects, recalculates descendants and runs descendant effects
|
||||||
local function update(node: Node<unknown>)
|
local function update<T>(node: Node<T>)
|
||||||
if node.children then
|
for _, child in ipairs(node) do
|
||||||
for _, child in node.children do
|
local scope = child.scope
|
||||||
open_scope(child)
|
assert(scope)
|
||||||
run_cleanups(child)
|
open_scope(scope :: Scope)
|
||||||
run_effect(child)
|
run_cleanups(scope :: Scope)
|
||||||
update(child)
|
run_effect(child)
|
||||||
close_scope()
|
update(child)
|
||||||
end
|
close_scope()
|
||||||
end
|
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
|
-- detect what nodes were referenced in the given callback and returns them in an array
|
||||||
local function capture<T, U>(fn: (U?) -> T, arg: U?): ({ Node<unknown> }, T)
|
local function capture<T, U>(fn: (U?) -> T, arg: U?): ({ Node<unknown> }, T)
|
||||||
if reff then throw("recursive capture detected") end
|
if reff then throw("recursive capture detected") end
|
||||||
|
|
@ -197,53 +154,51 @@ local function capture<T, U>(fn: (U?) -> T, arg: U?): ({ Node<unknown> }, T)
|
||||||
return refs, result :: T
|
return refs, result :: T
|
||||||
end
|
end
|
||||||
|
|
||||||
-- captures and links any detected nodes
|
local function capture_parents<T, U>(child: Node<T>, fn: (U?) -> T, arg: U?): T
|
||||||
local function capture_and_link<T>(child: Node<T>, derive: () -> T): T
|
local refs, result = capture(fn, arg)
|
||||||
local nodes, value = capture(derive, nil)
|
|
||||||
|
|
||||||
child.effect = function()
|
for _, parent in next, refs do
|
||||||
child.cache = derive()
|
|
||||||
end
|
|
||||||
for _, parent: Node<unknown> in next, nodes do
|
|
||||||
add_child(parent, child)
|
add_child(parent, child)
|
||||||
end
|
end
|
||||||
|
|
||||||
return value :: T
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
local function track(node: Node<unknown>)
|
local function track<T>(node: Node<T>)
|
||||||
if reff then table.insert(refs, node) end
|
if reff then table.insert(refs, node :: Node<any>) end
|
||||||
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 = {
|
local node = {
|
||||||
|
scope = create_scope(),
|
||||||
cache = value,
|
cache = value,
|
||||||
effect = function() end,
|
effect = function() end,
|
||||||
parents = false :: false,
|
|
||||||
children = false :: false,
|
|
||||||
cleanups = false :: false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return node
|
return node
|
||||||
end
|
end
|
||||||
|
|
||||||
return table.freeze {
|
return table.freeze {
|
||||||
init_scope = init_scope,
|
|
||||||
open_scope = open_scope,
|
open_scope = open_scope,
|
||||||
close_scope = close_scope,
|
close_scope = close_scope,
|
||||||
get_scope = get_scope,
|
get_scope = get_scope,
|
||||||
add_cleanup = add_cleanup,
|
add_cleanup = add_cleanup,
|
||||||
destroy = destroy,
|
destroy = destroy,
|
||||||
run_cleanups = run_cleanups,
|
run_cleanups = run_cleanups,
|
||||||
set_effect = set_effect,
|
|
||||||
track = track,
|
track = track,
|
||||||
update = update,
|
update = update,
|
||||||
link = link,
|
|
||||||
add_parent = add_parent,
|
|
||||||
add_child = add_child,
|
add_child = add_child,
|
||||||
add_children = add_children,
|
add_children = add_children,
|
||||||
capture = capture,
|
capture = capture,
|
||||||
capture_and_link = capture_and_link,
|
capture_parents = capture_parents,
|
||||||
create = create :: (<T>(value: T) -> (Node<T>, () -> T)) & (<T>() -> (Node<T>, () -> T)),
|
create_node = create_node,
|
||||||
|
create_scope = create_scope,
|
||||||
refs = refs
|
refs = refs
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
if not game then script = require "test/relative-string" end
|
if not game then script = require "test/relative-string" end
|
||||||
|
|
||||||
|
local root = require(script.root)
|
||||||
local create = require(script.create)
|
local create = require(script.create)
|
||||||
local apply = require(script.apply)
|
local apply = require(script.apply)
|
||||||
local source = require(script.source)
|
local source = require(script.source)
|
||||||
|
|
@ -48,6 +49,7 @@ end)
|
||||||
|
|
||||||
local vide = {
|
local vide = {
|
||||||
-- core
|
-- core
|
||||||
|
root = root,
|
||||||
create = create,
|
create = create,
|
||||||
source = source,
|
source = source,
|
||||||
watch = watch,
|
watch = watch,
|
||||||
|
|
|
||||||
140
src/maps.luau
140
src/maps.luau
|
|
@ -5,18 +5,19 @@ if not game then script = require "test/relative-string" end
|
||||||
local throw = require(script.Parent.throw)
|
local throw = require(script.Parent.throw)
|
||||||
local flags = require(script.Parent.flags)
|
local flags = require(script.Parent.flags)
|
||||||
local graph = require(script.Parent.graph)
|
local graph = require(script.Parent.graph)
|
||||||
|
type Scope = graph.Scope
|
||||||
type Node<T> = graph.Node<T>
|
type Node<T> = graph.Node<T>
|
||||||
local create = graph.create
|
local create_node = graph.create_node
|
||||||
local set = graph.set
|
local create_scope = graph.create_scope
|
||||||
|
local track = graph.track
|
||||||
|
local update = graph.update
|
||||||
local capture = graph.capture
|
local capture = graph.capture
|
||||||
local run_cleanups = graph.run_cleanups
|
local capture_parents = graph.capture_parents
|
||||||
local set_child = graph.set_child
|
local add_child = graph.add_child
|
||||||
local open_new_scope = graph.open_new_scope
|
|
||||||
local get_scope = graph.get_scope
|
local get_scope = graph.get_scope
|
||||||
local open_scope = graph.open_scope
|
local open_scope = graph.open_scope
|
||||||
local close_scope = graph.close_scope
|
local close_scope = graph.close_scope
|
||||||
local link = graph.link
|
local destroy = graph.destroy
|
||||||
local destroy_tree = graph.destroy_tree
|
|
||||||
|
|
||||||
type Map<K, V> = { [K]: V }
|
type Map<K, V> = { [K]: V }
|
||||||
|
|
||||||
|
|
@ -31,15 +32,19 @@ end
|
||||||
|
|
||||||
-- todo: optimize output array
|
-- todo: optimize output array
|
||||||
local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI, K) -> VO): () -> { VO }
|
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 input_cache = {} :: Map<K, VI>
|
||||||
local output_cache = {} :: Map<K, VO>
|
local output_cache = {} :: Map<K, VO>
|
||||||
local input_nodes = {} :: Map<K, Node<VI>>
|
local input_nodes = {} :: Map<K, Node<VI>>
|
||||||
local remove_queue = {} :: { K }
|
local remove_queue = {} :: { K }
|
||||||
local output_array = {} :: { VO }
|
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
|
-- queue removed values
|
||||||
for i in next, input_cache do
|
for i in next, input_cache do
|
||||||
if data[i] == nil then
|
if data[i] == nil then
|
||||||
|
|
@ -49,8 +54,7 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
|
||||||
|
|
||||||
-- remove queued values
|
-- remove queued values
|
||||||
for _, i in next, remove_queue do
|
for _, i in next, remove_queue do
|
||||||
destroy_tree(scopes[i])
|
destroy(scopes[i])
|
||||||
|
|
||||||
|
|
||||||
input_cache[i] = nil
|
input_cache[i] = nil
|
||||||
output_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)
|
table.clear(remove_queue)
|
||||||
|
|
||||||
|
open_scope(root)
|
||||||
|
|
||||||
-- process new or changed values
|
-- process new or changed values
|
||||||
for i, v in next, data do
|
for i, v in next, data do
|
||||||
local cv = input_cache[i]
|
local cv = input_cache[i]
|
||||||
|
|
||||||
if cv ~= v then
|
if cv ~= v then
|
||||||
if cv == nil 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
|
input_nodes[i] = node
|
||||||
output_cache[i] = transform(get_value, i)
|
|
||||||
input_cache[i] = v
|
input_cache[i] = v
|
||||||
|
output_cache[i] = transform(function()
|
||||||
|
track(node)
|
||||||
|
return node.cache
|
||||||
|
end, i)
|
||||||
|
|
||||||
close_scope()
|
close_scope()
|
||||||
else
|
else
|
||||||
set(input_nodes[i], v)
|
input_nodes[i].cache = v
|
||||||
|
update(input_nodes[i])
|
||||||
input_cache[i] = v
|
input_cache[i] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
close_scope()
|
||||||
|
|
||||||
-- output elements
|
-- output elements
|
||||||
table.clear(output_array)
|
table.clear(output_array)
|
||||||
for _, v in next, output_cache do
|
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
|
return output_array
|
||||||
end
|
end
|
||||||
|
|
||||||
local output, read_output_value = create(nil :: any)
|
local output = create_node(false :: any)
|
||||||
|
output.effect = function()
|
||||||
local scope = create(false)
|
update_children(input())
|
||||||
|
|
||||||
local function derive()
|
|
||||||
local _ = scope
|
|
||||||
return recompute(input())
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local nodes, value = capture(input)
|
local value = capture_parents(output, input)
|
||||||
|
|
||||||
for _, node in next, nodes do
|
output.cache = update_children(value)
|
||||||
link(node, output, derive)
|
|
||||||
|
return function()
|
||||||
|
track(output)
|
||||||
|
return output.cache
|
||||||
end
|
end
|
||||||
|
|
||||||
output.cache = recompute(value)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local scope_parent = get_scope()
|
|
||||||
|
|
||||||
set_child(scope_parent, scope)
|
|
||||||
|
|
||||||
return read_output_value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- todo: optimize output array
|
-- todo: optimize output array
|
||||||
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO): () -> { VO }
|
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 cur_input_cache_up = {} :: Map<VI, K>
|
||||||
local new_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 input_nodes = {} :: Map<VI, Node<K>>
|
||||||
local output_array = {} :: { VO }
|
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
|
local cur_input_cache, new_input_cache = cur_input_cache_up, new_input_cache_up
|
||||||
|
|
||||||
if flags.strict then
|
if flags.strict then
|
||||||
|
|
@ -143,6 +149,8 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
open_scope(root)
|
||||||
|
|
||||||
-- process data
|
-- process data
|
||||||
for i, v in next, data do
|
for i, v in next, data do
|
||||||
new_input_cache[v] = i
|
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]
|
local cv = cur_input_cache[v]
|
||||||
|
|
||||||
if cv == nil then
|
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
|
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
|
else
|
||||||
if cv ~= i then
|
if cv ~= i then
|
||||||
set(input_nodes[v], i)
|
input_nodes[v].cache = i
|
||||||
|
update(input_nodes[v])
|
||||||
end
|
end
|
||||||
cur_input_cache[v] = nil
|
cur_input_cache[v] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
close_scope()
|
||||||
|
|
||||||
-- remove old values
|
-- remove old values
|
||||||
for v in next, cur_input_cache do
|
for v in next, cur_input_cache do
|
||||||
for _, callback in next, cleanups[v] do
|
destroy(scopes[v])
|
||||||
callback() -- todo: pcall
|
|
||||||
end
|
|
||||||
|
|
||||||
output_cache[v] = nil
|
output_cache[v] = nil
|
||||||
input_nodes[v] = nil
|
input_nodes[v] = nil
|
||||||
cleanups[v] = nil
|
scopes[v] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- update buffer cache
|
-- update buffer cache
|
||||||
|
|
@ -187,33 +202,24 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
|
||||||
table.insert(output_array, v)
|
table.insert(output_array, v)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
check_primitives(output_array)
|
||||||
|
|
||||||
return output_array
|
return output_array
|
||||||
end
|
end
|
||||||
|
|
||||||
local output, read_output_value = create(nil :: any)
|
local output = create_node(false :: any)
|
||||||
|
output.effect = function()
|
||||||
local function derive()
|
update_children(input())
|
||||||
return recompute(input())
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local nodes, value = capture(input)
|
local value = capture_parents(output, input)
|
||||||
|
|
||||||
for _, node in next, nodes do
|
output.cache = update_children(value)
|
||||||
link(node, output, derive)
|
|
||||||
|
return function()
|
||||||
|
track(output)
|
||||||
|
return output.cache
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
return function() return indexes, values end
|
return function() return indexes, values end
|
||||||
|
|
|
||||||
|
|
@ -5,33 +5,25 @@ local throw = require(script.Parent.throw)
|
||||||
local on_gc = require(script.Parent.on_gc)()
|
local on_gc = require(script.Parent.on_gc)()
|
||||||
local graph = require(script.Parent.graph)
|
local graph = require(script.Parent.graph)
|
||||||
type Node<T> = graph.Node<T>
|
type Node<T> = graph.Node<T>
|
||||||
local create = graph.create
|
local create_scope = graph.create_scope
|
||||||
local init_scope = graph.init_scope
|
local open_scope = graph.open_scope
|
||||||
local close_scope = graph.close_scope
|
local close_scope = graph.close_scope
|
||||||
local get_scope = graph.get_scope
|
local get_scope = graph.get_scope
|
||||||
local destroy = graph.destroy
|
local destroy = graph.destroy
|
||||||
|
|
||||||
local refs = {} :: { [Node<unknown>]: unknown }
|
local function root<T>(fn: () -> T): (T, () -> ())
|
||||||
setmetatable(refs :: any, { __mode = "v" })
|
|
||||||
|
|
||||||
local function root<T>(fn: () -> T): T
|
|
||||||
assert(not get_scope())
|
assert(not get_scope())
|
||||||
|
local scope = create_scope()
|
||||||
|
|
||||||
local node = create(nil) -- todo: lifetime with return vaue from fn
|
open_scope(scope)
|
||||||
|
|
||||||
init_scope(node)
|
|
||||||
|
|
||||||
local v = fn()
|
local v = fn()
|
||||||
|
|
||||||
close_scope()
|
close_scope()
|
||||||
|
|
||||||
refs[node] = v
|
return v, function()
|
||||||
|
destroy(scope)
|
||||||
on_gc(v, function()
|
end
|
||||||
destroy(node)
|
|
||||||
end)
|
|
||||||
|
|
||||||
return v
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,17 @@ if not game then script = require "test/relative-string" end
|
||||||
|
|
||||||
local graph = require(script.Parent.graph)
|
local graph = require(script.Parent.graph)
|
||||||
type Node<T> = graph.Node<T>
|
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 track = graph.track
|
||||||
local update = graph.update
|
local update = graph.update
|
||||||
|
|
||||||
export type Source<T> = (() -> T) & ((T) -> T)
|
export type Source<T> = (() -> T) & ((T) -> T)
|
||||||
|
|
||||||
local function source<T>(initial_value: T): Source<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
|
return function(...): T
|
||||||
if select("#", ...) == 0 then -- no args were given
|
if select("#", ...) == 0 then -- no args were given
|
||||||
|
|
|
||||||
|
|
@ -24,10 +24,11 @@ Unsupported datatypes:
|
||||||
local throw = require(script.Parent.throw)
|
local throw = require(script.Parent.throw)
|
||||||
local graph = require(script.Parent.graph)
|
local graph = require(script.Parent.graph)
|
||||||
type Node<T> = graph.Node<T>
|
type Node<T> = graph.Node<T>
|
||||||
local create = graph.create
|
local create_node = graph.create_node
|
||||||
local set = graph.set
|
local update = graph.update
|
||||||
local set_child = graph.set_child
|
|
||||||
local capture = graph.capture
|
local capture = graph.capture
|
||||||
|
local add_child = graph.add_child
|
||||||
|
local track = graph.track
|
||||||
|
|
||||||
local UPDATE_RATE = 120
|
local UPDATE_RATE = 120
|
||||||
local TOLERANCE = 0.0001
|
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 function spring<T>(source: () -> T, period: number?, damping_ratio: number?): () -> T
|
||||||
local inputs, initial_value = capture(source)
|
local inputs, initial_value = capture(source)
|
||||||
local output, output_get = create(initial_value)
|
|
||||||
|
|
||||||
local vtype = typeof(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,
|
source_value = initial_value,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- reschedule spring for simulation on input update
|
local output = create_node(initial_value)
|
||||||
local function input_updated()
|
|
||||||
|
|
||||||
|
local updater = create_node(false)
|
||||||
|
updater.effect = function()
|
||||||
local v = source()
|
local v = source()
|
||||||
data.x1_123, data.x1_456 = type_to_vec6[typeof(v)](v)
|
data.x1_123, data.x1_456 = type_to_vec6[typeof(v)](v)
|
||||||
data.source_value = v
|
data.source_value = v
|
||||||
springs[data] = output -- todo: investigate why insertion is not O(1) at ~20k springs
|
springs[data] = output -- todo: investigate why insertion is not O(1) at ~20k springs
|
||||||
end
|
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
|
for _, input in next, inputs do
|
||||||
set_child(input, output)
|
add_child(input, updater)
|
||||||
end
|
end
|
||||||
|
|
||||||
return output_get, data
|
return function()
|
||||||
|
track(output)
|
||||||
|
return output.cache
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function step_springs(dt: number)
|
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
|
if (v_123 + v_456 + dx_123 + dx_456).Magnitude < TOLERANCE then
|
||||||
-- close enough to target, unshedule spring and set value to target
|
-- close enough to target, unshedule spring and set value to target
|
||||||
table.insert(remove_queue, data)
|
table.insert(remove_queue, data)
|
||||||
set(output, data.source_value)
|
output.cache = data.source_value
|
||||||
else
|
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
|
end
|
||||||
|
update(output)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, data in next, remove_queue do
|
for _, data in next, remove_queue do
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,23 @@
|
||||||
if not game then script = require "test/relative-string" end
|
if not game then script = require "test/relative-string" end
|
||||||
|
|
||||||
local graph = require(script.Parent.graph)
|
local graph = require(script.Parent.graph)
|
||||||
local create = graph.create
|
local create_node = graph.create_node
|
||||||
local add_parent = graph.add_parent
|
local capture_parents = graph.capture_parents
|
||||||
local add_child = graph.add_child
|
local get_scope = graph.get_scope
|
||||||
local capture = graph.capture
|
local open_scope = graph.open_scope
|
||||||
local init_scope = graph.init_scope
|
|
||||||
local close_scope = graph.close_scope
|
local close_scope = graph.close_scope
|
||||||
local destroy = graph.destroy
|
|
||||||
|
|
||||||
local function watch(effect: () -> ()): () -> ()
|
local function watch(effect: () -> ())
|
||||||
local node = create(false)
|
assert(get_scope())
|
||||||
|
|
||||||
init_scope(node)
|
|
||||||
|
|
||||||
local nodes = capture(effect :: () -> nil)
|
|
||||||
|
|
||||||
close_scope()
|
|
||||||
|
|
||||||
|
local node = create_node(false)
|
||||||
node.effect = effect
|
node.effect = effect
|
||||||
|
|
||||||
for _, parent in next, nodes do
|
open_scope(node.scope)
|
||||||
add_parent(node, parent)
|
|
||||||
add_child(parent, node)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
capture_parents(node, effect :: () -> any)
|
||||||
|
|
||||||
local function unwatch()
|
close_scope()
|
||||||
destroy(node)
|
|
||||||
end
|
|
||||||
|
|
||||||
return unwatch
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return watch
|
return watch
|
||||||
|
|
|
||||||
234
test/tests.luau
234
test/tests.luau
|
|
@ -18,22 +18,29 @@ local function weak<T>(t: T & {}): T
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function wrap_root(fn: () -> ())
|
||||||
|
return function()
|
||||||
|
local _, destroy = vide.root(fn :: any)
|
||||||
|
destroy()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
TEST("graph", function()
|
TEST("graph", function()
|
||||||
local graph = require "src/graph"
|
local graph = require "src/graph"
|
||||||
local create = graph.create
|
local create_node = graph.create_node
|
||||||
local track = graph.track
|
local track = graph.track
|
||||||
local capture = graph.capture
|
local capture = graph.capture
|
||||||
local update = graph.update
|
local update = graph.update
|
||||||
local add_child = graph.add_child
|
local add_child = graph.add_child
|
||||||
|
|
||||||
do CASE "node creation"
|
do CASE "node creation"
|
||||||
local node = create(1)
|
local node = create_node(1)
|
||||||
CHECK(node.cache == 1)
|
CHECK(node.cache == 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "capture nodes"
|
do CASE "capture nodes"
|
||||||
local node1 = create(nil)
|
local node1 = create_node(nil)
|
||||||
local node2 = create(nil)
|
local node2 = create_node(nil)
|
||||||
local captured = capture(function()
|
local captured = capture(function()
|
||||||
track(node1)
|
track(node1)
|
||||||
track(node2)
|
track(node2)
|
||||||
|
|
@ -44,8 +51,8 @@ TEST("graph", function()
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "linking nodes"
|
do CASE "linking nodes"
|
||||||
local parent = create(1)
|
local parent = create_node(1)
|
||||||
local child = create(0)
|
local child = create_node(0)
|
||||||
|
|
||||||
add_child(parent, child)
|
add_child(parent, child)
|
||||||
|
|
||||||
|
|
@ -61,13 +68,13 @@ TEST("graph", function()
|
||||||
-- todo: further tests
|
-- todo: further tests
|
||||||
|
|
||||||
do CASE "nodes garbage collection"
|
do CASE "nodes garbage collection"
|
||||||
local wref = weak { create(1) }
|
local wref = weak { create_node(1) }
|
||||||
gc()
|
gc()
|
||||||
CHECK(not wref[1])
|
CHECK(not wref[1])
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("source()", function()
|
TEST("source()", wrap_root(function()
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local watch = vide.watch
|
local watch = vide.watch
|
||||||
|
|
||||||
|
|
@ -132,9 +139,9 @@ TEST("source()", function()
|
||||||
src(b)
|
src(b)
|
||||||
CHECK(count == 1)
|
CHECK(count == 1)
|
||||||
end
|
end
|
||||||
end)
|
end))
|
||||||
|
|
||||||
TEST("derive()", function()
|
TEST("derive()", wrap_root(function()
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local derive = vide.derive
|
local derive = vide.derive
|
||||||
|
|
||||||
|
|
@ -182,9 +189,9 @@ TEST("derive()", function()
|
||||||
gc()
|
gc()
|
||||||
CHECK(wref[1])
|
CHECK(wref[1])
|
||||||
end
|
end
|
||||||
end)
|
end))
|
||||||
|
|
||||||
TEST("watch()", function()
|
TEST("watch()", wrap_root(function()
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local watch = vide.watch
|
local watch = vide.watch
|
||||||
local cleanup = vide.cleanup
|
local cleanup = vide.cleanup
|
||||||
|
|
@ -207,27 +214,13 @@ TEST("watch()", function()
|
||||||
CHECK(count == 3)
|
CHECK(count == 3)
|
||||||
end
|
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"
|
do CASE "side-effect cleanup"
|
||||||
local state = source(1)
|
local state = source(1)
|
||||||
|
|
||||||
local effect_count = 0
|
local effect_count = 0
|
||||||
local cleanup_count = 0
|
local cleanup_count = 0
|
||||||
|
|
||||||
local unwatch = watch(function()
|
watch(function()
|
||||||
state()
|
state()
|
||||||
effect_count += 1
|
effect_count += 1
|
||||||
cleanup(function() cleanup_count += 1 end)
|
cleanup(function() cleanup_count += 1 end)
|
||||||
|
|
@ -238,14 +231,6 @@ TEST("watch()", function()
|
||||||
state(2)
|
state(2)
|
||||||
CHECK(effect_count == 2)
|
CHECK(effect_count == 2)
|
||||||
CHECK(cleanup_count == 1)
|
CHECK(cleanup_count == 1)
|
||||||
|
|
||||||
unwatch()
|
|
||||||
unwatch = nil :: any
|
|
||||||
gc()
|
|
||||||
vide.step(0)
|
|
||||||
|
|
||||||
CHECK(effect_count == 2)
|
|
||||||
CHECK(cleanup_count == 2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "garbage collection"
|
do CASE "garbage collection"
|
||||||
|
|
@ -270,28 +255,6 @@ TEST("watch()", function()
|
||||||
CHECK(wref[1])
|
CHECK(wref[1])
|
||||||
end
|
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
|
do -- state can gc with watcher
|
||||||
local wref
|
local wref
|
||||||
|
|
||||||
|
|
@ -306,9 +269,9 @@ TEST("watch()", function()
|
||||||
CHECK(not wref[1])
|
CHECK(not wref[1])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end))
|
||||||
|
|
||||||
TEST("cleanup()", function()
|
TEST("cleanup()", wrap_root(function()
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local watch = vide.watch
|
local watch = vide.watch
|
||||||
local cleanup = vide.cleanup
|
local cleanup = vide.cleanup
|
||||||
|
|
@ -319,7 +282,7 @@ TEST("cleanup()", function()
|
||||||
local watched = 0
|
local watched = 0
|
||||||
local cleaned = 0
|
local cleaned = 0
|
||||||
|
|
||||||
local stop = watch(function()
|
watch(function()
|
||||||
state()
|
state()
|
||||||
watched += 1
|
watched += 1
|
||||||
cleanup(function()
|
cleanup(function()
|
||||||
|
|
@ -334,67 +297,6 @@ TEST("cleanup()", function()
|
||||||
|
|
||||||
CHECK(watched == 2)
|
CHECK(watched == 2)
|
||||||
CHECK(cleaned == 1)
|
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
|
end
|
||||||
|
|
||||||
do CASE "multiple cleanup"
|
do CASE "multiple cleanup"
|
||||||
|
|
@ -414,17 +316,9 @@ TEST("cleanup()", function()
|
||||||
state(3)
|
state(3)
|
||||||
CHECK(testkit.seq(queue, { 1, 2, 1, 2 }))
|
CHECK(testkit.seq(queue, { 1, 2, 1, 2 }))
|
||||||
end
|
end
|
||||||
|
end))
|
||||||
|
|
||||||
do CASE "no scope"
|
TEST("create()", wrap_root(function()
|
||||||
local ok = pcall(function()
|
|
||||||
cleanup(function() end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
CHECK(not ok)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
TEST("create()", function()
|
|
||||||
local create = vide.create
|
local create = vide.create
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
|
|
||||||
|
|
@ -534,6 +428,7 @@ TEST("create()", function()
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "binding garbage collection"
|
do CASE "binding garbage collection"
|
||||||
|
--[[
|
||||||
do -- instance should gc when unparented
|
do -- instance should gc when unparented
|
||||||
local state = source("Hi")
|
local state = source("Hi")
|
||||||
|
|
||||||
|
|
@ -546,7 +441,9 @@ TEST("create()", function()
|
||||||
gc()
|
gc()
|
||||||
CHECK(not wref[1])
|
CHECK(not wref[1])
|
||||||
end
|
end
|
||||||
|
]]
|
||||||
|
|
||||||
|
--[[
|
||||||
do -- instance should not gc when parented
|
do -- instance should not gc when parented
|
||||||
local state = source("Hi")
|
local state = source("Hi")
|
||||||
|
|
||||||
|
|
@ -573,7 +470,9 @@ TEST("create()", function()
|
||||||
gc()
|
gc()
|
||||||
CHECK(not wref[1])
|
CHECK(not wref[1])
|
||||||
end
|
end
|
||||||
|
]]
|
||||||
|
|
||||||
|
--[[
|
||||||
do -- instance does not allow gc of state
|
do -- instance does not allow gc of state
|
||||||
local label
|
local label
|
||||||
local wref
|
local wref
|
||||||
|
|
@ -591,6 +490,7 @@ TEST("create()", function()
|
||||||
CHECK(wref[2])
|
CHECK(wref[2])
|
||||||
CHECK(wref[1])
|
CHECK(wref[1])
|
||||||
end
|
end
|
||||||
|
]]
|
||||||
|
|
||||||
do -- state and instance should gc once both exit scope
|
do -- state and instance should gc once both exit scope
|
||||||
local wref
|
local wref
|
||||||
|
|
@ -610,6 +510,7 @@ TEST("create()", function()
|
||||||
CHECK(not wref.box)
|
CHECK(not wref.box)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
do -- binding should gc despite state still existing after instance is gc
|
do -- binding should gc despite state still existing after instance is gc
|
||||||
local state = source("Hi")
|
local state = source("Hi")
|
||||||
|
|
||||||
|
|
@ -622,7 +523,7 @@ TEST("create()", function()
|
||||||
Text = state,
|
Text = state,
|
||||||
}
|
}
|
||||||
|
|
||||||
local binding = assert(node.children)[1]
|
local binding = assert(node[1])
|
||||||
|
|
||||||
wref = weak {
|
wref = weak {
|
||||||
instance = instance,
|
instance = instance,
|
||||||
|
|
@ -636,6 +537,7 @@ TEST("create()", function()
|
||||||
CHECK(not wref.instance)
|
CHECK(not wref.instance)
|
||||||
CHECK(not wref.binding)
|
CHECK(not wref.binding)
|
||||||
end
|
end
|
||||||
|
]]
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "bind same state to multiple instance properties"
|
do CASE "bind same state to multiple instance properties"
|
||||||
|
|
@ -733,11 +635,9 @@ TEST("create()", function()
|
||||||
gc()
|
gc()
|
||||||
CHECK(wref.data and wref.proxy)
|
CHECK(wref.data and wref.proxy)
|
||||||
end
|
end
|
||||||
end)
|
end))
|
||||||
|
|
||||||
-- todo: gc and cleanup call check for removed element
|
TEST("indexes()", wrap_root(function()
|
||||||
|
|
||||||
TEST("indexes()", function()
|
|
||||||
local create = vide.create
|
local create = vide.create
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local indexes = vide.indexes
|
local indexes = vide.indexes
|
||||||
|
|
@ -839,21 +739,8 @@ TEST("indexes()", function()
|
||||||
local input = source { 1, 2, 3 }
|
local input = source { 1, 2, 3 }
|
||||||
|
|
||||||
local count = table.create(3, 0)
|
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)
|
local output = indexes(input, function(v, i)
|
||||||
-- check that overriden cleanup scopes don't affect cleanup calls
|
|
||||||
-- in other function scopes
|
|
||||||
unrelated()
|
|
||||||
|
|
||||||
cleanup(function()
|
cleanup(function()
|
||||||
count[i] += 1
|
count[i] += 1
|
||||||
end)
|
end)
|
||||||
|
|
@ -866,20 +753,10 @@ TEST("indexes()", function()
|
||||||
CHECK(count[1] == 0)
|
CHECK(count[1] == 0)
|
||||||
CHECK(count[2] == 0)
|
CHECK(count[2] == 0)
|
||||||
CHECK(count[3] == 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)
|
end))
|
||||||
|
|
||||||
TEST("values()", function()
|
TEST("values()", wrap_root(function()
|
||||||
local create = vide.create
|
local create = vide.create
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local values = vide.values
|
local values = vide.values
|
||||||
|
|
@ -967,21 +844,8 @@ TEST("values()", function()
|
||||||
local input = source { 1, 2, 3 }
|
local input = source { 1, 2, 3 }
|
||||||
|
|
||||||
local count = table.create(3, 0)
|
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)
|
local output = values(input, function(v, i)
|
||||||
-- check that overriden cleanup scopes don't affect cleanup calls
|
|
||||||
-- in other function scopes
|
|
||||||
unrelated()
|
|
||||||
|
|
||||||
cleanup(function()
|
cleanup(function()
|
||||||
count[i()] += 1
|
count[i()] += 1
|
||||||
end)
|
end)
|
||||||
|
|
@ -994,20 +858,10 @@ TEST("values()", function()
|
||||||
CHECK(count[1] == 0)
|
CHECK(count[1] == 0)
|
||||||
CHECK(count[2] == 0)
|
CHECK(count[2] == 0)
|
||||||
CHECK(count[3] == 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)
|
end))
|
||||||
|
|
||||||
TEST("spring()", function()
|
TEST("spring()", wrap_root(function()
|
||||||
local create = vide.create
|
local create = vide.create
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local spring = vide.spring
|
local spring = vide.spring
|
||||||
|
|
@ -1025,6 +879,7 @@ TEST("spring()", function()
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "garbage collection"
|
do CASE "garbage collection"
|
||||||
|
--[[
|
||||||
do -- `output` should not allow gc of `input`
|
do -- `output` should not allow gc of `input`
|
||||||
local input = source(10)
|
local input = source(10)
|
||||||
local _output = spring(input)
|
local _output = spring(input)
|
||||||
|
|
@ -1035,6 +890,7 @@ TEST("spring()", function()
|
||||||
gc()
|
gc()
|
||||||
CHECK(wref[1])
|
CHECK(wref[1])
|
||||||
end
|
end
|
||||||
|
]]
|
||||||
|
|
||||||
do -- `input` should allow gc of `output`
|
do -- `input` should allow gc of `output`
|
||||||
local input = source(10)
|
local input = source(10)
|
||||||
|
|
@ -1105,9 +961,9 @@ TEST("spring()", function()
|
||||||
vide.step(0) -- process spring queue
|
vide.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()", function()
|
TEST("untrack()", wrap_root(function()
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local watch = vide.watch
|
local watch = vide.watch
|
||||||
local untrack = vide.untrack
|
local untrack = vide.untrack
|
||||||
|
|
@ -1157,7 +1013,7 @@ TEST("untrack()", function()
|
||||||
b(1)
|
b(1)
|
||||||
CHECK(count == 1)
|
CHECK(count == 1)
|
||||||
end
|
end
|
||||||
end)
|
end))
|
||||||
|
|
||||||
TEST("events", function()
|
TEST("events", function()
|
||||||
local create = vide.create
|
local create = vide.create
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue