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

Add CompositeEventSourceBuilder #28

Merged
merged 2 commits into from
May 27, 2019
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
12 changes: 12 additions & 0 deletions Mobius.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
02BED1BB21DD20D20093FB47 /* ConnectableContramap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B9CE80421197FE000DB79A7 /* ConnectableContramap.swift */; };
2D54D0F021C11362002AAC19 /* AtomicBool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2D54D0EF21C11362002AAC19 /* AtomicBool.swift */; };
2D54D0F121C1167C002AAC19 /* AtomicBool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2D54D0EF21C11362002AAC19 /* AtomicBool.swift */; };
2DDF54C0229BDB4800D05861 /* CompositeEventSourceBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DDF54BF229BDB4700D05861 /* CompositeEventSourceBuilder.swift */; };
2DDF54C1229BDB4800D05861 /* CompositeEventSourceBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DDF54BF229BDB4700D05861 /* CompositeEventSourceBuilder.swift */; };
2DDF54C3229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DDF54C2229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift */; };
2DF4C2FC20DBDD5800A4B6DE /* Next.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BB287B5209995410043B530 /* Next.swift */; };
2DF4C2FD20DBDD5800A4B6DE /* Connection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BB287B6209995410043B530 /* Connection.swift */; };
2DF4C2FE20DBDD5800A4B6DE /* Connectable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B237EB9209C4F3C00764576 /* Connectable.swift */; };
Expand Down Expand Up @@ -258,6 +261,8 @@
/* Begin PBXFileReference section */
2D2FE60F20625E76002DFD69 /* Mobius.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = Mobius.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
2D54D0EF21C11362002AAC19 /* AtomicBool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AtomicBool.swift; sourceTree = "<group>"; };
2DDF54BF229BDB4700D05861 /* CompositeEventSourceBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompositeEventSourceBuilder.swift; sourceTree = "<group>"; };
2DDF54C2229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompositeEventSourceBuilderTests.swift; sourceTree = "<group>"; };
2DF4C2F520DBDD4700A4B6DE /* libMobiusCore.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMobiusCore.a; sourceTree = BUILT_PRODUCTS_DIR; };
2DF4C41D20DBDEFA00A4B6DE /* libMobiusExtras.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMobiusExtras.a; sourceTree = BUILT_PRODUCTS_DIR; };
2DF4C53320DBE03900A4B6DE /* libMobiusTest.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMobiusTest.a; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -547,6 +552,7 @@
isa = PBXGroup;
children = (
5B4A369921107D2600279C7D /* AnyEventSource.swift */,
2DDF54BF229BDB4700D05861 /* CompositeEventSourceBuilder.swift */,
5BB287C0209995410043B530 /* EventSource.swift */,
5B4A36952110783100279C7D /* MergedEventSource.swift */,
);
Expand All @@ -557,6 +563,7 @@
isa = PBXGroup;
children = (
5BB2885320999ACE0043B530 /* AnyEventSourceTests.swift */,
2DDF54C2229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift */,
5B4A369D21107F3200279C7D /* MergedEventSourceTests.swift */,
);
path = EventSources;
Expand Down Expand Up @@ -1198,6 +1205,7 @@
2DF4C30720DBDD5C00A4B6DE /* EventSource.swift in Sources */,
2DF4C2FD20DBDD5800A4B6DE /* Connection.swift in Sources */,
5B7095992109E89C0099298B /* EffectRouterBuilder.swift in Sources */,
2DDF54C1229BDB4800D05861 /* CompositeEventSourceBuilder.swift in Sources */,
5B1F1040210F5EE40067193C /* ConsumerConnectable.swift in Sources */,
5B1F1044210F5F590067193C /* ClosureConnectable.swift in Sources */,
2DF4C2FE20DBDD5800A4B6DE /* Connectable.swift in Sources */,
Expand Down Expand Up @@ -1256,6 +1264,7 @@
5BB288172099957D0043B530 /* Next.swift in Sources */,
5BB28827209995810043B530 /* MobiusLogger.swift in Sources */,
5BB28823209995810043B530 /* NoEffect.swift in Sources */,
2DDF54C0229BDB4800D05861 /* CompositeEventSourceBuilder.swift in Sources */,
5B1F103F210F5EE40067193C /* ConsumerConnectable.swift in Sources */,
5B1F1043210F5F590067193C /* ClosureConnectable.swift in Sources */,
5BB288182099957D0043B530 /* Connection.swift in Sources */,
Expand Down Expand Up @@ -1284,6 +1293,7 @@
5BB2887820999AD60043B530 /* AnyConnectionTests.swift in Sources */,
5B1F104B211037500067193C /* ConsumerConnectableTests.swift in Sources */,
5BB2887120999AD60043B530 /* LoggingInitTests.swift in Sources */,
2DDF54C3229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift in Sources */,
5B85AD0220AAA8CA00C4FCD5 /* MobiusHooksTests.swift in Sources */,
5BB2886D20999AD60043B530 /* MobiusControllerTests.swift in Sources */,
5BB2887B20999AD60043B530 /* ConnectablePublisherTests.swift in Sources */,
Expand Down Expand Up @@ -1552,6 +1562,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(PROJECT_DIR)/Carthage/Build/iOS";
PRODUCT_BUNDLE_IDENTIFIER = com.spotify.MobiusCoreTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_TREAT_WARNINGS_AS_ERRORS = NO;
};
name = Debug;
};
Expand All @@ -1566,6 +1577,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(PROJECT_DIR)/Carthage/Build/iOS";
PRODUCT_BUNDLE_IDENTIFIER = com.spotify.MobiusCoreTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_TREAT_WARNINGS_AS_ERRORS = NO;
};
name = Release;
};
Expand Down
61 changes: 61 additions & 0 deletions MobiusCore/Source/EventSources/CompositeEventSourceBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2019 Spotify AB.
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

