mirror of
https://github.com/centau/vide.git
synced 2026-08-20 23:01:37 +00:00
This commit is contained in:
parent
1aa74310f3
commit
676bbbcc00
15 changed files with 665 additions and 873 deletions
32
src/wrap.lua
32
src/wrap.lua
|
|
@ -2,41 +2,31 @@
|
|||
-- vide/wrap.lua
|
||||
------------------------------------------------------------------------------------------
|
||||
|
||||
if not game then script = (require :: any) "test/wrap-require" end
|
||||
if not game then script = require "test/wrap-require" end
|
||||
|
||||
local graph = require(script.Parent.graph)
|
||||
type State<T> = graph.State<T>
|
||||
type MaybeState<T> = graph.MaybeState<T>
|
||||
type Node<T> = graph.Node<T>
|
||||
local create = graph.create
|
||||
local get = graph.get
|
||||
local set = graph.set
|
||||
local wrapped = graph.wrapped
|
||||
|
||||
local throw = require(script.Parent.throw)
|
||||
local flags = require(script.Parent.flags)
|
||||
|
||||
type Setter<T> = (value: MaybeState<T> | (MaybeState<T>) -> MaybeState<T>, force: boolean?) -> T
|
||||
type State<T> = (() -> T) & ((T) -> T)
|
||||
|
||||
local function wrap<T>(value: MaybeState<T>?): (State<T>, Setter<T>)
|
||||
local state = create(if wrapped(value) then get(value :: State<T>) else value :: T)
|
||||
local function wrap<T>(value: T | () -> T): State<T>
|
||||
if type(value) == "function" then value = value() end
|
||||
|
||||
local function setter(vi: MaybeState<T> | (MaybeState<T>) -> MaybeState<T>, force: boolean?): T
|
||||
if type(vi) == "function" then
|
||||
vi = vi(get(state))
|
||||
end
|
||||
local node = create(value :: T)
|
||||
|
||||
local v = if wrapped(vi) then get(vi :: State<T>) else vi :: T
|
||||
return function(...): T
|
||||
if select("#", ...) == 0 then return get(node) end
|
||||
|
||||
if v ~= state.__cache or force then
|
||||
set(state, v)
|
||||
elseif flags.strict and type(v) == "table" then
|
||||
throw("attempt to set same table object")
|
||||
end
|
||||
local v = ... :: T
|
||||
if node.cache == v and type(v) ~= "table" then return v end
|
||||
|
||||
set(node, v)
|
||||
return v
|
||||
end
|
||||
|
||||
return state, setter
|
||||
end
|
||||
|
||||
return wrap
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue