Skip to content

Commit c2aeb3c

Browse files
author
pjechris
authored
fix(store): Fixed multiple store not working when alias is a collection (#62)
1 parent 41c846b commit c2aeb3c

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

Sources/CohesionKit/EntityStore.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public class IdentityMap {
7777
update?(&entity)
7878
}
7979

80-
let node = nodeStore(entity: entity, modifiedAt: modifiedAt)
81-
8280
if let key = named {
8381
storeAlias(content: entity, key: key, modifiedAt: modifiedAt)
8482
}
8583

84+
let node = nodeStore(entity: entity, modifiedAt: modifiedAt)
85+
8686
return EntityObserver(node: node, registry: registry)
8787
}
8888
}
@@ -91,12 +91,12 @@ public class IdentityMap {
9191
public func store<C: Collection>(entities: C, named: AliasKey<C>? = nil, modifiedAt: Stamp? = nil)
9292
-> EntityObserver<[C.Element]> where C.Element: Identifiable {
9393
transaction {
94-
let nodes = entities.map { nodeStore(entity: $0, modifiedAt: modifiedAt) }
95-
9694
if let key = named {
9795
storeAlias(content: entities, key: key, modifiedAt: modifiedAt)
9896
}
9997

98+
let nodes = entities.map { nodeStore(entity: $0, modifiedAt: modifiedAt) }
99+
100100
return EntityObserver(nodes: nodes, registry: registry)
101101
}
102102
}
@@ -105,12 +105,13 @@ public class IdentityMap {
105105
public func store<C: Collection>(entities: C, named: AliasKey<C>? = nil, modifiedAt: Stamp? = nil)
106106
-> EntityObserver<[C.Element]> where C.Element: Aggregate {
107107
transaction {
108-
let nodes = entities.map { nodeStore(entity: $0, modifiedAt: modifiedAt) }
109-
110108
if let key = named {
111109
storeAlias(content: entities, key: key, modifiedAt: modifiedAt)
112110
}
113111

112+
let nodes = entities.map { nodeStore(entity: $0, modifiedAt: modifiedAt) }
113+
114+
114115
return EntityObserver(nodes: nodes, registry: registry)
115116
}
116117
}
@@ -212,7 +213,6 @@ public class IdentityMap {
212213
return returnValue
213214
}
214215
}
215-
216216
}
217217

218218
// MARK: Update

Tests/CohesionKitTests/EntityStoreTests.swift

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import XCTest
22
@testable import CohesionKit
33

4-
// MARK: Store
54
class EntityStoreTests: XCTestCase {
5+
// MARK: Store Aggregate
66
func test_storeAggregate_nestedEntitiesAreStored() {
77
let entity = RootFixture(
88
id: 1,
@@ -95,6 +95,20 @@ class EntityStoreTests: XCTestCase {
9595
}
9696
}
9797

98+
func test_storeAggregate_named_itEnqueuesAliasInRegistry() {
99+
let root = SingleNodeFixture(id: 1)
100+
let registry = ObserverRegistryStub()
101+
let identityMap = IdentityMap(registry: registry)
102+
103+
withExtendedLifetime(identityMap.store(entity: root, named: .test)) {
104+
XCTAssertTrue(registry.hasPendingChange(for: AliasContainer<SingleNodeFixture>.self))
105+
XCTAssertTrue(registry.hasPendingChange(for: SingleNodeFixture.self))
106+
}
107+
}
108+
}
109+
110+
// MARK: Store Entities
111+
extension EntityStoreTests {
98112
/// make sure when inserting multiple time the same entity that it actually gets inserted only once
99113
func test_storeEntities_sameEntityPresentMultipleTimes_itIsInsertedOnce() {
100114
let registry = ObserverRegistryStub(queue: .main)
@@ -108,6 +122,20 @@ class EntityStoreTests: XCTestCase {
108122
XCTAssertEqual(registry.pendingChangeCount(for: commonEntity), 1)
109123
}
110124

125+
func test_storeEntities_named_calledMultipleTimes_lastValueIsStored() {
126+
let identityMap = IdentityMap()
127+
let root = SingleNodeFixture(id: 1)
128+
let root2 = SingleNodeFixture(id: 2)
129+
130+
_ = identityMap.store(entities: [root], named: .listOfNodes)
131+
_ = identityMap.store(entities: [root, root2], named: .listOfNodes)
132+
133+
XCTAssertEqual(identityMap.find(named: .listOfNodes).value, [root, root2])
134+
}
135+
}
136+
137+
// MARK: Store Identifiable
138+
extension EntityStoreTests {
111139
func test_storeIdentifiable_entityIsInsertedForThe1stTime_loggerIsCalled() {
112140
let logger = LoggerMock()
113141
let identityMap = IdentityMap(logger: logger)
@@ -140,17 +168,6 @@ class EntityStoreTests: XCTestCase {
140168

141169
wait(for: [expectation], timeout: 0)
142170
}
143-
144-
func test_storeAggregate_named_itEnqueuesAliasInRegistry() {
145-
let root = SingleNodeFixture(id: 1)
146-
let registry = ObserverRegistryStub()
147-
let identityMap = IdentityMap(registry: registry)
148-
149-
withExtendedLifetime(identityMap.store(entity: root, named: .test)) {
150-
XCTAssertTrue(registry.hasPendingChange(for: AliasContainer<SingleNodeFixture>.self))
151-
XCTAssertTrue(registry.hasPendingChange(for: SingleNodeFixture.self))
152-
}
153-
}
154171
}
155172

156173
// MARK: Find

0 commit comments

Comments
 (0)