Allow untrack() in non-reactive scopes

Closes #17
This commit is contained in:
Aaron Smith 2023-09-26 14:11:33 +01:00
parent 71bbaa2092
commit d1f86a3f9e
2 changed files with 13 additions and 11 deletions

View file

@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed
- Reactive scopes created within reactive scopes are now destroyed on rerun.
- `untrack()` can be called outside of reactive scopes.
---

View file

@ -7,10 +7,8 @@ local get_scope = graph.get_scope
local function untrack<T>(source: () -> T): T
local scope = get_scope()
if not scope then
throw("cannot untrack in non-reactive scope")
end; assert(scope)
if scope then
-- sources are only tracked if the node in scope has an effect
local effect = scope.effect
scope.effect = false
@ -22,6 +20,9 @@ local function untrack<T>(source: () -> T): T
if not ok then error(result, 0) end
return result
else
return source()
end
end
return untrack