import { RequestQueryOptions } from "../itwin-client/Request" ; internal export class ChunkedQueryContext { private _instancesLeft ? : number ; private _queryFinished : boolean = false ; private _skipToken : string = "" ; private _pageSize ? : number ; /** Count of instances that are left to query. This property is only used when both PageSize and Top are defined and PageSize is smaller than Top. */ public get instancesLeft ( ) : number | undefined { return this . _instancesLeft ; } /** Current query skip token. */ public get skipToken ( ) : string { return this . _skipToken ; } public set skipToken ( value ) { this . _skipToken = value ; } /** Returns true if chunked query finished executing. */ public get isQueryFinished ( ) : boolean { return ! this . _skipToken || this . _queryFinished ; } /** Prepares for next chunked query iteration. */ public handleIteration ( queryOptions ) { let pageSize = this . _pageSize ; if ( this . _instancesLeft ) { if ( this . _instancesLeft > pageSize ! ) { this . _instancesLeft -= pageSize ! ; } else { pageSize = this . _instancesLeft ; this . _queryFinished = true ; } } queryOptions . $top = pageSize ; } /** Creates chunked query context if it is applicable for provided query. Context is created if Top is not defined or Top is bigger than PageSize. */ public static create ( queryOptions ) { let chunkedQueryContext ; if ( ! queryOptions . $top ) { chunkedQueryContext = new ChunkedQueryContext ( ) ; chunkedQueryContext . _pageSize = queryOptions . $pageSize ; } else if ( queryOptions . $pageSize && queryOptions . $top > queryOptions . $pageSize ) { chunkedQueryContext = new ChunkedQueryContext ( ) ; chunkedQueryContext . _pageSize = queryOptions . $pageSize ; chunkedQueryContext . _instancesLeft = queryOptions . $top ; } queryOptions . $pageSize = undefined ; return chunkedQueryContext ; } }