Skip to content

Commit

Permalink
Changed toRACSignal from free function to protocol extension
Browse files Browse the repository at this point in the history
This is a leftover from the `RAC 3 -> RAC 4` (`Swift 1.2 -> Swift 2.0` migration).
  • Loading branch information
NachoSoto committed Nov 9, 2015
1 parent 49d7563 commit 465d2cc
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 90 deletions.
205 changes: 121 additions & 84 deletions ReactiveCocoa/Swift/ObjectiveCBridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,106 +79,138 @@ extension SignalType {
}
}

/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal<Value: AnyObject, Error>(producer: SignalProducer<Value, Error>) -> RACSignal {
return toRACSignal(producer.lift { $0.optionalize() })
// MARK: - toRACSignal

extension SignalProducerType where Value: AnyObject {
/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal() -> RACSignal {
return self
.lift { $0.optionalize() }
.toRACSignal()
}
}

/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal<Value: AnyObject, Error: NSError>(producer: SignalProducer<Value, Error>) -> RACSignal {
return toRACSignal(producer.lift { $0.optionalize() })
extension SignalProducerType where Value: OptionalType, Value.Wrapped: AnyObject {
/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal() -> RACSignal {
return self
.mapError { $0 as NSError }
.toRACSignal()
}
}

/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal<Value: AnyObject, Error>(producer: SignalProducer<Value?, Error>) -> RACSignal {
return toRACSignal(producer.mapError { $0 as NSError })
extension SignalProducerType where Value: AnyObject, Error: NSError {
/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal() -> RACSignal {
return self
.lift { $0.optionalize() }
.toRACSignal()
}
}

/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal<Value: AnyObject, Error: NSError>(producer: SignalProducer<Value?, Error>) -> RACSignal {
// This special casing of `Error: NSError` is a workaround for rdar://22708537
// which causes an NSError's UserInfo dictionary to get discarded
// during a cast from ErrorType to NSError in a generic function
return RACSignal.createSignal { subscriber in
let selfDisposable = producer.start { event in
switch event {
case let .Next(value):
subscriber.sendNext(value)
case let .Failed(error):
subscriber.sendError(error)
case .Completed:
subscriber.sendCompleted()
case .Interrupted:
break
extension SignalProducerType where Value: OptionalType, Value.Wrapped: AnyObject, Error: NSError {
/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal() -> RACSignal {
// This special casing of `Error: NSError` is a workaround for rdar://22708537
// which causes an NSError's UserInfo dictionary to get discarded
// during a cast from ErrorType to NSError in a generic function
return RACSignal.createSignal { subscriber in
let selfDisposable = self.start { event in
switch event {
case let .Next(value):
subscriber.sendNext(value.optional)
case let .Failed(error):
subscriber.sendError(error)
case .Completed:
subscriber.sendCompleted()
case .Interrupted:
break
}
}
}

return RACDisposable {
selfDisposable.dispose()
return RACDisposable {
selfDisposable.dispose()
}
}
}
}

/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal<Value: AnyObject, Error>(signal: Signal<Value, Error>) -> RACSignal {
return toRACSignal(signal.optionalize())
extension SignalType where Value: AnyObject {
/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal() -> RACSignal {
return self
.optionalize()
.toRACSignal()
}
}

/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal<Value: AnyObject, Error: NSError>(signal: Signal<Value, Error>) -> RACSignal {
return toRACSignal(signal.optionalize())
extension SignalType where Value: AnyObject, Error: NSError {
/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal() -> RACSignal {
return self
.optionalize()
.toRACSignal()
}
}

/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal<Value: AnyObject, Error>(signal: Signal<Value?, Error>) -> RACSignal {
return toRACSignal(signal.mapError { $0 as NSError })
extension SignalType where Value: OptionalType, Value.Wrapped: AnyObject {
/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal() -> RACSignal {
return self
.mapError { $0 as NSError }
.toRACSignal()
}
}

/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal<Value: AnyObject, Error: NSError>(signal: Signal<Value?, Error>) -> RACSignal {
// This special casing of `Error: NSError` is a workaround for rdar://22708537
// which causes an NSError's UserInfo dictionary to get discarded
// during a cast from ErrorType to NSError in a generic function
return RACSignal.createSignal { subscriber in
let selfDisposable = signal.observe { event in
switch event {
case let .Next(value):
subscriber.sendNext(value)
case let .Failed(error):
subscriber.sendError(error)
case .Completed:
subscriber.sendCompleted()
case .Interrupted:
break
}
}

return RACDisposable {
selfDisposable?.dispose()
}
}
extension SignalType where Value: OptionalType, Value.Wrapped: AnyObject, Error: NSError {
/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal() -> RACSignal {
// This special casing of `Error: NSError` is a workaround for rdar://22708537
// which causes an NSError's UserInfo dictionary to get discarded
// during a cast from ErrorType to NSError in a generic function
return RACSignal.createSignal { subscriber in
let selfDisposable = self.observe { event in
switch event {
case let .Next(value):
subscriber.sendNext(value.optional)
case let .Failed(error):
subscriber.sendError(error)
case .Completed:
subscriber.sendCompleted()
case .Interrupted:
break
}
}

return RACDisposable {
selfDisposable?.dispose()
}
}
}
}

// MARK: -

extension RACCommand {
/// Creates an Action that will execute the receiver.
///
Expand All @@ -204,8 +236,9 @@ extension RACCommand {

extension Action {
private var commandEnabled: RACSignal {
let enabled = self.enabled.producer.map { $0 as NSNumber }
return toRACSignal(enabled)
return self.enabled.producer
.map { $0 as NSNumber }
.toRACSignal()
}
}

Expand All @@ -216,7 +249,9 @@ extension Action {
/// the Action will always be marked as executing when the RACCommand is.
public func toRACCommand<Output: AnyObject, Error>(action: Action<AnyObject?, Output, Error>) -> RACCommand {
return RACCommand(enabled: action.commandEnabled) { input -> RACSignal in
return toRACSignal(action.apply(input))
return action
.apply(input)
.toRACSignal()
}
}

Expand All @@ -227,6 +262,8 @@ public func toRACCommand<Output: AnyObject, Error>(action: Action<AnyObject?, Ou
/// the Action will always be marked as executing when the RACCommand is.
public func toRACCommand<Output: AnyObject, Error>(action: Action<AnyObject?, Output?, Error>) -> RACCommand {
return RACCommand(enabled: action.commandEnabled) { input -> RACSignal in
return toRACSignal(action.apply(input))
return action
.apply(input)
.toRACSignal()
}
}
12 changes: 6 additions & 6 deletions ReactiveCocoaTests/Swift/ObjectiveCBridgingSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ObjectiveCBridgingSpec: QuickSpec {
describe("on a Signal") {
it("should forward events") {
let (signal, observer) = Signal<NSNumber, NoError>.pipe()
let racSignal = toRACSignal(signal)
let racSignal = signal.toRACSignal()

var lastValue: NSNumber?
var didComplete = false
Expand All @@ -114,7 +114,7 @@ class ObjectiveCBridgingSpec: QuickSpec {

it("should convert errors to NSError") {
let (signal, observer) = Signal<AnyObject, TestError>.pipe()
let racSignal = toRACSignal(signal)
let racSignal = signal.toRACSignal()

let expectedError = TestError.Error2
var error: NSError?
Expand All @@ -130,7 +130,7 @@ class ObjectiveCBridgingSpec: QuickSpec {

it("should maintain userInfo on NSError") {
let (signal, observer) = Signal<AnyObject, NSError>.pipe()
let racSignal = toRACSignal(signal)
let racSignal = signal.toRACSignal()

var error: NSError?

Expand All @@ -153,7 +153,7 @@ class ObjectiveCBridgingSpec: QuickSpec {
let producer = SignalProducer<NSNumber, NoError>.attempt {
return .Success(subscriptions++)
}
let racSignal = toRACSignal(producer)
let racSignal = producer.toRACSignal()

expect(racSignal.first() as? NSNumber).to(equal(0))
expect(racSignal.first() as? NSNumber).to(equal(1))
Expand All @@ -162,15 +162,15 @@ class ObjectiveCBridgingSpec: QuickSpec {

it("should convert errors to NSError") {
let producer = SignalProducer<AnyObject, TestError>(error: .Error1)
let racSignal = toRACSignal(producer).materialize()
let racSignal = producer.toRACSignal().materialize()

let event = racSignal.first() as? RACEvent
expect(event?.error).to(equal(TestError.Error1 as NSError))
}

it("should maintain userInfo on NSError") {
let producer = SignalProducer<AnyObject, NSError>(error: testNSError)
let racSignal = toRACSignal(producer).materialize()
let racSignal = producer.toRACSignal().materialize()

let event = racSignal.first() as? RACEvent
let userInfoValue = event?.error.userInfo[key] as? String
Expand Down

0 comments on commit 465d2cc

Please sign in to comment.