1
>
/*---------------------------------------------------------------------------------------------
paging.ts
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> {
47
return {