Lessons32. UI filters with startWith and combineLatest
Lessons · 32
32. UI filters with startWith and combineLatest
Practice: list filters
This lesson brings two earlier concepts together: combineLatest to assemble state and startWith for each filter's starting value.
Why startWith on each source
Remember the combineLatest trap: until every source has emitted at least once, nothing comes out. If the filters are created lazily (from input events, say), the user might not touch a field for a while. So each source is given a "default" via startWith.
What happens, step by step
- At
0msbothstartWiths emit immediately → outputFilter: /all. - At
20msquery$changes to'rxjs'→ outputFilter: rxjs/all. - At
40msstatus$changes to'active'→ outputFilter: rxjs/active.
Your task
- On
query$, addstartWith('')in the pipe (aftermap, comma-separated). - On
status$, addstartWith('all'). - The
combineLatest + maplogic is already done — no need to change it.
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 { timer, combineLatest, map, startWith } = Rx;
const query$ = timer(20).pipe(
map(() => 'rxjs'),
startWith('')
);
const status$ = timer(40).pipe(
map(() => 'active'),
startWith('all')
);
const filters$ = combineLatest([query$, status$]).pipe(
map(([query, status]) => 'Filter: ' + query + '/' + status)
); script.ts
CONSOLE · Console output
Hit Run to see the result...