Last active
July 18, 2017 15:12
-
-
Save jiaming0708/08101b5d8f3f35c562c2d7df774835c4 to your computer and use it in GitHub Desktop.
RxJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var create = Observable.create(observer => { | |
setTimeout(function () { | |
observer.next(3) | |
}, 100); | |
setTimeout(function () { | |
observer.next(4) | |
}, 200); | |
}); | |
var subject = new BehaviorSubject(10); | |
create.subscribe(subject); | |
setTimeout(function () { | |
subject.subscribe(val => { | |
console.log(val); | |
}); | |
}, 10); | |
//10 | |
//3 | |
//4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getData() { | |
return Observable.from([1, 2, 3, 4]); | |
} | |
Observable.from([10, 20, 40]) | |
.concat(getData()) | |
.subscribe(val => console.log(val)); | |
//10 | |
//20 | |
//40 | |
//1 | |
//2 | |
//3 | |
//4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rx.Observable.create(function(observer){ | |
observer.next(1); | |
observer.next(2); | |
observer.complete(); | |
observer.next(3); | |
observer.throw('error'); | |
observer.next(4); | |
}).subscribe( | |
val=>console.log(val), | |
err=>console.log(err), | |
complete=>console.log('complete!') | |
); | |
//1 | |
//2 | |
//complete! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obs = Rx.Observable.create(function(observer){ | |
observer.next(1); | |
observer.next(2); | |
observer.next(3); | |
setTimeout(function () { | |
observer.next(4); | |
}, 100); | |
}); | |
obs.subscribe( | |
val=>console.log(`a:${val}`) | |
); | |
setTimeout(function () { | |
obs.subscribe(val => { | |
console.log(`b:${val}`); | |
}); | |
}, 200); | |
//a:1 | |
//a:2 | |
//a:3 | |
//a:4 | |
//b:1 | |
//b:2 | |
//b:3 | |
//b:4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rx.Observable.from([1,2,3]) | |
.subscribe(val=>console.log(val)); | |
//1 | |
//2 | |
//3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let btn = document.getElementById('btnClick'); | |
Rx.Observable.fromEvent(btn, 'click') | |
.subscribe(val=>console.log('hi')); | |
//hi | |
//hi | |
//hi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var merge = Observable.create(observer => { | |
observer.next(11); | |
observer.next(21); | |
setTimeout(function () { | |
observer.next(31) | |
}, 100); | |
setTimeout(function () { | |
observer.next(41) | |
}, 200); | |
}); | |
merge | |
.merge(Observable.from([1, 2])) | |
.subscribe(val => console.log(val)); | |
//11 | |
//21 | |
//1 | |
//2 | |
//31 | |
//41 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var merge = Observable.create(observer => { | |
observer.next(11); | |
observer.next(21); | |
setTimeout(function () { | |
observer.next(31) | |
}, 100); | |
setTimeout(function () { | |
observer.next(41) | |
}, 200); | |
}); | |
merge | |
.mergeMap(p=>Observable.from([1, 2])) | |
.subscribe(val => console.log(val)); | |
//1 | |
//2 | |
//1 | |
//2 | |
//1 | |
//2 | |
//1 | |
//2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rx.Observable.of([1,2,3]) | |
.subscribe(val=>console.log(val)); | |
//[1,2,3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rx.Observable.from([1,2,3,4,5,6]) | |
.filter(val=>val > 3) | |
.map(val=>val * 10) | |
.subscribe(val=>console.log(val)); | |
//-1-2-3-4-5-6- | |
//filter > 3 | |
//-------4-5-6- | |
//map * 10 | |
//-------40-50-60- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var create = Observable.create(observer => { | |
observer.next(1); | |
observer.next(2); | |
setTimeout(function () { | |
observer.next(3) | |
}, 100); | |
setTimeout(function () { | |
observer.next(4) | |
}, 200); | |
}); | |
var subject = new ReplaySubject(2); | |
create.subscribe(subject); | |
setTimeout(function () { | |
subject.subscribe(val => { | |
console.log(val); | |
}); | |
}, 500); | |
//3 | |
//4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obs = Rx.Observable.create(function(observer){ | |
observer.next(1); | |
observer.next(2); | |
observer.next(3); | |
setTimeout(function () { | |
observer.next(4); | |
}, 100); | |
}); | |
obs.subscribe( | |
val=>console.log(`a:${val}`) | |
); | |
var subject = new Subject(); | |
create.subscribe(subject); | |
setTimeout(function () { | |
subject.subscribe(val => { | |
console.log(`b:${val}`); | |
}); | |
}, 50); | |
//a:1 | |
//a:2 | |
//a:3 | |
//a:4 | |
//b:4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment