Share
Embed
Domic — Reactive Virtual DOM. Why we built it, how it works.
In-depth technical talk explaining why Domic — Reactive Virtual DOM exists and how it works.
You'll learn how Domic:
- Uses diffing, debouncing, double buffering and other techniques to render changes efficiently
- Keeps state consistent with mutable Android DOM
- Uses advanced RxJava under the hood: sharing, custom operators, scheduling, subscription management
- Minimizes allocations and interactions with main thread
- Renders UI state in memory for unit/integration testing on JVM
- Integrates with existing design patterns: MVVM, MVI/Redux, MVP
Artem Zinnatullin
August 27, 2018
Transcript
143 slides
-
the api module - Mirrors Android Widgets & Views but
reactively - Contains only interfaces (okok and 1 inline function) - Type-safe -
the api module: Observe val textView: TextView = … textView.observe.textChanges:
Observable textView.observe.clicks: Observable textView.observe.etc: Observable -
the api module: Observe val textView: TextView = … textView.observe.textChanges:
Observable textView.observe.clicks: Observable textView.observe.etc: Observable Interface provided by Domic -
the api module: Change val textView: TextView = … textView.change.text(Observable)
textView.change.enabled(Observable) textView.change.etc(Observable) -
the api module: Change val textView: TextView = … textView.change.text(Observable)
textView.change.enabled(Observable) textView.change.etc(Observable) Interface provided by Domic -
the api module: type-safety View.Observe is read-only reference View.Change is
write-only reference View is read-write reference -
Useful side-effects of reactive api - Threading becomes an implementation
detail - Both subscribeOn and observeOn - Protects main thread from unnecessary work -
Useful side-effects of reactive api - Threading becomes an implementation
detail - No easy way for consumer to do blocking work -
Useful side-effects of reactive api - Threading becomes an implementation
detail - No easy way for consumer to do blocking work - Implementation can be complicated while being non-blocking -
the api module: View interface package com.lyft.domic.api interface View {
val observe: Observe val change: Change interface Observe { val clicks: Observable<Any> val focus: Observable<Boolean> val longClicks: Observable<Any> } interface Change { fun alpha(alphaValues: Observable<Float>): Disposable fun enabled(enabledValues: Observable<Boolean>): Disposable fun visibility(visibilityValues: Observable<Visibility>): Disp } } -
the api module: Observe package com.lyft.domic.api interface View { val
observe: Observe val change: Change interface Observe { val clicks: Observable<Any> val focus: Observable<Boolean> val longClicks: Observable<Any> } interface Change { fun alpha(alphaValues: Observable<Float>): Disposable fun enabled(enabledValues: Observable<Boolean>): Disposable fun visibility(visibilityValues: Observable<Visibility>): Disp } } -
the api module: Observe package com.lyft.domic.api interface View { val
observe: Observe val change: Change interface Observe { val clicks: Observable<Any> val focus: Observable<Boolean> val longClicks: Observable<Any> } interface Change { fun alpha(alphaValues: Observable<Float>): Disposable fun enabled(enabledValues: Observable<Boolean>): Disposable fun visibility(visibilityValues: Observable<Visibility>): Disp } } -
API: Observe, real world example signInButton .observe .clicks .withLatestFrom(credentials) {
_, creds -> creds } .flatMap { creds -> service.signIn(creds) } -
the api module: View interface package com.lyft.domic.api interface View {
val observe: Observe val change: Change interface Observe { val clicks: Observable<Any> val focus: Observable<Boolean> val longClicks: Observable<Any> } interface Change { fun alpha(alphaValues: Observable<Float>): Disposable fun enabled(enabledValues: Observable<Boolean>): Disposable fun visibility(visibilityValues: Observable<Visibility>): Disp } } -
the api module: Change package com.lyft.domic.api interface View { val
observe: Observe val change: Change interface Observe { val clicks: Observable<Any> val focus: Observable<Boolean> val longClicks: Observable<Any> } interface Change { fun alpha(alphaValues: Observable<Float>): Disposable fun enabled(enabledValues: Observable<Boolean>): Disposable fun visibility(visibilityValues: Observable<Visibility>): Disp } } -
the api module: Change package com.lyft.domic.api interface View { val
observe: Observe val change: Change interface Observe { val clicks: Observable<Any> val focus: Observable<Boolean> val longClicks: Observable<Any> } interface Change { fun alpha(alphaValues: Observable<Float>): Disposable fun enabled(enabledValues: Observable<Boolean>): Disposable fun visibility(visibilityValues: Observable<Visibility>): Disp } } -
API: Change, real world example disposable += signInRequest .map {
false } .subscribe(view.signInButton.change::enabled) -
100% reactive api? - Observables as inputs - Observables as
outputs - Proper cancellation - Property change can be cancelled within Frame window -
100% reactive api? - Observables as inputs - Observables as
outputs - Proper cancellation - No back-pressure tho, because UI generally can’t be back-pressured -
the android module - Implements API module - Binds to
real Android DOM (Widgets, Views) -
the android module - Implements API module - Binds to
real Android DOM (Widgets, Views) - Allows to reuse: - Layouts - Styles - Views/ViewGroups - Layout Preview - Support library - Any build system -
the android module - Implements API module - Binds to
real Android DOM (Widgets, Views) - Takes care of threading - subscribeOn(mainThread()) -
the android module - Implements API module - Binds to
real Android DOM (Widgets, Views) - Takes care of threading - subscribeOn(mainThread()) - observeOn() custom rendering -
the android module - Implements API module - Binds to
real Android DOM (Widgets, Views) - Takes care of threading - subscribeOn(mainThread()) - observeOn() custom rendering - Diffs each property’s new state against previous -
the android module - Implements API module - Binds to
real Android DOM (Widgets, Views) - Takes care of threading - subscribeOn(mainThread()) - observeOn() custom rendering - Diffs each property’s new state against previous - Naming convention: “Android” prefix, then the API interface name - val text: TextView = AndroidTextView(…) -
Binding to real Android DOM: RxBinding “What can be easier
than using RxBinding? , right? Famous last words -
Binding to real Android DOM: RxBinding “What can be easier
than using RxBinding? “Why would you even need a library on top of it?” , right? Famous last words -
Binding to real Android DOM: RxBinding - Observables are not
shared! - Some properties only allow one listener at a time! - ie RxView.clicks() -
Binding to real Android DOM: RxBinding - Observables are not
shared! - Some properties only allow one listener at a time! - ie RxView.clicks() - Subscription time threading is not enforced -
Binding to real Android DOM: RxBinding - Observables are not
shared! - Some properties only allow one listener at a time! - ie RxView.clicks() - Subscription time threading is not enforced - And it’s fine! RxBinding is a low-level, foundation layer -
Threading - subscribeOn(mainThread()) is okay - Async Handler? Maybe -
observeOn(mainThread()) is easy -
Threading - subscribeOn(mainThread()) is okay - Async Handler? Maybe -
observeOn(mainThread()) is easy - But that’s too easy - If you often render partially same state, you might want something better -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> This is how you get stable 60 fps! -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() This is how you get stable 60 fps! Reviewed by David Karnok -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() 1. It is a custom RxJava Operator 2. Uses AtomicReferenceArray as shared state container 3. Designed to handle multiple change streams bound to the same property at the same time This is how you get stable 60 fps! Reviewed by David Karnok -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() 3. Value maps to a Change object This is how you get stable 60 fps! -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() 3. Value maps to a Change object A. Optional optimizations, like PrecomputedText This is how you get stable 60 fps! -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() 3. Value maps to a Change object 4. Renderer observes the Change This is how you get stable 60 fps! -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() 3. Value maps to a Change object 4. Renderer observes the Change 5. Renderer adds or replaces the Change in Buffer This is how you get stable 60 fps! -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() 3. Value maps to a Change object 4. Renderer observes the Change 5. Renderer adds or replaces the Change in Buffer • Allows Domic “debounce” equal changes within frame interval! Basically a BackpressureStrategy.LATEST This is how you get stable 60 fps! -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() 3. Value maps to a Change object 4. Renderer observes Change 5. Renderer adds or replaces the Change in Buffer • Allows Domic “debounce” equal changes within frame interval! Basically a BackpressureStrategy.LATEST • Allows Domic to minimize jumps to the Main Thread. Basically a BackpressureStrategy.BUFFER This is how you get stable 60 fps! -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() 3. Value maps to a Change object 4. Renderer observes the Change 5. Renderer adds or replaces the Change in Buffer 6. Choreographer calls the Renderer This is how you get stable 60 fps! -
How a property change gets rendered 1. Domic subscribes to
Observable<Value> 2. Value goes through sharedDistinctUntilChanged() 3. Value maps to a Change object 4. Renderer observes the Change 5. Renderer adds or replaces the Change in Buffer 6. Choreographer calls the Renderer 7. Renderer swaps the Buffer and renders current one This is how you get stable 60 fps! -
the test module - Implements API module in memory -
Diffs the changes -
the test module - Implements API module in memory -
Adds Simulate interface to each Widget -
the test module - Implements API module in memory -
Adds Simulate interface to each Widget - Adds Check interface to each Widget -
the test module - Implements API module in memory -
Adds Simulate interface to each Widget - Adds Check interface to each Widget - Designed to work on JVM -
the test module - Implements API module in memory -
Adds Simulate interface to each Widget - Adds Check interface to each Widget - Designed to work on JVM - Allows to test ViewModels, Presenters, etc -
the test module - Implements API module in memory -
Adds Simulate interface to each Widget - Adds Check interface to each Widget - Designed to work on JVM - Allows to test ViewModels, Presenters, etc - Extreme: ~whole app can be rendered in memory -
the test module @Test fun `sign in button enabled if
valid credentials entered`() { view.emailEditText.simulate.text("[email protected]") view.passwordEditText.simulate.text("password") assertThat(view.signInButton.check.enabled).isTrue() } -
the test module @Test fun `sign in button enabled if
valid credentials entered`() { view.emailEditText.simulate.text("[email protected]") view.passwordEditText.simulate.text("password") assertThat(view.signInButton.check.enabled).isTrue() } -
the test module @Test fun `sign in button enabled if
valid credentials entered`() { view.emailEditText.simulate.text("[email protected]") view.passwordEditText.simulate.text("password") assertThat(view.signInButton.check.enabled).isTrue() } -
the test module @Test fun `sign in button enabled if
valid credentials entered`() { view.emailEditText.simulate.text("[email protected]") view.passwordEditText.simulate.text("password") assertThat(view.signInButton.check.enabled).isTrue() } -
the test module @Test fun `sign in button enabled if
valid credentials entered`() { view.emailEditText.simulate.text("[email protected]") view.passwordEditText.simulate.text("password") assertThat(view.signInButton.check.enabled).isTrue() } -
Domic + MVVM2: View interface SignInView { val emailEditText: EditText
val passwordEditText: EditText val signInButton: Button val resultTextView: TextView } -
Domic + MVVM2: AndroidView class AndroidSignInView(root: ViewGroup, renderer: Renderer) :
SignInView { override val emailEditText = AndroidEditText(root.findViewById(R.id.email_edit_text), renderer) override val passwordEditText = AndroidEditText(root.findViewById(R.id.password_edit_text), renderer) override val signInButton = AndroidButton(root.findViewById(R.id.sign_in_button), renderer) override val resultTextView = AndroidTextView(root.findViewById(R.id.sign_result_text_view), renderer) } -
Domic + MVVM2: Thoughts https://github.com/lyft/domic/tree/master/samples/mvvm - Often times emits same
property values due to its reactive nature -
Domic + MVVM2: Thoughts https://github.com/lyft/domic/tree/master/samples/mvvm - Often times emits same
property values due to its reactive nature - Spawns lots of individual rx streams which need to do proper threading -
Domic + MVVM2: Thoughts https://github.com/lyft/domic/tree/master/samples/mvvm - Often times emits same
property values due to its reactive nature - Spawns lots of individual rx streams which need to do proper threading - Uses same rx streams from View multiple times, they need to be shared -
Domic + MVVM2: Thoughts https://github.com/lyft/domic/tree/master/samples/mvvm - Often times emits same
property values due to its reactive nature - Spawns lots of individual rx streams which need to do proper threading - Uses same rx streams from View multiple times, they need to be shared - Reactive View layer becomes hard to swap with test implementation -
Domic + MVVM2: Thoughts https://github.com/lyft/domic/tree/master/samples/mvvm - Often times emits same
property values due to its reactive nature - Spawns lots of individual rx streams which need to do proper threading - Uses same rx streams from View multiple times, they need to be shared - Reactive View layer becomes hard to swap with test implementation - Domic fits MVVM2 really well and compliments it in many ways -
Domic + Redux: Thoughts - Most of the time emits
state that is mostly equal to previous one https://github.com/lyft/domic/tree/master/samples/redux -
Domic + Redux: Thoughts - Most of the time emits
state that is mostly equal to previous one - Spawns less rx streams compared to MVVM https://github.com/lyft/domic/tree/master/samples/redux -
Domic + Redux: Thoughts - Most of the time emits
state that is mostly equal to previous one - Spawns less rx streams compared to MVVM - Input rx streams are merged into a single stream, RxBinding actually works well here https://github.com/lyft/domic/tree/master/samples/redux -
Domic + Redux: Thoughts - Most of the time emits
state that is mostly equal to previous one - Spawns less rx streams compared to MVVM - Input rx streams are merged into a single stream, RxBinding actually works well here - Output rx stream is merged into a single one, threading is easy but still needs https://github.com/lyft/domic/tree/master/samples/redux -
Domic + Redux: Thoughts - Most of the time emits
state that is mostly equal to previous one - Spawns less rx streams compared to MVVM - Input rx streams are merged into a single stream, RxBinding actually works well here - Output rx stream is merged into a single one, threading is easy but still needs - View layer is basically a render(State) function and an actions: Observable<Action> property https://github.com/lyft/domic/tree/master/samples/redux -
Domic + Redux: Thoughts - Most of the time emits
state that is mostly equal to previous one - Spawns less rx streams compared to MVVM - Input rx streams are merged into a single stream, RxBinding actually works well here - Output rx stream is merged into a single one, threading is easy but still needs - View layer is basically a render(State) function and an actions: Observable<Action> property - Domic’s API as of now feels a bit off for Redux, but https://github.com/lyft/domic/tree/master/samples/redux -
DSL/declarative API? - Web developers love React, especially with Redux
- Lots of trade-offs and corner cases -
DSL/declarative API? - Web developers love React, especially with Redux
- Lots of trade-offs & corner cases - Layout files? -
DSL/declarative API? - Web developers love React, especially with Redux
- Lots of corner-cases & trade-offs - Layout files? - Styling? -
DSL/declarative API? - Web developers love React, especially with Redux
- Lots of corner-cases & trade-offs - Layout files? - Styling? - Reactive? -
DSL/declarative API? - Web developers love React, especially with Redux
- Lots of corner-cases & trade-offs - Layout files? - Styling? - Reactive? - Maybe!** **Is not a public offer, other restrictions apply -
Domic or fb/Litho - Controls the DOM - Declarative -
Own Component system - Components can’t really use Android Layouts/Widgets - Component system is designed to be used with annotation processing - Uses crazy-awesome fb/Yoga for efficient rendering - Scalable for complicated screens like RecyclerView - Calculates diffs - Reactive? https://fblitho.com/ -
Domic or airbnb/Epoxy - Controls the DOM - Declarative -
Own Component system - Components can use Android Layouts/Widgets/Views - Component system is designed to be used with annotation processing - Scalable like RecylerView and actually uses RV - Calculates diffs - Reactive? https://github.com/airbnb/epoxy -
Domic or fb/ReactNative - Controls the DOM - Declarative -
Own Component system - Components can use Android Layouts/Widgets/Views - JavaScript - Calculates diffs - Reactive? https://facebook.github.io/react-native/ -
Domic or passy/Konduit - Controls the DOM - Declarative -
Own Widget system - Widgets can use Android Layouts/Widgets/Views - Calculates diffs - Reactive? https://github.com/passsy/Konduit -
Should Domic …? - Control the DOM or only Bind
to it? - Provide declarative/DSL-based API? -
Should Domic …? - Control the DOM or only Bind
to it? - Provide declarative/DSL-based API? - Stay 100% reactive? -
Should Domic …? - Control the DOM or only Bind
to it? - Provide declarative/DSL-based API? - Stay 100% reactive? - Wait, did I mention reactive animations? -
Should Domic …? - Control the DOM or only Bind
to it? - Provide declarative/DSL-based API? - Be actually reactive? - Wait, did I mention reactive animations? Maybe yes!** Maybe no** **Is not a public offer, other restrictions apply