Last active
March 5, 2021 17:26
-
-
Save evxn/750702f7c8e8d5a32c7b53167fe14d8d 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