refactor: move parameter root_update_id to a new function bump_update_id

This commit is contained in:
ernisto 2025-03-24 18:35:48 -03:00
parent 21ed76f9e0
commit 54abda629b
5 changed files with 30 additions and 12 deletions

View file

@ -58,6 +58,11 @@ local function get_scope(): Node<unknown>?
return scopes[scopes.n]
end
local function bump_update_id()
update_id += 1
return update_id
end
local function assert_stable_scope(): Node<unknown>
local scope = get_scope()
@ -246,9 +251,8 @@ local function flush_update_queue(from: number)
update_queue.n = from
end
local function update_descendants<T>(root: SourceNode<T>, is_root_update: boolean?)
local function update_descendants<T>(root: SourceNode<T>)
local n0 = update_queue.n
if is_root_update and not flags.batch then update_id += 1 end
queue_children_for_update(root)
if flags.batch then return end
@ -324,6 +328,7 @@ return table.freeze {
flush_update_queue = flush_update_queue,
get_update_queue_length = get_update_queue_length,
set_context = set_context,
bump_update_id = bump_update_id,
scopes = scopes,
q = update_queue

View file

@ -86,7 +86,8 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
output_cache[i] = result
else -- update source
input_nodes[i].cache = v
update_descendants(input_nodes[i], true)
graph.bump_update_id()
update_descendants(input_nodes[i])
end
end
end
@ -170,7 +171,8 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
else -- update source
if cv ~= i then
input_nodes[v].cache = i
update_descendants(input_nodes[v], true)
graph.bump_update_id()
update_descendants(input_nodes[v])
end
cur_input_cache[v] = nil

View file

@ -21,7 +21,8 @@ local function source<T>(initial_value: T): Source<T>
end
node.cache = v
update_descendants(node, true)
graph.bump_update_id()
update_descendants(node)
return v
end

View file

@ -285,7 +285,8 @@ local function update_spring_sources()
output.cache = vec6_to_type[typeof(data.source_value)](x0_123, x0_456)
end
update_descendants(output, true)
graph.bump_update_id()
update_descendants(output)
end
end

View file

@ -36,6 +36,7 @@ vide.strict = false
TEST("graph", function()
local create_node = graph.create_node
local push_child_to_scope = graph.push_child_to_scope
local bump_update_id = graph.bump_update_id
local update_descendants = graph.update_descendants
local push_child = graph.push_child
local get_scope = graph.get_scope
@ -98,9 +99,13 @@ TEST("graph", function()
pop_scope()
CHECK(count == 1)
update_descendants(a, true)
bump_update_id()
update_descendants(a)
CHECK(count == 2)
update_descendants(b, true)
bump_update_id()
update_descendants(b)
CHECK(count == 3)
end
@ -119,7 +124,8 @@ TEST("graph", function()
push_scope(c); push_child_to_scope(a); pop_scope()
push_scope(d); push_child_to_scope(b); push_child_to_scope(c); pop_scope()
update_descendants(a, true)
bump_update_id()
update_descendants(a)
CHECK(b_cnt == 1)
CHECK(c_cnt == 1)
@ -138,7 +144,8 @@ TEST("graph", function()
push_scope(c); assert(type(c.effect) == "function" and c.effect)(NIL); pop_scope()
update_descendants(a, true)
bump_update_id()
update_descendants(a)
CHECK(#get_children(a) == 1)
CHECK(#get_children(b) == 1)
@ -286,14 +293,16 @@ TEST("graph", function()
local a, b, c, d, e, f = node(root), node(root), node(root), node(root), node(root), node(root)
function b.effect(x)
update_descendants(d, true)
bump_update_id()
update_descendants(d)
return not x
end
push_child(a, b); push_child(a, c)
push_child(d, e); push_child(d, f)
update_descendants(a, true)
bump_update_id()
update_descendants(a)
CHECK(true)
end