Cascadia.js: Don't Cross the Streams

Don’t Cross the Streams…

Matthew Podwysocki
@mattpodwysocki
Cascadia.js: Don't Cross the Streams
Callback Hell…
app.get('/index', function (req, res, next) {
    User.get(req.params.userId, function (err, user) {
        if (err) next(err);
        db.find({user: user.name}, function (err, cursor) {
            if (err) next(err);
            cursor.toArray(function (err, items) {
                if (err) next(err);
                res.send(items);
            });
        });
    });
});
                      …and the Pyramid of Doom
The promise of a better future…


tweetsAsync()
    .then(function (a) { return usersAsync(a); })
    .then(function (b) { return locationAsync(b); })
    .done(function (c) { render(); });
What about events?
• What about composition?
• How do I clean up handlers?
• How do I merge streams?
• Or filter?
• Or aggregate?
• Without having a lot of state hanging around?
• The list goes on and on…
In other words…




                  We cross the streams!
What is it?
• RxJS is…
  • a set of types representing asynchronous data streams
  • a set of operators over asynchronous data streams
  • a set of types to parameterize concurrency


RxJS = Observables + LINQ + Schedulers
Not another flow control library…

                             Why bother?
var mousedrag = mousedown.selectMany(function (md) {

      // calculate offsets when mouse down
      var startX = md.offsetX,
          startY = md.offsetY;

      // calculate diffs until mouse up
      return mousemove.select(function (mm) {
          return {
              left: mm.clientX - startX,
              top: mm.clientY - startY
          };
      }).takeUntil(mouseup);
});
                            Rich Composite Events…
var words = Rx.Observable.fromEvent(input, "keyup")
            .select(function(x) { return input.value; })
            .throttle(500)
            .distinctUntilChanged()
            .select(function(term) { return search(term); })
            .switchLatest();

words.subscribe(function (data) {
    // Bind data to the UI
});



                                         Control Flow…
stockTicks
  .groupBy(function (tick) { return tick.symbol; })
  .selectMany(
    function (g) { return g.bufferWithCount(2, 1); },
    function (g, b) { return { key: g.key, b: b }; })
  .select(function (c) {
    return { diff: (c.b[1] – c.b[0]) / c.b[0], symbol: c.key };
  })
  .where(function (c) { return c.diff > 0.1; })
  .select(function (c) {
    return { company: c.symbol, increase: c.diff };
  });
                     Complex Event Processing…
var sequence = '38,38,40,40,37,39,37,39,66,65';

var keyups = Rx.Observable.fromEvent(document, 'keyup')
     .select(function (x) { return x.keyCode; })
     .bufferWithCount(10, 10)
     .subscribe(function (x) {
           var matches = x.join() === sequence;
           if (matches) console.log('Konami');
     });
What is it?
• RxJS is…
  • a set of types representing asynchronous data streams
  • a set of operators over asynchronous data streams
  • a set of types to parameterize concurrency


RxJS = Observables + LINQ + Schedulers
The Essentials…
Observer = {
  onNext :
    function (data) { … },        Observable = {
  onError :                         subscribe :
    function (error) { … },           function (observer) { … }
  onCompleted :                   };
    function ()      { … }
};
                                           Observable

                      Subscribe


   Observer
The Grammar Police…

•   Zero or more values
                       *          ( OnError       | OnCompleted ) ?
•   E.g. events are ∞ sequences



                   0                   1                     2


                       0               1
                                                     • It’s a sequence
                   0        1          2             • No concurrent
                                                       callbacks
                                                     • One time termination
                            0      1          2
First Class Events…
What?
• RxJS is…
  • a set of types representing asynchronous data streams
  • a set of operators over asynchronous data streams
  • a set of types to parameterize concurrency


RxJS = Observables + LINQ + Schedulers
If you know ES5 Array “extras”…
Array              Observable
• Querying         • Querying
   •    concat        •   concat
   •    every         •   all
   •    filter        •   where
   •    map           •   select
   •    reduce        •   aggregate
   •    some          •   any

• Subscribing      • Subscribing
   • forEach          • subscribe


                             … then you know RxJS
Querying Asynchronous Streams

• Observables are data streams
   • Hence we can query them like we do with Arrays!



     function
return
Querying Asynchronous Streams

• Observables are asynchronous
  • Hence, they have a notion of time
Limited only by imagination…
aggregate                   generateWithRelativeTime   selectMany
all                         groupBy                    sequenceEqual
amb                                                    skip
                            groupByUntil               skipLast
any                         groupJoin                  skipUntil
asObservable                                           skipWhile
average                     ignoreElements
                            interval                   start
buffer                                                 startWith
                            join                       subscribe
bufferWithCount
bufferWithTime              materialize                subscribeOn
                            max                        sum
bufferWithTimeOrCount                                  switchLatest
catchException              maxBy                      take
combineLatest               merge                      takeLast
concat                      mergeObservable            takeLastBuffer
concatObservable            min                        takeUntil
contains                    minBy                      takeWhile
count                       multicast                  then
create                                                 throttle
                            never                      throwException
createWithDisposable
defaultIfEmpty              observeOn                  timeInterval
                            onErrorResumeNext          timeout
defer                                                  timer
delay                       publish                    using
dematerialize               publishLast                when
distinct                    range                      where
disinctUntilChanged         refCount                   window
doAction                    repeat                     windowWithCount
elementAt                                              windowWithAbsoluteTime
elementAtOrDefault          replay                     windowWithRelativeTime
empty                       retry                      zip
generate                    returnValue
generateWithAbsoluteTime    sample
                            scan
                            select
Creation operators
                   create*
                   empty
                 fromArray
                 generate*
                    never
                    range
                   repeat
                returnValue
Standard operators
                     distinct
             distinctUntilChanged
                    groupBy
                      select
                   selectMany
                      skip*
                      take*
                      where
Aggregation operators
                aggregate
                    all
                   any
                 contains
                  count
                   min
                   max
                   scan
Error handling operators

                catchException
                  finallyAction
              onErrorResumeNext
                      retry
                throwException
                      using
Complex Event operators


                 buffer*
                groupJoin
                   join
                window*
                 sample
Orchestration operators
               combineLatest
                   concat
                  forkJoin
                   merge
                 skipUntil
                switchLatest
                 takeUntil
                     zip
Time based operators

                    delay
                  interval
                  throttle
                  timeout
               timeInterval
                    timer
                timeStamp
What?
• RxJS is…
  • a set of types representing asynchronous data streams
  • a set of operators over asynchronous data streams
  • a set of types to parameterize concurrency


RxJS = Observables + LINQ + Schedulers
The Role of Schedulers…
                                                          Many
                                                     implementations
• Key questions:
                                Cancellation
  • How to run timers?
  • Where to produce events?                   d = scheduler.schedule(
                                                 function () {
  • Need to synchronize with the UI?
                                                   // Asynchronously
                                                   // running work
• Providing one answer:                          },
  • Schedulers introduce concurrency             1000);
  • Operators are parameterized by schedulers              Optional time
  • Provides test benefits as well
var scheduler = new TestScheduler();

var input = scheduler.createColdObservable(
    onNext(300, "Cascadia"),
    onNext(400, "JS"),
    onCompleted(500));

var results = scheduler.startWithCreate(function () {
    input.select(function (x) { return x.length; })
});

results.messages.assertEqual(
    onNext(300, 8),
    onNext(400, 2),                    Testability is King!
    onCompleted(500));
Start using it now
Node.js
> npm install rx

NuGet
> Install-Package RxJS

Open Source
http://rx.codeplex.com/
What’s Next?
• Ongoing work
  • Documentation
  • Tutorials


• Where’s the vision?
  • Scaling
  • Distributed
  • Merge with Node.js Streams
Cross the Streams…

Matthew Podwysocki
@mattpodwysocki
matthewp@microsoft.com
Credits
• Cross the Streams -
  https://oshea12566.wordpress.com/2012/02/20/jeep-meeting-and-
  trail-ride/
• NES Controller - http://en.wikipedia.org/wiki/File:NES-controller.jpg
1 of 35

More Related Content

What's hot(20)

Angular2 rxjsAngular2 rxjs
Angular2 rxjs
Christoffer Noring1.8K views
RxJS 5 in DepthRxJS 5 in Depth
RxJS 5 in Depth
C4Media1.4K views
Rxjs pptRxjs ppt
Rxjs ppt
Christoffer Noring3.7K views
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
Sandi Barr550 views
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
Brainhub3.7K views
rx.js make async programming simplerrx.js make async programming simpler
rx.js make async programming simpler
Alexander Mostovenko1.2K views
Oop assignment 02Oop assignment 02
Oop assignment 02
MamoonKhan39145 views
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJS
Mattia Occhiuto651 views
Reactive Programming with RxSwiftReactive Programming with RxSwift
Reactive Programming with RxSwift
Scott Gardner4.9K views
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
Christoffer Noring1.1K views
Rxjs kyivjs 2015Rxjs kyivjs 2015
Rxjs kyivjs 2015
Alexander Mostovenko662 views

Similar to Cascadia.js: Don't Cross the Streams(20)

响应式编程及框架响应式编程及框架
响应式编程及框架
jeffz1.5K views
Rx workshopRx workshop
Rx workshop
Ryan Riley2.4K views
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Sanjeev Tripathi583 views
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
sanjeeviniindia1186966 views
Reactive xReactive x
Reactive x
Gabriel Araujo96 views
Java Tutorial Java Tutorial
Java Tutorial
Akash Pandey172 views
Rxjs marble-testingRxjs marble-testing
Rxjs marble-testing
Christoffer Noring1.3K views
Akka london scala_user_groupAkka london scala_user_group
Akka london scala_user_group
Skills Matter1.1K views
Reacting with ReactiveUIReacting with ReactiveUI
Reacting with ReactiveUI
kiahiska2.2K views
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
Tarunsingh198221 views
Kotlin coroutines and spring frameworkKotlin coroutines and spring framework
Kotlin coroutines and spring framework
Sunghyouk Bae2.9K views
Java tut1Java tut1
Java tut1
Sumit Tambe188 views
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
Graham Royce964 views

Recently uploaded(20)

ChatGPT and AI for Web DevelopersChatGPT and AI for Web Developers
ChatGPT and AI for Web Developers
Maximiliano Firtman161 views
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
Prity Khastgir IPR Strategic India Patent Attorney Amplify Innovation24 views
Java Platform Approach 1.0 - Picnic MeetupJava Platform Approach 1.0 - Picnic Meetup
Java Platform Approach 1.0 - Picnic Meetup
Rick Ossendrijver24 views
Liqid: Composable CXL PreviewLiqid: Composable CXL Preview
Liqid: Composable CXL Preview
CXL Forum120 views
Green Leaf Consulting: Capabilities DeckGreen Leaf Consulting: Capabilities Deck
Green Leaf Consulting: Capabilities Deck
GreenLeafConsulting177 views
The Research Portal of Catalonia: Growing more (information) & more (services)The Research Portal of Catalonia: Growing more (information) & more (services)
The Research Portal of Catalonia: Growing more (information) & more (services)
CSUC - Consorci de Serveis Universitaris de Catalunya59 views

Cascadia.js: Don't Cross the Streams

  • 1. Don’t Cross the Streams… Matthew Podwysocki @mattpodwysocki
  • 3. Callback Hell… app.get('/index', function (req, res, next) { User.get(req.params.userId, function (err, user) { if (err) next(err); db.find({user: user.name}, function (err, cursor) { if (err) next(err); cursor.toArray(function (err, items) { if (err) next(err); res.send(items); }); }); }); }); …and the Pyramid of Doom
  • 4. The promise of a better future… tweetsAsync() .then(function (a) { return usersAsync(a); }) .then(function (b) { return locationAsync(b); }) .done(function (c) { render(); });
  • 5. What about events? • What about composition? • How do I clean up handlers? • How do I merge streams? • Or filter? • Or aggregate? • Without having a lot of state hanging around? • The list goes on and on…
  • 6. In other words… We cross the streams!
  • 7. What is it? • RxJS is… • a set of types representing asynchronous data streams • a set of operators over asynchronous data streams • a set of types to parameterize concurrency RxJS = Observables + LINQ + Schedulers
  • 8. Not another flow control library… Why bother?
  • 9. var mousedrag = mousedown.selectMany(function (md) { // calculate offsets when mouse down var startX = md.offsetX, startY = md.offsetY; // calculate diffs until mouse up return mousemove.select(function (mm) { return { left: mm.clientX - startX, top: mm.clientY - startY }; }).takeUntil(mouseup); }); Rich Composite Events…
  • 10. var words = Rx.Observable.fromEvent(input, "keyup") .select(function(x) { return input.value; }) .throttle(500) .distinctUntilChanged() .select(function(term) { return search(term); }) .switchLatest(); words.subscribe(function (data) { // Bind data to the UI }); Control Flow…
  • 11. stockTicks .groupBy(function (tick) { return tick.symbol; }) .selectMany( function (g) { return g.bufferWithCount(2, 1); }, function (g, b) { return { key: g.key, b: b }; }) .select(function (c) { return { diff: (c.b[1] – c.b[0]) / c.b[0], symbol: c.key }; }) .where(function (c) { return c.diff > 0.1; }) .select(function (c) { return { company: c.symbol, increase: c.diff }; }); Complex Event Processing…
  • 12. var sequence = '38,38,40,40,37,39,37,39,66,65'; var keyups = Rx.Observable.fromEvent(document, 'keyup') .select(function (x) { return x.keyCode; }) .bufferWithCount(10, 10) .subscribe(function (x) { var matches = x.join() === sequence; if (matches) console.log('Konami'); });
  • 13. What is it? • RxJS is… • a set of types representing asynchronous data streams • a set of operators over asynchronous data streams • a set of types to parameterize concurrency RxJS = Observables + LINQ + Schedulers
  • 14. The Essentials… Observer = { onNext : function (data) { … }, Observable = { onError : subscribe : function (error) { … }, function (observer) { … } onCompleted : }; function () { … } }; Observable Subscribe Observer
  • 15. The Grammar Police… • Zero or more values * ( OnError | OnCompleted ) ? • E.g. events are ∞ sequences 0 1 2 0 1 • It’s a sequence 0 1 2 • No concurrent callbacks • One time termination 0 1 2
  • 17. What? • RxJS is… • a set of types representing asynchronous data streams • a set of operators over asynchronous data streams • a set of types to parameterize concurrency RxJS = Observables + LINQ + Schedulers
  • 18. If you know ES5 Array “extras”… Array Observable • Querying • Querying • concat • concat • every • all • filter • where • map • select • reduce • aggregate • some • any • Subscribing • Subscribing • forEach • subscribe … then you know RxJS
  • 19. Querying Asynchronous Streams • Observables are data streams • Hence we can query them like we do with Arrays! function return
  • 20. Querying Asynchronous Streams • Observables are asynchronous • Hence, they have a notion of time
  • 21. Limited only by imagination… aggregate generateWithRelativeTime selectMany all groupBy sequenceEqual amb skip groupByUntil skipLast any groupJoin skipUntil asObservable skipWhile average ignoreElements interval start buffer startWith join subscribe bufferWithCount bufferWithTime materialize subscribeOn max sum bufferWithTimeOrCount switchLatest catchException maxBy take combineLatest merge takeLast concat mergeObservable takeLastBuffer concatObservable min takeUntil contains minBy takeWhile count multicast then create throttle never throwException createWithDisposable defaultIfEmpty observeOn timeInterval onErrorResumeNext timeout defer timer delay publish using dematerialize publishLast when distinct range where disinctUntilChanged refCount window doAction repeat windowWithCount elementAt windowWithAbsoluteTime elementAtOrDefault replay windowWithRelativeTime empty retry zip generate returnValue generateWithAbsoluteTime sample scan select
  • 22. Creation operators create* empty fromArray generate* never range repeat returnValue
  • 23. Standard operators distinct distinctUntilChanged groupBy select selectMany skip* take* where
  • 24. Aggregation operators aggregate all any contains count min max scan
  • 25. Error handling operators catchException finallyAction onErrorResumeNext retry throwException using
  • 26. Complex Event operators buffer* groupJoin join window* sample
  • 27. Orchestration operators combineLatest concat forkJoin merge skipUntil switchLatest takeUntil zip
  • 28. Time based operators delay interval throttle timeout timeInterval timer timeStamp
  • 29. What? • RxJS is… • a set of types representing asynchronous data streams • a set of operators over asynchronous data streams • a set of types to parameterize concurrency RxJS = Observables + LINQ + Schedulers
  • 30. The Role of Schedulers… Many implementations • Key questions: Cancellation • How to run timers? • Where to produce events? d = scheduler.schedule( function () { • Need to synchronize with the UI? // Asynchronously // running work • Providing one answer: }, • Schedulers introduce concurrency 1000); • Operators are parameterized by schedulers Optional time • Provides test benefits as well
  • 31. var scheduler = new TestScheduler(); var input = scheduler.createColdObservable( onNext(300, "Cascadia"), onNext(400, "JS"), onCompleted(500)); var results = scheduler.startWithCreate(function () { input.select(function (x) { return x.length; }) }); results.messages.assertEqual( onNext(300, 8), onNext(400, 2), Testability is King! onCompleted(500));
  • 32. Start using it now Node.js > npm install rx NuGet > Install-Package RxJS Open Source http://rx.codeplex.com/
  • 33. What’s Next? • Ongoing work • Documentation • Tutorials • Where’s the vision? • Scaling • Distributed • Merge with Node.js Streams
  • 34. Cross the Streams… Matthew Podwysocki @mattpodwysocki matthewp@microsoft.com
  • 35. Credits • Cross the Streams - https://oshea12566.wordpress.com/2012/02/20/jeep-meeting-and- trail-ride/ • NES Controller - http://en.wikipedia.org/wiki/File:NES-controller.jpg