UNPKG

@sapientpro/json-stream

Version:
37 lines (36 loc) 998 B
export class Suspendable { trace; _suspended = false; _resume = Promise.withResolvers(); _suspend = Promise.withResolvers(); constructor(trace = false) { this.trace = trace; } async suspend(value) { this.trace && this._trace('Suspending'); this._suspended = true; const { resolve } = this._suspend; this._suspend = Promise.withResolvers(); resolve({ value }); return await this._resume.promise; } async resume(value) { this.trace && this._trace('Resuming'); this._suspended = false; const { resolve } = this._resume; this._resume = Promise.withResolvers(); resolve({ value }); return this._suspend.promise; } get suspended() { return this._suspended; } _trace(type) { try { throw new Error(type); } catch (e) { console.log(type + ' ' + e.stack.split('\n')[3].trim()); } } }