Was'n das ?

Eventhandling


button.onclick = function(event) {
  ...
};

        

Callback Hell!


var ws = new WebSocketManagement();
ws.loadDataPool.call(that, function(){ 
  ws.loadDevices.call(that, function(){
    ws.loadBriefcase.call(that, function(){
      ws.loadSettings.call(that, function(){ 
        //console.log("finished loading settings");
        ws.loadVisualElements.call(that, function(){
          ...
        });
      });
    });
  });
});

        

Error Handling


object.asyncMethod(function(err, data) {
  if (err) throw err; // gone nowhere
});

// how to cancel that ?
$.post('/answer/never', function(data) {

});


        

Was ist der Unterschied zwischen einem Array und einem Event ?

Eventstreams erzeugen

Eventstreams erzeugen aus Promises

kompatibel zu A/+ bzw. ES2o15

var promise = new Promise((resolve, reject) => {
});
Rx.Observable.fromPromise(promise)

        
auch mit Funktion möglich

function promAjax(to) {
  return $.ajax({
    url: to,
    .....
  }).promise();
}
Rx.Observable.fromPromise(promAjax)

        

Callbacks


myApi.openSession(username, password, function(session) { ...});

let open = Rx.Observable.fromCallback(myApi.openSession);
}).first();
open(username, password);


        

Eventstreams erzeugen


Rx.Observable.return(32);
Rx.Observable.just(32);
Rx.Observable.throws('Im an error');
Rx.Observable.interval(100);
Rx.Observable.timer(100, 500).timestamp();
Rx.Observable.from([1, 2, 3, 4, 5]);
Rx.Observable.from(new Map([[key1, 10] [key2, 20]]));


      

Eventstreams erzeugen

wird Teil von ES7:

Rx.Observable.create(observer => {
  doSomethingAsync(
    function(data) { onNext(data)}),
    function(error) { onError(error)}
  });
  ....
  // some time later
  onCompleted(data);
});

        

Query Operatoren

Streams filtern

Eigener Observer


debugObs = Rx.Observer.create(
  d => console.log(d.data),
  e => console.log(e),
  f => console.log('eventsream finished!')
);

// eigenen Observer anwenden
stream.subscribe(debugObs);

        

Defer

Factory für Sreams einschieben

let stream = Rx.Observable.defer(() => {
  // do something for buildup here
  ...
  return Rx.Observable.return(42);
})

stream.subscribe() // jetzt wird defer ausgeführt