Skip to content

Commit c76edf2

Browse files
authored
Minor refactoring (#88)
1 parent e4d1404 commit c76edf2

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

Sources/Atoms/Core/StoreContext.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ internal struct StoreContext {
103103
let key = AtomKey(atom, overrideScopeKey: override?.scopeKey)
104104
let cache = lookupCache(of: atom, for: key)
105105
let newCache = cache ?? makeNewCache(of: atom, for: key, override: override)
106-
let isInserted = store.graph.children[key, default: []].insert(transaction.key).inserted
106+
let isNew = store.graph.children[key, default: []].insert(transaction.key).inserted
107107

108108
// Add an `Edge` from the upstream to downstream.
109109
store.graph.dependencies[transaction.key, default: []].insert(key)
110110

111-
if isInserted || cache == nil {
111+
if isNew || cache == nil {
112112
notifyUpdateToObservers()
113113
}
114114

@@ -132,15 +132,14 @@ internal struct StoreContext {
132132
requiresObjectUpdate: requiresObjectUpdate,
133133
notifyUpdate: notifyUpdate
134134
)
135-
let isInserted = store.state.subscriptions[key, default: [:]].updateValue(subscription, forKey: container.key) == nil
136135

137-
// Register the subscription to both the store and the container.
138-
container.subscriptions[key] = subscription
136+
store.state.subscriptions[key, default: [:]].updateValue(subscription, forKey: container.key)
139137
container.unsubscribe = { keys in
140138
unsubscribe(keys, for: container.key)
141139
}
140+
let isNew = container.subscribingKeys.insert(key).inserted
142141

143-
if isInserted || cache == nil {
142+
if isNew || cache == nil {
144143
notifyUpdateToObservers()
145144
}
146145

@@ -201,7 +200,7 @@ internal struct StoreContext {
201200
let override = lookupOverride(of: atom)
202201
let key = AtomKey(atom, overrideScopeKey: override?.scopeKey)
203202

204-
container.subscriptions.removeValue(forKey: key)
203+
container.subscribingKeys.remove(key)
205204
unsubscribe([key], for: container.key)
206205
}
207206

@@ -380,10 +379,10 @@ private extension StoreContext {
380379
}
381380
}
382381

383-
func unsubscribe(_ keys: [AtomKey], for subscriptionKey: SubscriptionKey) {
382+
func unsubscribe<Keys: Sequence<AtomKey>>(_ keys: Keys, for subscriptionKey: SubscriptionKey) {
384383
let store = getStore()
385384

386-
for key in keys {
385+
for key in ContiguousArray(keys) {
387386
store.state.subscriptions[key]?.removeValue(forKey: subscriptionKey)
388387
checkRelease(for: key)
389388
}

Sources/Atoms/Core/SubscriptionContainer.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
@usableFromInline
22
@MainActor
33
internal final class SubscriptionContainer {
4-
private var subscriptions = [AtomKey: Subscription]()
5-
private var unsubscribe: (([AtomKey]) -> Void)?
4+
private var subscribingKeys = Set<AtomKey>()
5+
private var unsubscribe: ((Set<AtomKey>) -> Void)?
66
private let token = SubscriptionKey.Token()
77

88
nonisolated init() {}
99

1010
deinit {
11-
unsubscribe?(Array(subscriptions.keys))
11+
unsubscribe?(subscribingKeys)
1212
}
1313

1414
func wrapper(location: SourceLocation) -> Wrapper {
@@ -25,12 +25,12 @@ internal extension SubscriptionContainer {
2525
let key: SubscriptionKey
2626
let location: SourceLocation
2727

28-
var subscriptions: [AtomKey: Subscription] {
29-
get { container?.subscriptions ?? [:] }
30-
nonmutating set { container?.subscriptions = newValue }
28+
var subscribingKeys: Set<AtomKey> {
29+
get { container?.subscribingKeys ?? [] }
30+
nonmutating set { container?.subscribingKeys = newValue }
3131
}
3232

33-
var unsubscribe: (([AtomKey]) -> Void)? {
33+
var unsubscribe: ((Set<AtomKey>) -> Void)? {
3434
get { container?.unsubscribe }
3535
nonmutating set { container?.unsubscribe = newValue }
3636
}

Tests/AtomsTests/Core/StoreContextTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ final class StoreContextTests: XCTestCase {
270270
}
271271

272272
XCTAssertEqual(initialValue, 0)
273-
XCTAssertNotNil(container!.wrapper.subscriptions[key])
273+
XCTAssertTrue(container!.wrapper.subscribingKeys.contains(key))
274274
XCTAssertNotNil(store.state.subscriptions[key]?[container!.wrapper.key])
275275
XCTAssertEqual((store.state.caches[key] as? AtomCache<TestAtom>)?.value, 0)
276276
XCTAssertEqual((store.state.caches[dependencyKey] as? AtomCache<DependencyAtom>)?.value, 0)

0 commit comments

Comments
 (0)