-
Notifications
You must be signed in to change notification settings - Fork 42
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
jeppes
merged 2 commits into
spotify:master
from
JensAyton:composite-event-source-builder
May 27, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
MobiusCore/Source/EventSources/CompositeEventSourceBuilder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
MobiusCore/Test/EventSources/CompositeEventSourceBuilderTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.