mirror of
https://github.com/centau/vide.git
synced 2026-08-21 07:21:36 +00:00
This commit is contained in:
parent
dafdc0739b
commit
470d2b5407
10 changed files with 242 additions and 450 deletions
140
src/maps.luau
140
src/maps.luau
|
|
@ -5,18 +5,19 @@ if not game then script = require "test/relative-string" end
|
|||
local throw = require(script.Parent.throw)
|
||||
local flags = require(script.Parent.flags)
|
||||
local graph = require(script.Parent.graph)
|
||||
type Scope = graph.Scope
|
||||
type Node<T> = graph.Node<T>
|
||||
local create = graph.create
|
||||
local set = graph.set
|
||||
local create_node = graph.create_node
|
||||
local create_scope = graph.create_scope
|
||||
local track = graph.track
|
||||
local update = graph.update
|
||||
local capture = graph.capture
|
||||
local run_cleanups = graph.run_cleanups
|
||||
local set_child = graph.set_child
|
||||
local open_new_scope = graph.open_new_scope
|
||||
local capture_parents = graph.capture_parents
|
||||
local add_child = graph.add_child
|
||||
local get_scope = graph.get_scope
|
||||
local open_scope = graph.open_scope
|
||||
local close_scope = graph.close_scope
|
||||
local link = graph.link
|
||||
local destroy_tree = graph.destroy_tree
|
||||
local destroy = graph.destroy
|
||||
|
||||
type Map<K, V> = { [K]: V }
|
||||
|
||||
|
|
@ -31,15 +32,19 @@ end
|
|||
|
||||
-- todo: optimize output array
|
||||
local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI, K) -> VO): () -> { VO }
|
||||
assert(get_scope())
|
||||
|
||||
local root = create_scope()
|
||||
|
||||
local input_cache = {} :: Map<K, VI>
|
||||
local output_cache = {} :: Map<K, VO>
|
||||
local input_nodes = {} :: Map<K, Node<VI>>
|
||||
local remove_queue = {} :: { K }
|
||||
local output_array = {} :: { VO }
|
||||
|
||||
local scopes = {} :: Map<K, Node<unknown>>
|
||||
local scopes = {} :: Map<K, Scope>
|
||||
|
||||
local function recompute(data)
|
||||
local function update_children(data)
|
||||
-- queue removed values
|
||||
for i in next, input_cache do
|
||||
if data[i] == nil then
|
||||
|
|
@ -49,8 +54,7 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
|
|||
|
||||
-- remove queued values
|
||||
for _, i in next, remove_queue do
|
||||
destroy_tree(scopes[i])
|
||||
|
||||
destroy(scopes[i])
|
||||
|
||||
input_cache[i] = nil
|
||||
output_cache[i] = nil
|
||||
|
|
@ -60,29 +64,38 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
|
|||
|
||||
table.clear(remove_queue)
|
||||
|
||||
open_scope(root)
|
||||
|
||||
-- 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
|
||||
local scope = create(false)
|
||||
local scope = create_scope()
|
||||
scopes[i] = scope
|
||||
|
||||
open_new_scope(scope)
|
||||
open_scope(scope)
|
||||
|
||||
local node, get_value = create(v)
|
||||
local node = create_node(v)
|
||||
input_nodes[i] = node
|
||||
output_cache[i] = transform(get_value, i)
|
||||
input_cache[i] = v
|
||||
output_cache[i] = transform(function()
|
||||
track(node)
|
||||
return node.cache
|
||||
end, i)
|
||||
|
||||
close_scope()
|
||||
else
|
||||
set(input_nodes[i], v)
|
||||
input_nodes[i].cache = v
|
||||
update(input_nodes[i])
|
||||
input_cache[i] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
close_scope()
|
||||
|
||||
-- output elements
|
||||
table.clear(output_array)
|
||||
for _, v in next, output_cache do
|
||||
|
|
@ -93,34 +106,27 @@ 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 scope = create(false)
|
||||
|
||||
local function derive()
|
||||
local _ = scope
|
||||
return recompute(input())
|
||||
local output = create_node(false :: any)
|
||||
output.effect = function()
|
||||
update_children(input())
|
||||
end
|
||||
|
||||
local nodes, value = capture(input)
|
||||
local value = capture_parents(output, input)
|
||||
|
||||
for _, node in next, nodes do
|
||||
link(node, output, derive)
|
||||
output.cache = update_children(value)
|
||||
|
||||
return function()
|
||||
track(output)
|
||||
return output.cache
|
||||
end
|
||||
|
||||
output.cache = recompute(value)
|
||||
|
||||
|
||||
|
||||
local scope_parent = get_scope()
|
||||
|
||||
set_child(scope_parent, scope)
|
||||
|
||||
return read_output_value
|
||||
end
|
||||
|
||||
-- todo: optimize output array
|
||||
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO): () -> { VO }
|
||||
assert(get_scope())
|
||||
|
||||
local root = create_scope()
|
||||
|
||||
local cur_input_cache_up = {} :: Map<VI, K>
|
||||
local new_input_cache_up = {} :: Map<VI, K>
|
||||
|
||||
|
|
@ -128,9 +134,9 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
|
|||
local input_nodes = {} :: Map<VI, Node<K>>
|
||||
local output_array = {} :: { VO }
|
||||
|
||||
local cleanups = {} :: Map<VI, { () -> () }>
|
||||
local scopes = {} :: Map<VI, Scope>
|
||||
|
||||
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
|
||||
|
|
@ -142,6 +148,8 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
|
|||
cache[v] = true
|
||||
end
|
||||
end
|
||||
|
||||
open_scope(root)
|
||||
|
||||
-- process data
|
||||
for i, v in next, data do
|
||||
|
|
@ -150,30 +158,37 @@ 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)
|
||||
local scope = create_scope()
|
||||
scopes[v] = scope
|
||||
|
||||
local node, get_value = create(i)
|
||||
open_scope(scope)
|
||||
|
||||
local node = create_node(i)
|
||||
input_nodes[v] = node
|
||||
output_cache[v] = transform(v, get_value)
|
||||
output_cache[v] = transform(v, function()
|
||||
track(node)
|
||||
return node.cache
|
||||
end)
|
||||
|
||||
cleanups[v] = manual_cleanup_mode(nil)
|
||||
close_scope()
|
||||
else
|
||||
if cv ~= i then
|
||||
set(input_nodes[v], i)
|
||||
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
|
||||
|
|
@ -187,33 +202,24 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
|
|||
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())
|
||||
local output = create_node(false :: any)
|
||||
output.effect = function()
|
||||
update_children(input())
|
||||
end
|
||||
|
||||
local nodes, value = capture(input)
|
||||
local value = capture_parents(output, input)
|
||||
|
||||
for _, node in next, nodes do
|
||||
link(node, output, derive)
|
||||
output.cache = update_children(value)
|
||||
|
||||
return function()
|
||||
track(output)
|
||||
return output.cache
|
||||
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
|
||||
end)
|
||||
|
||||
return read_output_value
|
||||
end
|
||||
|
||||
return function() return indexes, values end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue