From a425c6b8be38df892f29c295c60f23d468d5cba5 Mon Sep 17 00:00:00 2001 From: aaron <83140718+centau@users.noreply.github.com> Date: Wed, 13 Sep 2023 21:14:55 +0100 Subject: [PATCH] --- src/maps.luau | 22 ++++++------------- test/benchmark.luau | 2 +- test/tests.luau | 51 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/maps.luau b/src/maps.luau index db93d04..9b1560a 100644 --- a/src/maps.luau +++ b/src/maps.luau @@ -109,16 +109,12 @@ local function indexes(input: () -> Map, transform: (() -> VI, return output_array end - local output = create_node(false :: any) - output.effect = function() + local output = create_node(false :: any, function() return update_children(input()) - end + end) - open_scope(output) + evaluate_node(output) - output.cache = update_children(input()) - - close_scope() return function() track(output) @@ -126,7 +122,6 @@ local function indexes(input: () -> Map, transform: (() -> VI, end end --- todo: optimize output array local function values(input: () -> Map, transform: (VI, () -> K) -> VO): () -> { VO } local owner = get_scope() if not owner then @@ -211,16 +206,11 @@ local function values(input: () -> Map, transform: (VI, () -> return output_array end - local output = create_node(false :: any) - output.effect = function() + local output = create_node(false :: any, function() return update_children(input()) - end + end) - open_scope(output) - - output.cache = update_children(input()) - - close_scope() + evaluate_node(output) return function() track(output) diff --git a/test/benchmark.luau b/test/benchmark.luau index a5dec70..87fc2f5 100644 --- a/test/benchmark.luau +++ b/test/benchmark.luau @@ -73,7 +73,7 @@ BENCH("set derived value", function() local src = vide.source(1) vide.root(function() - local _derived = vide.derive(src) + local _derived = vide.derive(function() return src() end) for i = 1, START(N) do src(i) diff --git a/test/tests.luau b/test/tests.luau index 45e0ed9..4cf873e 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -399,7 +399,7 @@ TEST("derive()", wrap_root(function() local count = 0 - effect(function() c() count += 1 end) + effect(function() c(); count += 1 end) b(true) CHECK(c() == "b") @@ -453,11 +453,28 @@ TEST("derive()", wrap_root(function() gc() CHECK(wref[1]) end + + do CASE "raw derive" + local a = source(1) + local b = derive(a) + + local count = 0 + + effect(function() + b() + count += 1 + end) + + CHECK(count == 1) + a(2) + CHECK(count == 2) + end end)) TEST("effect()", wrap_root(function() local source = vide.source local effect = vide.effect + local derive = vide.derive do CASE "rerun on source change" local a = source(1) @@ -476,6 +493,38 @@ TEST("effect()", wrap_root(function() b(2) CHECK(count == 3) end + + do CASE "rerun on derived source change" + local num = source(0) + + local text = derive(function() return tostring(num()) end) + + local count = 0 + effect(function() + text() + count += 1 + end) + + num(1) + + CHECK(count == 2) + end + + do CASE "cache" + local num = source(0) + + local count + + effect(function(x: number) + num() + count = x + 1 + return x + 1 + end, 0) + + num(1) + + CHECK(count == 2) + end end)) TEST("cleanup()", wrap_root(function()