Skip to content

Instantly share code, notes, and snippets.

@perjerz
Forked from alexzuza/with-loading.pipe.ts
Created November 9, 2020 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perjerz/661b0cef6a55c4ca540a75aa4905a95b to your computer and use it in GitHub Desktop.
Save perjerz/661b0cef6a55c4ca540a75aa4905a95b to your computer and use it in GitHub Desktop.
With loading pipe long living stream
import { Pipe, PipeTransform } from '@angular/core';
import { isObservable, of } from 'rxjs';
import { map, startWith, catchError } from 'rxjs/operators';
@Pipe({
name: 'withLoading',
})
export class WithLoadingPipe implements PipeTransform {
transform(val) {
return isObservable(val)
? val.pipe(
map((value: any) => ({
loading: value.type === 'start',
value: value.type ? value.value : value
})),
startWith({ loading: true }),
catchError(error => of({ loading: false, error }))
)
: val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment