Implement parent()

This commit is contained in:
aaron 2024-11-05 17:47:09 +00:00
parent baf308ccf3
commit c129153cd0
4 changed files with 118 additions and 1 deletions

26
src/_parent.luau Normal file
View file

@ -0,0 +1,26 @@
if not game then script = require "test/relative-string" end
local graph = require(script.Parent.graph)
type Node<T> = graph.Node<T>
export type Source<T> = (() -> T) & ((value: T) -> T)
local function parent<T>(fn: () -> T): T
local node = graph.get_scope()
if not node then error("") end
local parent_node = node.owner
if not parent_node then error("") end
graph.push_scope(parent_node :: Node<unknown>)
local ok, result = pcall(fn)
graph.pop_scope()
if not ok then error(result, 0) end
return result
end
return parent

View file

@ -9,7 +9,7 @@ local push_scope = graph.push_scope
local pop_scope = graph.pop_scope
local set_context = graph.set_context
export type Context<T> = (() -> T) & (<U>(T, () -> U) -> U)
export type Context<T> = (() -> T) & (<U>(T, () -> U) -> U) & ((T, () -> ()) -> ())
local nil_symbol = newproxy()
local count = 0

View file

@ -15,6 +15,7 @@ local effect = require(script.effect)
local derive = require(script.derive)
local cleanup = require(script.cleanup)
local untrack = require(script.untrack)
local parent = require(script._parent)
local read = require(script.read)
local batch = require(script.batch)
local context = require(script.context)
@ -68,6 +69,7 @@ local vide = {
-- util
cleanup = cleanup,
untrack = untrack,
parent = parent,
read = read,
batch = batch,
context = context,