Created
December 30, 2020 17:51
-
-
Save andrewarosario/a8ea44ea5abb23f06ed833da94c0464a to your computer and use it in GitHub Desktop.
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
export class AppComponent { | |
note = new FormControl(""); | |
saveIndicator$: Observable<string>; | |
saveCount = 0; | |
ngOnInit() { | |
const inputToSave$ = this.note.valueChanges.pipe( | |
debounceTime(300), | |
distinctUntilChanged(), | |
share() | |
); | |
const savesInProgress$ = inputToSave$.pipe( | |
mapTo(of("Salvando...")), | |
tap(() => this.saveCount++) | |
); | |
const savesCompleted$ = inputToSave$.pipe( | |
mergeMap(value => this.saveChanges(value)), | |
tap(() => this.saveCount--), | |
filter(() => !this.saveCount), | |
mapTo( | |
concat( | |
of("Salvo!"), | |
empty().pipe(delay(2000)), | |
defer(() => | |
of(`Última Atualização: ${format(Date.now(), "dd/MM/yyyy hh:mm")}`) | |
) | |
) | |
) | |
); | |
this.saveIndicator$ = merge(savesInProgress$, savesCompleted$).pipe( | |
switchAll(), | |
startWith("Todas as mudanças foram salvas") | |
); | |
inputToSave$.subscribe(console.log); | |
} | |
saveChanges(value: string) { | |
return of(value).pipe(delay(1500)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment