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

Connectable class is no longer abstract #161

Merged
merged 3 commits into from
Apr 30, 2020
Merged
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
37 changes: 10 additions & 27 deletions MobiusExtras/Source/ConnectableClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
import Foundation
import MobiusCore

/// Abstract superclass that allows for easy implementation of a Mobius loop `Connectable`
/// Superclass that allows for easy implementation of a Mobius loop `Connectable`
///
/// - Attention: Should not be used directly. Instead a subclass should be used which overrides the `handle` and
/// `disposed` functions.
/// - Attention: Should not be used directly. Instead a subclass should be used which overrides any of combination of
/// the `handle`, `onConnect`, and `onDispose` functions.
///
/// This class automatically handles creation of `Connection`. Any subclass should override the following functions:
/// This class automatically handles creation of `Connection`. Any subclass can override any of the following functions:
/// - `handle`: this function handles any input (i.e. T.Effects). If a `T.Event` is should be sent to the loop,
/// it should be passed to the `send` function which will pass it to the Mobius loop
///
/// - `disposed`: this function is called when the loop has disposed of the `Connectable`. Any resources used by the
/// - `onConnect`: this function is called when the connection is being established. It can be used to allocate and
/// initialize any resources used by the subclass.
///
/// - `onDispose`: this function is called when the loop has disposed of the `Connectable`. Any resources used by the
/// subclass should be freed here. When this function is called, the base class has already released all its
/// resources so no further functions should be run on the base class.
open class ConnectableClass<Input, Output>: Connectable {
Expand Down Expand Up @@ -65,34 +68,14 @@ open class ConnectableClass<Input, Output>: Connectable {
}

/// Called when the `Connectable` receives input to allow the subclass to react to it.
///
/// - Attention: This function has to be overridden by a subclass or an error will be thrown to the MobiusHooks
/// error handler.
open func handle(_ input: Input) {
MobiusHooks.errorHandler(
"The function `\(#function)` must be overridden in subclass \(type(of: self))",
#file,
#line
)
}
open func handle(_ input: Input) {}

/// Called when the connection is being established. This function can be used to allocate and initialize any
/// resources used by the subclass.
///
/// - Note: This function is optional to override by subclasses.
open func onConnect() {}

/// Called when the connection is being disposed. This function should release any resources used by the subclass.
///
/// - Attention: This function has to be overridden by a subclass or an error will be thrown to the MobiusHooks
/// error handler.
open func onDispose() {
MobiusHooks.errorHandler(
"The function `\(#function)` must be overridden in subclass \(type(of: self))",
#file,
#line
)
}
open func onDispose() {}

public final func connect(_ consumer: @escaping (Output) -> Void) -> Connection<Input> {
lock.lock()
Expand Down
26 changes: 0 additions & 26 deletions MobiusExtras/Test/ConnectableClassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,6 @@ class ConnectableTests: QuickSpec {
return exception != nil
}

context("when attempting to use the base class directly") {
var sut: ConnectableClass<String, String>!

beforeEach {
sut = ConnectableClass()
}

context("when attempting to use handle") {
it("should cause a mobius error") {
let errorThrown = catchError {
sut.handle("something")
}
expect(errorThrown).to(beTrue())
}
}

context("when attempting to use disposed") {
it("should cause a mobius error") {
let errorThrown = catchError {
sut.onDispose()
}
expect(errorThrown).to(beTrue())
}
}
}

context("when creating a connection") {
var sut: SubclassedConnectableClass!
var connection: Connection<String>!
Expand Down