feat: initial commit

This commit is contained in:
Richard 2024-02-25 03:41:09 -08:00
commit 18723c1377
50 changed files with 3238 additions and 0 deletions

46
src/subscribe.luau Normal file
View 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