Skip to content

Commit

Permalink
Merge pull request #497 from ReactiveCocoa/init-overloads-for-noerror
Browse files Browse the repository at this point in the history
Add `SignalProducer.init` overloads for `NoError`
  • Loading branch information
andersio committed Aug 11, 2017
2 parents 4e23c21 + cc4ef96 commit 7bc128a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
46 changes: 46 additions & 0 deletions Sources/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,52 @@ public struct SignalProducer<Value, Error: Swift.Error> {
}
}

extension SignalProducer where Error == NoError {
/// Creates a producer for a `Signal` that will immediately send one value
/// then complete.
///
/// - parameters:
/// - value: A value that should be sent by the `Signal` in a `value`
/// event.
public init(value: Value) {
self.init { observer, _ in
observer.send(value: value)
observer.sendCompleted()
}
}

/// Creates a producer for a Signal that will immediately send the values
/// from the given sequence, then complete.
///
/// - parameters:
/// - values: A sequence of values that a `Signal` will send as separate
/// `value` events and then complete.
public init<S: Sequence>(_ values: S) where S.Iterator.Element == Value {
self.init { observer, lifetime in
for value in values {
observer.send(value: value)

if lifetime.hasEnded {
break
}
}

observer.sendCompleted()
}
}

/// Creates a producer for a Signal that will immediately send the values
/// from the given sequence, then complete.
///
/// - parameters:
/// - first: First value for the `Signal` to send.
/// - second: Second value for the `Signal` to send.
/// - tail: Rest of the values to be sent by the `Signal`.
public init(values first: Value, _ second: Value, _ tail: Value...) {
self.init([ first, second ] + tail)
}
}

extension SignalProducer where Error == AnyError {
/// Create a `SignalProducer` that will attempt the given failable operation once for
/// each invocation of `start()`.
Expand Down
4 changes: 2 additions & 2 deletions Tests/ReactiveSwiftTests/SignalProducerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ class SignalProducerSpec: QuickSpec {
it("should send a successful value then complete") {
let operationReturnValue = "OperationValue"

let signalProducer = SignalProducer { () throws -> String in
let signalProducer = SignalProducer<String, AnyError> { () throws -> String in
operationReturnValue
}

Expand All @@ -415,7 +415,7 @@ class SignalProducerSpec: QuickSpec {
it("should send the error") {
let operationError = TestError.default

let signalProducer = SignalProducer { () throws -> String in
let signalProducer = SignalProducer<String, AnyError> { () throws -> String in
throw operationError
}

Expand Down

0 comments on commit 7bc128a

Please sign in to comment.