mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 21:01:38 +00:00
fix: use subscribe for explicit dependencies
This commit is contained in:
parent
a430cf63e5
commit
01a895e2eb
2 changed files with 4 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
local atom = require(script.Parent.atom)
|
local atom = require(script.Parent.atom)
|
||||||
local effect = require(script.Parent.effect)
|
|
||||||
local store = require(script.Parent.store)
|
local store = require(script.Parent.store)
|
||||||
|
local subscribe = require(script.Parent.subscribe)
|
||||||
local types = require(script.Parent.types)
|
local types = require(script.Parent.types)
|
||||||
type Atom<T> = types.Atom<T>
|
type Atom<T> = types.Atom<T>
|
||||||
type Selector<T> = types.Selector<T>
|
type Selector<T> = types.Selector<T>
|
||||||
|
|
@ -26,12 +26,11 @@ local function mapped<K0, V0, K1, V1>(callback: Selector<{ [K0]: V0 }>, mapper:
|
||||||
local prevMappedItems: { [K1]: V1 } = {}
|
local prevMappedItems: { [K1]: V1 } = {}
|
||||||
local unsubscribe
|
local unsubscribe
|
||||||
|
|
||||||
unsubscribe = effect(function()
|
unsubscribe = subscribe(callback, function(items)
|
||||||
if not mappedAtomRef.current then
|
if not mappedAtomRef.current then
|
||||||
return unsubscribe()
|
return unsubscribe()
|
||||||
end
|
end
|
||||||
|
|
||||||
local items = callback()
|
|
||||||
local mappedItems = table.clone(store.peek(mappedAtomRef.current))
|
local mappedItems = table.clone(store.peek(mappedAtomRef.current))
|
||||||
local mappedKeys = {}
|
local mappedKeys = {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
local effect = require(script.Parent.effect)
|
local subscribe = require(script.Parent.subscribe)
|
||||||
local types = require(script.Parent.types)
|
local types = require(script.Parent.types)
|
||||||
type Selector<T> = types.Selector<T>
|
type Selector<T> = types.Selector<T>
|
||||||
|
|
||||||
|
|
@ -16,9 +16,7 @@ local function noop() end
|
||||||
local function observe<K, V>(callback: Selector<{ [K]: V }>, factory: (value: V, key: K) -> (() -> ())?): () -> ()
|
local function observe<K, V>(callback: Selector<{ [K]: V }>, factory: (value: V, key: K) -> (() -> ())?): () -> ()
|
||||||
local connections: { [K]: () -> () } = {}
|
local connections: { [K]: () -> () } = {}
|
||||||
|
|
||||||
local unsubscribe = effect(function()
|
local unsubscribe = subscribe(callback, function(state)
|
||||||
local state = callback()
|
|
||||||
|
|
||||||
for key, disconnect in next, connections do
|
for key, disconnect in next, connections do
|
||||||
if state[key] == nil then
|
if state[key] == nil then
|
||||||
connections[key] = nil
|
connections[key] = nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue