packageDocumentation module import { BentleyError } from "./BentleyError" ; beta export class AbandonedError extends Error { } /**
 * An object that returns a Promise when you call [[init]], but supplies a way to abandon the promise if it is no longer relevant.
 * When you call abandon, the promise will be rejected. You must supply a [[run]] method to the constructor that
 * creates the real Promise for the underlying action. Notice that to use this class there are really two
 * Promises involved that are chained together. That makes this class less efficient than just using a Promise directly.
 */ class PromiseWithAbandon < T > { /** Method to abandon the Promise created by [[init]] while it is outstanding. The promise will be rejected. */ public abandon ! : ( msg ? : string ) => void ; private _resolve ! : ( val : any ) => void ; param _run param _args constructor ( private _run , private _args ) { } /** Create a Promise that is chained to the underlying Promise, but is connected to the abandon method. */ public async init ( msg ) { return new Promise < T > ( ( resolve , reject ) => { this . abandon = ( message ? ) => reject ( new AbandonedError ( message ?? msg ) ) ; this . _resolve = resolve ; } ) ; } /** Call the [[run]] method supplied to the ctor to start the underlying Promise. */ public async start ( ) { try { this . _resolve ( await this . _run ( ... this . _args ) ) ; } catch ( err ) { this . abandon ( BentleyError . getErrorMessage ( err ) ) ; } } } beta export class OneAtATimeAction < T > { private _active ? : PromiseWithAbandon < T > ; private _pending ? : PromiseWithAbandon < T > ; private _run : ( ... args : any [ ] ) => Promise < T > ; public msg : string ; param run constructor ( run , msg = "abandoned" ) { this . _run = run ; this . msg = msg ; } note public async request ( ... args ) { const entry = new PromiseWithAbandon < T > ( this . _run , args ) ; const promise = entry . init ( this . msg ) ; if ( this . _active !== undefined ) { if ( this . _pending ) this . _pending . abandon ( ) ; this . _pending = entry ; } else { this . _active = entry ; entry . start ( ) ; } try { return await promise ; } finally { this . _active = this . _pending ; this . _pending = undefined ; if ( this . _active ) this . _active . start ( ) ; } } }