From 109cca45f306d7b4786b1a14fb1b37c0c1f2c791 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Sep 2023 19:44:48 -0700 Subject: [PATCH] Echo initial value if given a source --- src/source.luau | 6 +++++- test/tests.luau | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/source.luau b/src/source.luau index f1307fc..87b144e 100644 --- a/src/source.luau +++ b/src/source.luau @@ -9,6 +9,10 @@ local update = graph.update export type Source = (() -> T) & ((T) -> T) local function source(initial_value: T): Source + if type(initial_value) == "function" then + return initial_value + end + local node = create_start_node(initial_value) return function(...): T @@ -28,4 +32,4 @@ local function source(initial_value: T): Source end end -return source :: ((initial_value: T) -> Source) & (() -> Source) +return source :: ((source: Source) -> Source) & ((initial_value: T) -> Source) & (() -> Source) diff --git a/test/tests.luau b/test/tests.luau index b0e7e67..4e79fff 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -359,6 +359,12 @@ TEST("source()", wrap_root(function() CHECK(src() == 2) end + do CASE "echo if value is a source" + local a = source(1) + local b = source(a) + CHECK(a == b) + end + do CASE "does not update if same value" local src = source(1)