Last active
January 16, 2017 04:12
-
-
Save ORESoftware/de09ab4b15bd2fbff3b345636f29dcf5 to your computer and use it in GitHub Desktop.
simple observable creation
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
//import RxJS5 | |
const Rx = require('rxjs'); | |
// create an observable | |
// An observable is: "just a function that takes a subscriber and returns a function" | |
const obs = Rx.Observable.create(sub => { | |
const to = setTimeout(() => { | |
// do some work | |
// then fire the following *callbacks* | |
sub.next(); | |
//if we are one and done, then fire complete() | |
sub.complete(); | |
}, 200); | |
return function unsubscribe(){ | |
// if we unsubscribe from this observable before the timeout fires | |
// this function is invoked, essentially the purpose is for cleanup | |
clearTimeout(to); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment