Skip to content

Commit

Permalink
Improve IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilioPelaez committed Jan 2, 2025
1 parent a908199 commit d5edd14
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Sources/HierarchyResponder/Events/View+EventPublishing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<E: Event>(for event: E.Type, id: String, destination: PublishingDestination = .lastLevel, register: @escaping (EventPublisher<E>?) -> Void) -> some View {
func publisher<E: Event>(for event: E.Type, id: String = UUID().uuidString, destination: PublishingDestination = .lastLevel, register: @escaping (EventPublisher<E>?) -> Void) -> some View {
modifier(EventPublisherModifier(id: id, destination: destination, register: register))
}

Expand All @@ -47,7 +47,7 @@ public extension View {
event is published, as long as the current subscriber matches the event's
destination.
*/
func subscribe<E: Event>(to event: E.Type, id: String, handler: @escaping (E) -> Void) -> some View {
func subscribe<E: Event>(to event: E.Type, id: String = UUID().uuidString, handler: @escaping (E) -> Void) -> some View {
modifier(EventSubscriberModifier(id: id, eventType: event, handler: handler))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ struct EventPublisherModifier<E: Event>: ViewModifier {

@State var containers: Set<PublishersContainer> = []

init(id: String = UUID().uuidString, destination: PublishingDestination, register: @escaping (EventPublisher<E>?) -> Void) {
init(id: String, destination: PublishingDestination, register: @escaping (EventPublisher<E>?) -> Void) {
self.id = id
self.destination = destination
self.register = register
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct EventSubscriberModifier<E: Event>: 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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
import SwiftUI

extension View {
func publisherRegistrar<E: Event>(for event: E.Type, childContainers: Binding<Set<PublishersContainer>>) -> some View {
modifier(PublisherRegistrarModifier(event: event, childContainers: childContainers))
func publisherRegistrar<E: Event>(for event: E.Type, id: String, childContainers: Binding<Set<PublishersContainer>>) -> some View {
modifier(PublisherRegistrarModifier(event: event, id: id, childContainers: childContainers))
}
}

struct PublisherRegistrarModifier<E: Event>: ViewModifier {
@Environment(\.eventSubscriptionRegistrars) var registrars

let id: String
@Binding var childContainers: Set<PublishersContainer>

@State var registrar: EventSubscriptionRegistrar = .init { _, _ in }
@State var registrar: EventSubscriptionRegistrar

init(event: E.Type, childContainers: Binding<Set<PublishersContainer>>) {
init(event: E.Type, id: String, childContainers: Binding<Set<PublishersContainer>>) {
self.id = id
self._childContainers = childContainers
self._registrar = .init(initialValue: .init(id: id) { _, _ in })
}

var updatedRegistrars: [ObjectIdentifier: EventSubscriptionRegistrar] {
Expand All @@ -37,7 +40,7 @@ struct PublisherRegistrarModifier<E: Event>: 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 }) {
Expand Down

0 comments on commit d5edd14

Please sign in to comment.