Liang Zhou Liang Zhou - 1 month ago 7
Javascript Question

Combine a list of ordered operations into one Observable

var state = [];
var operation1 = function() {
return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
state.push(1, 2);
setTimeout(resolve, 300, state);
}));
};
var operation2 = function() {
return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
state = state.map(x => x * 2);
setTimeout(resolve, 200, state);
}));
};
var operation3 = function() {
return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
state = state.reduce( (prev, next) => prev + next );
setTimeout(resolve, 100, state);
}));
};
var operations = [operation1, operation2, operation3];


Given the code above, I am trying to combine
operations
into one
Observable
that emits the state of each operation. So the
Observable
needs to do either one of the following:


  • emits 3 times:
    [1, 2], [2, 4], 6

  • emits 1 time:
    [[1, 2], [2, 4], 6]


Answer

You could try (jsbin)

var state = [];
var operation1 = Rx.Observable.defer(function() {
    return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
        state.push(1, 2);
        setTimeout(resolve, 300, state);
    }));
});
var operation2 = Rx.Observable.defer(function() {
    return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
        state = state.map(x => x * 2);
        setTimeout(resolve, 200, state);
    }));
});
var operation3 = Rx.Observable.defer(function() {
    return Rx.Observable.fromPromise(new Promise((resolve, reject) => {
        state = state.reduce( (prev, next) => prev + next );
        setTimeout(resolve, 100, state);
    }));
});
var operations = Rx.Observable.from([operation1, operation2, operation3]).merge(1);
operations.subscribe(function(x){console.log(x)})

Please check if that does the trick, I will elaborate later on how this works.