Echo initial value if given a source

This commit is contained in:
Richard 2023-09-18 19:44:48 -07:00 committed by Aaron
parent 1a1b4d9f39
commit 6b465280eb
2 changed files with 11 additions and 1 deletions

View file

@ -9,6 +9,10 @@ local update = graph.update
export type Source<T> = (() -> T) & ((T) -> T)
local function source<T>(initial_value: T): Source<T>
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<T>(initial_value: T): Source<T>
end
end
return source :: (<T>(initial_value: T) -> Source<T>) & (<T>() -> Source<T>)
return source :: (<T>(source: Source<T>) -> Source<T>) & (<T>(initial_value: T) -> Source<T>) & (<T>() -> Source<T>)

View file

@ -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)