Paged search: loading, pagination and disposal
Project milestone
Complete the search controller by enforcing legal transitions. Load more only from ready state with a next cursor, retain previous results on failure and prevent updates after disposal.
Run the examples
In projects/paged-search/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {createSearch}=require('./search.cjs');(async()=>{const s=createSearch(async({cursor})=>cursor===null?{items:[{id:'a',label:'A'}],next:'p2'}:{items:[{id:'b',label:'B'}],next:null});await s.start('x');await s.more();console.log(s.snapshot().items.map(x=>x.id).join(','),await s.more());})();Expected console output:
a,b falseTwo pages are merged and null terminates pagination.
In projects/paged-search/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {createSearch}=require('./search.cjs');(async()=>{let finish;const s=createSearch(()=>new Promise(r=>finish=r));const p=s.start('x');s.dispose();finish({items:[],next:null});console.log(await p,s.snapshot().phase);})();Expected console output:
false disposedDisposal invalidates the pending version before it settles.
Implementation and contract
more() rejects redundant calls while loading and returns false when next is null. The request function detects an immediate repeated cursor and bounds accumulated rows; longer cursor cycles are not fully detected. dispose() invalidates pending work and resets state. This is a transport-independent controller, not a complete network UI or retry policy.
Common mistakes
Do not allow multiple load-more operations to append the same page. Do not interpret false as a detailed error code; inspect the phase.
Acceptance task
Extend the controller with a bounded set of visited cursors, and test a cycle longer than one page.
Project source, setup and tests: projects/paged-search/README.md. Read the implementation before modifying it; the tests check the supplied baseline, not every possible implementation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Paged search: loading, pagination and disposal — Knowledge Check
Assess implementation decisions and project behavior.
When is load-more legal?
What is retained when a next-page load fails?
Which limitation does repeated-cursor detection leave?
In the Paged search: loading, pagination and disposal lab, what does this independent example log? const {createSearch}=require('./search.cjs'); (async()=>{const s=createSearch(async({cursor})=>cursor===null?{items:[{id:'a',label:'A'}],next:'p2'}:{items:[{id:'b',label:'B'}],next:null});await s.start('x');await s.more();console.log(s.snapshot().items.map(x=>x.id).join(','),await s.more());})();
In the Paged search: loading, pagination and disposal lab, what does this independent example log? const {createSearch}=require('./search.cjs'); (async()=>{let finish;const s=createSearch(()=>new Promise(r=>finish=r));const p=s.start('x');s.dispose();finish({items:[],next:null});console.log(await p,s.snapshot().phase);})();