Skip to content

Commit

Permalink
[alias] store alias nested entities
Browse files Browse the repository at this point in the history
  • Loading branch information
pjechris committed Oct 10, 2023
1 parent 0848e79 commit 1838d81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
20 changes: 8 additions & 12 deletions Sources/CohesionKit/Identity/IdentityStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public class IdentityMap {
}
}

func nodeStore<T: Identifiable>(entity: T, modifiedAt: Stamp?) -> EntityNode<T> {
let node = storage[entity, new: EntityNode(entity, modifiedAt: nil) { [registry] in
func nodeStore<T: Identifiable>(in node: EntityNode<T>? = nil, entity: T, modifiedAt: Stamp?) -> EntityNode<T> {
let node = node ?? storage[entity, new: EntityNode(entity, modifiedAt: nil) { [registry] in
registry.enqueueChange(for: $0)
}]

Expand All @@ -167,8 +167,8 @@ public class IdentityMap {
return node
}

func nodeStore<T: Aggregate>(entity: T, modifiedAt: Stamp?) -> EntityNode<T> {
let node = storage[entity, new: EntityNode(entity, modifiedAt: nil) { [registry] in
func nodeStore<T: Aggregate>(in node: EntityNode<T>? = nil, entity: T, modifiedAt: Stamp?) -> EntityNode<T> {
let node = node ?? storage[entity, new: EntityNode(entity, modifiedAt: nil) { [registry] in
registry.enqueueChange(for: $0)
}]

Expand Down Expand Up @@ -196,16 +196,12 @@ public class IdentityMap {

private func storeAlias<T>(content: T, key: AliasKey<T>, modifiedAt: Stamp?) {
let aliasNode = refAliases[safe: key]
let aliasContainer = AliasContainer(key: key, content: content)

do {
try aliasNode.updateEntity(AliasContainer(key: key, content: content), modifiedAt: modifiedAt)
_ = nodeStore(in: aliasNode, entity: aliasContainer, modifiedAt: modifiedAt)

registry.enqueueChange(for: aliasNode)
logger?.didRegisterAlias(key)
}
catch {

}
registry.enqueueChange(for: aliasNode)
logger?.didRegisterAlias(key)
}

private func transaction<T>(_ body: () -> T) -> T {
Expand Down
38 changes: 21 additions & 17 deletions Sources/CohesionKit/Storage/AliasContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,36 @@ struct AliasContainer<T>: Identifiable, Aggregate {
var content: T?
}

extension AliasContainer where T: Aggregate {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[.init(\.content)]
extension AliasContainer {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<Self>] {
if let self = self as? IdentifiableKeyPathsEraser {
return self.erasedEntitiesKeyPaths as! [PartialIdentifiableKeyPath<Self>]
}

if let self = self as? CollectionIdentifiableKeyPathsEraser {
return self.erasedEntitiesKeyPaths as! [PartialIdentifiableKeyPath<Self>]
}

return []
}
}

extension AliasContainer where T: Identifiable {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[.init(\.content)]
}
private protocol IdentifiableKeyPathsEraser {
var erasedEntitiesKeyPaths: [Any] { get }
}

extension AliasContainer where T: MutableCollection, T.Element: Aggregate, T.Index: Hashable {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[.init(\.content)]
extension AliasContainer: IdentifiableKeyPathsEraser where T: Identifiable {
var erasedEntitiesKeyPaths: [Any] {
[PartialIdentifiableKeyPath<Self>(\.content)]
}
}

extension AliasContainer where T: MutableCollection, T.Element: Identifiable, T.Index: Hashable {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[.init(\.content)]
}
private protocol CollectionIdentifiableKeyPathsEraser {
var erasedEntitiesKeyPaths: [Any] { get }
}

extension AliasContainer {
var nestedEntitiesKeyPaths: [PartialIdentifiableKeyPath<AliasContainer<T>>] {
[]
extension AliasContainer: CollectionIdentifiableKeyPathsEraser where T: MutableCollection, T.Element: Identifiable, T.Index: Hashable {
var erasedEntitiesKeyPaths: [Any] {
[PartialIdentifiableKeyPath<Self>(\.content)]
}
}

0 comments on commit 1838d81

Please sign in to comment.