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

38
src/observe.luau Normal file
View file

@ -0,0 +1,38 @@
local atom = require(script.Parent.atom)
type Atom<T> = atom.Atom<T>
local subscribe = require(script.Parent.subscribe)
local function observe<Item, Key>(atom: Atom<{ [Key]: Item }>, factory: (value: Item, key: Key) -> () -> ()): (() -> (), () -> ())
local connections: { [Key]: () -> () } = {}
local function update(state: { [Key]: Item })
for key, disconnect in connections do
if state[key] == nil then
connections[key] = nil
disconnect()
end
end
for key, value in state do
if not connections[key] then
connections[key] = factory(value, key)
end
end
end
local unsubscribe, flush = subscribe(atom, update)
local function cleanup()
unsubscribe()
for _, disconnect in connections do
disconnect()
end
end
update(atom:get())
return cleanup, flush
end
return observe