mirror of
https://github.com/centau/vide.git
synced 2026-08-20 23:01: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
211
src/maps.luau
211
src/maps.luau
|
|
@ -1,16 +1,20 @@
|
|||
if not game then script = require "test/relative-string" end
|
||||
|
||||
-- todo: more testing needed regarding `cleanup()` usage
|
||||
|
||||
local throw = require(script.Parent.throw)
|
||||
local flags = require(script.Parent.flags)
|
||||
local graph = require(script.Parent.graph)
|
||||
local _, _, manual_cleanup_mode, cleanup_ref = require(script.Parent.cleanup)()
|
||||
type Node<T> = graph.Node<T>
|
||||
local create = graph.create
|
||||
local set = graph.set
|
||||
local capture = graph.capture
|
||||
local link = graph.link
|
||||
type StartNode<T> = graph.StartNode<T>
|
||||
local create_node = graph.create_node
|
||||
local create_start_node = graph.create_start_node
|
||||
local set_owner = graph.set_owner
|
||||
local track = graph.track
|
||||
local update = graph.update
|
||||
local get_scope = graph.get_scope
|
||||
local open_scope = graph.open_scope
|
||||
local close_scope = graph.close_scope
|
||||
local evaluate_node = graph.evaluate_node
|
||||
local destroy = graph.destroy
|
||||
|
||||
type Map<K, V> = { [K]: V }
|
||||
|
||||
|
|
@ -23,17 +27,22 @@ local function check_primitives(t: {})
|
|||
end
|
||||
end
|
||||
|
||||
-- todo: optimize output array
|
||||
local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI, K) -> VO): () -> { VO }
|
||||
local owner = get_scope()
|
||||
if not owner then
|
||||
throw("cannot derive in non-reactive scope")
|
||||
end; assert(owner)
|
||||
|
||||
local subowner = create_node(false, false)
|
||||
set_owner(subowner, owner)
|
||||
|
||||
local input_cache = {} :: Map<K, VI>
|
||||
local output_cache = {} :: Map<K, VO>
|
||||
local input_nodes = {} :: Map<K, Node<VI>>
|
||||
local input_nodes = {} :: Map<K, StartNode<VI>>
|
||||
local remove_queue = {} :: { K }
|
||||
local output_array = {} :: { VO }
|
||||
local scopes = {} :: Map<K, Node<unknown>>
|
||||
|
||||
local cleanups = {} :: Map<K, { () -> () }>
|
||||
|
||||
local function recompute(data)
|
||||
local function update_children(data)
|
||||
-- queue removed values
|
||||
for i in next, input_cache do
|
||||
if data[i] == nil then
|
||||
|
|
@ -43,41 +52,58 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
|
|||
|
||||
-- remove queued values
|
||||
for _, i in next, remove_queue do
|
||||
for _, callback in next, cleanups[i] do
|
||||
callback() -- todo: pcall
|
||||
end
|
||||
destroy(scopes[i])
|
||||
|
||||
input_cache[i] = nil
|
||||
output_cache[i] = nil
|
||||
input_nodes[i] = nil
|
||||
cleanups[i] = nil
|
||||
scopes[i] = nil
|
||||
end
|
||||
|
||||
table.clear(remove_queue)
|
||||
|
||||
open_scope(subowner)
|
||||
|
||||
-- process new or changed values
|
||||
for i, v in next, data do
|
||||
local cv = input_cache[i]
|
||||
|
||||
if cv ~= v then
|
||||
if cv == nil then
|
||||
manual_cleanup_mode(transform)
|
||||
if cv == nil then -- create new scope and run transform
|
||||
local scope = create_node(false, false)
|
||||
scopes[i] = scope :: Node<any>
|
||||
|
||||
local node, get_value = create(v)
|
||||
local node = create_start_node(v)
|
||||
|
||||
set_owner(scope, subowner)
|
||||
open_scope(scope)
|
||||
|
||||
local ok, result = pcall(transform, function()
|
||||
track(node)
|
||||
return node.cache
|
||||
end, i)
|
||||
|
||||
close_scope()
|
||||
|
||||
if not ok then
|
||||
close_scope() -- subowner scope
|
||||
error(result, 0)
|
||||
end
|
||||
|
||||
input_nodes[i] = node
|
||||
output_cache[i] = transform(get_value, i)
|
||||
input_cache[i] = v
|
||||
|
||||
cleanups[i] = manual_cleanup_mode(nil)
|
||||
else
|
||||
set(input_nodes[i], v)
|
||||
input_cache[i] = v
|
||||
output_cache[i] = result
|
||||
else -- update source
|
||||
input_nodes[i].cache = v
|
||||
update(input_nodes[i])
|
||||
end
|
||||
|
||||
input_cache[i] = v
|
||||
end
|
||||
end
|
||||
|
||||
-- output elements
|
||||
table.clear(output_array)
|
||||
close_scope()
|
||||
|
||||
local output_array = table.create(#scopes)
|
||||
for _, v in next, output_cache do
|
||||
table.insert(output_array, v)
|
||||
end
|
||||
|
|
@ -86,43 +112,34 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
|
|||
return output_array
|
||||
end
|
||||
|
||||
local output, read_output_value = create(nil :: any)
|
||||
|
||||
local function derive()
|
||||
return recompute(input())
|
||||
end
|
||||
|
||||
local nodes, value = capture(input)
|
||||
|
||||
for _, node in next, nodes do
|
||||
link(node, output, derive)
|
||||
end
|
||||
|
||||
output.cache = recompute(value)
|
||||
|
||||
cleanup_ref(tostring(output), output, function()
|
||||
for _, callbacks in next, cleanups do
|
||||
for _, callback in next, callbacks do
|
||||
callback() -- todo: pcall
|
||||
end
|
||||
end
|
||||
local node = create_node(false :: any, function()
|
||||
return update_children(input())
|
||||
end)
|
||||
|
||||
return read_output_value
|
||||
evaluate_node(node)
|
||||
|
||||
return function()
|
||||
track(node)
|
||||
return node.cache
|
||||
end
|
||||
end
|
||||
|
||||
-- todo: optimize output array
|
||||
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO): () -> { VO }
|
||||
local owner = get_scope()
|
||||
if not owner then
|
||||
throw("cannot derive in non-reactive scope")
|
||||
end; assert(owner)
|
||||
|
||||
local subowner = create_node(false, false)
|
||||
set_owner(subowner, owner)
|
||||
|
||||
local cur_input_cache_up = {} :: Map<VI, K>
|
||||
local new_input_cache_up = {} :: Map<VI, K>
|
||||
|
||||
local output_cache = {} :: Map<VI, VO>
|
||||
local input_nodes = {} :: Map<VI, Node<K>>
|
||||
local output_array = {} :: { VO }
|
||||
local input_nodes = {} :: Map<VI, StartNode<K>>
|
||||
local scopes = {} :: Map<VI, Node<unknown>>
|
||||
|
||||
local cleanups = {} :: Map<VI, { () -> () }>
|
||||
|
||||
local function recompute(data: Map<K, VI>)
|
||||
local function update_children(data: Map<K, VI>)
|
||||
local cur_input_cache, new_input_cache = cur_input_cache_up, new_input_cache_up
|
||||
|
||||
if flags.strict then
|
||||
|
|
@ -134,6 +151,8 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
|
|||
cache[v] = true
|
||||
end
|
||||
end
|
||||
|
||||
open_scope(subowner)
|
||||
|
||||
-- process data
|
||||
for i, v in next, data do
|
||||
|
|
@ -141,71 +160,73 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
|
|||
|
||||
local cv = cur_input_cache[v]
|
||||
|
||||
if cv == nil then
|
||||
manual_cleanup_mode(transform)
|
||||
if cv == nil then -- create new scope and run transform
|
||||
local scope = create_node(false, false)
|
||||
scopes[v] = scope :: Node<any>
|
||||
|
||||
local node, get_value = create(i)
|
||||
input_nodes[v] = node
|
||||
output_cache[v] = transform(v, get_value)
|
||||
local node = create_start_node(i)
|
||||
|
||||
set_owner(scope, subowner)
|
||||
open_scope(scope)
|
||||
|
||||
local ok, result = pcall(transform, v, function()
|
||||
track(node)
|
||||
return node.cache
|
||||
end)
|
||||
|
||||
close_scope()
|
||||
|
||||
cleanups[v] = manual_cleanup_mode(nil)
|
||||
else
|
||||
if cv ~= i then
|
||||
set(input_nodes[v], i)
|
||||
if not ok then
|
||||
close_scope() -- subowner scope
|
||||
error(result, 0)
|
||||
end
|
||||
|
||||
input_nodes[v] = node
|
||||
output_cache[v] = result
|
||||
else -- update source
|
||||
if cv ~= i then
|
||||
input_nodes[v].cache = i
|
||||
update(input_nodes[v])
|
||||
end
|
||||
|
||||
cur_input_cache[v] = nil
|
||||
end
|
||||
end
|
||||
|
||||
close_scope()
|
||||
|
||||
-- remove old values
|
||||
for v in next, cur_input_cache do
|
||||
for _, callback in next, cleanups[v] do
|
||||
callback() -- todo: pcall
|
||||
end
|
||||
destroy(scopes[v])
|
||||
|
||||
output_cache[v] = nil
|
||||
input_nodes[v] = nil
|
||||
cleanups[v] = nil
|
||||
scopes[v] = nil
|
||||
end
|
||||
|
||||
-- update buffer cache
|
||||
table.clear(cur_input_cache)
|
||||
cur_input_cache_up, new_input_cache_up = new_input_cache, cur_input_cache
|
||||
|
||||
-- output elements
|
||||
table.clear(output_array)
|
||||
|
||||
local output_array = table.create(#scopes)
|
||||
for _, v in next, output_cache do
|
||||
table.insert(output_array, v)
|
||||
end
|
||||
check_primitives(output_array)
|
||||
|
||||
return output_array
|
||||
end
|
||||
|
||||
local output, read_output_value = create(nil :: any)
|
||||
|
||||
local function derive()
|
||||
return recompute(input())
|
||||
end
|
||||
|
||||
local nodes, value = capture(input)
|
||||
|
||||
for _, node in next, nodes do
|
||||
link(node, output, derive)
|
||||
end
|
||||
check_primitives(output_array)
|
||||
|
||||
output.cache = recompute(value)
|
||||
|
||||
cleanup_ref(tostring(output), output, function()
|
||||
for _, callbacks in next, cleanups do
|
||||
for _, callback in next, callbacks do
|
||||
callback() -- todo: pcall
|
||||
end
|
||||
end
|
||||
local node = create_node(false :: any, function()
|
||||
return update_children(input())
|
||||
end)
|
||||
|
||||
return read_output_value
|
||||
evaluate_node(node)
|
||||
|
||||
return function()
|
||||
track(node)
|
||||
return node.cache
|
||||
end
|
||||
end
|
||||
|
||||
return function() return indexes, values end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue