@sapientpro/json-stream
Version:
A JSON stream parser
40 lines (39 loc) • 1.15 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Suspendable = void 0;
class Suspendable {
constructor(trace = false) {
this.trace = trace;
this._suspended = false;
this._resume = Promise.withResolvers();
this._suspend = Promise.withResolvers();
}
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());
}
}
}
exports.Suspendable = Suspendable;
;