Skip to content

Commit

Permalink
Update PublishingDestination Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilioPelaez committed Dec 31, 2024
1 parent 20f9972 commit 56b07f8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
27 changes: 23 additions & 4 deletions Sources/HierarchyResponder/Events/EventPublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,31 @@ import SwiftUI
`PublishingDestination` is used in the `.publisher` view modifier to specify
which publishers should receive the event.

The order is determined by the order they are declared in the view hierarchy.
The "level" of each value refers to the depth of the subscriber in the view
hierarchy, the first level being the subscribers declared closest to the
publisher.

Using container views (like Group, etc.) will create multiple subscribers at
the same level. For example, adding a `.subscribe` modifier to all the cells
in a list could cause all of them to share the last/deepest level of
subscribers in the hierarchy.

The order in which subscribers are called when publishing an event is not
guaranteed to be stable.
*/
public enum PublishingDestination {
case firstSubscriber
case allSubscribers
case lastSubscriber
/**
Publishers at the shallowest level of the hierarchy
*/
case firstLevel
/**
Publishers at all the levels of the hierarchy
*/
case allLevels
/**
Publishers at the deepest level of the hierarchy
*/
case lastLevel
}

/**
Expand Down
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 = .lastSubscriber, register: @escaping (EventPublisher<E>?) -> Void) -> some View {
func publisher<E: Event>(for event: E.Type, id: String, destination: PublishingDestination = .lastLevel, register: @escaping (EventPublisher<E>?) -> Void) -> some View {
modifier(EventPublisherModifier(id: id, destination: destination, register: register))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ struct EventPublisherModifier<E: Event>: ViewModifier {
guard !containers.isEmpty else { return register(nil) }
let publishers: [any EventPublisherProtocol]
switch destination {
case .firstSubscriber:
case .firstLevel:
publishers = containers.map(\.publisher)
case .allSubscribers:
case .allLevels:
let allContainers = containers.flatMap(\.allContainers)
publishers = allContainers.map(\.publisher)
case .lastSubscriber:
case .lastLevel:
let lastContainers = lastContainers(in: containers)
publishers = lastContainers.map(\.publisher)
}
Expand Down

0 comments on commit 56b07f8

Please sign in to comment.