Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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