You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 18, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: Part 3 - Taming the sequence/7. Custom operators.md
+14-4Lines changed: 14 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -183,9 +183,9 @@ Internally, every Rx operator does 3 things
183
183
2. It transforms the observed sequence according to the operator's purpose.
184
184
3. It pushes the modified sequence to its own subscribers, by calling `onNext`, `onError` and `onCompleted`.
185
185
186
-
The `compose` operator works with a method that makes an observable out of another. By doing this, it spares you the trouble of doing the 3 steps above manually. That presumes that you can do the transformation by using existing operators. If the operators don't already exist, or if you think you can get better performance manually, you need to receive items manually, process them and re-push them. An `Observable.Transformer` that does this would include a`subscribe` to the source `Observable` and the creation of a new `Observable` to be returned, possibly a `Subject`.
186
+
The `compose` operator works with a method that makes an observable out of another. In doing so, it spares you the trouble of doing the 3 steps above manually: the intermediate subscribing and pushing is implicit within an Rx chain. That presumes that you can do the transformation by using existing operators. If the operators don't already exist, or if you think you can get better performance manually, you need to receive items manually, process them and re-push them. An `Observable.Transformer` that does this would include an explicit`subscribe` to the source `Observable` and the creation of a new `Observable` to be returned, possibly a `Subject`.
187
187
188
-
There's a simpler way with `lift`. The `lift` operator is similar to `compose`. Instead of transforming `Observable`, it transforms `Subscriber`.
188
+
There's a simpler way with `lift`. The `lift` operator is similar to `compose`. Instead of transforming `Observable`, it transforms `Subscriber`. The boilerplate of subscribing with be handled by `lift`.
189
189
190
190
```java
191
191
publicfinal<R>Observable<R> lift(Observable.Operator<? extends R,? super T> lift)
@@ -299,7 +299,7 @@ Completed
299
299
Unsubscribed
300
300
```
301
301
302
-
Despite what our observable tried to emit, the end result obeyed the Rx contract. That happened because `subscribe`will temrinate the subscription when the sequence terminates (or was supposed to have terminated). This doesn't mean that the problem will always be taken care for us. There is also a method called `unsafeSubscribe`, which won't unsubscribe automatically.
302
+
Despite what our observable tried to emit, the end result obeyed the Rx contract. That happened because `subscribe`terminated the subscription when it (very reasonably) thought that the sequence ended. This doesn't mean that the problem will always be taken care for us. There is also a method called `unsafeSubscribe`, which won't unsubscribe automatically.
Our subscriber's intended behaviour was identical to the previous example (we created an instance of `Subscriber` because `unsafeSubscribe` doesn't have overloads that take lambdas). However, we can see here we weren't unsubscribed and we kept receiving notifications.
340
+
Our subscriber's intended behaviour was identical to the previous example (we created an instance of `Subscriber` because `unsafeSubscribe` doesn't have overloads that take lambdas). However, we can see here we weren't unsubscribed and we kept receiving notifications.
341
341
342
342
`unsafeSubscribe` is unsafe in other regards as well, such as error handling. It's usefulness is limited. The documentation says that it should only be used for custom operators that use nested subscriptions. To protect such operators from receiving and illegal sequence, we can apply the `serialize` operator
343
343
@@ -388,6 +388,16 @@ We here that, despite the fact that we did not unsubscribe, the illegal notifica
388
388
If the ability to use your custom operator in the chain like a standard operator is not convincing enough, using `lift` has one more unexpected advantage. Standard operators are also implemented using `lift`, which makes `lift` a hot method at runtime. JVM optimises for `lift` and operators that use `lift` receive a performance boost. That can include your operator, if you use `lift`.
389
389
390
390
391
+
## Choosing between `lift` and `compose`
392
+
393
+
Both `lift` and `compose` are meta-operators, used for injecting a custom operator into the chain. In both cases, the custom operator can be implemented as a function or a class.
394
+
*`compose`: `Observable.Transformer` or `Func<Observable<TSource>, Observable<TReturn>>`
395
+
*`lift`: `Observable.Operator` or `Func<Subscriber<TReturn>, Subscriber<TSource>>`
396
+
397
+
Theoretically, any operator can be implemented as both `Observable.Operator` and `Observable.Transformer`. The choice between the two is a question of convenience, and what kind of boilerplate you want to avoid.
398
+
399
+
* If the custom operator is a composite of existing operators, `compose` is a natural fit.
400
+
* If the custom operator needs to extract values from the pipeline to process them and then push them back, `lift` is a better fit.
0 commit comments