Skip to content

Instantly share code, notes, and snippets.

@alvieirajr
Created October 9, 2016 02:43
Show Gist options
  • Save alvieirajr/746084748cf65d42ccdb780bb5d383e3 to your computer and use it in GitHub Desktop.
Save alvieirajr/746084748cf65d42ccdb780bb5d383e3 to your computer and use it in GitHub Desktop.
//
// (BACKEND)
//
//var sourceRandom = Rx.Observable.from([1, 2, 3, 6, 8, 7, 5, 4, 9]);
//+ Create a stream of random numbers. (BACKEND)
var sourceRandom = Rx.Observable
.interval(100)
.timeInterval()
.map(function(value) {
let max = 10;
let min = 1;
let new_value = Math.floor(Math.random() * (max - min)) + min;
return new_value;
});
//
// (FRONT END)
//
var arrayIn = [];
var arrayOut = [];
var wish_order = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var currentElement = 0;
var subject = new Rx.Subject();
//+ Process the current CHUNCK.
var subscriptToOutput = subject.subscribe(
function (x) {
console.log('---> Renderizando: ' + JSON.stringify(x));
},
function (err) {
console.log('--> Error: ' + err);
},
function () {
console.log('--> Completed');
});
//+
var subscription = sourceRandom.subscribe(
function (x) {
if (arrayIn.indexOf(x) >= 0) {
//console.log('The element ' + x + ' has been exist.');
} else {
//console.log('Empilhando : ' + JSON.stringify(x));
arrayIn.push(x);
arrayIn.sort();
if (wish_order[currentElement] == x) {
//console.log(wish_order[currentElement] + ' finalmente chegou!');
let i = 0;
for(i = currentElement; i < wish_order.length ; i++) {
if (wish_order[i] == arrayIn[i]) {
arrayOut.push(wish_order[i]);
subject.onNext(wish_order[i]);
} else {
break;
}
}
currentElement = i;
//console.log('currentElement agora é : ' + wish_order[currentElement])
//console.log(arrayOut.length);
if (wish_order.length === arrayOut.length) {
subject.onCompleted();
}
}
}
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
subject.onCompleted();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment