Git Product home page Git Product logo

reactiveobjcbridge's Introduction

ReactiveObjCBridge

After the announcement of Swift, ReactiveCocoa was rewritten in Swift. This framework creates a bridge between those Swift and Objective-C APIs (now known as ReactiveSwift and ReactiveObjC respectively).

Because the APIs are based on fundamentally different designs, the conversion is not always one-to-one; however, every attempt has been made to faithfully translate the concepts between the two APIs (and languages).

The bridged types include:

  1. RACSignal and SignalProducer or Signal
  2. RACCommand and Action
  3. RACScheduler and SchedulerType
  4. RACDisposable and Disposable

For the complete bridging API, including documentation, see ObjectiveCBridging.swift.

RACSignal and SignalProducer or Signal

In ReactiveSwift, “cold” signals are represented by the SignalProducer type, and “hot” signals are represented by the Signal type.

“Cold” RACSignals can be converted into SignalProducers using the bridgedSignalProducer() free function:

func bridgedSignalProducer<T>(from signal: RACSignal<T>) -> SignalProducer<T?, NSError>

“Hot” RACSignals cannot be directly converted into Signals, because any RACSignal subscription could potentially involve side effects. To obtain a Signal, use RACSignal.toSignalProducer followed by SignalProducer.start, which will make those potential side effects explicit.

For the other direction, use the toRACSignal() instance method.

When invoked on a SignalProducer, these functions will create a RACSignal to start() the producer once for each subscription:

extension SignalProducerProtocol where Value: AnyObject {
    func toRACSignal() -> RACSignal<Value>
}

extension SignalProducerProtocol where Value: OptionalProtocol, Value.Wrapped: AnyObject {
	func toRACSignal() -> RACSignal<Value.Wrapped>
}

When inoked on a Signal, these methods will create a RACSignal that simply observes it:

extension SignalProtocol where Value: AnyObject {
    func toRACSignal() -> RACSignal<Value.Wrapped> {
}

extension SignalProtocol where Value: OptionalProtocol, Value.Wrapped: AnyObject {
    func toRACSignal() -> RACSignal<Value.Wrapped> {
}

RACCommand and Action

To convert RACCommands into the new Action type, use the bridgedAction() free function:

func bridgedAction<I, O>(from command: RACCommand<I, O>) -> Action<I?, O?, NSError>

To convert Actions into RACCommands, use the toRACCommand() instance method:

extension ActionProtocol where Input: AnyObject, Output: AnyObject {
	func toRACCommand() -> RACCommand<Input, Output>
}

extension ActionProtocol where Input: OptionalProtocol, Input.Wrapped: AnyObject, Output: AnyObject {
	func toRACCommand() -> RACCommand<Input.Wrapped, Output>
}

extension ActionProtocol where Input: AnyObject, Output: OptionalProtocol, Output.Wrapped: AnyObject {
	func toRACCommand() -> RACCommand<Input, Output.Wrapped>
}

extension ActionProtocol where Input: OptionalProtocol, Input.Wrapped: AnyObject, Output: OptionalProtocol, Output.Wrapped: AnyObject {
	func toRACCommand() -> RACCommand<Input.Wrapped, Output.Wrapped>
}

NOTE: The executing properties of actions and commands are not synchronized across the API bridge. To ensure consistency, only observe the executing property from the base object (the one passed into the bridge, not retrieved from it), so updates occur no matter which object is used for execution.

RACScheduler and SchedulerType

Any RACScheduler instance is automatically a DateSchedulerType (and therefore a SchedulerType), and can be passed directly into any function or method that expects one.

Some (but not all) SchedulerTypes from RAC 3 can be converted into RACScheduler instances, using the toRACScheduler() method:

extension ImmediateScheduler {
	func toRACScheduler() -> RACScheduler
}

extension UIScheduler {
	func toRACScheduler() -> RACScheduler
}

extension QueueScheduler {
	func toRACScheduler() -> RACScheduler
}

RACDisposable and Disposable

Any RACDisposable instance is automatically a Disposable, and can be used directly anywhere a type conforming to Disposable is expected.

Although there is no direct conversion from Disposable into RACDisposable, it is easy to do manually:

let swiftDisposable: Disposable
let objcDisposable = RACDisposable {
    swiftDisposable.dispose()
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.