mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 12:41:37 +00:00
refactor: use new type solver
This commit is contained in:
parent
748dbc5014
commit
22b440e35f
32 changed files with 169 additions and 155 deletions
|
|
@ -84,7 +84,7 @@ end)
|
|||
test("should receive previous value", function()
|
||||
local calls = {}
|
||||
local src, setSrc = signal(0)
|
||||
local c1 = computed(function(current): number
|
||||
local c1 = computed(function(current: number?): number
|
||||
local incoming = src()
|
||||
return if current == incoming or incoming % 2 == 1 then current or incoming else incoming
|
||||
end)
|
||||
|
|
@ -192,10 +192,10 @@ test("should trigger effect even computed already dirty", function()
|
|||
table.insert(calls, c2())
|
||||
end)
|
||||
assert(table.concat(calls, ",") == "0foo", table.concat(calls, ","))
|
||||
assert(v() == 1, v())
|
||||
assert(v() == 1, `expected 1 for v(), got {v()}`)
|
||||
v(2)
|
||||
assert(table.concat(calls, ",") == "0foo,2foo", table.concat(calls, ","))
|
||||
assert(v() == 2, v())
|
||||
assert(v() == 2, `expected 2 for v(), got {v()}`)
|
||||
end)
|
||||
|
||||
return {}
|
||||
|
|
|
|||
|
|
@ -440,7 +440,7 @@ end)
|
|||
test("should run the cleanup if the effect disposes itself", function()
|
||||
local a, setA = signal(0)
|
||||
local calls = 0
|
||||
local dispose
|
||||
local dispose: () -> ()
|
||||
|
||||
dispose = effect(function()
|
||||
if a() > 0 then
|
||||
|
|
@ -460,7 +460,7 @@ end)
|
|||
test("should not run the effect if the cleanup function disposes it", function()
|
||||
local a, setA = signal(0)
|
||||
local calls = 0
|
||||
local dispose
|
||||
local dispose: () -> ()
|
||||
|
||||
dispose = effect(function()
|
||||
a()
|
||||
|
|
|
|||
|
|
@ -31,8 +31,8 @@ end)
|
|||
|
||||
test("should run indirect scope cleanup", function()
|
||||
local cleanups = 0
|
||||
local cleanup: () -> ()
|
||||
|
||||
local cleanup
|
||||
local stopScope = effectScope(function()
|
||||
cleanup = effectScope(function()
|
||||
onCleanup(function()
|
||||
|
|
@ -50,8 +50,8 @@ end)
|
|||
|
||||
test("should run indirect effect cleanup", function()
|
||||
local cleanups = 0
|
||||
|
||||
local a, setA = signal(false)
|
||||
|
||||
local stopScope = effectScope(function()
|
||||
effect(function()
|
||||
a()
|
||||
|
|
@ -73,7 +73,7 @@ end)
|
|||
test("should dispose inner effects if created in an effect", function()
|
||||
local source, setSource = signal(1)
|
||||
local triggers = 0
|
||||
local dispose
|
||||
local dispose: () -> ()
|
||||
|
||||
effect(function()
|
||||
dispose = effectScope(function()
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ end
|
|||
immediately closing it, causing alien-signals to unlink the computed value
|
||||
from its dependencies and allow it to be garbage collected.
|
||||
]]
|
||||
local function unlink(getter: () -> ())
|
||||
local function unlink(getter: () -> unknown)
|
||||
effectScope(function()
|
||||
getter()
|
||||
end)()
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ test("should handle recursive additions and removals", function()
|
|||
|
||||
observe(items, function(item)
|
||||
if item < 5 then
|
||||
setItems(function(state)
|
||||
setItems(function(state: { number })
|
||||
state = table.clone(state)
|
||||
table.insert(state, item + 1)
|
||||
return state
|
||||
|
|
@ -117,7 +117,7 @@ test("should handle self removal", function()
|
|||
local edits = {}
|
||||
|
||||
observe(items, function(item, index)
|
||||
setItems(function(state)
|
||||
setItems(function(state: { number })
|
||||
state = table.clone(state)
|
||||
state[index] = nil
|
||||
return state
|
||||
|
|
@ -134,7 +134,7 @@ end)
|
|||
test("should allow disposal during addition", function()
|
||||
local items, setItems = signal({})
|
||||
local edits = {}
|
||||
local dispose
|
||||
local dispose: () -> ()
|
||||
|
||||
dispose = observe(items, function(item)
|
||||
if item == 1 then
|
||||
|
|
@ -153,7 +153,7 @@ end)
|
|||
test("should allow disposal during removal", function()
|
||||
local items, setItems = signal({ 1, 2, 3, 4 })
|
||||
local edits = {}
|
||||
local dispose
|
||||
local dispose: () -> ()
|
||||
|
||||
dispose = observe(items, function(item)
|
||||
table.insert(edits, `+{item}`)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ local effect = Charm.effect
|
|||
|
||||
test("should accept equality function", function()
|
||||
local calls = {}
|
||||
local src, setSrc = signal(0, function(current, incoming)
|
||||
local src, setSrc = signal(0, function(current: number, incoming: number)
|
||||
return current == incoming or incoming % 2 == 1
|
||||
end)
|
||||
|
||||
|
|
|
|||
|
|
@ -442,18 +442,16 @@ end)
|
|||
test.describe("error handling", function()
|
||||
test("should keep graph consistent on errors during activation", function()
|
||||
local a = atom(0)
|
||||
local b = computed(function()
|
||||
local b = computed(function(): nil
|
||||
error("fail")
|
||||
end)
|
||||
local c = computed(function()
|
||||
return a()
|
||||
end)
|
||||
|
||||
local success, result = pcall(function()
|
||||
b()
|
||||
end)
|
||||
local success, result = pcall(b)
|
||||
assert(not success, "should throw")
|
||||
assert(string.match(result, "fail"), "should throw fail")
|
||||
assert(string.match(tostring(result), "fail"), "should throw fail")
|
||||
|
||||
a(1)
|
||||
assert(c() == 1, "c should recover and return 1")
|
||||
|
|
@ -461,7 +459,7 @@ test.describe("error handling", function()
|
|||
|
||||
test("should keep graph consistent on errors in computeds", function()
|
||||
local a = atom(0)
|
||||
local b = computed(function()
|
||||
local b = computed(function(): number
|
||||
if a() == 1 then
|
||||
error("fail")
|
||||
end
|
||||
|
|
@ -474,11 +472,9 @@ test.describe("error handling", function()
|
|||
assert(c() == 0, "c should be 0")
|
||||
|
||||
a(1)
|
||||
local success, result = pcall(function()
|
||||
b()
|
||||
end)
|
||||
local success, result = pcall(b)
|
||||
assert(not success, "should throw")
|
||||
assert(string.match(result, "fail"), "should throw fail")
|
||||
assert(string.match(tostring(result), "fail"), "should throw fail")
|
||||
|
||||
a(2)
|
||||
assert(c() == 2, "c should recover and return 2")
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ end)
|
|||
|
||||
test("should run detached from effect scope", function()
|
||||
local effects = 0
|
||||
local stopEffect
|
||||
local stopEffect: () -> ()
|
||||
|
||||
local stopScope = effectScope(function()
|
||||
untracked(function()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue