Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow EffectRouter to be passed to Mobius.loop directly #116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 71 additions & 12 deletions MobiusCore/Source/Mobius.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public typealias Initiate<Model, Effect> = (Model) -> First<Model, Effect>

public enum Mobius {}

// MARK: - Building a Mobius Loop

public extension Mobius {

// MARK: - Creating a Mobius Loop builder

/// Create a `Builder` to help you configure a `MobiusLoop` before starting it.
///
/// The builder is immutable. When setting various properties, a new instance of a builder will be returned.
Expand All @@ -71,28 +71,83 @@ public extension Mobius {
/// Once done configuring the loop you can start the loop using `start(from:)`.
///
/// - Parameters:
/// - update: the `Update` function of the loop
/// - effectHandler: an instance conforming to `Connectable`. Will be used to handle effects by the loop.
/// - update: the update function of the loop.
/// - effectHandler: the `EffectRouter` that will be used by the loop to handle effects.
/// - Returns: a `Builder` instance that you can further configure before starting the loop
static func loop<Model, Event, Effect>(
update: Update<Model, Event, Effect>,
effectHandler: EffectRouter<Effect, Event>
) -> Builder<Model, Event, Effect> {
return Builder(
update: update,
effectHandler: effectHandler.asConnectable,
initiate: nil,
eventSource: AnyEventSource({ _ in AnonymousDisposable(disposer: {}) }),
eventConsumerTransformer: { $0 },
logger: AnyMobiusLogger(NoopLogger())
)
}

/// Create a `Builder` to help you configure a `MobiusLoop` before starting it.
///
/// The builder is immutable. When setting various properties, a new instance of a builder will be returned.
/// It is therefore recommended to chain the loop configuration functions
///
/// Once done configuring the loop you can start the loop using `start(from:)`.
///
/// - Parameters:
/// - update: the update function of the loop.
/// - effectHandler: the entity that will be used by the loop to handle effects. An instance conforming to
/// `Connectable` with `Effect` as input and `Event` as output. **Note:** there is an overload which takes an
/// `EffectRouter` instead, which should be preferred in most cases.
/// - Returns: a `Builder` instance that you can further configure before starting the loop.
static func loop<Model, Event, Effect, EffectHandler: Connectable>(
update: Update<Model, Event, Effect>,
effectHandler: EffectHandler
) -> Builder<Model, Event, Effect> where EffectHandler.Input == Effect, EffectHandler.Output == Event {
return Builder(
update: update,
effectHandler: effectHandler,
effectHandler: AnyConnectable(effectHandler),
initiate: nil,
eventSource: AnyEventSource({ _ in AnonymousDisposable(disposer: {}) }),
eventConsumerTransformer: { $0 },
logger: AnyMobiusLogger(NoopLogger())
)
}

/// A convenience version of `loop` that takes an unwrapped update function.
/// Create a `Builder` to help you configure a `MobiusLoop` before starting it.
///
/// The builder is immutable. When setting various properties, a new instance of a builder will be returned.
/// It is therefore recommended to chain the loop configuration functions
///
/// Once done configuring the loop you can start the loop using `start(from:)`.
///
/// - Parameters:
/// - update: the update function of the loop.
/// - effectHandler: the `EffectRouter` that will be used by the loop to handle effects.
/// - Returns: a `Builder` instance that you can further configure before starting the loop
static func loop<Model, Event, Effect>(
update: @escaping (Model, Event) -> Next<Model, Effect>,
effectHandler: EffectRouter<Effect, Event>
) -> Builder<Model, Event, Effect> {
return self.loop(
update: Update(update),
effectHandler: effectHandler
)
}

/// Create a `Builder` to help you configure a `MobiusLoop` before starting it.
///
/// The builder is immutable. When setting various properties, a new instance of a builder will be returned.
/// It is therefore recommended to chain the loop configuration functions
///
/// Once done configuring the loop you can start the loop using `start(from:)`.
///
/// - Parameters:
/// - update: the update function of the loop
/// - effectHandler: an instance conforming to `Connectable`. Will be used to handle effects by the loop.
/// - update: the update function of the loop.
/// - effectHandler: the entity that will be used by the loop to handle effects. An instance conforming to
/// `Connectable` with `Effect` as input and `Event` as output. **Note:** there is an overload which takes an
/// `EffectRouter` instead, which should be preferred in most cases.
/// - Returns: a `Builder` instance that you can further configure before starting the loop
static func loop<Model, Event, Effect, EffectHandler: Connectable>(
update: @escaping (Model, Event) -> Next<Model, Effect>,
Expand All @@ -117,22 +172,24 @@ public extension Mobius {
private let logger: AnyMobiusLogger<Model, Event, Effect>
private let eventConsumerTransformer: ConsumerTransformer<Event>

fileprivate init<EffectHandler: Connectable>(
fileprivate init(
update: Update<Model, Event, Effect>,
effectHandler: EffectHandler,
effectHandler: AnyConnectable<Effect, Event>,
initiate: Initiate<Model, Effect>?,
eventSource: AnyEventSource<Event>,
eventConsumerTransformer: @escaping ConsumerTransformer<Event>,
logger: AnyMobiusLogger<Model, Event, Effect>
) where EffectHandler.Input == Effect, EffectHandler.Output == Event {
) {
self.update = update
self.effectHandler = AnyConnectable(effectHandler)
self.effectHandler = effectHandler
self.initiate = initiate
self.eventSource = eventSource
self.logger = logger
self.eventConsumerTransformer = eventConsumerTransformer
}

// MARK: - Configuring the builder

/// Return a copy of this builder with a new [event source].
///
/// If a `MobiusLoop` is created from the builder by calling `start`, the event source will be subscribed to
Expand Down Expand Up @@ -204,6 +261,8 @@ public extension Mobius {
)
}

// MARK: - Creating a loop

/// Create a `MobiusLoop` from the builder, and optionally dispatch one or more effects.
///
/// - Parameters:
Expand Down
5 changes: 2 additions & 3 deletions MobiusCore/Test/MobiusLoopTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,11 @@ class MobiusLoopTests: QuickSpec {
}
}
let payload: (Int) -> Int? = { $0 }
let effectConnectable = EffectRouter<Int, Int>()
let effectRouter = EffectRouter<Int, Int>()
.routeEffects(withPayload: payload).to(effectHandler)
.asConnectable
let update = Update { (_: Int, _: Int) -> Next<Int, Int> in Next.dispatchEffects([1]) }
loop = Mobius
.loop(update: update, effectHandler: effectConnectable)
.loop(update: update, effectHandler: effectRouter)
.start(from: 0)
}
afterEach {
Expand Down
5 changes: 2 additions & 3 deletions MobiusCore/Test/NonReentrancyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ class NonReentrancyTests: QuickSpec {
return AnonymousDisposable {}
}

let effectConnectable = EffectRouter<Effect, Event>()
let effectRouter = EffectRouter<Effect, Event>()
.routeEffects(equalTo: .testEffect).to(testEffectHandler)
.asConnectable

loop = Mobius.loop(update: update, effectHandler: effectConnectable)
loop = Mobius.loop(update: update, effectHandler: effectRouter)
.start(from: 0)
}

Expand Down