Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use ObjectKey
Browse files Browse the repository at this point in the history
pjechris committed Aug 4, 2024
1 parent 97db6f5 commit 21668ef
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Sources/CohesionKit/Observer/ObserverRegistry.swift
Original file line number Diff line number Diff line change
@@ -9,6 +9,10 @@ extension ObjectKey {

self.init(key.hashValue)
}

init<T>(_ node: EntityNode<T>) {
self.init(of: T.self, id: node.hashValue)
}
}

/// Registers observers associated to an ``EntityNode``.
@@ -41,7 +45,7 @@ class ObserverRegistry {
}
}

return subscribeHandler(handler, for: node, key: node.hashValue)
return subscribeHandler(handler, for: node, key: ObjectKey(node))
}

/// Add an observer handler to multiple nodes.
@@ -63,7 +67,7 @@ class ObserverRegistry {
}
}

let subscriptions = nodes.map { node in subscribeHandler(handler, for: node, key: node.hashValue) }
let subscriptions = nodes.map { node in subscribeHandler(handler, for: node, key: ObjectKey(node)) }

return Subscription {
subscriptions.forEach { $0.unsubscribe() }
@@ -72,15 +76,15 @@ class ObserverRegistry {

/// Mark a node as changed. Observers won't be notified of the change until ``postChanges`` is called
func enqueueChange<T>(for node: EntityNode<T>) {
enqueueChange(for: node, key: node.hashValue)
enqueueChange(for: node, key: ObjectKey(node))
}

func enqueueChange<T>(for node: EntityNode<T>, key: ObjectKey) {
pendingChanges[key] = Weak(value: node)
}

func hasPendingChange<T>(for node: EntityNode<T>) -> Bool {
hasPendingChange(for: node.hashValue)
hasPendingChange(for: ObjectKey(node))
}

func hasPendingChange(for key: ObjectKey) -> Bool {

0 comments on commit 21668ef

Please sign in to comment.