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
Copy file name to clipboardExpand all lines: README.md
+79Lines changed: 79 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -229,6 +229,85 @@ bindings.add(binding2);
229
229
bindings.dispose();
230
230
```
231
231
232
+
###CompositeObservable
233
+
234
+
In UI development, it is not uncommon to have an event triggered in multiple places, or have inputs coming from multiple UI controls. Let's say you want to make a `refresh()` method callable from a `Button`, a `MenuItem`, and a <kbd>CTRL</kbd> + <kbd>R</kbd> hotkey combination.
But this is preferable only if all the declarations are easily accessible. If they are in separate places throughout your UI code, this is problematic. Complex UI's are likely to be highly decoupled and have a model backing all the event flows. You cannot "add" and "remove" Observables in an `Observable.merge()` operation, and this can make designing the model rather frustrating.
262
+
263
+
At this point, you may be tempted to resort to a `Subject` to act as a sort of [event bus](https://github.com/google/guava/wiki/EventBusExplained) accepting inputs from any number of sources and outputs to any number of subscribers. Although this is a valid use case, Subjects are prone to abuse and can introduce many antipatterns.
264
+
265
+
Introducing the `CompositeObservable`. It is a tighter, safer alternative to a `Subject` or an event bus. You can `add()` and `remove()` Observables at any time from a `CompositeObservable`, and this is useful to put in an event model backing the application.
Wherever the three controls are declared, you can `add()` the `Observable<ActionEvent>` from each control to the `CompositeObservable<ActionEvent>`. It will then merge them. You can then call the `toObservable()` to subscribe wherever those events are needed.
Every time you `add()` or `remove()` an `Observable` to a `CompositeObservable`, it will affect all existing Subscribers. For UI development, this is good because there is no sensitivity to the order of adding Observables and subscribing.
308
+
309
+
Of course, you can pass around any type `T` in a `CompositeObservable<T>` and not just `ActionEvent`. It can be = helpful to pass around entire data structures, such as `CompositeObservable<Set<MyType>>`, to relay requests and inputs between controls.
0 commit comments