@luxorai/sandbox
Version:
TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud
69 lines • 1.81 kB
JavaScript
export class Stream {
abortController;
queue = [];
waiters = [];
draining = false;
closed = false;
finalResult;
finalError;
waitPromise;
resolveWait;
rejectWait;
constructor(abortController) {
this.abortController = abortController;
this.waitPromise = new Promise((resolve, reject) => {
this.resolveWait = resolve;
this.rejectWait = reject;
});
}
next() {
if (this.queue.length > 0) {
return Promise.resolve(this.queue.shift());
}
if (this.closed) {
return this.finalError ? Promise.reject(this.finalError) : Promise.resolve(null);
}
return new Promise((resolve, reject) => {
this.waiters.push({ resolve, reject });
});
}
wait() {
this.draining = true;
this.queue = [];
for (const waiter of this.waiters.splice(0)) {
waiter.resolve(null);
}
return this.waitPromise;
}
cancel(reason) {
this.abortController.abort(reason);
}
push(output) {
if (this.draining) {
return;
}
const waiter = this.waiters.shift();
if (waiter) {
waiter.resolve(output);
return;
}
this.queue.push(output);
}
finish(result) {
this.closed = true;
this.finalResult = result;
this.resolveWait(result);
for (const waiter of this.waiters.splice(0)) {
waiter.resolve(null);
}
}
fail(error) {
this.closed = true;
this.finalError = error;
this.rejectWait(error);
for (const waiter of this.waiters.splice(0)) {
waiter.reject(error);
}
}
}
//# sourceMappingURL=stream.js.map