Reactive Interfaces Keuller Magalhães @keullermag
Agenda ● Reactive Extensions ● Observer Pattern ● Iterator Pattern ● Observable Stream ● State Management ● Demos ● Q&A
Reactive Extensions ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern and Functional Programming.
Observer Pattern Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically.
Iterator Pattern This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation.
Reactive Extensions ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences.
Observable Stream Observable model allows you to treat streams of asynchronous events with the same sort of simple, composable operations that you use for collections of data items like arrays. Observer + Iterator = Observable
Observable Stream let source = Rx.Observable.create(function(observer) { observer.next(1); observer.next(2); observer.next(3); observer.complete(); return function() { console.log(‘disposed!’); } }); source.subscribe( function(value) { console.log(‘Next:’, value); }, function(err) { console.error(err) }, function() { console.log(‘completed!’); );
Observable Stream // output Next: 1 Next: 2 Next: 3 completed!
Observable Stream Operators by Category: - Creating - Transforming - Filtering - Combining - Error Handling - Conditional - Mathematical - Backpressure
State Management State is the information that represents the current User’s context. Application state is composed by: - All data persisted across page refresh - All data persisted across route change - All data used in multiple places in the UI
State Management Flux Pattern
State Management Redux Pattern
State Management Flux Pattern Redux Pattern
State Management How can I manage the application state using RxJS ?
State Management const stream$ = new Rx.BehaviorSubject() const createStore = (rootReducer, initialState) => stream$.mergeMap(ensureStream) .scan(rootReducer, initialState) const dispatch = (actionType, data) => stream$.next({ type: actionType, payload: data})
State Management const isStream = (value) => (value instanceof Rx.Observable) const ensureStream = (value) => (isStream(value) ? action : Rx.Observable.of(value)) const createAction = (actionType) => { return (data) => { dispatcher(actionType, data) } }
State Management How to handle asynchronous actions ? // dispatcher v2 const dispatch = (actionType, data) => { stream$.next({ type: actionType, payload: data}) if (isStream(data)) { stream$.next(data) } }
State Management How to create asynchronous actions ? const fetchContacts = createAction(‘Bbt.Fetch’); const loadContacts = createAction(‘Bbt.Load’); let source = Rx.Observable.fromPromise(fetch(url) .then(resp => resp.json())) .map(list => loadContacts(list)) fetchContacts(source)
Referências http://reactivex.io https://www.tutorialspoint.com/design_pattern/observer_pattern.htm https://www.tutorialspoint.com/design_pattern/iterator_pattern.htm http://rxmarbles.com/ Keuller Magalhães kmagalhaes@uolinc.com

JS Experience 2017 - Reactive Interfaces com React & RxJS