diffusion
Version:
Diffusion JavaScript client
148 lines (147 loc) • 5.34 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamImpl = void 0;
/**
* Implementation of the {@link Stream} interface.
*
* @inheritdoc
*/
var StreamImpl = /** @class */ (function () {
/**
* Create a new `Stream` object
*
* @param listeners the listeners that will be attached to this stream
* @param error a callback to call in the case of an error
* @param close a callback to call when the stream is closed
* @param events a list of allowed event names. If `events` is not specified
* then any events are allowed in the `on` and `off`
* methods. If events are specified, then any event
* listener will be checked agains the list of allowed
* events. The events `close`, `error`, and `complete` are
* always allowed because they are used by {@link Stream}
* and {@link Result} internally.
*/
function StreamImpl(helper) {
this.listeners = helper.getListeners();
this.errorCallback = helper.getErrorCallback();
this.closeCallback = helper.getCloseCallback();
}
/**
* @inheritdoc
*/
StreamImpl.prototype.on = function (events, listener) {
this.attach(events, listener);
return this;
};
/**
* @inheritdoc
*/
StreamImpl.prototype.off = function (events, listener) {
this.remove(events, listener);
return this;
};
/**
* @inheritdoc
*
* Will cause `closeCallback` to be called.
*
* @param reason the reason for closing the stream. This can be of any type.
* The type of the close reason should be documented in the
* class extending StreamImpl.
*/
StreamImpl.prototype.close = function (reason) {
this.closeCallback(reason);
return this;
};
/**
* Close the stream. This will emit a 'close' event to any assigned listeners.
* No further events will be emitted.
*/
StreamImpl.prototype.error = function (reason) {
this.errorCallback(reason);
return this;
};
/**
* Set one or more properties on an object;
*
* @param {Object} object - The object to attach properties to
* @param {String|Object} key - The key, or an object map of properties
* @param {*} value - If the key is a string, this is the value to be associated with it
*
* @returns The object, with attached properties
*/
StreamImpl.prototype.attach = function (key, value) {
var e_1, _a;
if (typeof key === 'object') {
try {
for (var _b = __values(Object.keys(key)), _c = _b.next(); !_c.done; _c = _b.next()) {
var k = _c.value;
this.attach(k, key[k]);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
}
else if (value) {
if (!this.listeners.hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins
this.listeners[key] = [];
}
this.listeners[key].push(value);
}
};
/**
* Remove one or more properties from an object
*
* @param {Object} object - The object to remove properties from
* @param {String|Object} key - The key, or an object map of properties
* @param {*} value - If the key is a string, this is the value to remove from it
*
* @returns The object, sans specified properties
*/
StreamImpl.prototype.remove = function (key, value) {
var e_2, _a;
if (typeof key === 'object') {
try {
for (var _b = __values(Object.keys(key)), _c = _b.next(); !_c.done; _c = _b.next()) {
var k = _c.value;
this.remove(k, key[k]);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
}
else if (this.listeners.hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins
if (value) {
this.listeners[key] = this.listeners[key].filter(function (e) {
return e !== value;
});
}
else {
this.listeners[key] = [];
}
}
};
return StreamImpl;
}());
exports.StreamImpl = StreamImpl;