-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We currently have MergedEventSource to compose multiple event sources with the same event type, but it’s unergonomic because it takes an array of event sources, but you can’t construct an array of heterogeneous event sources due to limitations on protocols with associated types. The current workaround is to explicitly wrap the members of the array in AnyEventSource, but explicit use of type erasure in clients is unpleasant. Using a builder lets us take each individual event source as a generic parameter and handle the type erasure internally. I called it CompositeEventSourceBuilder rather than MergedEventSource by analogy to CompositeDisposable.
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
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
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
// 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 es = AnyEventSource<Event>(source) | ||
let sources = eventSources + [es] | ||
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