From d1f86a3f9e85d6a114905aa001ffd477e15ecbb9 Mon Sep 17 00:00:00 2001 From: Aaron Smith <83140718+centau@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:11:33 +0100 Subject: [PATCH] Allow `untrack()` in non-reactive scopes Closes #17 --- CHANGELOG.md | 1 + src/untrack.luau | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c42c5ca..6d6423e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. --- diff --git a/src/untrack.luau b/src/untrack.luau index 230674d..d9ca120 100644 --- a/src/untrack.luau +++ b/src/untrack.luau @@ -7,21 +7,22 @@ local get_scope = graph.get_scope local function untrack(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 - -- sources are only tracked if the node in scope has an effect - local effect = scope.effect - scope.effect = false + local ok, result = pcall(source) - local ok, result = pcall(source) + scope.effect = effect :: () -> () - scope.effect = effect :: () -> () + if not ok then error(result, 0) end - if not ok then error(result, 0) end - - return result + return result + else + return source() + end end return untrack