This commit is contained in:
aaron 2023-07-30 14:35:46 +01:00
parent 1aa74310f3
commit 676bbbcc00
15 changed files with 665 additions and 873 deletions

View file

@ -11,9 +11,10 @@ end
local graph = require(script.Parent.graph)
type State<T> = graph.State<T>
type Node<T> = graph.Node<T>
local get = graph.get
local setEffect = graph.setEffect
local set_effect = graph.set_effect
local capture = graph.capture
local throw = require(script.Parent.throw)
local flags = require(script.Parent.flags)
@ -22,15 +23,18 @@ local hold: { Instance? } = {}
local weak: { Instance? } = setmetatable({}, { __mode = "v" }) :: any
local bindcount = 0
local srcs do
local src1 = debug.info(1, "s")
local srctrunc = string.sub(src1, 1, #src1-4)
srcs = {
src1,
srctrunc .. "applyProperties",
srctrunc .. "apply",
srctrunc .. "create",
srctrunc .. "apply"
}
end
@ -43,18 +47,22 @@ local function traceback() -- ensures trace begins outside of any vide library f
return debug.traceback("", s)
end
function setup(state: State<any>, instance: Instance, updateInstance: (Instance) -> ())
function setup(instance: Instance, deriver: () -> unknown, setter: (Instance) -> ())
if flags.strict then
local fn = updateInstance
local fn = setter
local trace = traceback()
updateInstance = function(instance)
setter = function(instance)
local ok, err: string? = pcall(fn, instance)
if not ok then warn(`error occured updating state binding:\n{err}\nset from:{trace}`) end
end
end
updateInstance(instance)
setEffect(state, updateInstance, instance)
local nodes = table.clone((capture(setter :: (Instance) -> unknown, instance)))
for _, node in next, nodes do
set_effect(node, setter, instance)
end
bindcount += 1
local key = bindcount
@ -62,7 +70,7 @@ function setup(state: State<any>, instance: Instance, updateInstance: (Instance)
weak[key] = instance
local function ref()
local _ = state -- prevent gc of state while instance exists
local _ = nodes -- prevent gc of state while instance exists
local instance = weak[key] :: Instance
hold[key] = instance.Parent and instance or nil -- prevent gc of instance while parented
end
@ -71,28 +79,28 @@ function setup(state: State<any>, instance: Instance, updateInstance: (Instance)
instance:GetPropertyChangedSignal("Parent"):Connect(ref)
end
local function bindProperty(state: State<unknown>, instance_STRONG: Instance, property: string)
setup(state, instance_STRONG, function(instance)
(instance :: any)[property] = get(state)
local function bind_property(instance: Instance, property: string, fn: () -> unknown)
setup(instance, fn, function(instance_weak: any)
instance_weak[property] = fn()
end)
end
local function bindParent(state: State<Instance?>, instance_STRONG)
instance_STRONG.Destroying:Connect(function()
instance_STRONG = nil :: any -- allow gc when destroyed
local function bind_parent(instance: Instance, fn: () -> Instance?)
instance.Destroying:Connect(function()
instance= nil :: any -- allow gc when destroyed
end)
setup(state, instance_STRONG, function(instance)
local _ = instance_STRONG -- state will strongly reference instance when parent is bound
instance.Parent = get(state)
setup(instance, fn, function(instance)
local _ = instance -- state will strongly reference instance when parent is bound
instance.Parent = fn()
end)
end
local function bindChildren(state: State<{ Instance }?>, parent_STRONG: Instance)
local function bind_children(parent: Instance, fn: () -> { Instance })
local currentChildrenSet: { [Instance]: true } = {} -- cache of all children parented before update
local newChildrenSet: { [Instance]: true } = {} -- cache of all children parented after update
setup(state, parent_STRONG, function(parent)
local newChildren = get(state) -- all (and only) children that should be parented after this update
setup(parent, fn, function(parent_weak)
local newChildren = fn() -- all (and only) children that should be parented after this update
if newChildren and type(newChildren) ~= "table" then
throw(`Cannot parent instance of type { type(newChildren) } `)
end
@ -101,7 +109,7 @@ local function bindChildren(state: State<{ Instance }?>, parent_STRONG: Instance
for _, child in next, newChildren do
newChildrenSet[child] = true -- record child set from this update
if not currentChildrenSet[child] then
child.Parent = parent -- if child wasn't already parented then parent it
child.Parent = parent_weak -- if child wasn't already parented then parent it
else
currentChildrenSet[child] = nil -- remove child from cache if it was already in cache
end
@ -117,23 +125,8 @@ local function bindChildren(state: State<{ Instance }?>, parent_STRONG: Instance
end)
end
local function bindEvent(state: State<() -> ()?>, instance_STRONG: Instance, event: RBXScriptSignal)
local current: RBXScriptConnection? = nil
setup(state, instance_STRONG, function(instance)
if current then
current:Disconnect()
current = nil
end
local listener = get(state)
if listener then
current = event:Connect(listener)
end
end)
end
return {
property = bindProperty,
parent = bindParent,
children = bindChildren,
event = bindEvent
property = bind_property,
parent = bind_parent,
children = bind_children,
}