diff --git a/Sources/HierarchyResponder/Events/View+EventPublishing.swift b/Sources/HierarchyResponder/Events/View+EventPublishing.swift index a8780a6..5e2de45 100644 --- a/Sources/HierarchyResponder/Events/View+EventPublishing.swift +++ b/Sources/HierarchyResponder/Events/View+EventPublishing.swift @@ -37,7 +37,7 @@ public extension View { once it's published. The order of the subscribers is determined by the order in which they were added to the view hierarchy. */ - func publisher(for event: E.Type, id: String, destination: PublishingDestination = .lastLevel, register: @escaping (EventPublisher?) -> Void) -> some View { + func publisher(for event: E.Type, id: String = UUID().uuidString, destination: PublishingDestination = .lastLevel, register: @escaping (EventPublisher?) -> Void) -> some View { modifier(EventPublisherModifier(id: id, destination: destination, register: register)) } @@ -47,7 +47,7 @@ public extension View { event is published, as long as the current subscriber matches the event's destination. */ - func subscribe(to event: E.Type, id: String, handler: @escaping (E) -> Void) -> some View { + func subscribe(to event: E.Type, id: String = UUID().uuidString, handler: @escaping (E) -> Void) -> some View { modifier(EventSubscriberModifier(id: id, eventType: event, handler: handler)) } } diff --git a/Sources/HierarchyResponder/Internal/Publishing/EventPublisherModifier.swift b/Sources/HierarchyResponder/Internal/Publishing/EventPublisherModifier.swift index 061389f..61dfe4a 100644 --- a/Sources/HierarchyResponder/Internal/Publishing/EventPublisherModifier.swift +++ b/Sources/HierarchyResponder/Internal/Publishing/EventPublisherModifier.swift @@ -26,7 +26,7 @@ struct EventPublisherModifier: ViewModifier { @State var containers: Set = [] - init(id: String = UUID().uuidString, destination: PublishingDestination, register: @escaping (EventPublisher?) -> Void) { + init(id: String, destination: PublishingDestination, register: @escaping (EventPublisher?) -> Void) { self.id = id self.destination = destination self.register = register @@ -34,8 +34,8 @@ struct EventPublisherModifier: ViewModifier { func body(content: Content) -> some View { content - .publisherRegistrar(for: E.self, childContainers: $containers) - .onChange(of: containers) { containers in + .publisherRegistrar(for: E.self, id: id, childContainers: $containers) + .onAppearAndChange(of: containers) { containers in updatePublisher(containers: containers, destination: destination) } .onChange(of: destination) { destination in diff --git a/Sources/HierarchyResponder/Internal/Publishing/EventSubscriberModifier.swift b/Sources/HierarchyResponder/Internal/Publishing/EventSubscriberModifier.swift index f6368de..801f116 100644 --- a/Sources/HierarchyResponder/Internal/Publishing/EventSubscriberModifier.swift +++ b/Sources/HierarchyResponder/Internal/Publishing/EventSubscriberModifier.swift @@ -34,7 +34,7 @@ struct EventSubscriberModifier: ViewModifier { func body(content: Content) -> some View { content - .publisherRegistrar(for: E.self, childContainers: $containers) + .publisherRegistrar(for: E.self, id: id, childContainers: $containers) .onAppearAndChange(of: registrars) { registrars in registerPublisher(registrars, containers: containers) } diff --git a/Sources/HierarchyResponder/Internal/Publishing/PublisherRegistrarModifier.swift b/Sources/HierarchyResponder/Internal/Publishing/PublisherRegistrarModifier.swift index 0d8df84..034694f 100644 --- a/Sources/HierarchyResponder/Internal/Publishing/PublisherRegistrarModifier.swift +++ b/Sources/HierarchyResponder/Internal/Publishing/PublisherRegistrarModifier.swift @@ -8,20 +8,23 @@ import SwiftUI extension View { - func publisherRegistrar(for event: E.Type, childContainers: Binding>) -> some View { - modifier(PublisherRegistrarModifier(event: event, childContainers: childContainers)) + func publisherRegistrar(for event: E.Type, id: String, childContainers: Binding>) -> some View { + modifier(PublisherRegistrarModifier(event: event, id: id, childContainers: childContainers)) } } struct PublisherRegistrarModifier: ViewModifier { @Environment(\.eventSubscriptionRegistrars) var registrars + let id: String @Binding var childContainers: Set - @State var registrar: EventSubscriptionRegistrar = .init { _, _ in } + @State var registrar: EventSubscriptionRegistrar - init(event: E.Type, childContainers: Binding>) { + init(event: E.Type, id: String, childContainers: Binding>) { + self.id = id self._childContainers = childContainers + self._registrar = .init(initialValue: .init(id: id) { _, _ in }) } var updatedRegistrars: [ObjectIdentifier: EventSubscriptionRegistrar] { @@ -37,7 +40,7 @@ struct PublisherRegistrarModifier: ViewModifier { } func createRegistrar() { - registrar = .init { id, container in + registrar = .init(id: id) { id, container in if let container { childContainers.update(with: container) } else if let container = childContainers.first(where: { $0.id == id }) {