feat: add signal api

This commit is contained in:
littensy 2025-10-31 00:36:55 -07:00
parent bf92413a97
commit 1e0b10aef6
21 changed files with 300 additions and 293 deletions

View file

@ -1,10 +1,10 @@
local Charm = require("../packages/charm/src")
local bench = require("./bench")
local atom = Charm.atom
local computed = Charm.computed
local mapped = Charm.mapped
local setCurrentSub = Charm.core.setCurrentSub
local setActiveSub = Charm.core.setActiveSub
local signal = Charm.signal
type Entry = { number }
@ -25,9 +25,9 @@ local function simpleMapped(source: () -> { Entry }, mapper: (Entry, number) ->
local nextState = {}
for index, value in state do
local currentSub = setCurrentSub(nil)
local currentSub = setActiveSub(nil)
local success, valueOut, keyOut = pcall(mapper, value, index)
setCurrentSub(currentSub)
setActiveSub(currentSub)
if success then
nextState[keyOut] = valueOut
@ -52,26 +52,26 @@ end
for _, percent in { 0.1, 0.5, 1 } do
bench.run(`mapped {percent}`, function()
local src = atom(initialState)
local src, setSrc = signal(initialState)
local out = mapped(src, mapper)
bench.profilebegin("init")
out()
bench.profileend()
src(updatePercentage(percent))
setSrc(updatePercentage(percent))
bench.profilebegin("update")
out()
bench.profileend()
end)
bench.run(`control {percent}`, function()
local src = atom(initialState)
local src, setSrc = signal(initialState)
local out = simpleMapped(src, mapper)
bench.profilebegin("init")
out()
bench.profileend()
src(updatePercentage(percent))
setSrc(updatePercentage(percent))
bench.profilebegin("update")
out()
bench.profileend()

View file

@ -1,15 +1,15 @@
local Charm = require("../packages/charm/src")
local bench = require("./bench")
local atom = Charm.atom
local computed = Charm.computed
local effect = Charm.effect
local listen = Charm.listen
local signal = Charm.signal
for _, w in { 10, 100 } do
for _, h in { 10, 100 } do
bench.run(`propagate {w}*{h}`, function()
local src = atom(1)
local src, setSrc = signal(1)
for _ = 1, w do
local last = src
for _ = 1, h do
@ -22,7 +22,7 @@ for _, w in { 10, 100 } do
last()
end)
end
src(src() + 1)
setSrc(src() + 1)
end)
end
end
@ -30,7 +30,7 @@ end
for _, w in { 10, 100 } do
for _, h in { 10, 100 } do
bench.run(`propagate (listen) {w}*{h}`, function()
local src = atom(1)
local src, setSrc = signal(1)
for _ = 1, w do
local last = src
for _ = 1, h do
@ -41,7 +41,7 @@ for _, w in { 10, 100 } do
end
listen(last, function() end)
end
src(src() + 1)
setSrc(src() + 1)
end)
end
end