mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
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:
parent
1a65a4a0de
commit
6a9c580bd7
24 changed files with 805 additions and 76 deletions
|
|
@ -2,13 +2,24 @@ local store = require(script.Parent.store)
|
|||
local types = require(script.Parent.types)
|
||||
type Molecule<T> = types.Molecule<T>
|
||||
|
||||
--[=[
|
||||
Subscribes to changes in the given atom or molecule. The callback is
|
||||
called with the current state and the previous state immediately after a
|
||||
change occurs.
|
||||
|
||||
@param molecule The atom or molecule to subscribe to.
|
||||
@param callback The function to call when the state changes.
|
||||
@return A function that unsubscribes the callback.
|
||||
]=]
|
||||
local function subscribe<T>(molecule: Molecule<T>, callback: (state: T, prev: T) -> ()): () -> ()
|
||||
local captured, state = store.capture(molecule)
|
||||
|
||||
local function listener()
|
||||
local prevState = state
|
||||
state = molecule()
|
||||
callback(state, prevState)
|
||||
if state ~= prevState then
|
||||
callback(state, prevState)
|
||||
end
|
||||
end
|
||||
|
||||
for atom in captured do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue