fix recursive queue flush

This commit is contained in:
alice 2024-08-15 01:51:17 +02:00 committed by centau
parent b85419088c
commit 1eab1aa598
3 changed files with 51 additions and 20 deletions

View file

@ -6,17 +6,18 @@ local graph = require(script.Parent.graph)
local function batch(setter: () -> ()) local function batch(setter: () -> ())
local already_batching = flags.batch local already_batching = flags.batch
local flush
if not already_batching then
flags.batch = true flags.batch = true
flush = graph.flush_update_queue()
end
local ok, err: string? = pcall(setter) local ok, err: string? = pcall(setter)
if not already_batching then if not already_batching then
flags.batch = false flags.batch = false
flush()
if not already_batching then
graph.flush_update_queue()
end
end end
if not ok then throw(`error occured while batching updates: {err}`) end if not ok then throw(`error occured while batching updates: {err}`) end

View file

@ -186,11 +186,12 @@ end
local _flushing = false local _flushing = false
local function flush_update_queue() local function flush_update_queue()
local n0 = update_queue.n
return function()
assert(not _flushing, "recursive queue flush occured") -- todo assert(not _flushing, "recursive queue flush occured") -- todo
_flushing = true _flushing = true
local n0 = 0
local i = n0 + 1 local i = n0 + 1
while i <= update_queue.n do while i <= update_queue.n do
local node = update_queue[i] local node = update_queue[i]
@ -207,6 +208,7 @@ local function flush_update_queue()
update_queue.n = n0 update_queue.n = n0
_flushing = false _flushing = false
end
end end
local function update_descendants<T>(root: SourceNode<T>) local function update_descendants<T>(root: SourceNode<T>)

View file

@ -1920,6 +1920,34 @@ TEST("batch()", wrap_root(function()
CHECK(b2() == 3) CHECK(b2() == 3)
CHECK(b3() == 4) CHECK(b3() == 4)
end end
do CASE "recursive queue flush"
local a0 = source(0)
local a1 = source(1)
derive(function()
a0(a1() + 1)
end)
derive(function()
print("update")
a0()
batch(function()
end)
return 1
end)
a1(2)
-- if it didnt error, all is fine!
CHECK(true)
end
end)) end))
TEST("read()", wrap_root(function() TEST("read()", wrap_root(function()