Skip to content

Commit a2b4db1

Browse files
committed
Add SignalProducer.init overloads for NoError
1 parent 187f0f1 commit a2b4db1

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

Sources/SignalProducer.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,69 @@ 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 immediately sends one value, then
254+
/// completes.
255+
///
256+
/// This initializer differs from `init(value:)` in that its sole `value`
257+
/// event is constructed lazily by invoking the supplied `action` when
258+
/// the `SignalProducer` is started.
259+
///
260+
/// - parameters:
261+
/// - action: A action that yields a value to be sent by the `Signal` as
262+
/// a `value` event.
263+
public init(_ action: @escaping () -> Value) {
264+
self.init { observer, _ in
265+
observer.send(value: action())
266+
observer.sendCompleted()
267+
}
268+
}
269+
270+
/// Creates a producer for a Signal that will immediately send the values
271+
/// from the given sequence, then complete.
272+
///
273+
/// - parameters:
274+
/// - values: A sequence of values that a `Signal` will send as separate
275+
/// `value` events and then complete.
276+
public init<S: Sequence>(_ values: S) where S.Iterator.Element == Value {
277+
self.init { observer, lifetime in
278+
for value in values {
279+
observer.send(value: value)
280+
281+
if lifetime.hasEnded {
282+
break
283+
}
284+
}
285+
286+
observer.sendCompleted()
287+
}
288+
}
289+
290+
/// Creates a producer for a Signal that will immediately send the values
291+
/// from the given sequence, then complete.
292+
///
293+
/// - parameters:
294+
/// - first: First value for the `Signal` to send.
295+
/// - second: Second value for the `Signal` to send.
296+
/// - tail: Rest of the values to be sent by the `Signal`.
297+
public init(values first: Value, _ second: Value, _ tail: Value...) {
298+
self.init([ first, second ] + tail)
299+
}
300+
}
301+
239302
extension SignalProducer where Error == AnyError {
240303
/// Create a `SignalProducer` that will attempt the given failable operation once for
241304
/// each invocation of `start()`.

Tests/ReactiveSwiftTests/SignalProducerSpec.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,11 @@ class SignalProducerSpec: QuickSpec {
229229
let resultAction2: () -> Result<String, AnyError> = { .success("") }
230230
let throwableResultAction: () throws -> Result<String, NoError> = { .success("") }
231231

232-
expect(type(of: SignalProducer(action))) == SignalProducer<String, AnyError>.self
233-
expect(type(of: SignalProducer<String, NoError>(action))) == SignalProducer<String, NoError>.self
234-
expect(type(of: SignalProducer<String, TestError>(action))) == SignalProducer<String, TestError>.self
232+
expect(type(of: SignalProducer(action))) == SignalProducer<String, NoError>.self
233+
expect(type(of: SignalProducer<String, AnyError>(action))) == SignalProducer<String, AnyError>.self
235234

236-
expect(type(of: SignalProducer(resultAction1))) == SignalProducer<String, NoError>.self
237-
expect(type(of: SignalProducer(resultAction2))) == SignalProducer<String, AnyError>.self
235+
expect(type(of: SignalProducer<String, NoError>(resultAction1))) == SignalProducer<String, NoError>.self
236+
expect(type(of: SignalProducer<String, AnyError>(resultAction2))) == SignalProducer<String, AnyError>.self
238237

239238
expect(type(of: SignalProducer(throwableAction))) == SignalProducer<String, AnyError>.self
240239
expect(type(of: SignalProducer(throwableResultAction))) == SignalProducer<Result<String, NoError>, AnyError>.self
@@ -367,8 +366,8 @@ class SignalProducerSpec: QuickSpec {
367366
return .success("OperationValue")
368367
}
369368

370-
SignalProducer(operation).start()
371-
SignalProducer(operation).start()
369+
SignalProducer<String, NSError>(operation).start()
370+
SignalProducer<String, NSError>(operation).start()
372371

373372
expect(operationRunTimes) == 2
374373
}
@@ -379,7 +378,7 @@ class SignalProducerSpec: QuickSpec {
379378
return .success(operationReturnValue)
380379
}
381380

382-
let signalProducer = SignalProducer(operation)
381+
let signalProducer = SignalProducer<String, NSError>(operation)
383382

384383
expect(signalProducer).to(sendValue(operationReturnValue, sendError: nil, complete: true))
385384
}
@@ -390,7 +389,7 @@ class SignalProducerSpec: QuickSpec {
390389
return .failure(operationError)
391390
}
392391

393-
let signalProducer = SignalProducer(operation)
392+
let signalProducer = SignalProducer<String, NSError>(operation)
394393

395394
expect(signalProducer).to(sendValue(nil, sendError: operationError, complete: false))
396395
}
@@ -400,7 +399,7 @@ class SignalProducerSpec: QuickSpec {
400399
it("should send a successful value then complete") {
401400
let operationReturnValue = "OperationValue"
402401

403-
let signalProducer = SignalProducer { () throws -> String in
402+
let signalProducer = SignalProducer<String, AnyError> { () throws -> String in
404403
operationReturnValue
405404
}
406405

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

418-
let signalProducer = SignalProducer { () throws -> String in
417+
let signalProducer = SignalProducer<String, AnyError> { () throws -> String in
419418
throw operationError
420419
}
421420

0 commit comments

Comments
 (0)