Skip to content

Commit

Permalink
Add check for destruction of active scope
Browse files Browse the repository at this point in the history
  • Loading branch information
centau committed Nov 13, 2024
1 parent a155240 commit 0897821
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/api/strict-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Currently, strict mode will:
3. Checks for `indexes()` and `values()` outputting primitive values.
4. Checks for `values()` input having duplicate values.
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
computations are pure, and that any cleanup is done correctly.
Expand Down
8 changes: 7 additions & 1 deletion src/graph.luau
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ local function unparent<T>(node: Node<T>)
end

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)
unparent(node)

Expand Down Expand Up @@ -296,5 +300,7 @@ return table.freeze {
flush_update_queue = flush_update_queue,
get_update_queue_length = get_update_queue_length,
set_context = set_context,
scopes = scopes
scopes = scopes,

q = update_queue
}
53 changes: 53 additions & 0 deletions test/tests.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2483,11 +2483,14 @@ end))
TEST("strict", wrap_root(function()
vide.strict = true

local root = vide.root
local show = vide.show
local create = vide.create
local source = vide.source
local derive = vide.derive
local effect = vide.effect
local indexes, values = vide.indexes, vide.values
local untrack = vide.untrack

do CASE "error on derived callback yield"
local src = source(1)
Expand Down Expand Up @@ -2627,6 +2630,56 @@ TEST("strict", wrap_root(function()

CHECK(count == 4)
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))

local ok = FINISH()
Expand Down

0 comments on commit 0897821

Please sign in to comment.