Skip to content

Commit b3859f8

Browse files
committed
Gate onMainActor routing DSL to Swift 5.10+
1 parent bae449e commit b3859f8

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

MobiusCore/Source/EffectHandlers/EffectRouterDSL.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ public extension _PartialEffectRouter {
4040
/// Route main-isolated effects through the same queue path as `.on(queue: .main)`.
4141
///
4242
/// This returns a dedicated builder exposing `to(...)` for `@MainActor` closures.
43+
#if compiler(>=5.10)
4344
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
4445
func onMainActor() -> _MainActorPartialEffectRouter<Effect, EffectParameters, Event> {
4546
return _MainActorPartialEffectRouter(partialRouter: on(queue: .main))
4647
}
48+
#endif
4749

4850
/// Route to a closure which returns an optional event when given the parameters as input.
4951
///
@@ -62,6 +64,7 @@ public extension _PartialEffectRouter {
6264
}
6365
}
6466

67+
#if compiler(>=5.10)
6568
/// A `_MainActorPartialEffectRouter` represents the state between an `onMainActor` call and a `to`.
6669
///
6770
/// Client code should not refer to this type directly.
@@ -78,14 +81,9 @@ public extension _MainActorPartialEffectRouter {
7881
_ fireAndForget: @MainActor @Sendable @escaping (EffectParameters) -> Void
7982
) -> EffectRouter<Effect, Event> {
8083
return partialRouter.to { parameters, callback in
81-
#if compiler(>=5.10)
82-
MainActor.assumeIsolated {
83-
fireAndForget(parameters)
84-
}
85-
#else
86-
dispatchPrecondition(condition: .onQueue(.main))
84+
MainActor.assumeIsolated {
8785
fireAndForget(parameters)
88-
#endif
86+
}
8987
callback.end()
9088
return AnonymousDisposable {}
9189
}
@@ -101,16 +99,12 @@ public extension _MainActorPartialEffectRouter where EffectParameters == Void {
10199
_ fireAndForget: @MainActor @Sendable @escaping () -> Void
102100
) -> EffectRouter<Effect, Event> {
103101
return partialRouter.to { _, callback in
104-
#if compiler(>=5.10)
105-
MainActor.assumeIsolated {
106-
fireAndForget()
107-
}
108-
#else
109-
dispatchPrecondition(condition: .onQueue(.main))
102+
MainActor.assumeIsolated {
110103
fireAndForget()
111-
#endif
104+
}
112105
callback.end()
113106
return AnonymousDisposable {}
114107
}
115108
}
116109
}
110+
#endif

MobiusCore/Test/EffectHandlers/EffectRouterDSLTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class EffectRouterDSLTests: QuickSpec {
202202
expect(didDispatchEvents).to(beFalse())
203203
}
204204

205+
#if compiler(>=5.10)
205206
it("Supports routing to an onMainActor side-effecting function") {
206207
guard #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) else {
207208
return
@@ -245,6 +246,7 @@ class EffectRouterDSLTests: QuickSpec {
245246
expect(effectPerformedCount.items).toEventually(equal([1]))
246247
expect(didDispatchEvents).to(beFalse())
247248
}
249+
#endif
248250

249251
it("Supports routing to an event-returning function") {
250252
var events: [Event] = []

0 commit comments

Comments
 (0)