Skip to content

Commit 8395e64

Browse files
committed
ComponentCallbacks: Don't recreate *WithInitial publishers on every access
1 parent 5b5d66c commit 8395e64

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

Sources/allonet2/PlaceContents+Changes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extension PlaceState
1010
/// Find the historical state at the given revision, and apply the given changeset to it, and use it as the freshly updated current state.
1111
internal func applyChangeSet(_ changeSet: PlaceChangeSet) -> Bool
1212
{
13+
logger.trace("Applying changeSet \(changeSet.fromRevision)->\(changeSet.toRevision) with \(changeSet.changes.count) changes")
1314
guard let old = getHistory(at: changeSet.fromRevision) else { return false }
1415

1516
guard let new = old.applyChangeSet(changeSet) else {

Sources/allonet2/PlaceContents.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,20 @@ public struct PlaceObservers
259259
}
260260

261261
@MainActor
262-
public struct ComponentCallbacks<T: Component> : AnyComponentCallbacksProtocol
262+
public class ComponentCallbacks<T: Component> : AnyComponentCallbacksProtocol
263263
{
264264
/// An entity has received a new component of this type
265265
public var added: AnyPublisher<(EntityID, T), Never> { addedSubject.eraseToAnyPublisher() }
266-
public var addedWithInitial: AnyPublisher<(EntityID, T), Never> {
266+
public lazy var addedWithInitial: AnyPublisher<(EntityID, T), Never> = {
267267
let initial = state.current.components[T.self].map { ($0.key, $0.value) }
268268
return added.prepend(initial).eraseToAnyPublisher()
269-
}
269+
}()
270270
/// An entity has received an update to a component with the following contents. NOTE: This is also called immediately after `added`, so you can put all your "react to property changes regardless of add or update" in one place.
271271
public var updated: AnyPublisher<(EntityID, T), Never> { updatedSubject.eraseToAnyPublisher() }
272-
public var updatedWithInitial: AnyPublisher<(EntityID, T), Never> {
272+
public lazy var updatedWithInitial: AnyPublisher<(EntityID, T), Never> = {
273273
let initial = state.current.components[T.self].map { ($0.key, $0.value) }
274274
return updated.prepend(initial).eraseToAnyPublisher()
275-
}
275+
}()
276276
/// A component has been removed from an entity.
277277
public var removed: AnyPublisher<(EntityData, T), Never> { removedSubject.eraseToAnyPublisher() }
278278

0 commit comments

Comments
 (0)