/// A `CompositeEventSourceBuilder` gathers the provided event sources together and builds a single event source that
/// subscribes to all of them when its `subscribe` method is called.
public struct CompositeEventSourceBuilder<Event> {
private let eventSources: [AnyEventSource<Event>]

/// Initializes a `CompositeEventSourceBuilder`.
public init() {
self.init(eventSources: [])
}

private init(eventSources: [AnyEventSource<Event>]) {
self.eventSources = eventSources
}

/// Returns a new `CompositeEventSourceBuilder` with the specified event source added to it.
public func addEventSource<Source: EventSource>(_ source: Source)
-> CompositeEventSourceBuilder<Event> where Source.Event == Event {
let sources = eventSources + [AnyEventSource<Event>(source)]
return CompositeEventSourceBuilder(eventSources: sources)
}

/// Builds an event source that composes all the event sources that have been added to the builder.
///
/// - Returns: An event source which represents the composition of the builder’s input event sources. The type
/// of this source is an implementation detail; consumers should avoid spelling it out if possible.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In particular, I’d like the return type to be some EventSource<.Event == Event>, but Swift 5.1 will only have unconstrained opaque types.

public func build() -> AnyEventSource<Event> {
switch eventSources.count {
case 0:
return AnyEventSource<Event> { _ in AnonymousDisposable {} }
case 1:
return eventSources[0]
default:
let eventSources = self.eventSources
return AnyEventSource { consumer in
let disposables = eventSources.map {
$0.subscribe(consumer: consumer)
}
return CompositeDisposable(disposables: disposables)
}
}
}
}
1 change: 1 addition & 0 deletions MobiusCore/Source/EventSources/MergedEventSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Foundation

/// A `MergedEventSource` holds onto the provided event sources and subscribes consumers to all of them once its
/// `subscribe` method is called.
@available(*, deprecated, message: "use CompositeEventSourceBuilder instead")
public final class MergedEventSource<Event>: EventSource {
private let eventSources: [AnyEventSource<Event>]

Expand Down
139 changes: 139 additions & 0 deletions MobiusCore/Test/EventSources/CompositeEventSourceBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright (c) 2019 Spotify AB.
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

@testable import MobiusCore
import Nimble
import Quick

class CompositeEventSourceBuilderTest: QuickSpec {
// swiftlint:disable function_body_length
override func spec() {
var eventsReceived: [Int]!
var compositeEventSource: AnyEventSource<Int>!
var disposable: Disposable!

describe("CompositeEventSourceBuilder") {
context("when configuring the composite event source builder") {
context("with no event sources") {
beforeEach {
let sut = CompositeEventSourceBuilder<Int>()
compositeEventSource = sut.build()
eventsReceived = []
}

it("should produce an event source") {
// In particular, we want a do-nothing event source rather than an assertion or crash.
disposable = compositeEventSource.subscribe {
eventsReceived.append($0)
}
disposable.dispose()

expect(eventsReceived).to(equal([]))
}
}

context("with one event source") {
var eventSource: TestEventSource!

beforeEach {
eventSource = TestEventSource()
let sut = CompositeEventSourceBuilder<Int>()
.addEventSource(eventSource)

compositeEventSource = sut.build()
eventsReceived = []

disposable = compositeEventSource.subscribe {
eventsReceived.append($0)
}
}

it("should provide an event source equivalent to the input event source") {
eventSource.dispatch(1)
eventSource.dispatch(2)

let expectedEvents = [1, 2]
expect(eventsReceived).to(equal(expectedEvents))
}

it("should return a disposable that disposes the original event source") {
disposable?.dispose()

expect(eventSource.isDisposed).to(beTrue())
}
}

context("with several event sources") {
var eventSources: [TestEventSource]!

beforeEach {
eventSources = [TestEventSource(), TestEventSource(), TestEventSource()]
var sut = CompositeEventSourceBuilder<Int>()
eventSources.forEach {
sut = sut.addEventSource($0)
}

compositeEventSource = sut.build()
eventsReceived = []

disposable = compositeEventSource.subscribe {
eventsReceived.append($0)
}
}

it("should produce an event source that emits the events from all input sources") {
eventSources.enumerated().forEach { index, source in
source.dispatch(index)
}

let expectedEvents = [0, 1, 2]
expect(eventsReceived).to(equal(expectedEvents))
}

it("should return a disposable that disposes of all the input event sources") {
disposable?.dispose()

eventSources.forEach {
expect($0.isDisposed).to(beTrue())
}
}
}
}
}
}
}

private class TestEventSource: EventSource, Disposable {
typealias Event = Int

var consumer: Consumer<Int>?
func subscribe(consumer: @escaping Consumer<Int>) -> Disposable {
self.consumer = consumer
return self
}

var isDisposed = false
func dispose() {
isDisposed = true
}

func dispatch(_ event: Int) {
consumer?(event)
}
}