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

MobiusExtras: Add compactMap operation to Connectable #202

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Next Next commit
MobiusExtras: Add compactMap operation to Connectable
Frugghi committed Sep 7, 2023
commit acd7a3cc807bdd5a54a12de9852d6c5a34c16a6a
30 changes: 30 additions & 0 deletions MobiusExtras/Source/ConnectableCompactMap.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2019-2022 Spotify AB.
//
// Licensed 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
/// and returning only the non-`nil` values.
///
/// - 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 compactMap<NewOutput>(_ transform: @escaping (Output) -> NewOutput?) -> AnyConnectable<Input, NewOutput> {
AnyConnectable { dispatch in
self.connect { output in
transform(output).map(dispatch)
}
}
}
}
66 changes: 66 additions & 0 deletions MobiusExtras/Test/ConnectableCompactMapTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2019-2022 Spotify AB.
//
// Licensed 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

final class ConnectableCompactMapTests: QuickSpec {
override func spec() {
context("Connectable Compact Map") {
it("applies the `transform` function to the output") {
var output: [Int] = []
let connection = TestConnectable()
.compactMap { Int($0) }
.connect {
output.append($0)
}

connection.accept("1")
connection.accept("A")
connection.accept("2")
connection.accept("B")
connection.accept("3")
connection.accept("C")

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 }
)
}
}