mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Merge reactive scope refactor
This commit is contained in:
parent
0e439f084f
commit
efc4798ddb
48 changed files with 2750 additions and 1949 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue