Skip to content

Commit

Permalink
Add CompositeEventSourceBuilder
Browse files Browse the repository at this point in the history
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
JensAyton committed May 27, 2019
1 parent 23324ec commit 6f680f4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Mobius.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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 */; };
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 +260,7 @@
/* 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>"; };
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 +550,7 @@
isa = PBXGroup;
children = (
5B4A369921107D2600279C7D /* AnyEventSource.swift */,
2DDF54BF229BDB4700D05861 /* CompositeEventSourceBuilder.swift */,
5BB287C0209995410043B530 /* EventSource.swift */,
5B4A36952110783100279C7D /* MergedEventSource.swift */,
);
Expand Down Expand Up @@ -1198,6 +1202,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 +1261,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
62 changes: 62 additions & 0 deletions MobiusCore/Source/EventSources/CompositeEventSourceBuilder.swift
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)
}
}
}
}
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

0 comments on commit 6f680f4

Please sign in to comment.