Lessons50. Project: autosave draft
Lessons · 50
50. Project: autosave draft
Project 2 — autosaving a draft
Similar to typeahead, but with one key difference: instead of switchMap, here you need concatMap.
Why concatMap, not switchMap
For search, the old results aren't needed — they can be cancelled. For saving, you can't cancel: once the data has gone to the server, a client-side cancel undoes nothing on the server. Worse — run two saves in parallel and, if the second finishes first, an old response can overwrite the newer state.
concatMap queues the saves: one after another. Safe and predictable.
Your task
- In
createAutosave$, insidepipe:debounceTime(20),distinctUntilChanged(),concatMap(value => saveDraft(value)).
Deterministic check
This task's check runs in TestScheduler virtual time. If your solution is right, the console shows only Test Passed!. Don't add extra console.logs, or the check will fail.
Solution spoiler · click to reveal
const { debounceTime, distinctUntilChanged, concatMap } = Rx;
function createAutosave$(formValue$, saveDraft) {
return formValue$.pipe(
debounceTime(20),
distinctUntilChanged(),
concatMap(value => saveDraft(value))
);
} script.ts
CONSOLE · Console output
Hit Run to see the result...