-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Store.swift
782 lines (731 loc) · 27.9 KB
/
Store.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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
import Combine
import Foundation
/// A store represents the runtime that powers the application. It is the object that you will pass
/// around to views that need to interact with the application.
///
/// You will typically construct a single one of these at the root of your application:
///
/// ```swift
/// @main
/// struct MyApp: App {
/// var body: some Scene {
/// WindowGroup {
/// RootView(
/// store: Store(
/// initialState: AppReducer.State(),
/// reducer: AppReducer()
/// )
/// )
/// }
/// }
/// }
/// ```
///
/// …and then use the ``scope(state:action:)`` method to derive more focused stores that can be
/// passed to subviews.
///
/// ### Scoping
///
/// The most important operation defined on ``Store`` is the ``scope(state:action:)`` method, which
/// allows you to transform a store into one that deals with child state and actions. This is
/// necessary for passing stores to subviews that only care about a small portion of the entire
/// application's domain.
///
/// For example, if an application has a tab view at its root with tabs for activity, search, and
/// profile, then we can model the domain like this:
///
/// ```swift
/// struct State {
/// var activity: Activity.State
/// var profile: Profile.State
/// var search: Search.State
/// }
///
/// enum Action {
/// case activity(Activity.Action)
/// case profile(Profile.Action)
/// case search(Search.Action)
/// }
/// ```
///
/// We can construct a view for each of these domains by applying ``scope(state:action:)`` to a
/// store that holds onto the full app domain in order to transform it into a store for each
/// sub-domain:
///
/// ```swift
/// struct AppView: View {
/// let store: StoreOf<AppReducer>
///
/// var body: some View {
/// TabView {
/// ActivityView(store: self.store.scope(state: \.activity, action: App.Action.activity))
/// .tabItem { Text("Activity") }
///
/// SearchView(store: self.store.scope(state: \.search, action: App.Action.search))
/// .tabItem { Text("Search") }
///
/// ProfileView(store: self.store.scope(state: \.profile, action: App.Action.profile))
/// .tabItem { Text("Profile") }
/// }
/// }
/// }
/// ```
///
/// ### Thread safety
///
/// The `Store` class is not thread-safe, and so all interactions with an instance of ``Store``
/// (including all of its scopes and derived ``ViewStore``s) must be done on the same thread the
/// store was created on. Further, if the store is powering a SwiftUI or UIKit view, as is
/// customary, then all interactions must be done on the _main_ thread.
///
/// The reason stores are not thread-safe is due to the fact that when an action is sent to a store,
/// a reducer is run on the current state, and this process cannot be done from multiple threads.
/// It is possible to make this process thread-safe by introducing locks or queues, but this
/// introduces new complications:
///
/// * If done simply with `DispatchQueue.main.async` you will incur a thread hop even when you are
/// already on the main thread. This can lead to unexpected behavior in UIKit and SwiftUI, where
/// sometimes you are required to do work synchronously, such as in animation blocks.
///
/// * It is possible to create a scheduler that performs its work immediately when on the main
/// thread and otherwise uses `DispatchQueue.main.async` (_e.g._, see Combine Schedulers'
/// [UIScheduler][uischeduler]).
///
/// This introduces a lot more complexity, and should probably not be adopted without having a very
/// good reason.
///
/// This is why we require all actions be sent from the same thread. This requirement is in the same
/// spirit of how `URLSession` and other Apple APIs are designed. Those APIs tend to deliver their
/// outputs on whatever thread is most convenient for them, and then it is your responsibility to
/// dispatch back to the main queue if that's what you need. The Composable Architecture makes you
/// responsible for making sure to send actions on the main thread. If you are using an effect that
/// may deliver its output on a non-main thread, you must explicitly perform `.receive(on:)` in
/// order to force it back on the main thread.
///
/// This approach makes the fewest number of assumptions about how effects are created and
/// transformed, and prevents unnecessary thread hops and re-dispatching. It also provides some
/// testing benefits. If your effects are not responsible for their own scheduling, then in tests
/// all of the effects would run synchronously and immediately. You would not be able to test how
/// multiple in-flight effects interleave with each other and affect the state of your application.
/// However, by leaving scheduling out of the ``Store`` we get to test these aspects of our effects
/// if we so desire, or we can ignore if we prefer. We have that flexibility.
///
/// [uischeduler]: https://github.com/pointfreeco/combine-schedulers/blob/main/Sources/CombineSchedulers/UIScheduler.swift
///
/// #### Thread safety checks
///
/// The store performs some basic thread safety checks in order to help catch mistakes. Stores
/// constructed via the initializer ``init(initialState:reducer:prepareDependencies:)`` are assumed
/// to run only on the main thread, and so a check is executed immediately to make sure that is the
/// case. Further, all actions sent to the store and all scopes (see ``scope(state:action:)``) of
/// the store are also checked to make sure that work is performed on the main thread.
public final class Store<State, Action> {
private var bufferedActions: [Action] = []
@_spi(Internals) public var effectCancellables: [UUID: AnyCancellable] = [:]
private var isSending = false
var parentCancellable: AnyCancellable?
#if swift(>=5.7)
private let reducer: any ReducerProtocol<State, Action>
#else
private let reducer: (inout State, Action) -> EffectTask<Action>
fileprivate var scope: AnyStoreScope?
#endif
@_spi(Internals) public var state: CurrentValueSubject<State, Never>
#if DEBUG
private let mainThreadChecksEnabled: Bool
#endif
/// Initializes a store from an initial state and a reducer.
///
/// - Parameters:
/// - initialState: The state to start the application in.
/// - reducer: The reducer that powers the business logic of the application.
/// - prepareDependencies: A closure that can be used to override dependencies that will be accessed
/// by the reducer.
public convenience init<R: ReducerProtocol>(
initialState: @autoclosure () -> R.State,
reducer: R,
prepareDependencies: ((inout DependencyValues) -> Void)? = nil
) where R.State == State, R.Action == Action {
if let prepareDependencies = prepareDependencies {
self.init(
initialState: withDependencies(prepareDependencies) { initialState() },
reducer: reducer.transformDependency(\.self, transform: prepareDependencies),
mainThreadChecksEnabled: true
)
} else {
self.init(
initialState: initialState(),
reducer: reducer,
mainThreadChecksEnabled: true
)
}
}
/// Scopes the store to one that exposes child state and actions.
///
/// This can be useful for deriving new stores to hand to child views in an application. For
/// example:
///
/// ```swift
/// // Application state made from child states.
/// struct State { var login: LoginState, ... }
/// enum Action { case login(LoginAction), ... }
///
/// // A store that runs the entire application.
/// let store = Store(
/// initialState: AppReducer.State(),
/// reducer: AppReducer()
/// )
///
/// // Construct a login view by scoping the store to one that works with only login domain.
/// LoginView(
/// store: store.scope(
/// state: \.login,
/// action: AppReducer.Action.login
/// )
/// )
/// ```
///
/// Scoping in this fashion allows you to better modularize your application. In this case,
/// `LoginView` could be extracted to a module that has no access to `App.State` or `App.Action`.
///
/// Scoping also gives a view the opportunity to focus on just the state and actions it cares
/// about, even if its feature domain is larger.
///
/// For example, the above login domain could model a two screen login flow: a login form followed
/// by a two-factor authentication screen. The second screen's domain might be nested in the
/// first:
///
/// ```swift
/// struct LoginState: Equatable {
/// var email = ""
/// var password = ""
/// var twoFactorAuth: TwoFactorAuthState?
/// }
///
/// enum LoginAction: Equatable {
/// case emailChanged(String)
/// case loginButtonTapped
/// case loginResponse(Result<TwoFactorAuthState, LoginError>)
/// case passwordChanged(String)
/// case twoFactorAuth(TwoFactorAuthAction)
/// }
/// ```
///
/// The login view holds onto a store of this domain:
///
/// ```swift
/// struct LoginView: View {
/// let store: Store<LoginState, LoginAction>
///
/// var body: some View { ... }
/// }
/// ```
///
/// If its body were to use a view store of the same domain, this would introduce a number of
/// problems:
///
/// * The login view would be able to read from `twoFactorAuth` state. This state is only intended
/// to be read from the two-factor auth screen.
///
/// * Even worse, changes to `twoFactorAuth` state would now cause SwiftUI to recompute
/// `LoginView`'s body unnecessarily.
///
/// * The login view would be able to send `twoFactorAuth` actions. These actions are only
/// intended to be sent from the two-factor auth screen (and reducer).
///
/// * The login view would be able to send non user-facing login actions, like `loginResponse`.
/// These actions are only intended to be used in the login reducer to feed the results of
/// effects back into the store.
///
/// To avoid these issues, one can introduce a view-specific domain that slices off the subset of
/// state and actions that a view cares about:
///
/// ```swift
/// extension LoginView {
/// struct State: Equatable {
/// var email: String
/// var password: String
/// }
///
/// enum Action: Equatable {
/// case emailChanged(String)
/// case loginButtonTapped
/// case passwordChanged(String)
/// }
/// }
/// ```
///
/// One can also introduce a couple helpers that transform feature state into view state and
/// transform view actions into feature actions.
///
/// ```swift
/// extension LoginState {
/// var view: LoginView.State {
/// .init(email: self.email, password: self.password)
/// }
/// }
///
/// extension LoginView.Action {
/// var feature: LoginAction {
/// switch self {
/// case let .emailChanged(email)
/// return .emailChanged(email)
/// case .loginButtonTapped:
/// return .loginButtonTapped
/// case let .passwordChanged(password)
/// return .passwordChanged(password)
/// }
/// }
/// }
/// ```
///
/// With these helpers defined, `LoginView` can now scope its store's feature domain into its view
/// domain:
///
/// ```swift
/// var body: some View {
/// WithViewStore(
/// self.store, observe: \.view, send: \.feature
/// ) { viewStore in
/// ...
/// }
/// }
/// ```
///
/// This view store is now incapable of reading any state but view state (and will not recompute
/// when non-view state changes), and is incapable of sending any actions but view actions.
///
/// - Parameters:
/// - toChildState: A function that transforms `State` into `ChildState`.
/// - fromChildAction: A function that transforms `ChildAction` into `Action`.
/// - Returns: A new store with its domain (state and action) transformed.
public func scope<ChildState, ChildAction>(
state toChildState: @escaping (State) -> ChildState,
action fromChildAction: @escaping (ChildAction) -> Action
) -> Store<ChildState, ChildAction> {
self.threadCheck(status: .scope)
#if swift(>=5.7)
return self.reducer.rescope(self, state: toChildState, action: { fromChildAction($1) })
#else
return (self.scope ?? StoreScope(root: self))
.rescope(self, state: toChildState, action: { fromChildAction($1) })
#endif
}
/// Scopes the store to one that exposes child state.
///
/// A version of ``scope(state:action:)`` that leaves the action type unchanged.
///
/// - Parameter toChildState: A function that transforms `State` into `ChildState`.
/// - Returns: A new store with its domain (state and action) transformed.
public func scope<ChildState>(
state toChildState: @escaping (State) -> ChildState
) -> Store<ChildState, Action> {
self.scope(state: toChildState, action: { $0 })
}
func filter(
_ isSent: @escaping (State, Action) -> Bool
) -> Store<State, Action> {
self.threadCheck(status: .scope)
#if swift(>=5.7)
return self.reducer.rescope(self, state: { $0 }, action: { isSent($0, $1) ? $1 : nil })
#else
return (self.scope ?? StoreScope(root: self))
.rescope(self, state: { $0 }, action: { isSent($0, $1) ? $1 : nil })
#endif
}
@_spi(Internals) public func send(
_ action: Action,
originatingFrom originatingAction: Action? = nil
) -> Task<Void, Never>? {
self.threadCheck(status: .send(action, originatingAction: originatingAction))
self.bufferedActions.append(action)
guard !self.isSending else { return nil }
self.isSending = true
var currentState = self.state.value
let tasks = Box<[Task<Void, Never>]>(wrappedValue: [])
defer {
withExtendedLifetime(self.bufferedActions) {
self.bufferedActions.removeAll()
}
self.state.value = currentState
self.isSending = false
if !self.bufferedActions.isEmpty {
if let task = self.send(
self.bufferedActions.removeLast(), originatingFrom: originatingAction
) {
tasks.wrappedValue.append(task)
}
}
}
var index = self.bufferedActions.startIndex
while index < self.bufferedActions.endIndex {
defer { index += 1 }
let action = self.bufferedActions[index]
#if swift(>=5.7)
let effect = self.reducer.reduce(into: ¤tState, action: action)
#else
let effect = self.reducer(¤tState, action)
#endif
switch effect.operation {
case .none:
break
case let .publisher(publisher):
var didComplete = false
let boxedTask = Box<Task<Void, Never>?>(wrappedValue: nil)
let uuid = UUID()
let effectCancellable = withEscapedDependencies { continuation in
publisher
.handleEvents(
receiveCancel: { [weak self] in
self?.threadCheck(status: .effectCompletion(action))
self?.effectCancellables[uuid] = nil
}
)
.sink(
receiveCompletion: { [weak self] _ in
self?.threadCheck(status: .effectCompletion(action))
boxedTask.wrappedValue?.cancel()
didComplete = true
self?.effectCancellables[uuid] = nil
},
receiveValue: { [weak self] effectAction in
guard let self = self else { return }
if let task = continuation.yield({
self.send(effectAction, originatingFrom: action)
}) {
tasks.wrappedValue.append(task)
}
}
)
}
if !didComplete {
let task = Task<Void, Never> { @MainActor in
for await _ in AsyncStream<Void>.never {}
effectCancellable.cancel()
}
boxedTask.wrappedValue = task
tasks.wrappedValue.append(task)
self.effectCancellables[uuid] = effectCancellable
}
case let .run(priority, operation):
withEscapedDependencies { continuation in
tasks.wrappedValue.append(
Task(priority: priority) { @MainActor in
#if DEBUG
var isCompleted = false
defer { isCompleted = true }
#endif
await operation(
Send { effectAction in
#if DEBUG
if isCompleted {
runtimeWarn(
"""
An action was sent from a completed effect:
Action:
\(debugCaseOutput(effectAction))
Effect returned from:
\(debugCaseOutput(action))
Avoid sending actions using the 'send' argument from 'EffectTask.run' after \
the effect has completed. This can happen if you escape the 'send' argument in \
an unstructured context.
To fix this, make sure that your 'run' closure does not return until you're \
done calling 'send'.
"""
)
}
#endif
if let task = continuation.yield({
self.send(effectAction, originatingFrom: action)
}) {
tasks.wrappedValue.append(task)
}
}
)
}
)
}
}
}
guard !tasks.wrappedValue.isEmpty else { return nil }
return Task {
await withTaskCancellationHandler {
var index = tasks.wrappedValue.startIndex
while index < tasks.wrappedValue.endIndex {
defer { index += 1 }
await tasks.wrappedValue[index].value
}
} onCancel: {
var index = tasks.wrappedValue.startIndex
while index < tasks.wrappedValue.endIndex {
defer { index += 1 }
tasks.wrappedValue[index].cancel()
}
}
}
}
/// Returns a "stateless" store by erasing state to `Void`.
public var stateless: Store<Void, Action> {
self.scope(state: { _ in () })
}
/// Returns an "actionless" store by erasing action to `Never`.
public var actionless: Store<State, Never> {
func absurd<A>(_ never: Never) -> A {}
return self.scope(state: { $0 }, action: absurd)
}
private enum ThreadCheckStatus {
case effectCompletion(Action)
case `init`
case scope
case send(Action, originatingAction: Action?)
}
@inline(__always)
private func threadCheck(status: ThreadCheckStatus) {
#if DEBUG
guard self.mainThreadChecksEnabled && !Thread.isMainThread
else { return }
switch status {
case let .effectCompletion(action):
runtimeWarn(
"""
An effect completed on a non-main thread. …
Effect returned from:
\(debugCaseOutput(action))
Make sure to use ".receive(on:)" on any effects that execute on background threads to \
receive their output on the main thread.
The "Store" class is not thread-safe, and so all interactions with an instance of \
"Store" (including all of its scopes and derived view stores) must be done on the main \
thread.
"""
)
case .`init`:
runtimeWarn(
"""
A store initialized on a non-main thread. …
The "Store" class is not thread-safe, and so all interactions with an instance of \
"Store" (including all of its scopes and derived view stores) must be done on the main \
thread.
"""
)
case .scope:
runtimeWarn(
"""
"Store.scope" was called on a non-main thread. …
The "Store" class is not thread-safe, and so all interactions with an instance of \
"Store" (including all of its scopes and derived view stores) must be done on the main \
thread.
"""
)
case let .send(action, originatingAction: nil):
runtimeWarn(
"""
"ViewStore.send" was called on a non-main thread with: \(debugCaseOutput(action)) …
The "Store" class is not thread-safe, and so all interactions with an instance of \
"Store" (including all of its scopes and derived view stores) must be done on the main \
thread.
"""
)
case let .send(action, originatingAction: .some(originatingAction)):
runtimeWarn(
"""
An effect published an action on a non-main thread. …
Effect published:
\(debugCaseOutput(action))
Effect returned from:
\(debugCaseOutput(originatingAction))
Make sure to use ".receive(on:)" on any effects that execute on background threads to \
receive their output on the main thread.
The "Store" class is not thread-safe, and so all interactions with an instance of \
"Store" (including all of its scopes and derived view stores) must be done on the main \
thread.
"""
)
}
#endif
}
init<R: ReducerProtocol>(
initialState: R.State,
reducer: R,
mainThreadChecksEnabled: Bool
) where R.State == State, R.Action == Action {
self.state = CurrentValueSubject(initialState)
#if swift(>=5.7)
self.reducer = reducer
#else
self.reducer = reducer.reduce
#endif
#if DEBUG
self.mainThreadChecksEnabled = mainThreadChecksEnabled
#endif
self.threadCheck(status: .`init`)
}
}
/// A convenience type alias for referring to a store of a given reducer's domain.
///
/// Instead of specifying two generics:
///
/// ```swift
/// let store: Store<Feature.State, Feature.Action>
/// ```
///
/// You can specify a single generic:
///
/// ```swift
/// let store: StoreOf<Feature>
/// ```
public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
#if swift(>=5.7)
extension ReducerProtocol {
fileprivate func rescope<ChildState, ChildAction>(
_ store: Store<State, Action>,
state toChildState: @escaping (State) -> ChildState,
action fromChildAction: @escaping (ChildState, ChildAction) -> Action?
) -> Store<ChildState, ChildAction> {
(self as? any AnyScopedReducer ?? ScopedReducer(rootStore: store))
.rescope(store, state: toChildState, action: fromChildAction)
}
}
private final class ScopedReducer<
RootState, RootAction, ScopedState, ScopedAction
>: ReducerProtocol {
let rootStore: Store<RootState, RootAction>
let toScopedState: (RootState) -> ScopedState
private let parentStores: [Any]
let fromScopedAction: (ScopedState, ScopedAction) -> RootAction?
private(set) var isSending = false
@inlinable
init(rootStore: Store<RootState, RootAction>)
where RootState == ScopedState, RootAction == ScopedAction {
self.rootStore = rootStore
self.toScopedState = { $0 }
self.parentStores = []
self.fromScopedAction = { $1 }
}
@inlinable
init(
rootStore: Store<RootState, RootAction>,
state toScopedState: @escaping (RootState) -> ScopedState,
action fromScopedAction: @escaping (ScopedState, ScopedAction) -> RootAction?,
parentStores: [Any]
) {
self.rootStore = rootStore
self.toScopedState = toScopedState
self.fromScopedAction = fromScopedAction
self.parentStores = parentStores
}
@inlinable
func reduce(
into state: inout ScopedState, action: ScopedAction
) -> EffectTask<ScopedAction> {
self.isSending = true
defer {
state = self.toScopedState(self.rootStore.state.value)
self.isSending = false
}
if let action = self.fromScopedAction(state, action), let task = self.rootStore.send(action) {
return .fireAndForget { await task.cancellableValue }
} else {
return .none
}
}
}
protocol AnyScopedReducer {
func rescope<ScopedState, ScopedAction, RescopedState, RescopedAction>(
_ store: Store<ScopedState, ScopedAction>,
state toRescopedState: @escaping (ScopedState) -> RescopedState,
action fromRescopedAction: @escaping (RescopedState, RescopedAction) -> ScopedAction?
) -> Store<RescopedState, RescopedAction>
}
extension ScopedReducer: AnyScopedReducer {
@inlinable
func rescope<ScopedState, ScopedAction, RescopedState, RescopedAction>(
_ store: Store<ScopedState, ScopedAction>,
state toRescopedState: @escaping (ScopedState) -> RescopedState,
action fromRescopedAction: @escaping (RescopedState, RescopedAction) -> ScopedAction?
) -> Store<RescopedState, RescopedAction> {
let fromScopedAction = self.fromScopedAction as! (ScopedState, ScopedAction) -> RootAction?
let reducer = ScopedReducer<RootState, RootAction, RescopedState, RescopedAction>(
rootStore: self.rootStore,
state: { _ in toRescopedState(store.state.value) },
action: { fromRescopedAction($0, $1).flatMap { fromScopedAction(store.state.value, $0) } },
parentStores: self.parentStores + [store]
)
let childStore = Store<RescopedState, RescopedAction>(
initialState: toRescopedState(store.state.value),
reducer: reducer
)
childStore.parentCancellable = store.state
.dropFirst()
.sink { [weak childStore] newValue in
guard !reducer.isSending else { return }
childStore?.state.value = toRescopedState(newValue)
}
return childStore
}
}
#else
private protocol AnyStoreScope {
func rescope<ScopedState, ScopedAction, RescopedState, RescopedAction>(
_ store: Store<ScopedState, ScopedAction>,
state toRescopedState: @escaping (ScopedState) -> RescopedState,
action fromRescopedAction: @escaping (RescopedState, RescopedAction) -> ScopedAction?
) -> Store<RescopedState, RescopedAction>
}
private struct StoreScope<RootState, RootAction>: AnyStoreScope {
let root: Store<RootState, RootAction>
let fromScopedAction: Any
init(root: Store<RootState, RootAction>) {
self.init(
root: root,
fromScopedAction: { (state: RootState, action: RootAction) -> RootAction? in action }
)
}
private init<ScopedState, ScopedAction>(
root: Store<RootState, RootAction>,
fromScopedAction: @escaping (ScopedState, ScopedAction) -> RootAction?
) {
self.root = root
self.fromScopedAction = fromScopedAction
}
func rescope<ScopedState, ScopedAction, RescopedState, RescopedAction>(
_ scopedStore: Store<ScopedState, ScopedAction>,
state toRescopedState: @escaping (ScopedState) -> RescopedState,
action fromRescopedAction: @escaping (RescopedState, RescopedAction) -> ScopedAction?
) -> Store<RescopedState, RescopedAction> {
let fromScopedAction = self.fromScopedAction as! (ScopedState, ScopedAction) -> RootAction?
var isSending = false
let rescopedStore = Store<RescopedState, RescopedAction>(
initialState: toRescopedState(scopedStore.state.value),
reducer: .init { rescopedState, rescopedAction, _ in
isSending = true
defer { isSending = false }
guard
let scopedAction = fromRescopedAction(rescopedState, rescopedAction),
let rootAction = fromScopedAction(scopedStore.state.value, scopedAction)
else { return .none }
let task = self.root.send(rootAction)
rescopedState = toRescopedState(scopedStore.state.value)
if let task = task {
return .fireAndForget { await task.cancellableValue }
} else {
return .none
}
},
environment: ()
)
rescopedStore.parentCancellable = scopedStore.state
.dropFirst()
.sink { [weak rescopedStore] newValue in
guard !isSending else { return }
rescopedStore?.state.value = toRescopedState(newValue)
}
rescopedStore.scope = StoreScope<RootState, RootAction>(
root: self.root,
fromScopedAction: {
fromRescopedAction($0, $1).flatMap { fromScopedAction(scopedStore.state.value, $0) }
}
)
return rescopedStore
}
}
#endif