Skip to content

Instantly share code, notes, and snippets.

@andrewarosario
Created December 30, 2020 17:47
Show Gist options
  • Save andrewarosario/ce2ca027d9585b45e87a1e7eb7e2e9f0 to your computer and use it in GitHub Desktop.
Save andrewarosario/ce2ca027d9585b45e87a1e7eb7e2e9f0 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(of("Salvo!"))
);
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