Merge reactive scope refactor

This commit is contained in:
aaron 2023-09-15 12:54:42 +01:00
parent 0e439f084f
commit efc4798ddb
48 changed files with 2750 additions and 1949 deletions

View file

@ -1,20 +1,27 @@
if not game then script = require "test/relative-string" end
local throw = require(script.Parent.throw)
local graph = require(script.Parent.graph)
type Node<T> = graph.Node<T>
local refs = graph.refs
local get_scope = graph.get_scope
local function untrack<T>(source: () -> T): T
local initial = #refs
local scope = get_scope()
if not scope then
throw("cannot untrack in non-reactive scope")
end; assert(scope)
local value = source()
-- sources are only tracked if the node in scope has an effect
local effect = scope.effect
scope.effect = false
-- remove any references made since `untrack()` was called
for i = initial, #refs do
refs[i] = nil
end
local ok, result = pcall(source)
return value
scope.effect = effect :: () -> ()
if not ok then error(result, 0) end
return result
end
return untrack