-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
PresentationReducer.swift
730 lines (675 loc) · 26.1 KB
/
PresentationReducer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
@_spi(Reflection) import CasePaths
import Combine
import Perception
/// A property wrapper for state that can be presented.
///
/// Use this property wrapper for modeling a feature's domain that needs to present a child feature
/// using ``Reducer/ifLet(_:action:destination:fileID:line:)-4f2at``.
///
/// For example, if you have a `ChildFeature` reducer that encapsulates the logic and behavior for a
/// feature, then any feature that wants to present that feature will hold onto `ChildFeature.State`
/// like so:
///
/// ```swift
/// @Reducer
/// struct ParentFeature {
/// struct State {
/// @PresentationState var child: ChildFeature.State?
/// // ...
/// }
/// // ...
/// }
/// ```
///
/// For the most part your feature's logic can deal with `child` as a plain optional value, but
/// there are times you need to know that you are secretly dealing with `PresentationState`. For
/// example, when using the ``Reducer/ifLet(_:action:destination:fileID:line:)-4f2at`` reducer operator to
/// integrate the parent and child features together, you will construct a key path to the projected
/// value `\.$child`:
///
/// ```swift
/// @Reducer
/// struct ParentFeature {
/// // ...
/// var body: some ReducerOf<Self> {
/// Reduce { state, action in
/// // Core logic for parent feature
/// }
/// .ifLet(\.$child, action: \.child) {
/// ChildFeature()
/// }
/// }
/// }
/// ```
///
/// See the dedicated article on <doc:Navigation> for more information on the library's navigation
/// tools, and in particular see <doc:TreeBasedNavigation> for information on modeling navigation
/// using optionals and enums.
@dynamicMemberLookup
@propertyWrapper
public struct PresentationState<State> {
private class Storage: @unchecked Sendable {
var state: State?
init(state: State?) {
self.state = state
}
}
private var storage: Storage
@usableFromInline var presentedID: NavigationIDPath?
public init(wrappedValue: State?) {
self.storage = Storage(state: wrappedValue)
}
public var wrappedValue: State? {
get {
self._$observationRegistrar.access(self, keyPath: \.wrappedValue)
return self.storage.state
}
set {
func update() {
if !isKnownUniquelyReferenced(&self.storage) {
self.storage = Storage(state: newValue)
} else {
self.storage.state = newValue
}
}
if
!_$isIdentityEqual(self.storage.state, newValue)
{
self._$observationRegistrar.withMutation(of: self, keyPath: \.wrappedValue) {
update()
}
} else {
update()
}
}
}
public var projectedValue: Self {
get { self }
set { self = newValue }
}
public subscript<Case>(
dynamicMember keyPath: CaseKeyPath<State, Case>
) -> PresentationState<Case>
where State: CasePathable {
PresentationState<Case>(wrappedValue: self.wrappedValue.flatMap { $0[case: keyPath] })
}
public subscript<Member>(
dynamicMember keyPath: KeyPath<State, Member>
) -> PresentationState<Member> {
PresentationState<Member>(wrappedValue: self.wrappedValue?[keyPath: keyPath])
}
/// Accesses the value associated with the given case for reading and writing.
///
/// > Note: Accessing the wrong case will result in a runtime warning.
public subscript<Case>(case path: CaseKeyPath<State, Case>) -> Case?
where State: CasePathable {
_read { yield self[case: AnyCasePath(path)] }
_modify { yield &self[case: AnyCasePath(path)] }
}
@available(
iOS,
deprecated: 9999,
message:
"Use the version of this subscript with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@available(
macOS,
deprecated: 9999,
message:
"Use the version of this subscript with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@available(
tvOS,
deprecated: 9999,
message:
"Use the version of this subscript with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@available(
watchOS,
deprecated: 9999,
message:
"Use the version of this subscript with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
public subscript<Case>(case path: AnyCasePath<State, Case>) -> Case? {
_read { yield self.wrappedValue.flatMap(path.extract) }
_modify {
let root = self.wrappedValue
var value = root.flatMap(path.extract)
let success = value != nil
yield &value
guard success else {
var description: String?
if let root = root,
let metadata = EnumMetadata(State.self),
let caseName = metadata.caseName(forTag: metadata.tag(of: root))
{
description = caseName
}
runtimeWarn(
"""
Can't modify unrelated case\(description.map { " \($0.debugDescription)" } ?? "")
"""
)
return
}
self.wrappedValue = value.map(path.embed)
}
}
func sharesStorage(with other: Self) -> Bool {
self.storage === other.storage
}
private let _$observationRegistrar = ObservationRegistrarWrapper()
}
#if canImport(Observation)
@available(macOS 14, iOS 17, watchOS 10, tvOS 17, *)
extension PresentationState: Observable {}
#endif
extension PresentationState: Perceptible {}
extension PresentationState: Equatable where State: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.wrappedValue == rhs.wrappedValue
}
}
extension PresentationState: Hashable where State: Hashable {
public func hash(into hasher: inout Hasher) {
self.wrappedValue.hash(into: &hasher)
}
}
extension PresentationState: Sendable where State: Sendable {}
extension PresentationState: Decodable where State: Decodable {
public init(from decoder: Decoder) throws {
do {
self.init(wrappedValue: try decoder.singleValueContainer().decode(State.self))
} catch {
self.init(wrappedValue: try .init(from: decoder))
}
}
}
extension PresentationState: Encodable where State: Encodable {
public func encode(to encoder: Encoder) throws {
do {
var container = encoder.singleValueContainer()
try container.encode(self.wrappedValue)
} catch {
try self.wrappedValue.encode(to: encoder)
}
}
}
extension PresentationState: CustomReflectable {
public var customMirror: Mirror {
Mirror(reflecting: self.wrappedValue as Any)
}
}
/// A wrapper type for actions that can be presented.
///
/// Use this wrapper type for modeling a feature's domain that needs to present a child
/// feature using ``Reducer/ifLet(_:action:destination:fileID:line:)-4f2at``.
///
/// For example, if you have a `ChildFeature` reducer that encapsulates the logic and behavior
/// for a feature, then any feature that wants to present that feature will hold onto
/// `ChildFeature.Action` like so:
///
/// ```swift
/// @Reducer
/// struct ParentFeature {
/// // ...
/// enum Action {
/// case child(PresentationAction<ChildFeature.Action>)
/// // ...
/// }
/// // ...
/// }
/// ```
///
/// The ``PresentationAction`` enum has two cases that represent the two fundamental operations
/// you can do when presenting a child feature: ``PresentationAction/presented(_:)`` represents
/// an action happening _inside_ the child feature, and ``PresentationAction/dismiss`` represents
/// dismissing the child feature by `nil`-ing its state.
///
/// See the dedicated article on <doc:Navigation> for more information on the library's navigation
/// tools, and in particular see <doc:TreeBasedNavigation> for information on modeling navigation
/// using optionals and enums.
public enum PresentationAction<Action> {
/// An action sent to `nil` out the associated presentation state.
case dismiss
/// An action sent to the associated, non-`nil` presentation state.
indirect case presented(Action)
}
extension PresentationAction: CasePathable {
public static var allCasePaths: AllCasePaths {
AllCasePaths()
}
@dynamicMemberLookup
public struct AllCasePaths {
public var dismiss: AnyCasePath<PresentationAction, Void> {
AnyCasePath(
embed: { .dismiss },
extract: {
guard case .dismiss = $0 else { return nil }
return ()
}
)
}
public var presented: AnyCasePath<PresentationAction, Action> {
AnyCasePath(
embed: PresentationAction.presented,
extract: {
guard case let .presented(value) = $0 else { return nil }
return value
}
)
}
public subscript<AppendedAction>(
dynamicMember keyPath: CaseKeyPath<Action, AppendedAction>
) -> AnyCasePath<PresentationAction, AppendedAction>
where Action: CasePathable {
AnyCasePath<PresentationAction, AppendedAction>(
embed: { .presented(keyPath($0)) },
extract: {
guard case let .presented(action) = $0 else { return nil }
return action[case: keyPath]
}
)
}
@_disfavoredOverload
public subscript<AppendedAction>(
dynamicMember keyPath: CaseKeyPath<Action, AppendedAction>
) -> AnyCasePath<PresentationAction, PresentationAction<AppendedAction>>
where Action: CasePathable {
AnyCasePath<PresentationAction, PresentationAction<AppendedAction>>(
embed: {
switch $0 {
case .dismiss:
return .dismiss
case let .presented(action):
return .presented(keyPath(action))
}
},
extract: {
switch $0 {
case .dismiss:
return .dismiss
case let .presented(action):
return action[case: keyPath].map { .presented($0) }
}
}
)
}
}
}
extension PresentationAction: Equatable where Action: Equatable {}
extension PresentationAction: Hashable where Action: Hashable {}
extension PresentationAction: Sendable where Action: Sendable {}
extension PresentationAction: Decodable where Action: Decodable {}
extension PresentationAction: Encodable where Action: Encodable {}
extension Reducer {
/// Embeds a child reducer in a parent domain that works on an optional property of parent state.
///
/// This version of `ifLet` requires the usage of ``PresentationState`` and ``PresentationAction``
/// in your feature's domain.
///
/// For example, if a parent feature holds onto a piece of optional child state, then it can
/// perform its core logic _and_ the child's logic by using the `ifLet` operator:
///
/// ```swift
/// @Reducer
/// struct Parent {
/// struct State {
/// @PresentationState var child: Child.State?
/// // ...
/// }
/// enum Action {
/// case child(PresentationAction<Child.Action>)
/// // ...
/// }
///
/// var body: some ReducerOf<Self> {
/// Reduce { state, action in
/// // Core logic for parent feature
/// }
/// .ifLet(\.$child, action: \.child) {
/// Child()
/// }
/// }
/// }
/// ```
///
/// The `ifLet` operator does a number of things to make integrating parent and child features
/// ergonomic and enforce correctness:
///
/// * It forces a specific order of operations for the child and parent features:
/// * When a ``PresentationAction/dismiss`` action is sent, it runs the parent feature
/// before the child state is `nil`'d out. This gives the parent feature an opportunity to
/// inspect the child state one last time before the state is cleared.
/// * When a ``PresentationAction/presented(_:)`` action is sent it runs the
/// child first, and then the parent. If the order was reversed, then it would be possible
/// for the parent feature to `nil` out the child state, in which case the child feature
/// would not be able to react to that action. That can cause subtle bugs.
///
/// * It automatically cancels all child effects when it detects the child's state is `nil`'d
/// out.
///
/// * Automatically `nil`s out child state when an action is sent for alerts and confirmation
/// dialogs.
///
/// * It gives the child feature access to the ``DismissEffect`` dependency, which allows the
/// child feature to dismiss itself without communicating with the parent.
///
/// - Parameters:
/// - toPresentationState: A writable key path from parent state to a property containing child
/// presentation state.
/// - toPresentationAction: A case path from parent action to a case containing child actions.
/// - destination: A reducer that will be invoked with child actions against presented child
/// state.
/// - Returns: A reducer that combines the child reducer with the parent reducer.
@warn_unqualified_access
@inlinable
public func ifLet<DestinationState, DestinationAction, Destination: Reducer>(
_ toPresentationState: WritableKeyPath<State, PresentationState<DestinationState>>,
action toPresentationAction: CaseKeyPath<Action, PresentationAction<DestinationAction>>,
@ReducerBuilder<DestinationState, DestinationAction> destination: () -> Destination,
fileID: StaticString = #fileID,
line: UInt = #line
) -> _PresentationReducer<Self, Destination>
where Destination.State == DestinationState, Destination.Action == DestinationAction {
_PresentationReducer(
base: self,
toPresentationState: toPresentationState,
toPresentationAction: AnyCasePath(toPresentationAction),
destination: destination(),
fileID: fileID,
line: line
)
}
/// A special overload of ``Reducer/ifLet(_:action:destination:fileID:line:)-4f2at`` for alerts
/// and confirmation dialogs that does not require a child reducer.
@warn_unqualified_access
@inlinable
public func ifLet<DestinationState: _EphemeralState, DestinationAction>(
_ toPresentationState: WritableKeyPath<State, PresentationState<DestinationState>>,
action toPresentationAction: CaseKeyPath<Action, PresentationAction<DestinationAction>>,
fileID: StaticString = #fileID,
line: UInt = #line
) -> _PresentationReducer<Self, EmptyReducer<DestinationState, DestinationAction>> {
self.ifLet(
toPresentationState,
action: toPresentationAction,
destination: {},
fileID: fileID,
line: line
)
}
@available(
iOS,
deprecated: 9999,
message:
"Use the version of this operator with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@available(
macOS,
deprecated: 9999,
message:
"Use the version of this operator with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@available(
tvOS,
deprecated: 9999,
message:
"Use the version of this operator with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@available(
watchOS,
deprecated: 9999,
message:
"Use the version of this operator with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@warn_unqualified_access
@inlinable
public func ifLet<DestinationState, DestinationAction, Destination: Reducer>(
_ toPresentationState: WritableKeyPath<State, PresentationState<DestinationState>>,
action toPresentationAction: AnyCasePath<Action, PresentationAction<DestinationAction>>,
@ReducerBuilder<DestinationState, DestinationAction> destination: () -> Destination,
fileID: StaticString = #fileID,
line: UInt = #line
) -> _PresentationReducer<Self, Destination>
where Destination.State == DestinationState, Destination.Action == DestinationAction {
_PresentationReducer(
base: self,
toPresentationState: toPresentationState,
toPresentationAction: toPresentationAction,
destination: destination(),
fileID: fileID,
line: line
)
}
@available(
iOS,
deprecated: 9999,
message:
"Use the version of this operator with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@available(
macOS,
deprecated: 9999,
message:
"Use the version of this operator with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@available(
tvOS,
deprecated: 9999,
message:
"Use the version of this operator with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@available(
watchOS,
deprecated: 9999,
message:
"Use the version of this operator with case key paths, instead. See the following migration guide for more information:\n\nhttps://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.4#Using-case-key-paths"
)
@warn_unqualified_access
@inlinable
public func ifLet<DestinationState: _EphemeralState, DestinationAction>(
_ toPresentationState: WritableKeyPath<State, PresentationState<DestinationState>>,
action toPresentationAction: AnyCasePath<Action, PresentationAction<DestinationAction>>,
fileID: StaticString = #fileID,
line: UInt = #line
) -> _PresentationReducer<Self, EmptyReducer<DestinationState, DestinationAction>> {
self.ifLet(
toPresentationState,
action: toPresentationAction,
destination: {},
fileID: fileID,
line: line
)
}
}
public struct _PresentationReducer<Base: Reducer, Destination: Reducer>: Reducer {
@usableFromInline let base: Base
@usableFromInline let toPresentationState:
WritableKeyPath<Base.State, PresentationState<Destination.State>>
@usableFromInline let toPresentationAction:
AnyCasePath<Base.Action, PresentationAction<Destination.Action>>
@usableFromInline let destination: Destination
@usableFromInline let fileID: StaticString
@usableFromInline let line: UInt
@Dependency(\.navigationIDPath) var navigationIDPath
@usableFromInline
init(
base: Base,
toPresentationState: WritableKeyPath<Base.State, PresentationState<Destination.State>>,
toPresentationAction: AnyCasePath<Base.Action, PresentationAction<Destination.Action>>,
destination: Destination,
fileID: StaticString,
line: UInt
) {
self.base = base
self.toPresentationState = toPresentationState
self.toPresentationAction = toPresentationAction
self.destination = destination
self.fileID = fileID
self.line = line
}
public func reduce(into state: inout Base.State, action: Base.Action) -> Effect<Base.Action> {
// TODO: don't hold onto this. just compute the things we need from it
let initialPresentationState = state[keyPath: self.toPresentationState]
let presentationAction = self.toPresentationAction.extract(from: action)
let destinationEffects: Effect<Base.Action>
let baseEffects: Effect<Base.Action>
switch (initialPresentationState.wrappedValue, presentationAction) {
case let (.some(destinationState), .some(.dismiss)):
destinationEffects = .none
baseEffects = self.base.reduce(into: &state, action: action)
if self.navigationIDPath(for: destinationState)
== state[keyPath: self.toPresentationState].wrappedValue.map(self.navigationIDPath(for:))
{
state[keyPath: self.toPresentationState].wrappedValue = nil
}
case let (.some(destinationState), .some(.presented(destinationAction))):
let destinationNavigationIDPath = self.navigationIDPath(for: destinationState)
destinationEffects = self.destination
.dependency(
\.dismiss,
DismissEffect { @MainActor in
Task._cancel(id: PresentationDismissID(), navigationID: destinationNavigationIDPath)
}
)
.dependency(\.navigationIDPath, destinationNavigationIDPath)
.reduce(
into: &state[keyPath: self.toPresentationState].wrappedValue!, action: destinationAction
)
.map { self.toPresentationAction.embed(.presented($0)) }
._cancellable(navigationIDPath: destinationNavigationIDPath)
baseEffects = self.base.reduce(into: &state, action: action)
if let ephemeralType = ephemeralType(of: destinationState),
destinationNavigationIDPath
== state[keyPath: self.toPresentationState].wrappedValue.map(self.navigationIDPath(for:)),
ephemeralType.canSend(destinationAction)
{
state[keyPath: self.toPresentationState].wrappedValue = nil
}
case (.none, .none), (.some, .none):
destinationEffects = .none
baseEffects = self.base.reduce(into: &state, action: action)
case (.none, .some):
runtimeWarn(
"""
An "ifLet" at "\(self.fileID):\(self.line)" received a presentation action when \
destination state was absent. …
Action:
\(debugCaseOutput(action))
This is generally considered an application logic error, and can happen for a few \
reasons:
• A parent reducer set destination state to "nil" before this reducer ran. This reducer \
must run before any other reducer sets destination state to "nil". This ensures that \
destination reducers can handle their actions while their state is still present.
• This action was sent to the store while destination state was "nil". Make sure that \
actions for this reducer can only be sent from a view store when state is present, or \
from effects that start from this reducer. In SwiftUI applications, use a Composable \
Architecture view modifier like "sheet(store:…)".
"""
)
destinationEffects = .none
baseEffects = self.base.reduce(into: &state, action: action)
}
let presentationIdentityChanged =
initialPresentationState.presentedID
!= state[keyPath: self.toPresentationState].wrappedValue.map(self.navigationIDPath(for:))
let dismissEffects: Effect<Base.Action>
if presentationIdentityChanged,
let presentedPath = initialPresentationState.presentedID,
initialPresentationState.wrappedValue.map({
self.navigationIDPath(for: $0) == presentedPath && !isEphemeral($0)
})
?? true
{
dismissEffects = ._cancel(navigationID: presentedPath)
} else {
dismissEffects = .none
}
if presentationIdentityChanged, state[keyPath: self.toPresentationState].wrappedValue == nil {
state[keyPath: self.toPresentationState].presentedID = nil
}
let presentEffects: Effect<Base.Action>
if presentationIdentityChanged || state[keyPath: self.toPresentationState].presentedID == nil,
let presentationState = state[keyPath: self.toPresentationState].wrappedValue,
!isEphemeral(presentationState)
{
let presentationDestinationID = self.navigationIDPath(for: presentationState)
state[keyPath: self.toPresentationState].presentedID = presentationDestinationID
presentEffects = .concatenate(
.publisher { Empty(completeImmediately: false) }
._cancellable(id: PresentationDismissID(), navigationIDPath: presentationDestinationID),
.publisher { Just(self.toPresentationAction.embed(.dismiss)) }
)
._cancellable(navigationIDPath: presentationDestinationID)
._cancellable(id: OnFirstAppearID(), navigationIDPath: .init())
} else {
presentEffects = .none
}
return .merge(
destinationEffects,
baseEffects,
dismissEffects,
presentEffects
)
}
func navigationIDPath(for state: Destination.State) -> NavigationIDPath {
self.navigationIDPath.appending(
NavigationID(
base: state,
keyPath: self.toPresentationState.appending(path: \.wrappedValue)
)
)
}
}
@usableFromInline
struct PresentationDismissID: Hashable {
@usableFromInline init() {}
}
@usableFromInline
struct OnFirstAppearID: Hashable {
@usableFromInline init() {}
}
public struct _PresentedID: Hashable {
@inlinable
public init() {
self.init(internal: ())
}
@usableFromInline
init(internal: Void) {}
}
extension Task where Success == Never, Failure == Never {
internal static func _cancel<ID: Hashable>(
id: ID,
navigationID: NavigationIDPath
) {
withDependencies {
$0.navigationIDPath = navigationID
} operation: {
Task.cancel(id: id)
}
}
}
extension Effect {
internal func _cancellable<ID: Hashable>(
id: ID = _PresentedID(),
navigationIDPath: NavigationIDPath,
cancelInFlight: Bool = false
) -> Self {
withDependencies {
$0.navigationIDPath = navigationIDPath
} operation: {
self.cancellable(id: id, cancelInFlight: cancelInFlight)
}
}
internal static func _cancel<ID: Hashable>(
id: ID = _PresentedID(),
navigationID: NavigationIDPath
) -> Self {
withDependencies {
$0.navigationIDPath = navigationID
} operation: {
.cancel(id: id)
}
}
}