src/vs/base/common/paging.ts

392 LOC · 278 covered · 114 uncovered · 60 ranges · 29 concepts · 22 introducers · 23 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filesrc/vs/base/common/arrays.ts · 949 LOCcommon/arrays.tspaging.test|title=PagedModel cancellation works|occurrence=1 · 0 introduced LOCpaging.test|title=PagedM…paging.test|title=PagedModel same page cancellation works|occurrence=1 · 0 introduced LOCpaging.test|title=PagedM…paging.ts ×3 · 11 introduced LOCpaging.ts ×3paging.ts ×1 · 4 introduced LOCpaging.ts ×1paging.ts ×5 · 22 introduced LOCpaging.ts ×5paging.ts ×1 · 2 introduced LOCpaging.ts ×1paging.ts ×1 · 5 introduced LOCpaging.ts ×1paging.ts ×2 · 2 introduced LOCpaging.ts ×2arrays.ts ×3 · 36 introduced LOCarrays.ts ×3paging.ts ×1 · 2 introduced LOCpaging.ts ×1iterativePaging.test|title=IterativePagedModel sequential page loads work correctly|occurrence=1, iterativePaging.test|title=IterativePagedModel accessing items after loading all pages|occurrence=1 · 0 introduced LOCiterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel load multiple pages|occurrence=1, iterativePaging.test|title=IterativePagedModel pager with varying page sizes|occurrence=1 · 0 introduced LOCiterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel reaching end of data removes sentinel|occurrence=1 · 0 introduced LOCiterativePaging.test|tit…paging.ts ×1 · 3 introduced LOCpaging.ts ×1paging.ts ×1 · 2 introduced LOCpaging.ts ×1paging.ts ×5 · 25 introduced LOCpaging.ts ×5paging.ts ×1 · 2 introduced LOCpaging.ts ×1iterativePaging.test|title=IterativePagedModel load first page via sentinel access|occurrence=1 · 0 introduced LOCiterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel single page pager with no more pages|occurrence=1, iterativePaging.test|title=IterativePagedModel accessing regular items does not trigger loading|occurrence=1 · 0 introduced LOCiterativePaging.test|tit…paging.ts ×2 · 7 introduced LOCpaging.ts ×2paging.ts ×1 · 2 introduced LOCpaging.ts ×1paging.ts ×1 · 2 introduced LOCpaging.ts ×1paging.ts ×1 · 1 introduced LOCpaging.ts ×1paging.ts ×2 · 3 introduced LOCpaging.ts ×2paging.ts ×3 · 3 introduced LOCpaging.ts ×3paging.ts ×2 · 2 introduced LOCpaging.ts ×2paging.ts ×1 · 2 introduced LOCpaging.ts ×1paging.ts ×2 · 6 introduced LOCpaging.ts ×2paging.ts ×21 · 152 introduced LOCpaging.ts ×21iterativePaging.test|title=IterativePagedModel accessing item beyond loaded range throws|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel accessing item beyond loaded range throws|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel accessing items after loading all pages|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel accessing items after loading all pages|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel accessing regular items does not trigger loading|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel accessing regular items does not trigger loading|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel cancelled token during initial resolve|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel cancelled token during initial resolve|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel concurrent access to sentinel only loads once|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel concurrent access to sentinel only loads once|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel empty pager with no items|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel empty pager with no items|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel event fires for each page load|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel event fires for each page load|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel initial state|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel initial state|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel load first page via sentinel access|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel load first page via sentinel access|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel load multiple pages|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel load multiple pages|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel onDidIncrementLength event fires correctly|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel onDidIncrementLength event fires correctly|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel pager with varying page sizes|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel pager with varying page sizes|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel reaching end of data removes sentinel|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel reaching end of data removes sentinel|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel resolving item beyond all pages throws|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel resolving item beyond all pages throws|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel sequential page loads work correctly|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel sequential page loads work correctly|occurrence=1iterativePaging.test|tit…iterativePaging.test|title=IterativePagedModel single page pager with no more pages|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/iterativePaging.test|title=IterativePagedModel single page pager with no more pages|occurrence=1iterativePaging.test|tit…paging.test|title=PagedModel cancellation works|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/paging.test|title=PagedModel cancellation works|occurrence=1paging.test|title=PagedM…paging.test|title=PagedModel isResolved|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/paging.test|title=PagedModel isResolved|occurrence=1paging.test|title=PagedM…paging.test|title=PagedModel preemptive cancellation works|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/paging.test|title=PagedModel preemptive cancellation works|occurrence=1paging.test|title=PagedM…paging.test|title=PagedModel resolve page 2|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/paging.test|title=PagedModel resolve page 2|occurrence=1paging.test|title=PagedM…paging.test|title=PagedModel resolve page|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/paging.test|title=PagedModel resolve page|occurrence=1paging.test|title=PagedM…paging.test|title=PagedModel resolve single|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/paging.test|title=PagedModel resolve single|occurrence=1paging.test|title=PagedM…paging.test|title=PagedModel same page cancellation works|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/paging.test|title=PagedModel same page cancellation works|occurrence=1paging.test|title=PagedM…Focused file · src/vs/base/common/paging.ts · 392 LOCcommon/paging.ts

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- paging.ts ×21
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { range } from './arrays.js';
7 > import { CancellationToken, CancellationTokenSource } from './cancellation.js';
8 > import { CancellationError } from './errors.js';
9 > import { Event, Emitter } from './event.js';
10 >
11 > /**
12 > * A Pager is a stateless abstraction over a paged collection.
13 > */
14 > export interface IPager<T> {
15 > firstPage: T[];
16 > total: number;
17 > pageSize: number;
18 > getPage(pageIndex: number, cancellationToken: CancellationToken): Promise<T[]>;
19 > }
20 >
21 > export interface IIterativePage<T> {
22 > readonly items: T[];
23 > readonly hasMore: boolean;
24 > }
25 >
26 > export interface IIterativePager<T> {
27 > readonly firstPage: IIterativePage<T>;
28 > getNextPage(cancellationToken: CancellationToken): Promise<IIterativePage<T>>;
29 > }
30 >
31 > export interface IPageIterator<T> {
32 > elements: T[];
33 > total: number;
34 > hasNextPage: boolean;
35 > getNextPage(cancellationToken: CancellationToken): Promise<IPageIterator<T>>;
36 > }
37 >
38 > interface IPage<T> {
39 > isResolved: boolean;
40 > promise: Promise<void> | null;
41 > cts: CancellationTokenSource | null;
42 > promiseIndexes: Set<number>;
43 > elements: T[];
44 > }
45 >
46 > function createPage<T>(elements?: T[]): IPage<T> { arrays.ts ×3
47 > return {
48 > isResolved: !!elements,
49 > promise: null,
50 > cts: null,
51 > promiseIndexes: new Set<number>(),
52 > elements: elements || []
53 > };
54 > }
56 > /**
57 > * A PagedModel is a stateful model over an abstracted paged collection.
58 > */
59 > export interface IPagedModel<T> {
60 > readonly length: number;
61 > readonly onDidIncrementLength: Event<number>;
62 > isResolved(index: number): boolean;
63 > get(index: number): T;
64 > resolve(index: number, cancellationToken: CancellationToken): Promise<T>;
65 > }
66 >
67 > export function singlePagePager<T>(elements: T[]): IPager<T> {
68 return {
69 firstPage: elements,
70 total: elements.length,
71 pageSize: elements.length,
72 getPage: (pageIndex: number, cancellationToken: CancellationToken): Promise<T[]> => {
73 return Promise.resolve(elements);
74 }
75 };
76 }
78 > export class PagedModel<T> implements IPagedModel<T> {
79 >
80 > private pager: IPager<T>;
81 > private pages: IPage<T>[] = [];
82 >
83 > get length(): number { return this.pager.total; }
84 > readonly onDidIncrementLength = Event.None;
85 >
86 > constructor(arg: IPager<T> | T[]) {
87 > this.pager = Array.isArray(arg) ? singlePagePager<T>(arg) : arg; arrays.ts ×3
88 >
89 > const totalPages = Math.ceil(this.pager.total / this.pager.pageSize);
90 >
91 > this.pages = [
92 > createPage(this.pager.firstPage.slice()),
93 > ...range(totalPages - 1).map(() => createPage<T>())
94 > ];
95 > }
97 > isResolved(index: number): boolean {
98 > const pageIndex = Math.floor(index / this.pager.pageSize); paging.ts ×1
99 > const page = this.pages[pageIndex];
100 >
101 > return !!page.isResolved;
102 > }
104 > get(index: number): T {
105 const pageIndex = Math.floor(index / this.pager.pageSize);
106 const indexInPage = index % this.pager.pageSize;
107 const page = this.pages[pageIndex];
108
109 return page.elements[indexInPage];
110 }
112 > resolve(index: number, cancellationToken: CancellationToken): Promise<T> {
113 > if (cancellationToken.isCancellationRequested) { paging.ts ×2
114 > return Promise.reject(new CancellationError()); paging.ts ×1
115 > }
117 > const pageIndex = Math.floor(index / this.pager.pageSize);
118 > const indexInPage = index % this.pager.pageSize;
119 > const page = this.pages[pageIndex];
120 >
121 > if (page.isResolved) {
122 return Promise.resolve(page.elements[indexInPage]);
123 }
125 > if (!page.promise) {
126 > page.cts = new CancellationTokenSource();
127 > page.promise = this.pager.getPage(pageIndex, page.cts.token)
128 > .then(elements => {
129 > page.elements = elements; paging.ts ×1
130 > page.isResolved = true;
131 > page.promise = null;
132 > page.cts = null;
133 > }, err => { paging.ts ×5
134 > page.isResolved = false; paging.ts ×3
135 > page.promise = null;
136 > page.cts = null;
137 > return Promise.reject(err);
138 > }); paging.ts ×5
139 > }
140 >
141 > const listener = cancellationToken.onCancellationRequested(() => {
142 > if (!page.cts) { paging.ts ×3
143 return;
144 }
146 > page.promiseIndexes.delete(index);
147 >
148 > if (page.promiseIndexes.size === 0) {
149 > page.cts.cancel();
150 > }
151 > }); paging.ts ×5
152 >
153 > page.promiseIndexes.add(index);
154 >
155 > return page.promise.then(() => page.elements[indexInPage])
156 > .finally(() => listener.dispose());
157 > } paging.ts ×2
158 > } paging.ts ×21
159 >
160 > export class DelayedPagedModel<T> implements IPagedModel<T> {
161 >
162 > get length(): number { return this.model.length; }
163 > get onDidIncrementLength() { return this.model.onDidIncrementLength; }
164 >
165 > constructor(private readonly model: IPagedModel<T>, private timeout: number = 500) { }
166 >
167 > isResolved(index: number): boolean {
168 return this.model.isResolved(index);
169 }
171 > get(index: number): T {
172 return this.model.get(index);
173 }
175 > resolve(index: number, cancellationToken: CancellationToken): Promise<T> {
176 return new Promise((c, e) => {
177 if (cancellationToken.isCancellationRequested) {
178 return e(new CancellationError());
179 }
180
181 const timer = setTimeout(() => {
182 if (cancellationToken.isCancellationRequested) {
183 return e(new CancellationError());
184 }
185
186 timeoutCancellation.dispose();
187 this.model.resolve(index, cancellationToken).then(c, e);
188 }, this.timeout);
189
190 const timeoutCancellation = cancellationToken.onCancellationRequested(() => {
191 clearTimeout(timer);
192 timeoutCancellation.dispose();
193 e(new CancellationError());
194 });
195 });
196 }
197 > } paging.ts ×21
198 >
199 > /**
200 > * A PageIteratorPager wraps an IPageIterator to provide IPager functionality.
201 > * It caches pages as they are accessed and supports random page access by
202 > * sequentially loading pages until the requested page is reached.
203 > */
204 > export class PageIteratorPager<T> implements IPager<T> {
205 > private cachedPages: T[][] = [];
206 > private currentIterator: IPageIterator<T>;
207 > private isComplete: boolean = false;
208 > private pendingRequests = new Map<number, Promise<void>>();
209 >
210 > public readonly firstPage: T[];
211 > public readonly pageSize: number;
212 > public readonly total: number;
213 >
214 > constructor(initialIterator: IPageIterator<T>) {
215 this.currentIterator = initialIterator;
216 this.firstPage = [...initialIterator.elements];
217 this.pageSize = initialIterator.elements.length || 1; // Use first page size as page size
218 this.cachedPages[0] = this.firstPage;
219 this.isComplete = !initialIterator.hasNextPage;
220 this.total = initialIterator.total;
221 }
223 > async getPage(pageIndex: number, cancellationToken: CancellationToken): Promise<T[]> {
224 if (cancellationToken.isCancellationRequested) {
225 throw new CancellationError();
226 }
227
228 // If we already have this page cached, return it
229 if (pageIndex < this.cachedPages.length) {
230 return this.cachedPages[pageIndex];
231 }
232
233 // If we're complete and don't have this page, it doesn't exist
234 if (this.isComplete) {
235 throw new Error(`Page ${pageIndex} is out of bounds. Total pages: ${this.cachedPages.length}`);
236 }
237
238
239 // Check if there's already a pending request that will load this index
240 // (any pending request for an index >= our requested index)
241 let promise: Promise<void> | undefined;
242 for (const [pendingPageIndex, pendingPromise] of this.pendingRequests) {
243 if (pendingPageIndex >= pageIndex) {
244 promise = pendingPromise;
245 break;
246 }
247 }
248
249 if (!promise) {
250 promise = this.loadPagesUntil(pageIndex, cancellationToken);
251 this.pendingRequests.set(pageIndex, promise);
252 }
253
254 try {
255 await promise;
256 if (pageIndex >= this.cachedPages.length) {
257 throw new Error(`Page ${pageIndex} is out of bounds. Total pages: ${this.cachedPages.length}`);
258 }
259 return this.cachedPages[pageIndex];
260 } finally {
261 this.pendingRequests.delete(pageIndex);
262 }
263 }
265 > private async loadPagesUntil(targetPageIndex: number, cancellationToken: CancellationToken): Promise<void> {
266 while (targetPageIndex >= this.cachedPages.length && this.currentIterator.hasNextPage) {
267 if (cancellationToken.isCancellationRequested) {
268 throw new CancellationError();
269 }
270
271 this.currentIterator = await this.currentIterator.getNextPage(cancellationToken);
272 this.cachedPages.push([...this.currentIterator.elements]);
273 }
274 if (!this.currentIterator.hasNextPage) {
275 this.isComplete = true;
276 }
277 }
278 > } paging.ts ×21
279 >
280 > export class IterativePagedModel<T> implements IPagedModel<T> {
281 >
282 > private items: T[] = [];
283 > private _hasNextPage = true;
284 > private readonly _onDidIncrementLength = new Emitter<number>();
285 > private loadingPromise: Promise<void> | null = null;
286 >
287 > private readonly pager: IIterativePager<T>;
288 >
289 > constructor(pager: IIterativePager<T>) {
290 > this.pager = pager; paging.ts ×2
291 > this.items = [...pager.firstPage.items];
292 > this._hasNextPage = pager.firstPage.hasMore;
293 > }
295 > get onDidIncrementLength(): Event<number> {
296 > return this._onDidIncrementLength.event; paging.ts ×1
297 > }
299 > /**
300 > * Returns actual length + 1 if there are more pages (sentinel approach)
301 > */
302 > get length(): number {
303 > return this.items.length + (this._hasNextPage ? 1 : 0); paging.ts ×1
304 > }
306 > /**
307 > * Sentinel item is never resolved - it triggers loading
308 > */
309 > isResolved(index: number): boolean {
310 > if (index === this.items.length && this._hasNextPage) { paging.ts ×2
311 > return false; // This will trigger resolve() call paging.ts ×1
312 > }
313 > return index < this.items.length; paging.ts ×2
314 > }
316 > get(index: number): T {
317 > if (index < this.items.length) { paging.ts ×2
318 > return this.items[index]; paging.ts ×1
319 > }
320 > throw new Error('Item not resolved yet'); paging.ts ×1
321 > } paging.ts ×2
323 > /**
324 > * When sentinel item is accessed, load next page
325 > */
326 > async resolve(index: number, cancellationToken: CancellationToken): Promise<T> {
327 > if (cancellationToken.isCancellationRequested) { paging.ts ×3
328 > return Promise.reject(new CancellationError()); paging.ts ×1
329 > }
331 > // If trying to resolve the sentinel item, load next page
332 > if (index === this.items.length && this._hasNextPage) { paging.ts ×3
333 > await this.loadNextPage(cancellationToken); paging.ts ×5
334 > }
336 > // After loading, the requested index should now be valid
337 > if (index < this.items.length) {
338 > return this.items[index];
339 > }
341 > throw new Error('Index out of bounds');
342 > } paging.ts ×3
344 > private async loadNextPage(cancellationToken: CancellationToken): Promise<void> {
345 > if (!this._hasNextPage) { paging.ts ×5
346 return;
347 }
349 > // If already loading, return the cached promise
350 > if (this.loadingPromise) {
351 > await this.loadingPromise; paging.ts ×1
352 > return;
353 > }
355 > const pagePromise = this.pager.getNextPage(cancellationToken);
356 >
357 > this.loadingPromise = pagePromise
358 > .then(page => {
359 > this.items.push(...page.items);
360 > this._hasNextPage = page.hasMore;
361 >
362 > // Clear the loading promise before firing the event
363 > // so that event handlers can trigger loading the next page if needed
364 > this.loadingPromise = null;
365 >
366 > // Fire length update event
367 > this._onDidIncrementLength.fire(this.length);
368 > }, err => {
369 this.loadingPromise = null;
370 throw err;
371 > }); paging.ts ×5
372 >
373 > await this.loadingPromise;
374 > }
376 > dispose(): void {
377 > this._onDidIncrementLength.dispose(); paging.ts ×2
378 > }
379 > } paging.ts ×21
380 >
381 > /**
382 > * Similar to array.map, `mapPager` lets you map the elements of an
383 > * abstract paged collection to another type.
384 > */
385 > export function mapPager<T, R>(pager: IPager<T>, fn: (t: T) => R): IPager<R> {
386 return {
387 firstPage: pager.firstPage.map(fn),
388 total: pager.total,
389 pageSize: pager.pageSize,
390 getPage: (pageIndex, token) => pager.getPage(pageIndex, token).then(r => r.map(fn))
391 };
392 }