-
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.
Merge pull request #150 from spotify/connectable-map
Add `Connectable.map` to `MobiusExtras`
- Loading branch information
Showing
4 changed files
with
122 additions
and
13 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
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
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,35 @@ | ||
// Copyright (c) 2020 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. | ||
|
||
import MobiusCore | ||
|
||
public extension Connectable { | ||
/// Transform the output type of this `Connectable` by applying the `transform` function to each output. | ||
/// | ||
/// - Parameter transform: The function which should be used to transform the output of this `Connectable` | ||
/// - Returns: A `Connectable` which applies `transform` to each output value. | ||
func map<NewOutput>(_ transform: @escaping (Output) -> NewOutput) -> AnyConnectable<Input, NewOutput> { | ||
return AnyConnectable { dispatch in | ||
return self.connect { output in | ||
let newOutput = transform(output) | ||
dispatch(newOutput) | ||
} | ||
} | ||
} | ||
} |
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,68 @@ | ||
// Copyright (c) 2020 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. | ||
|
||
import MobiusCore | ||
import MobiusExtras | ||
import Nimble | ||
import Quick | ||
|
||
class ConnectableMapTests: QuickSpec { | ||
override func spec() { | ||
context("Connectable Map") { | ||
it("applies the `transform` function to the output") { | ||
var output: [Int?] = [] | ||
let connection = TestConnectable() | ||
.map { Int($0) } | ||
.connect { | ||
output.append($0) | ||
} | ||
|
||
connection.accept("1") | ||
connection.accept("2") | ||
connection.accept("3") | ||
|
||
expect(output).to(equal([1, 2, 3])) | ||
|
||
connection.dispose() | ||
} | ||
|
||
it("preserves the connectable's `Disposable` conformance") { | ||
let testConnectable = TestConnectable() | ||
expect(testConnectable.isDisposed).to(beFalse()) | ||
|
||
testConnectable | ||
.connect { _ in } | ||
.dispose() | ||
|
||
expect(testConnectable.isDisposed).to(beTrue()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private final class TestConnectable: Connectable { | ||
var isDisposed = false | ||
|
||
func connect(_ consumer: @escaping Consumer<String>) -> Connection<String> { | ||
return Connection( | ||
acceptClosure: consumer, | ||
disposeClosure: { self.isDisposed = true } | ||
) | ||
} | ||
} |