Created
August 28, 2019 17:50
-
-
Save alexzuza/5aef14372eb4c465ef0ffb8efc3a5e8b to your computer and use it in GitHub Desktop.
With loading pipe long living stream
This file contains 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 { 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