mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
32 lines
845 B
Lua
32 lines
845 B
Lua
------------------------------------------------------------------------------------------
|
|
-- vide/wrap.lua
|
|
------------------------------------------------------------------------------------------
|
|
|
|
if not game then script = require "test/wrap-require" end
|
|
|
|
local graph = require(script.Parent.graph)
|
|
type Node<T> = graph.Node<T>
|
|
local create = graph.create
|
|
local get = graph.get
|
|
local set = graph.set
|
|
|
|
|
|
type State<T> = (() -> T) & ((T) -> T)
|
|
|
|
local function source<T>(value: T | () -> T): State<T>
|
|
if type(value) == "function" then value = value() end
|
|
|
|
local node = create(value :: T)
|
|
|
|
return function(...): T
|
|
if select("#", ...) == 0 then return get(node) end
|
|
|
|
local v = ... :: T
|
|
if node.cache == v and type(v) ~= "table" then return v end
|
|
|
|
set(node, v)
|
|
return v
|
|
end
|
|
end
|
|
|
|
return source
|