feat: add effect cleanups, add docs (#2)

* docs: update readme

* feat: add cleanup function to effects

* refactor: move peek function to store module

* test: add nested atom check

* docs: add doc comments

* fix: `effect()` type

* refactor: remove divider comment
This commit is contained in:
richard 2024-06-02 01:25:00 -07:00 • committed by GitHub
parent 1a65a4a0de
commit 6a9c580bd7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 805 additions and 76 deletions

View file

@ -24,7 +24,23 @@ return function()
unsubscribe()
a(4)
expect(current).to.equal(6)
end)
it("accepts a cleanup function", function()
local a = atom(0)
local cleanups = 0
local unsubscribe = effect(function()
a()
return function()
cleanups += 1
end
end)
expect(cleanups).to.equal(0)
a(1)
expect(cleanups).to.equal(1)
unsubscribe()
expect(cleanups).to.equal(2)
end)
end

View file

@ -1,12 +1,11 @@
local atom = require(script.Parent.Parent.atom)
local peek = require(script.Parent.Parent.peek)
local store = require(script.Parent.Parent.store)
return function()
it("calls an atom without capturing it", function()
local a, b, c = atom(1), atom(1), atom(1)
local captured, result = store.capture(function()
return a() + peek(b) + c()
return a() + store.peek(b) + c()
end)
expect(captured[a]).to.be.ok()
expect(captured[b]).to.never.be.ok()
@ -15,8 +14,8 @@ return function()
end)
it("passes through arguments", function()
expect(peek(1)).to.equal(1)
expect(peek(function()
expect(store.peek(1)).to.equal(1)
expect(store.peek(function()
return 1
end)).to.equal(1)
end)

View file

@ -1,3 +1,4 @@
local atom = require(script.Parent.Parent.Parent.atom)
local validate = require(script.Parent.Parent.Parent.sync.validate)
return function()
@ -13,6 +14,8 @@ return function()
number = { value = 1, expected = true },
number_infinity = { value = math.huge, expected = false },
number_nan = { value = 0 / 0, expected = false },
nested_atom = { value = atom(0), expected = false },
}
for key, case in cases do