Skip to content

Commit

Permalink
Add Rx migration cheatsheet (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
mluisbrown committed Jun 10, 2020
1 parent f037139 commit 58d92aa
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 3 deletions.
118 changes: 118 additions & 0 deletions Documentation/RxCheatsheet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# RxSwift to ReactiveSwift Cheatsheet
This is a Cheatsheet for [RxSwift](https://github.com/ReactiveX/RxSwift) developers migrating to projects using [ReactiveSwift](https://github.com/ReactiveCocoa/ReactiveSwift).

Inspired by the [RxSwift to Combine cheatsheet](https://github.com/CombineCommunity/rxswift-to-combine-cheatsheet)

## Basics

| | RxSwift | ReactiveSwift |
|-----------------------|----------------------------------|--------------------------------------------|
| Deployment Target | iOS 8.0+ | iOS 8.0+
| Platforms supported | iOS, macOS, tvOS, watchOS, Linux | iOS, macOS, tvOS, watchOS, Linux
| Spec | Reactive Extensions (ReactiveX) | Originally ReactiveX, with significant divergence
| Framework Consumption | Third-party | Third-party
| Maintained by | Open-Source / Community | Open-Source / Community
| UI Bindings | RxCocoa | ReactiveCocoa


## Core Components

| RxSwift | ReactiveSwift | Notes |
|---------------------------|---------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| AnyObserver | Signal.Observer | In practice, since there are no different Observer types, the AnyObserver concept is redundant in ReactiveSwift
| BehaviorRelay | Property / MutableProperty | Since `MutableProperty` can never have errors, we don't need a Relay specific version.
| BehaviorSubject | Property / MutableProperty |
| Completable | Signal / SignalProducer | A `Signal` or `SignalProducer` where `Value == Never` can only complete or emit an error event
| CompositeDisposable | CompositeDisposable |
| ConnectableObservableType ||
| Disposable | Disposable | In ReactiveSwift you rarely have to keep hold of Disposables or manage their lifetime manually, it's mostly automatic.
| DisposeBag | CompositeDisposable | The concepte of a DisposeBag is not really needed in ReactiveSwift, see above.
| Driver ||
| Maybe | ❌ | Trivial to create using `take(first: 1)`
| Observable | Signal / SignalProducer | Signal is a "hot" observable, and SignalProducer is a "cold" observable that will only emit values once a subscription is started
| Observer | Signal.Observer |
| PublishRelay | ❌ | Could be recreated easily in ReactiveSwift using the `flatMapError` operator on a Signal.pipe()
| PublishSubject | Signal.pipe() | There is no Subject type, but `Signal.pipe()` returns a tuple of `(output: Signal, input: Signal.Observer)` which you use to both observe and send values
| ReplaySubject | ❌ | Can be created using the `replayLazily(upTo:)` operator
| ScheduledDisposable ||
| SchedulerType | Scheduler |
| SerialDisposable | SerialDisposable |
| Signal | ❌ | Not to be confused with ReactiveSwift `Signal` which is completely different
| Single | ❌ | Could easily be created as an initializer for `SignalProducer`
| SubjectType | Signal.pipe() | There is no Subject type, but `Signal.pipe()` returns a tuple of `(output: Signal, input: Signal.Observer)` which you use to both observe and send values
| TestScheduler | TestScheduler |


## Operators

| RxSwift | Combine | Notes |
|-----------------------|------------------------------------------|----------------------------------------------------------------------------------------------------------|
| amb() | flatten(.race) |
| asObservable() | ❌ | Not required in ReactiveSwift, although `Property.producer` and `Property.signal` are similar
| asObserver() ||
| bind(to:) | <~ operator (BindingTargets) |
| buffer | ❌ | (it used to exist, but was removed)
| catchError | flatMapError |
| catchErrorJustReturn | ❌ | Easy to create as `flatMapError { _ in SignalProducer<Value, Never> (value: value) }`
| combineLatest | combineLatest |
| compactMap | compactMap |
| concat | concat / prefix |
| concatMap ||
| create | SignalProducer.init { } |
| debounce | debounce |
| debug | logEvents |
| deferred | ❌ | Trivial to create
| delay | delay |
| delaySubscription ||
| dematerialize | dematerialize |
| distinctUntilChanged | skipRepeats |
| do | on |
| elementAt ||
| empty | SignalProducer.empty |
| enumerated ||
| error | SignalProducer.init(error:) |
| filter | filter |
| first | take(first:) | See also `take(last:)`
| flatMap | flatMap(.merge) |
| flatMapFirst | flatMap(.throttle) |
| flatMapLatest | flatMap(.latest) |
| from(optional:) | ❌ | Easy to create using `.init(.value: Value?).skipNil()`
| groupBy ||
| ifEmpty(default:) ||
| ifEmpty(switchTo:) ||
| ignoreElements | ❌ | Easy to create
| interval ||
| just | SignalProducer.init(value:) |
| map | map |
| materialize | materialize |
| merge | merge |
| merge(maxConcurrent:) ||
| multicast | replayLazily(upTo:) |
| never | SignalProducer.never |
| observeOn | observe(on:) |
| of | SignalProducer.init(_ values:) |
| publish ||
| range ||
| reduce | reduce |
| refCount | ❌ | Not meaningful in ReactiveSwift
| repeatElement | repeat |
| retry, retry(3) | retry(upTo:) |
| retryWhen ||
| sample | sample(on:), sample(with:) |
| scan | scan |
| share | replayLazily(upTo:) |
| skip | skip(first:) |
| skipUntil | skip(until:) |
| skipWhile | skip(while:) |
| startWith | prefix |
| subscribe | startWithValues / observeValues |
| subscribeOn | start(on:) / observe(on:) |
| takeLast | take(last:) |
| takeUntil | take(until:) |
| throttle | throttle |
| timeout | timeout |
| timer | SignalProducer.timer |
| toArray | collect |
| window ||
| withLatestFrom | combineLatest |
| zip | zip |
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Learn about the **[Core Reactive Primitives][]** in ReactiveSwift, and **[Basic Operators][]** available offered by these primitives.

### Extended modules

<table>
<tr>
<th>Module</th>
Expand Down Expand Up @@ -48,7 +48,7 @@ Learn about the **[Core Reactive Primitives][]** in ReactiveSwift, and **[Basic
</td>
<td><p>The <a href="https://github.com/pointfreeco/swift-composable-architecture">Pointfree Composable Architecture</a> using ReactiveSwift instead of Combine.</p></td>
</tr>
</table>
</table>

## What is ReactiveSwift in a nutshell?
__ReactiveSwift__ offers composable, declarative and flexible primitives that are built around the grand concept of ___streams of values over time___.
Expand All @@ -69,6 +69,8 @@ code and state to bridge the gap.

1. **[Debugging Techniques][]**

1. **[RxSwift Migration Cheatsheet][]**

## Installation

ReactiveSwift supports macOS 10.9+, iOS 8.0+, watchOS 2.0+, tvOS 9.0+ and Linux.
Expand Down Expand Up @@ -120,7 +122,7 @@ We also provide a Playground, so you can get used to ReactiveCocoa's operators.

1. Clone the ReactiveSwift repository.
1. Retrieve the project dependencies using one of the following terminal commands from the ReactiveSwift project root directory:
- `git submodule update --init --recursive` **OR**, if you have [Carthage][] installed
- `git submodule update --init --recursive` **OR**, if you have [Carthage][] installed
- `carthage checkout`
1. Open `ReactiveSwift.xcworkspace`
1. Build `ReactiveSwift-macOS` scheme
Expand All @@ -143,6 +145,7 @@ ReactiveSwift has no plan to declare ABI and module stability at the moment. It
[API Contracts]: Documentation/APIContracts.md
[API Reference]: http://reactivecocoa.io/reactiveswift/docs/latest/
[Debugging Techniques]: Documentation/DebuggingTechniques.md
[RxSwift Migration Cheatsheet]: Documentation/RxCheatsheet.md
[Online Searching]: Documentation/Example.OnlineSearch.md
[_UI Examples_ playground]: https://github.com/ReactiveCocoa/ReactiveSwift/blob/master/ReactiveSwift-UIExamples.playground/Pages/ValidatingProperty.xcplaygroundpage/Contents.swift

Expand Down

0 comments on commit 58d92aa

Please sign in to comment.