From e35887f7d14ce836b02072654416adf948ef6f86 Mon Sep 17 00:00:00 2001 From: aaron <83140718+centau@users.noreply.github.com> Date: Mon, 11 Sep 2023 00:28:53 +0100 Subject: [PATCH] --- src/untrack.luau | 12 ++++++------ test/tests.luau | 23 +++++++++++++---------- todo.md | 15 ++++----------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/untrack.luau b/src/untrack.luau index d56b606..0dbde8a 100644 --- a/src/untrack.luau +++ b/src/untrack.luau @@ -4,20 +4,20 @@ local create = require(script.Parent.create) local graph = require(script.Parent.graph) type Node = graph.Node local create_node = graph.create_node -local get_stack_scope = graph.get_stack_scope +local get_scope = graph.get_scope local open_scope = graph.open_scope local close_scope = graph.close_scope -local non_tracking_scope = create_node(false) - local function untrack(source: () -> T): T - local scope = get_stack_scope(1) + local scope = get_scope() + assert(scope) - open_scope(scope or non_tracking_scope) + local effect = scope.effect + scope.effect = false local v = source() - close_scope() + scope.effect = effect :: () -> () return v end diff --git a/test/tests.luau b/test/tests.luau index c67caf1..8cb0f9f 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -1062,7 +1062,7 @@ TEST("untrack()", wrap_root(function() local a = source(0) local b = source(0) - local count = -1 + local count = 0 watch(function() count += 1 @@ -1071,10 +1071,10 @@ TEST("untrack()", wrap_root(function() end) b(1) - CHECK(count == 1) + CHECK(count == 2) a(1) - CHECK(count == 1) + CHECK(count == 2) CHECK(a() == untrack(a)) end @@ -1088,7 +1088,7 @@ TEST("untrack()", wrap_root(function() return a() + b() end - local count = -1 + local count = 0 watch(function() count += 1 @@ -1097,11 +1097,11 @@ TEST("untrack()", wrap_root(function() end) c(1) - CHECK(count == 1) + CHECK(count == 2) a(1) b(1) - CHECK(count == 1) + CHECK(count == 2) end do CASE "outer scope" @@ -1141,16 +1141,19 @@ TEST("untrack()", wrap_root(function() input(1) - -- todo - CHECK(outer_count == 2) - CHECK(inner_count == 3) + CHECK(outer_count == 1) + CHECK(inner_count == 2) CHECK(cleaned_count == 1) local output3 = output() CHECK(output2() == "1") CHECK(output3() == "1") - CHECK(output2 ~= output3) + CHECK(output2 == output3) + + destroy() + + CHECK(cleaned_count == 2) end end)) diff --git a/todo.md b/todo.md index 1586429..393b7a7 100644 --- a/todo.md +++ b/todo.md @@ -3,22 +3,15 @@ - better error reporting and stack traces in strict mode - auto-enable of strict mode depending on compiler optimizaton level - investigate if weak table iteration can be invalidated -- have derived sources/bindings track sources dynamically? - - solves case where sources are used in if-branching guarded by another - source - - significantly reduces performance -- solution to component cleanup - - rely on `Instance.Destroying` event and manual destruction when cleanup is - needed? - - expand behavior of `vide.cleanup()` to detect garbage collection of - arbitrary values, not needing manual destruction - - look into SolidJS's reactive contexts +- define behavior of `cleanup()` in `untrack()` scopes + - - solution to nested reactivity, see: SolidJS stores - SolidJS control flow components - equality checking of derived sources +- implement from solid: - Show - Switch - Dynamic - Portal -- batch source updates + - batch - optimize `indexes()` double-diffing