You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
927 B
JavaScript
36 lines
927 B
JavaScript
"use strict";
|
|
|
|
const debug = require("debug")("promistream:from-node-stream:readable:attach-handlers");
|
|
|
|
module.exports = function attachReadableStreamHandlers({ stream, onClose, onError, onData }) {
|
|
function detachEventHandlers() {
|
|
debug("Detaching event handlers");
|
|
stream.removeListener("end", onCloseWrapper);
|
|
stream.removeListener("close", onCloseWrapper);
|
|
stream.removeListener("error", onErrorWrapper);
|
|
stream.removeListener("data", onData);
|
|
}
|
|
|
|
function attachEventHandlers() {
|
|
debug("Attaching event handlers");
|
|
stream.on("end", onCloseWrapper);
|
|
stream.on("close", onCloseWrapper);
|
|
stream.on("error", onErrorWrapper);
|
|
stream.on("data", onData);
|
|
}
|
|
|
|
function onCloseWrapper() {
|
|
debug("onCloseWrapper called");
|
|
onClose();
|
|
detachEventHandlers();
|
|
}
|
|
|
|
function onErrorWrapper(error) {
|
|
debug("onErrorWrapper called");
|
|
onError(error);
|
|
detachEventHandlers();
|
|
}
|
|
|
|
attachEventHandlers();
|
|
};
|