diff --git a/Sources/SignalProducer.swift b/Sources/SignalProducer.swift index d9b1d755e..ec8aabe9d 100644 --- a/Sources/SignalProducer.swift +++ b/Sources/SignalProducer.swift @@ -236,6 +236,52 @@ public struct SignalProducer { } } +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(_ 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()`. diff --git a/Tests/ReactiveSwiftTests/SignalProducerSpec.swift b/Tests/ReactiveSwiftTests/SignalProducerSpec.swift index 35e35c2bc..ca3f2053c 100644 --- a/Tests/ReactiveSwiftTests/SignalProducerSpec.swift +++ b/Tests/ReactiveSwiftTests/SignalProducerSpec.swift @@ -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 { () throws -> String in operationReturnValue } @@ -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 { () throws -> String in throw operationError }