mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
fix: do not track implicit dependencies in observe() and mapped() (#39)
* fix: use `subscribe` for explicit dependencies * fix: call listeners initially once * fix: wrap initial run in `peek()`
This commit is contained in:
parent
a430cf63e5
commit
6be2fbb746
2 changed files with 22 additions and 12 deletions
|
|
@ -1,6 +1,6 @@
|
|||
local atom = require(script.Parent.atom)
|
||||
local effect = require(script.Parent.effect)
|
||||
local store = require(script.Parent.store)
|
||||
local subscribe = require(script.Parent.subscribe)
|
||||
local types = require(script.Parent.types)
|
||||
type Atom<T> = types.Atom<T>
|
||||
type Selector<T> = types.Selector<T>
|
||||
|
|
@ -26,13 +26,14 @@ local function mapped<K0, V0, K1, V1>(callback: Selector<{ [K0]: V0 }>, mapper:
|
|||
local prevMappedItems: { [K1]: V1 } = {}
|
||||
local unsubscribe
|
||||
|
||||
unsubscribe = effect(function()
|
||||
if not mappedAtomRef.current then
|
||||
local function listener(items: { [K0]: V0 })
|
||||
local mappedAtom = mappedAtomRef.current
|
||||
|
||||
if not mappedAtom then
|
||||
return unsubscribe()
|
||||
end
|
||||
|
||||
local items = callback()
|
||||
local mappedItems = table.clone(store.peek(mappedAtomRef.current))
|
||||
local mappedItems = table.clone(mappedAtom())
|
||||
local mappedKeys = {}
|
||||
|
||||
-- TODO: Only call mapper if the item has changed.
|
||||
|
|
@ -56,6 +57,12 @@ local function mapped<K0, V0, K1, V1>(callback: Selector<{ [K0]: V0 }>, mapper:
|
|||
|
||||
prevMappedItems = mappedItems
|
||||
mappedAtom(mappedItems)
|
||||
end
|
||||
|
||||
unsubscribe = subscribe(callback, listener)
|
||||
|
||||
store.peek(function(): ()
|
||||
listener(callback())
|
||||
end)
|
||||
|
||||
return mappedAtom
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
local effect = require(script.Parent.effect)
|
||||
local store = require(script.Parent.store)
|
||||
local subscribe = require(script.Parent.subscribe)
|
||||
local types = require(script.Parent.types)
|
||||
type Selector<T> = types.Selector<T>
|
||||
|
||||
|
|
@ -16,9 +17,7 @@ local function noop() end
|
|||
local function observe<K, V>(callback: Selector<{ [K]: V }>, factory: (value: V, key: K) -> (() -> ())?): () -> ()
|
||||
local connections: { [K]: () -> () } = {}
|
||||
|
||||
local unsubscribe = effect(function()
|
||||
local state = callback()
|
||||
|
||||
local function listener(state: { [K]: V })
|
||||
for key, disconnect in next, connections do
|
||||
if state[key] == nil then
|
||||
connections[key] = nil
|
||||
|
|
@ -31,17 +30,21 @@ local function observe<K, V>(callback: Selector<{ [K]: V }>, factory: (value: V,
|
|||
connections[key] = factory(value, key) or noop
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local unsubscribe = subscribe(callback, listener)
|
||||
|
||||
store.peek(function(): ()
|
||||
listener(callback())
|
||||
end)
|
||||
|
||||
local function cleanup()
|
||||
return function()
|
||||
unsubscribe()
|
||||
for _, disconnect in next, connections do
|
||||
disconnect()
|
||||
end
|
||||
table.clear(connections)
|
||||
end
|
||||
|
||||
return cleanup
|
||||
end
|
||||
|
||||
return observe
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue