mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
This commit is contained in:
parent
c0c3d598b0
commit
e03082c941
3 changed files with 200 additions and 7 deletions
|
|
@ -10,7 +10,7 @@ local source = require(script.source)
|
||||||
local watch = require(script.watch)
|
local watch = require(script.watch)
|
||||||
local cleanup, clean_garbage = require(script.cleanup)()
|
local cleanup, clean_garbage = require(script.cleanup)()
|
||||||
local derive = require(script.derive)
|
local derive = require(script.derive)
|
||||||
local map = require(script.map)
|
local indexes, values = require(script.maps)()
|
||||||
local spring, update_springs = require(script.spring)()
|
local spring, update_springs = require(script.spring)()
|
||||||
local action = require(script.action)()
|
local action = require(script.action)()
|
||||||
|
|
||||||
|
|
@ -23,7 +23,8 @@ local vide = {
|
||||||
watch = watch,
|
watch = watch,
|
||||||
cleanup = cleanup,
|
cleanup = cleanup,
|
||||||
derive = derive,
|
derive = derive,
|
||||||
map = map,
|
indexes = indexes,
|
||||||
|
values = values,
|
||||||
|
|
||||||
-- animations
|
-- animations
|
||||||
spring = spring,
|
spring = spring,
|
||||||
|
|
|
||||||
136
src/maps.luau
Normal file
136
src/maps.luau
Normal file
|
|
@ -0,0 +1,136 @@
|
||||||
|
if not game then script = (require :: any) "test/wrap-require" end
|
||||||
|
|
||||||
|
local graph = require(script.Parent.graph)
|
||||||
|
type Node<T> = graph.Node<T>
|
||||||
|
local create = graph.create
|
||||||
|
local set = graph.set
|
||||||
|
local capture = graph.capture
|
||||||
|
local link = graph.link
|
||||||
|
|
||||||
|
type Map<K, V> = { [K]: V }
|
||||||
|
|
||||||
|
-- todo: optimize, double buffering?
|
||||||
|
|
||||||
|
local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI, K) -> VO): () -> { VO }
|
||||||
|
local input_cache = {} :: Map<K, VI>
|
||||||
|
local output_cache = {} :: Map<K, VO>
|
||||||
|
local input_nodes = {} :: Map<K, Node<VI>>
|
||||||
|
local remove_queue = {} :: { K }
|
||||||
|
|
||||||
|
local function recompute(data)
|
||||||
|
-- queue removed values
|
||||||
|
for k in next, input_cache do
|
||||||
|
if data[k] == nil then
|
||||||
|
table.insert(remove_queue, k)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- remove queued values
|
||||||
|
for _, k in next, remove_queue do
|
||||||
|
input_cache[k] = nil
|
||||||
|
output_cache[k] = nil
|
||||||
|
input_nodes[k] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- process new or changed values
|
||||||
|
for k, v in next, data do
|
||||||
|
if input_cache[k] == nil then
|
||||||
|
local node, get_value = create(v)
|
||||||
|
input_nodes[k] = node
|
||||||
|
output_cache[k] = transform(get_value, k)
|
||||||
|
elseif input_cache[k] ~= v then
|
||||||
|
set(input_nodes[k], v)
|
||||||
|
end
|
||||||
|
input_cache[k] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
local output = {}
|
||||||
|
|
||||||
|
for _, v in next, output_cache do
|
||||||
|
table.insert(output, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
return output
|
||||||
|
end
|
||||||
|
|
||||||
|
local function derive()
|
||||||
|
return recompute(input())
|
||||||
|
end
|
||||||
|
|
||||||
|
local output, output_get = create(nil :: any)
|
||||||
|
|
||||||
|
local nodes, value = capture(input)
|
||||||
|
|
||||||
|
for _, node in next, nodes do
|
||||||
|
link(node, output, derive)
|
||||||
|
end
|
||||||
|
|
||||||
|
output.cache = recompute(value)
|
||||||
|
|
||||||
|
return output_get
|
||||||
|
end
|
||||||
|
|
||||||
|
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO): () -> { VO }
|
||||||
|
local input_cache = {} :: Map<VI, K>
|
||||||
|
local output_cache = {} :: Map<VI, VO>
|
||||||
|
local input_nodes = {} :: Map<VI, Node<K>>
|
||||||
|
local remove_queue = {} :: { VI }
|
||||||
|
|
||||||
|
local function recompute(data: Map<K, VI>)
|
||||||
|
local inverted_data = {}
|
||||||
|
|
||||||
|
-- process new or changed values
|
||||||
|
for i, v in next, data do
|
||||||
|
if input_cache[v] == nil then
|
||||||
|
local node, get_value = create(i)
|
||||||
|
input_nodes[v] = node
|
||||||
|
input_cache[v] = i
|
||||||
|
output_cache[v] = transform(v, get_value)
|
||||||
|
elseif input_cache[v] ~= i then
|
||||||
|
set(input_nodes[v], i)
|
||||||
|
end
|
||||||
|
|
||||||
|
inverted_data[v] = i
|
||||||
|
end
|
||||||
|
|
||||||
|
-- queue removed values
|
||||||
|
for v, k in next, input_cache do
|
||||||
|
if inverted_data[v] == nil then
|
||||||
|
table.insert(remove_queue, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- remove queued values
|
||||||
|
for _, k in next, remove_queue do
|
||||||
|
input_cache[k] = nil
|
||||||
|
output_cache[k] = nil
|
||||||
|
input_nodes[k] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local output = {}
|
||||||
|
|
||||||
|
for _, v in next, output_cache do
|
||||||
|
table.insert(output, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
return output
|
||||||
|
end
|
||||||
|
|
||||||
|
local function derive()
|
||||||
|
return recompute(input())
|
||||||
|
end
|
||||||
|
|
||||||
|
local output, output_get = create(nil :: any)
|
||||||
|
|
||||||
|
local nodes, value = capture(input)
|
||||||
|
|
||||||
|
for _, node in next, nodes do
|
||||||
|
link(node, output, derive)
|
||||||
|
end
|
||||||
|
|
||||||
|
output.cache = recompute(value)
|
||||||
|
|
||||||
|
return output_get
|
||||||
|
end
|
||||||
|
|
||||||
|
return function() return indexes, values end
|
||||||
|
|
@ -874,14 +874,16 @@ TEST("create()", function()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("map()", function()
|
-- todo: more comprehensive tests for maps
|
||||||
|
|
||||||
|
TEST("indexes()", function()
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local map = vide.map
|
local indexes = vide.indexes
|
||||||
|
|
||||||
do CASE "Use state"
|
do CASE "Use state"
|
||||||
local input = source { 1, 2, 3 }
|
local input = source { 1, 2, 3 }
|
||||||
|
|
||||||
local output = map(input, function(v, k)
|
local output = indexes(input, function(v, k)
|
||||||
return tostring(v())
|
return tostring(v())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
@ -895,7 +897,7 @@ TEST("map()", function()
|
||||||
|
|
||||||
local runcount = table.create(3, 0)
|
local runcount = table.create(3, 0)
|
||||||
|
|
||||||
local output = map(input, function(v, i)
|
local output = indexes(input, function(v, i)
|
||||||
runcount[i] += 1
|
runcount[i] += 1
|
||||||
return v
|
return v
|
||||||
end)
|
end)
|
||||||
|
|
@ -914,7 +916,7 @@ TEST("map()", function()
|
||||||
do CASE "Removal reflected"
|
do CASE "Removal reflected"
|
||||||
local input = source { 1, 2, 3 }
|
local input = source { 1, 2, 3 }
|
||||||
|
|
||||||
local output = map(input, function(v, i)
|
local output = indexes(input, function(v, i)
|
||||||
return v
|
return v
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
@ -1011,6 +1013,60 @@ TEST("map()", function()
|
||||||
end]]
|
end]]
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
TEST("values()", function()
|
||||||
|
local source = vide.source
|
||||||
|
local values = vide.values
|
||||||
|
|
||||||
|
do CASE "Use state"
|
||||||
|
local input = source { 1, 2, 3 }
|
||||||
|
|
||||||
|
local output = values(input, function(v, k)
|
||||||
|
return tostring(v)
|
||||||
|
end)
|
||||||
|
|
||||||
|
CHECK("" .. input()[1] == output()[1])
|
||||||
|
CHECK("" .. input()[2] == output()[2])
|
||||||
|
CHECK("" .. input()[3] == output()[3])
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "Cache result"
|
||||||
|
local input = source { 1, 2, 3 }
|
||||||
|
|
||||||
|
local runcount = table.create(3, 0)
|
||||||
|
|
||||||
|
local output = values(input, function(v, i)
|
||||||
|
runcount[v] += 1
|
||||||
|
return i
|
||||||
|
end)
|
||||||
|
|
||||||
|
input { 1, 3, 2 }
|
||||||
|
|
||||||
|
CHECK(output()[1]() == 1)
|
||||||
|
CHECK(output()[2]() == 3)
|
||||||
|
CHECK(output()[3]() == 2)
|
||||||
|
|
||||||
|
CHECK(runcount[1] == 1)
|
||||||
|
CHECK(runcount[2] == 1)
|
||||||
|
CHECK(runcount[3] == 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "Removal reflected"
|
||||||
|
local input = source { 1, 2, 3 }
|
||||||
|
|
||||||
|
local output = values(input, function(v, i)
|
||||||
|
return v
|
||||||
|
end)
|
||||||
|
|
||||||
|
input { 1, 2 }
|
||||||
|
|
||||||
|
local t = output()
|
||||||
|
|
||||||
|
CHECK(t[1] == 1)
|
||||||
|
CHECK(t[2] == 2)
|
||||||
|
CHECK(t[3] == nil)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
TEST("spring()", function()
|
TEST("spring()", function()
|
||||||
local create = vide.create
|
local create = vide.create
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue