Skip to content

Instantly share code, notes, and snippets.

@GenesisCoast
Last active August 28, 2018 16:43
Show Gist options
  • Save GenesisCoast/eaa3b8c75032fea5ae99de034dfadd81 to your computer and use it in GitHub Desktop.
Save GenesisCoast/eaa3b8c75032fea5ae99de034dfadd81 to your computer and use it in GitHub Desktop.
Example of implementing common behaviour accross multiple RxJS requests.
// 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