-
-
Save bloadvenro/09e6f269914a7e08ee62e7a2e8fba583 to your computer and use it in GitHub Desktop.
rxjs on subscribe hook, callback on subscribe, doOnSubscribe operator, doOnSubscribe pipe, rxjs onSubscribe
This file contains hidden or 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 {defer} from 'rxjs/observable/defer'; | |
import {Observable} from 'rxjs/Observable'; | |
/** Example | |
import {from} from 'rxjs/observable/from'; | |
from([1, 2, 3]) | |
.pipe(doOnSubscribe(() => console.log('subscribed to stream'))) | |
.subscribe(x => console.log(x), null, () => console.log('completed')); | |
*/ | |
export function doOnSubscribe<T>(onSubscribe: () => void): (source: Observable<T>) => Observable<T> { | |
return function inner(source: Observable<T>): Observable<T> { | |
return defer(() => { | |
onSubscribe(); | |
return source; | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment