mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
feat: initial commit
This commit is contained in:
commit
18723c1377
50 changed files with 3238 additions and 0 deletions
46
src/subscribe.luau
Normal file
46
src/subscribe.luau
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
local atom = require(script.Parent.atom)
|
||||
type Atom<T> = atom.Atom<T>
|
||||
|
||||
local reference = require(script.Parent.utils.reference)
|
||||
|
||||
local function subscribe<State>(atom: Atom<State>, callback: (state: State, lastState: State) -> ()): (() -> (), () -> ())
|
||||
local pending: thread? = nil
|
||||
local state = atom:get()
|
||||
|
||||
local function emit()
|
||||
local lastState = state
|
||||
state = atom:get()
|
||||
callback(state, lastState)
|
||||
end
|
||||
|
||||
local function update()
|
||||
pending = pending or task.defer(function()
|
||||
pending = nil
|
||||
emit()
|
||||
end)
|
||||
end
|
||||
|
||||
local function flush()
|
||||
if pending then
|
||||
pending = task.cancel(pending)
|
||||
emit()
|
||||
end
|
||||
end
|
||||
|
||||
local function unsubscribe()
|
||||
reference.release(atom)
|
||||
atom.listeners[update] = nil
|
||||
|
||||
if pending then
|
||||
pending = task.cancel(pending)
|
||||
end
|
||||
end
|
||||
|
||||
-- Ensure the atom is held in memory while the subscription is active
|
||||
reference.hold(atom)
|
||||
atom.listeners[update] = true
|
||||
|
||||
return unsubscribe, flush
|
||||
end
|
||||
|
||||
return subscribe
|
||||
Loading…
Add table
Add a link
Reference in a new issue