mirror of
https://github.com/centau/vide.git
synced 2026-08-20 23:01:37 +00:00
This commit is contained in:
parent
7a9d7ccc12
commit
1f0b734956
3 changed files with 104 additions and 125 deletions
|
|
@ -13,3 +13,4 @@ local function cleanup(fn: () -> ())
|
||||||
end
|
end
|
||||||
|
|
||||||
return cleanup
|
return cleanup
|
||||||
|
|
||||||
|
|
|
||||||
160
src/graph.luau
160
src/graph.luau
|
|
@ -3,34 +3,22 @@ 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)
|
||||||
|
|
||||||
export type Scope = {
|
|
||||||
parent: Scope | false,
|
|
||||||
cleanups: { () -> () } | false,
|
|
||||||
[number]: Scope -- children
|
|
||||||
}
|
|
||||||
|
|
||||||
export type StartNode<T> = {
|
export type StartNode<T> = {
|
||||||
cache: T,
|
cache: T,
|
||||||
|
n: number,
|
||||||
[number]: Node<T>
|
[number]: Node<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Node<T> = StartNode<T> & {
|
export type Node<T> = StartNode<T> & {
|
||||||
scope: Scope,
|
|
||||||
effect: (T) -> (),
|
effect: (T) -> (),
|
||||||
|
cleanups: { () -> () } | false,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- flag used to detect when node reference capturing is active
|
local scopes = { n = 0 } :: { [number]: Node<any>, n: number }
|
||||||
local reff = false
|
|
||||||
-- array of all nodes referenced since above flag was set
|
|
||||||
local refs = {} :: { StartNode<unknown> }
|
|
||||||
|
|
||||||
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"
|
||||||
|
|
||||||
setmetatable(refs :: any, WEAK_VALUES)
|
|
||||||
|
|
||||||
-- runs a given callback in a context that Luau does not allow yielding in
|
-- runs a given callback in a context that Luau does not allow yielding in
|
||||||
local check_for_yield: <T...>(fn: (T...) -> unknown, T...) -> () do
|
local check_for_yield: <T...>(fn: (T...) -> unknown, T...) -> () do
|
||||||
local t = { __mode = "kv" }
|
local t = { __mode = "kv" }
|
||||||
|
|
@ -57,14 +45,32 @@ local check_for_yield: <T...>(fn: (T...) -> unknown, T...) -> () do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_scope(): Scope
|
local function get_scope(): Node<unknown>
|
||||||
return scopes[scopes.n]
|
return scopes[scopes.n]
|
||||||
end
|
end
|
||||||
|
|
||||||
local function open_scope(scope: Scope)
|
local function add_child<T>(parent: Node<any>, child: Node<any>)
|
||||||
|
local n = parent.n + 1
|
||||||
|
parent.n = n
|
||||||
|
parent[n] = child
|
||||||
|
end
|
||||||
|
|
||||||
|
-- local function open_root_scope<T>(node: Node<T>)
|
||||||
|
-- assert(not scopes[1])
|
||||||
|
|
||||||
|
-- local n = scopes.n + 1
|
||||||
|
-- scopes.n = n
|
||||||
|
-- scopes[n] = node
|
||||||
|
-- end
|
||||||
|
|
||||||
|
local function open_scope<T>(node: Node<T>)
|
||||||
local n = scopes.n + 1
|
local n = scopes.n + 1
|
||||||
scopes.n = n
|
scopes.n = n
|
||||||
scopes[n] = scope
|
scopes[n] = node
|
||||||
|
|
||||||
|
-- local parent = scopes[n-1]
|
||||||
|
-- assert(parent)
|
||||||
|
-- add_child(parent, node)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function close_scope()
|
local function close_scope()
|
||||||
|
|
@ -73,124 +79,74 @@ local function close_scope()
|
||||||
scopes[n] = nil
|
scopes[n] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function add_cleanup(scope: Scope, cleanup: () -> ())
|
local function add_cleanup<T>(node: Node<T>, cleanup: () -> ())
|
||||||
if scope.cleanups then
|
if node.cleanups then
|
||||||
table.insert(scope.cleanups, cleanup)
|
table.insert(node.cleanups, cleanup)
|
||||||
else
|
else
|
||||||
scope.cleanups = { cleanup }
|
node.cleanups = { cleanup }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function run_cleanups(scope: Scope)
|
local function run_cleanups<T>(node: Node<T>)
|
||||||
if scope.cleanups then
|
if node.cleanups then
|
||||||
for _, fn in next, scope.cleanups do
|
for _, fn in next, node.cleanups do
|
||||||
fn()
|
fn()
|
||||||
end
|
end
|
||||||
table.clear(scope.cleanups)
|
table.clear(node.cleanups)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[
|
|
||||||
|
|
||||||
Each node side-effect is registered with a corresponding weak key.
|
|
||||||
This makes the lifetime of the side-effect tied to the key's.
|
|
||||||
The main usecase of this is to tie a side-effect to an instance, while allowing
|
|
||||||
the instance to be garbage collected even when the node still exists.
|
|
||||||
|
|
||||||
The weak key is passed as an argument to its side-effect callback.
|
|
||||||
|
|
||||||
]]
|
|
||||||
|
|
||||||
local function run_effect<T>(node: Node<T>)
|
local function run_effect<T>(node: Node<T>)
|
||||||
node.effect(node.cache)
|
node.effect(node.cache)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function add_child(parent: StartNode<any>, child: Node<any>)
|
local function destroy<T>(node: Node<T>)
|
||||||
table.insert(parent, child)
|
run_cleanups(node)
|
||||||
end
|
for _, child in ipairs(node) do
|
||||||
|
|
||||||
local function add_children(parent: Node<any>, children: { Node<any> })
|
|
||||||
for _, child in next, children do
|
|
||||||
table.insert(parent, child)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function destroy(scope: Scope)
|
|
||||||
run_cleanups(scope)
|
|
||||||
for _, child in ipairs(scope) do
|
|
||||||
destroy(child)
|
destroy(child)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- runs node effects, recalculates descendants and runs descendant effects
|
-- runs node effects, recalculates descendants and runs descendant effects
|
||||||
local function update<T>(node: StartNode<T>)
|
local function update<T>(node: StartNode<T>)
|
||||||
for _, child in ipairs(node) do
|
local children = { unpack(node) }
|
||||||
local scope = child.scope
|
for i = 1, node.n do
|
||||||
assert(scope)
|
node[i] = nil
|
||||||
open_scope(scope :: Scope)
|
end
|
||||||
run_cleanups(scope :: Scope)
|
node.n = 0
|
||||||
|
|
||||||
|
for _, child in children do
|
||||||
|
open_scope(child)
|
||||||
|
run_cleanups(child)
|
||||||
run_effect(child)
|
run_effect(child)
|
||||||
update(child)
|
|
||||||
close_scope()
|
close_scope()
|
||||||
|
update(child)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- detect what nodes were referenced in the given callback and returns them in an array
|
local function track<T>(node: Node<T>)
|
||||||
local function capture<T, U>(fn: (U?) -> T, arg: U?): ({ StartNode<unknown> }, T)
|
add_child(node, get_scope())
|
||||||
if reff then throw("recursive capture detected") end
|
|
||||||
|
|
||||||
table.clear(refs)
|
|
||||||
reff = true
|
|
||||||
|
|
||||||
local ok: boolean, result: T|string
|
|
||||||
|
|
||||||
if arg == nil then
|
|
||||||
ok, result = pcall(fn)
|
|
||||||
else
|
|
||||||
ok, result = pcall(fn, arg)
|
|
||||||
end
|
|
||||||
|
|
||||||
reff = false
|
|
||||||
|
|
||||||
if not ok then throw(EVALUATION_ERR .. result :: string) end
|
|
||||||
|
|
||||||
return refs, result :: T
|
|
||||||
end
|
|
||||||
|
|
||||||
local function capture_parents<T, U>(child: Node<T>, fn: (U?) -> T, arg: U?): T
|
|
||||||
local refs, result = capture(fn, arg)
|
|
||||||
|
|
||||||
for _, parent in next, refs do
|
|
||||||
add_child(parent, child)
|
|
||||||
end
|
|
||||||
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
local function track<T>(node: StartNode<T>)
|
|
||||||
if reff then table.insert(refs, node :: Node<any>) end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function create_scope(): Scope
|
|
||||||
return {
|
|
||||||
parent = get_scope() or false,
|
|
||||||
cleanups = false
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function create_node<T>(value: T): Node<T>
|
local function create_node<T>(value: T): Node<T>
|
||||||
return {
|
return {
|
||||||
scope = create_scope(),
|
|
||||||
cache = value,
|
cache = value,
|
||||||
effect = function() end,
|
effect = function() end,
|
||||||
|
cleanups = false :: false,
|
||||||
|
n = 0
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function get_children(node: Node<unknown>): { Node<unknown> }
|
||||||
|
return { unpack(node) }
|
||||||
|
end
|
||||||
|
|
||||||
local function create_start_node<T>(value: T): StartNode<T>
|
local function create_start_node<T>(value: T): StartNode<T>
|
||||||
return { cache = value }
|
return { cache = value, n = 0 }
|
||||||
end
|
end
|
||||||
|
|
||||||
return table.freeze {
|
return table.freeze {
|
||||||
|
open_root_scope = open_root_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,
|
||||||
|
|
@ -200,11 +156,7 @@ return table.freeze {
|
||||||
track = track,
|
track = track,
|
||||||
update = update,
|
update = update,
|
||||||
add_child = add_child,
|
add_child = add_child,
|
||||||
add_children = add_children,
|
|
||||||
capture = capture,
|
|
||||||
capture_parents = capture_parents,
|
|
||||||
create_node = create_node,
|
create_node = create_node,
|
||||||
create_start_node = create_start_node,
|
create_start_node = create_start_node,
|
||||||
create_scope = create_scope,
|
get_children = get_children
|
||||||
refs = refs
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
local testkit = require("test/testkit")
|
local testkit = require("test/testkit")
|
||||||
local TEST, CASE, CHECK, FINISH = testkit.test()
|
local TEST, CASE, CHECK, FINISH, SKIP = testkit.test()
|
||||||
|
|
||||||
|
SKIP"graph"
|
||||||
|
|
||||||
local mock = require "test/mock"
|
local mock = require "test/mock"
|
||||||
local Instance, Signal = mock.Instance, mock.Signal
|
local Instance, Signal = mock.Instance, mock.Signal
|
||||||
|
|
@ -29,40 +31,60 @@ TEST("graph", function()
|
||||||
local graph = require "src/graph"
|
local graph = require "src/graph"
|
||||||
local create_node = graph.create_node
|
local create_node = graph.create_node
|
||||||
local track = graph.track
|
local track = graph.track
|
||||||
local capture = graph.capture
|
|
||||||
local update = graph.update
|
local update = graph.update
|
||||||
local add_child = graph.add_child
|
local add_child = graph.add_child
|
||||||
|
local open_root_scope = graph.open_root_scope
|
||||||
|
local open_scope = graph.open_scope
|
||||||
|
local close_scope = graph.close_scope
|
||||||
|
local get_children = graph.get_children
|
||||||
|
|
||||||
do CASE "node creation"
|
do CASE "node creation"
|
||||||
local node = create_node(1)
|
local node = create_node(1)
|
||||||
CHECK(node.cache == 1)
|
CHECK(node.cache == 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "capture nodes"
|
do CASE "link nodes"
|
||||||
local node1 = create_node(nil)
|
local a = create_node(nil)
|
||||||
local node2 = create_node(nil)
|
local b = create_node(nil)
|
||||||
local captured = capture(function()
|
local c = create_node(nil)
|
||||||
track(node1)
|
|
||||||
track(node2)
|
open_scope(c)
|
||||||
return nil
|
|
||||||
end)
|
track(a)
|
||||||
CHECK(captured[1] == node1)
|
track(b)
|
||||||
CHECK(captured[2] == node2)
|
|
||||||
|
close_scope()
|
||||||
|
|
||||||
|
CHECK(get_children(a)[1] == c)
|
||||||
|
CHECK(get_children(b)[1] == c)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "linking nodes"
|
do CASE "rerun linked nodes"
|
||||||
local parent = create_node(1)
|
local a = create_node(nil)
|
||||||
local child = create_node(0)
|
local b = create_node(nil)
|
||||||
|
local c = create_node(nil)
|
||||||
|
|
||||||
add_child(parent, child)
|
local count = 0
|
||||||
|
|
||||||
local ran = false
|
local function effect()
|
||||||
child.effect = function()
|
track(a)
|
||||||
ran = true
|
track(b)
|
||||||
|
count += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
update(parent)
|
c.effect = effect
|
||||||
CHECK(ran)
|
|
||||||
|
open_scope(c)
|
||||||
|
|
||||||
|
effect()
|
||||||
|
|
||||||
|
close_scope()
|
||||||
|
|
||||||
|
CHECK(count == 1)
|
||||||
|
update(a)
|
||||||
|
CHECK(count == 2)
|
||||||
|
update(b)
|
||||||
|
CHECK(count == 3)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- todo: further tests
|
-- todo: further tests
|
||||||
|
|
@ -72,6 +94,10 @@ TEST("graph", function()
|
||||||
gc()
|
gc()
|
||||||
CHECK(not wref[1])
|
CHECK(not wref[1])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do CASE "test"
|
||||||
|
local x = 1
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("source()", wrap_root(function()
|
TEST("source()", wrap_root(function()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue