Skip to content

Commit 7bc128a

Browse files
authored
Merge pull request #497 from ReactiveCocoa/init-overloads-for-noerror
Add `SignalProducer.init` overloads for `NoError`
2 parents 4e23c21 + cc4ef96 commit 7bc128a

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Sources/SignalProducer.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,52 @@ public struct SignalProducer<Value, Error: Swift.Error> {
236236
}
237237
}
238238

239+
extension SignalProducer where Error == NoError {
240+
/// Creates a producer for a `Signal` that will immediately send one value
241+
/// then complete.
242+
///
243+
/// - parameters:
244+
/// - value: A value that should be sent by the `Signal` in a `value`
245+
/// event.
246+
public init(value: Value) {
247+
self.init { observer, _ in
248+
observer.send(value: value)
249+
observer.sendCompleted()
250+
}
251+
}
252+
253+
/// Creates a producer for a Signal that will immediately send the values
254+
/// from the given sequence, then complete.
255+
///
256+
/// - parameters:
257+
/// - values: A sequence of values that a `Signal` will send as separate
258+
/// `value` events and then complete.
259+
public init<S: Sequence>(_ values: S) where S.Iterator.Element == Value {
260+
self.init { observer, lifetime in
261+
for value in values {
262+
observer.send(value: value)
263+
264+
if lifetime.hasEnded {
265+
break
266+
}
267+
}
268+
269+
observer.sendCompleted()
270+
}
271+
}
272+
273+
/// Creates a producer for a Signal that will immediately send the values
274+
/// from the given sequence, then complete.
275+
///
276+
/// - parameters:
277+
/// - first: First value for the `Signal` to send.
278+
/// - second: Second value for the `Signal` to send.
279+
/// - tail: Rest of the values to be sent by the `Signal`.
280+
public init(values first: Value, _ second: Value, _ tail: Value...) {
281+
self.init([ first, second ] + tail)
282+
}
283+
}
284+
239285
extension SignalProducer where Error == AnyError {
240286
/// Create a `SignalProducer` that will attempt the given failable operation once for
241287
/// each invocation of `start()`.

Tests/ReactiveSwiftTests/SignalProducerSpec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ class SignalProducerSpec: QuickSpec {
400400
it("should send a successful value then complete") {
401401
let operationReturnValue = "OperationValue"
402402

403-
let signalProducer = SignalProducer { () throws -> String in
403+
let signalProducer = SignalProducer<String, AnyError> { () throws -> String in
404404
operationReturnValue
405405
}
406406

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

418-
let signalProducer = SignalProducer { () throws -> String in
418+
let signalProducer = SignalProducer<String, AnyError> { () throws -> String in
419419
throw operationError
420420
}
421421

0 commit comments

Comments
 (0)