Skip to content

Instantly share code, notes, and snippets.

@andrewarosario
Created December 30, 2020 17:51
Show Gist options
  • Save andrewarosario/a8ea44ea5abb23f06ed833da94c0464a to your computer and use it in GitHub Desktop.
Save andrewarosario/a8ea44ea5abb23f06ed833da94c0464a to your computer and use it in GitHub Desktop.
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