class StreamPipelineElem {
constructor() {
this.next = null;
}
then = (handler) => {
const next = new StreamThen(handler);
this.next = next;
return next;
};
map = (transform) => {
const next = new StreamMap(transform);
this.next = next;
return next;
};
catch = (handler) => {
const next = new StreamCatch(handler);
this.next = next;
return next;
};
finally = (handler) => {
const next = new StreamFinally(handler);
this.next = next;
return next;
};
}
class StreamThen extends StreamPipelineElem {
constructor(handler) {
super();
this.handler = handler;
}
onMessage = (item) => {
try {
this.handler(item);
} catch (error) {
this.onError(error);
return; }
if (this.next !== null) {
this.next.onMessage(item);
}
};
onError = (error) => {
if (this.next !== null) {
this.next.onError(error);
} else {
throw error;
}
};
onFinally = () => {
if (this.next !== null) {
this.next.onFinally();
}
};
}
class StreamMap extends StreamPipelineElem {
constructor(transform) {
super();
this.transform = transform;
}
onMessage = (item) => {
let transformed = null;
try {
transformed = this.transform(item);
} catch (error) {
this.onError(error);
return; }
if (this.next !== null) {
this.next.onMessage(transformed);
}
};
onError = (error) => {
if (this.next !== null) {
this.next.onError(error);
} else {
throw error;
}
};
onFinally = () => {
if (this.next !== null) {
this.next.onFinally();
}
};
}
class StreamCatch extends StreamPipelineElem {
constructor(handler) {
super();
this.handler = handler;
}
onMessage = (item) => {
if (this.next !== null) {
this.next.onMessage(item);
}
};
onError = (error) => {
try {
this.handler(error);
} catch (otherError) {
if (this.next !== null) {
this.next.onError(otherError);
} else {
throw otherError;
}
}
};
onFinally = () => {
if (this.next !== null) {
this.next.onFinally();
}
};
}
class StreamFinally extends StreamPipelineElem {
constructor(handler) {
super();
this.handler = handler;
}
onMessage = (item) => {
if (this.next !== null) {
this.next.onMessage(item);
}
};
onError = (error) => {
if (this.next !== null) {
this.next.onError(error);
} else {
throw error;
}
};
onFinally = () => {
try {
this.handler();
} catch (error) {
this.onError(error);
return; }
if (this.next !== null) {
this.next.onFinally();
}
};
}
class StreamEventSource extends StreamPipelineElem {
constructor(uri, parameters, noReconnect) {
super();
const finalUri = uriEnqueueArgs(uri, parameters);
this.noReconnect = noReconnect ?? false;
this.source = new EventSource(finalUri, { withCredentials: true });
this.source.addEventListener("message", this.onMessage);
this.source.addEventListener("error", this.onError);
}
onMessage = (message) => {
if (this.next !== null) {
this.next.onMessage(message);
}
};
onError = (error) => {
if (this.noReconnect) {
this.source.close();
this.onFinally();
} else if (this.next !== null) {
if (this.next !== null) {
this.next.onError(error);
}
}
};
onFinally = () => {
if (this.next !== null) {
this.next.onFinally();
}
};
}
function uriEnqueueArgs(uri, parameters) {
if (parameters !== null) {
let names = Object.getOwnPropertyNames(parameters);
let first = true;
for (let p = 0; p !== names.length; p++) {
let value = parameters[names[p]];
if (Array.isArray(value)) {
for (let i = 0; i !== value.length; i++) {
uri += first ? "?" : "&";
uri += names[p];
uri += "=";
uri += encodeURIComponent(value[i]);
first = false;
}
} else {
uri += first ? "?" : "&";
uri += names[p];
uri += "=";
uri += encodeURIComponent(value);
}
first = false;
}
}
return uri;
}