Add check for destruction of active scope

This commit is contained in:
aaron 2024-11-13 21:56:22 +00:00
parent a1552402cb
commit 0897821e1f
3 changed files with 62 additions and 2 deletions

View file

@ -19,7 +19,8 @@ Currently, strict mode will:
3. Checks for `indexes()` and `values()` outputting primitive values. 3. Checks for `indexes()` and `values()` outputting primitive values.
4. Checks for `values()` input having duplicate values. 4. Checks for `values()` input having duplicate values.
5. Checks for duplicate nested properties at same depth. 5. Checks for duplicate nested properties at same depth.
6. Better error reporting and stack traces + creation traces of property bindings. 6. Checks for destruction of an active scope.
7. Better error reporting and stack traces + creation traces of property bindings.
By rerunning reactive scopes twice each time they update, it helps ensure that By rerunning reactive scopes twice each time they update, it helps ensure that
computations are pure, and that any cleanup is done correctly. computations are pure, and that any cleanup is done correctly.

View file

@ -108,6 +108,10 @@ local function unparent<T>(node: Node<T>)
end end
local function destroy<T>(node: Node<T>) local function destroy<T>(node: Node<T>)
if flags.strict and table.find(scopes, node) then
throw("attempt to destroy an active scope")
end
flush_cleanups(node) flush_cleanups(node)
unparent(node) unparent(node)
@ -296,5 +300,7 @@ return table.freeze {
flush_update_queue = flush_update_queue, flush_update_queue = flush_update_queue,
get_update_queue_length = get_update_queue_length, get_update_queue_length = get_update_queue_length,
set_context = set_context, set_context = set_context,
scopes = scopes scopes = scopes,
q = update_queue
} }

View file

@ -2483,11 +2483,14 @@ end))
TEST("strict", wrap_root(function() TEST("strict", wrap_root(function()
vide.strict = true vide.strict = true
local root = vide.root
local show = vide.show
local create = vide.create local create = vide.create
local source = vide.source local source = vide.source
local derive = vide.derive local derive = vide.derive
local effect = vide.effect local effect = vide.effect
local indexes, values = vide.indexes, vide.values local indexes, values = vide.indexes, vide.values
local untrack = vide.untrack
do CASE "error on derived callback yield" do CASE "error on derived callback yield"
local src = source(1) local src = source(1)
@ -2627,6 +2630,56 @@ TEST("strict", wrap_root(function()
CHECK(count == 4) CHECK(count == 4)
end end
do CASE "destruction of active scope"
local src = source(false)
root(function()
show(src, function()
src(false)
return {}
end)
end)
local ok = pcall(function()
src(true)
end)
CHECK(not ok)
end
-- todo: review intended behavior here
-- do CASE "destruction of active scope in indexes"
-- local src = source {}
-- local tmp
-- root(function()
-- effect(function()
-- untrack(function()
-- tmp = indexes(src, function()
-- vide.cleanup(function() print "test" end)
-- src {}
-- print "updated"
-- vide.cleanup(function() print "test2" end)
-- print "end"
-- return {}
-- end)
-- return nil
-- end)
-- end)
-- end)
-- print "setting"
-- local ok = pcall(function()
-- src { 1 }
-- print "done"
-- end)
-- print(#tmp())
-- CHECK(not ok)
-- end
end)) end))
local ok = FINISH() local ok = FINISH()