Last active
August 28, 2018 16:43
-
-
Save GenesisCoast/eaa3b8c75032fea5ae99de034dfadd81 to your computer and use it in GitHub Desktop.
Example of implementing common behaviour accross multiple RxJS requests.
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
// Original - Usage and Implementation | |
httpClient | |
.get("https://google.com") | |
.pipe( | |
share(), | |
retry(2), | |
catchError((err, caught) => { | |
console.log(`${fileName} - ${err.statusText} (HTTP ${err.status})`, err, caught, optionalParams); | |
return throwError(err); | |
}) | |
) | |
httpClient | |
.get("https://bing.com") | |
.pipe( | |
share(), | |
retry(2), | |
catchError((err, caught) => { | |
console.log(`${fileName} - ${err.statusText} (HTTP ${err.status})`, err, caught, optionalParams); | |
return throwError(err); | |
}) | |
) | |
// New - Common Function | |
public static readonly standardBehaviour = (fileName: string, ...optionalParams) => | |
<T>(source: Observable<T>) => | |
source.pipe( | |
share(), | |
retry(), | |
catchError((err, caught) => { | |
console.error(`${fileName} - ${err.statusText} (HTTP ${err.status})`, err, caught, optionalParams) | |
return throwError(err); | |
}) | |
); | |
// New - Usage | |
httpClient | |
.get("https://google.com") | |
.pipe(this.standardBehaviour('myFile.ts', null)) | |
httpClient | |
.get("https://bing.com") | |
.pipe(this.standardBehaviour('myFile.ts', null)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment