Created
November 23, 2017 08:45
-
-
Save viciouslabs/6eac734c1b3f8c85df28f2ba40ecd401 to your computer and use it in GitHub Desktop.
RxJS: Stream Completion
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
// An array stream is a stream that completes | |
Rx.Observable.fromArray([1,2,3,4]) | |
.subscribe( | |
function next(input) { | |
console.log('Received event data', input) | |
}, | |
function error(err) { | |
consle.log('Log error', err) | |
}, | |
function complete() { | |
console.log('Observable just expired.') | |
} | |
) | |
/** | |
* Event streams however, never complete. | |
* In order to complete an event stream, something like takeUntil can be used. | |
*/ | |
const mouseMoveListener = Rx.Observable.fromEvent(document, 'mousemove') | |
.subscribe( | |
function next(input) { | |
console.log('Received event') | |
}, | |
function error(err) { | |
console.log('Log error', err) | |
}, | |
function () { | |
// Note: that this will never be called. this is unreachable code. | |
console.log('Mouse move subscription just expired.') | |
} | |
) | |
/** | |
* The following code ensures that after | |
* Please note: that the text 'Mouse move subscription just expired.' will never be | |
* printed in the console. This is because unsubscribing and completion of stream are | |
* two very different events. | |
*/ | |
setTimeout(function unsubscribe() { | |
console.log('Unsubscribing mouse move.') | |
mouseMoveListener.unsubscribe() | |
}, 5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment