mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Merge reactive scope refactor
This commit is contained in:
parent
0e439f084f
commit
efc4798ddb
48 changed files with 2750 additions and 1949 deletions
|
|
@ -2,23 +2,30 @@ if not game then script = require "test/relative-string" end
|
|||
|
||||
local graph = require(script.Parent.graph)
|
||||
type Node<T> = graph.Node<T>
|
||||
local create = graph.create
|
||||
local set = graph.set
|
||||
local create_start_node = graph.create_start_node
|
||||
local track = graph.track
|
||||
local update = graph.update
|
||||
|
||||
export type Source<T> = (() -> T) & ((T) -> T)
|
||||
|
||||
local function source<T>(value: T): Source<T>
|
||||
local node, read_node_value = create(value :: T)
|
||||
local function source<T>(initial_value: T): Source<T>
|
||||
local node = create_start_node(initial_value)
|
||||
|
||||
return function(...): T
|
||||
if select("#", ...) == 0 then return read_node_value() end -- check if any args were given
|
||||
if select("#", ...) == 0 then -- no args were given
|
||||
track(node)
|
||||
return node.cache
|
||||
end
|
||||
|
||||
local v = ... :: T
|
||||
if node.cache == v and (type(v) ~= "table" or table.isfrozen(v)) then return v end
|
||||
if node.cache == v and (type(v) ~= "table" or table.isfrozen(v)) then
|
||||
return v
|
||||
end
|
||||
|
||||
set(node, v)
|
||||
node.cache = v
|
||||
update(node)
|
||||
return v
|
||||
end
|
||||
end
|
||||
|
||||
return source :: (<T>(value: T) -> Source<T>) & (<T>() -> Source<T>)
|
||||
return source :: (<T>(initial_value: T) -> Source<T>) & (<T>() -> Source<T>)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue