webserv
Version:
a quick, flexible, fully typed development server
87 lines • 3.52 kB
JavaScript
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const DONE = Symbol();
/**
* Provide a stream of responses from a producer
*/
class BufferedResponse {
constructor() {
this.data = [];
this.done = false;
this.notifiers = [];
}
add(item) {
if (!this.done) {
this.data.push(item);
this.notify();
}
}
close() {
if (!this.done) {
this.done = true;
this.data.push(DONE);
this.notify();
}
}
[Symbol.asyncIterator]() {
return __asyncGenerator(this, arguments, function* _a() {
let i = 0;
let value = yield __await(this.wait(i++));
while (this.isValue(value)) {
yield yield __await(value);
value = yield __await(this.wait(i++));
}
});
}
isValue(value) {
return value !== DONE;
}
notify() {
for (let i = this.notifiers.length - 1; i >= 0; i--) {
const notifier = this.notifiers[i];
if (this.data.length > notifier.offset) {
notifier.resolve(this.data[notifier.offset]);
this.notifiers.slice(i, 1);
}
else if (this.done) {
notifier.resolve(DONE);
this.notifiers.slice(i, 1);
}
}
}
wait(i) {
if (i < this.data.length) {
return Promise.resolve(this.data[i]);
}
return new Promise((resolve) => {
this.notifiers.push({
offset: i,
resolve
});
});
}
}
exports.default = BufferedResponse;
});
//# sourceMappingURL=BufferedResponse.js.map