mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
refactor!: rename derive, map
This commit is contained in:
parent
f2f7a16599
commit
b715b81fec
18 changed files with 163 additions and 133 deletions
|
|
@ -1,5 +1,5 @@
|
|||
local atom = require(script.Parent.Parent.atom)
|
||||
local derive = require(script.Parent.Parent.derive)
|
||||
local computed = require(script.Parent.Parent.computed)
|
||||
local effect = require(script.Parent.Parent.effect)
|
||||
local store = require(script.Parent.Parent.store)
|
||||
local subscribe = require(script.Parent.Parent.subscribe)
|
||||
|
|
@ -37,15 +37,15 @@ return function()
|
|||
expect(value).to.equal(1)
|
||||
end)
|
||||
|
||||
it("batches derived atoms", function()
|
||||
it("batches computed atoms", function()
|
||||
local source = atom(1)
|
||||
local updates = 0
|
||||
local computed = 0
|
||||
local double = derive(function()
|
||||
computed += 1
|
||||
local computations = 0
|
||||
local double = computed(function()
|
||||
computations += 1
|
||||
return source() * 2
|
||||
end)
|
||||
computed = 0 -- derived atoms will run once initially
|
||||
computations = 0 -- computed atoms will run once initially
|
||||
subscribe(double, function()
|
||||
updates += 1
|
||||
end)
|
||||
|
|
@ -55,7 +55,7 @@ return function()
|
|||
source(4)
|
||||
end)
|
||||
expect(updates).to.equal(1)
|
||||
expect(computed).to.equal(1)
|
||||
expect(computations).to.equal(1)
|
||||
expect(double()).to.equal(8)
|
||||
end)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
local Package = game:GetService("ReplicatedStorage").Packages.charm
|
||||
|
||||
local atom = require(Package.atom)
|
||||
local derive = require(Package.derive)
|
||||
local computed = require(Package.computed)
|
||||
|
||||
local function parameterGenerator()
|
||||
return atom({ count = 0 })
|
||||
|
|
@ -17,8 +17,8 @@ local benchmarks = {
|
|||
end
|
||||
end,
|
||||
|
||||
derived = function(_, source: any)
|
||||
derive(function()
|
||||
computed = function(_, source: any)
|
||||
computed(function()
|
||||
return { count = 2 * source().count }
|
||||
end)
|
||||
for _ = 1, 500 do
|
||||
|
|
@ -26,11 +26,11 @@ local benchmarks = {
|
|||
end
|
||||
end,
|
||||
|
||||
derived_nested = function(_, source: any)
|
||||
local double = derive(function()
|
||||
computed_nested = function(_, source: any)
|
||||
local double = computed(function()
|
||||
return { count = 2 * source().count }
|
||||
end)
|
||||
derive(function()
|
||||
computed(function()
|
||||
return { count = 2 * double().count }
|
||||
end)
|
||||
for _ = 1, 500 do
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
local atom = require(script.Parent.Parent.atom)
|
||||
local derive = require(script.Parent.Parent.derive)
|
||||
local computed = require(script.Parent.Parent.computed)
|
||||
local store = require(script.Parent.Parent.store)
|
||||
local collect = require(script.Parent.Parent.utils.collect)
|
||||
local count = require(script.Parent.Parent.utils.count)
|
||||
|
|
@ -9,7 +9,7 @@ return function()
|
|||
local calls = 0
|
||||
local a = atom(1)
|
||||
local b = atom(2)
|
||||
local double = derive(function()
|
||||
local double = computed(function()
|
||||
calls += 1
|
||||
return a() * b()
|
||||
end)
|
||||
|
|
@ -26,10 +26,10 @@ return function()
|
|||
|
||||
it("can be nested", function()
|
||||
local source = atom(1)
|
||||
local double = derive(function()
|
||||
local double = computed(function()
|
||||
return source() * 2
|
||||
end)
|
||||
local quadruple = derive(function()
|
||||
local quadruple = computed(function()
|
||||
return double() * 2
|
||||
end)
|
||||
expect(quadruple()).to.equal(4)
|
||||
|
|
@ -37,34 +37,35 @@ return function()
|
|||
expect(quadruple()).to.equal(8)
|
||||
end)
|
||||
|
||||
it("collects derived atoms", function()
|
||||
it("collects computed atoms", function()
|
||||
local source = atom(0)
|
||||
|
||||
derive(function()
|
||||
computed(function()
|
||||
return source()
|
||||
end)
|
||||
|
||||
collect()
|
||||
|
||||
assert(count(store.listeners[source]) == 0, "derived atom did not gc")
|
||||
assert(count(store.listeners[source]) == 0, "computed atom did not gc")
|
||||
end)
|
||||
|
||||
it("collects nested derived atoms", function()
|
||||
it("collects nested computed atoms", function()
|
||||
local source = atom(0)
|
||||
|
||||
do
|
||||
-- Declare this in another scope to ensure no references are kept
|
||||
local derived = derive(function()
|
||||
return source()
|
||||
-- Declare this in another scope to ensure no references exist at the
|
||||
-- time of garbage collection.
|
||||
local double = computed(function()
|
||||
return source() * 2
|
||||
end)
|
||||
|
||||
derive(function()
|
||||
return derived() * 2
|
||||
computed(function()
|
||||
return double() * 2
|
||||
end)
|
||||
end
|
||||
|
||||
collect()
|
||||
|
||||
assert(count(store.listeners[source]) == 0, "nested derived atom did not gc")
|
||||
assert(count(store.listeners[source]) == 0, "nested computed atom did not gc")
|
||||
end)
|
||||
end
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
local atom = require(script.Parent.Parent.atom)
|
||||
local map = require(script.Parent.Parent.map)
|
||||
local mapped = require(script.Parent.Parent.mapped)
|
||||
local observe = require(script.Parent.Parent.observe)
|
||||
local store = require(script.Parent.Parent.store)
|
||||
|
||||
return function()
|
||||
it("returns mapped atoms", function()
|
||||
local itemsAtom = atom({ "a", "b", "c" })
|
||||
local mappedAtom = map(itemsAtom, function(value, key)
|
||||
local mappedAtom = mapped(itemsAtom, function(value, key)
|
||||
return string.upper(value), key
|
||||
end)
|
||||
local updates = 0
|
||||
|
|
@ -35,7 +35,7 @@ return function()
|
|||
|
||||
it("uses key extractor", function()
|
||||
local itemsAtom = atom({ { key = 1 }, { key = 2 }, { key = 3 } })
|
||||
local mappedAtom = map(itemsAtom, function(item)
|
||||
local mappedAtom = mapped(itemsAtom, function(item)
|
||||
return item, item.key
|
||||
end)
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ return function()
|
|||
{ key = 3, value = "c" },
|
||||
})
|
||||
|
||||
local mappedAtom = map(itemsAtom, function(item)
|
||||
local mappedAtom = mapped(itemsAtom, function(item)
|
||||
return item, item.key
|
||||
end)
|
||||
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
local atom = require(script.Parent.Parent.atom)
|
||||
local derive = require(script.Parent.Parent.derive)
|
||||
local computed = require(script.Parent.Parent.computed)
|
||||
local store = require(script.Parent.Parent.store)
|
||||
local subscribe = require(script.Parent.Parent.subscribe)
|
||||
local collect = require(script.Parent.Parent.utils.collect)
|
||||
|
|
@ -31,22 +31,22 @@ return function()
|
|||
local updates = 0
|
||||
|
||||
-- Create a new scope to encapsulate variables with a limited lifetime, like
|
||||
-- 'unsubscribe' and 'derived', to ensure they are garbage collected.
|
||||
-- 'unsubscribe' and 'computed', to ensure they are garbage collected.
|
||||
--
|
||||
-- The idea here is to test whether subscribe() prevents every derived
|
||||
-- The idea here is to test whether subscribe() prevents every computed
|
||||
-- atom in a chain from being garbage collected. If any are GCed, then
|
||||
-- the subscription will stop updating and the test will fail.
|
||||
do
|
||||
local unsubscribe
|
||||
|
||||
do
|
||||
local derived = derive(function()
|
||||
return source()
|
||||
local double = computed(function()
|
||||
return source() * 2
|
||||
end)
|
||||
|
||||
unsubscribe = subscribe(
|
||||
derive(function()
|
||||
return derived() * 2
|
||||
computed(function()
|
||||
return double() * 2
|
||||
end),
|
||||
function()
|
||||
updates += 1
|
||||
|
|
@ -58,13 +58,13 @@ return function()
|
|||
source(source() + 1)
|
||||
|
||||
assert(updates == 1, "subscription stopped updating")
|
||||
assert(count(store.listeners[source]) == 1, "derived atom was garbage collected")
|
||||
assert(count(store.listeners[source]) == 1, "computed atom was garbage collected")
|
||||
|
||||
unsubscribe()
|
||||
end
|
||||
|
||||
collect()
|
||||
|
||||
assert(count(store.listeners[source]) == 0, "derived atom did not collect")
|
||||
assert(count(store.listeners[source]) == 0, "computed atom did not collect")
|
||||
end)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
local atom = require(script.Parent.Parent.Parent.atom)
|
||||
local sync = require(script.Parent.Parent.Parent.sync)
|
||||
local patch = require(script.Parent.Parent.Parent.sync.patch)
|
||||
local types = require(script.Parent.Parent.Parent.types)
|
||||
|
||||
return function()
|
||||
|
|
@ -24,7 +25,7 @@ return function()
|
|||
end)
|
||||
|
||||
it("sends state patches", function()
|
||||
local a = atom({ b = 1, c = 2 })
|
||||
local a = atom({ b = 1, c = 2 } :: { [string]: number? })
|
||||
local server = sync.server({ atoms = { a = a }, interval = -1 })
|
||||
local player: Player, payload: types.SyncPayload
|
||||
|
||||
|
|
@ -33,13 +34,20 @@ return function()
|
|||
end)
|
||||
|
||||
a({ b = 1, c = 5 })
|
||||
server:flush(target)
|
||||
server:_sendPatch(target)
|
||||
|
||||
expect(player).to.equal(target)
|
||||
expect(payload.type).to.equal("patch")
|
||||
expect(payload.data.a.b).to.never.be.ok()
|
||||
expect(payload.data.a.c).to.equal(5)
|
||||
|
||||
a({ b = 1, d = 1 })
|
||||
server:_sendPatch(target)
|
||||
|
||||
expect(payload.data.a.b).to.never.be.ok()
|
||||
expect(payload.data.a.c).to.equal(patch.NONE)
|
||||
expect(payload.data.a.d).to.equal(1)
|
||||
|
||||
disconnect()
|
||||
end)
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue