@mavrykdynamics/taquito
Version:
High level functionality that builds upon the other packages in the Mavryk Typescript Library Suite.
80 lines (79 loc) • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObservableSubscription = exports.UnsupportedEventError = void 0;
/* eslint-disable no-dupe-class-members */
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const errors_1 = require("./errors");
var errors_2 = require("./errors");
Object.defineProperty(exports, "UnsupportedEventError", { enumerable: true, get: function () { return errors_2.UnsupportedEventError; } });
class ObservableSubscription {
constructor(obs, shouldRetry = false, operatorFunction = (0, operators_1.retry)()) {
this.shouldRetry = shouldRetry;
this.operatorFunction = operatorFunction;
this.errorListeners = [];
this.messageListeners = [];
this.closeListeners = [];
this.completed$ = new rxjs_1.Subject();
obs
.pipe((0, operators_1.takeUntil)(this.completed$), (0, operators_1.tap)((data) => {
this.call(this.messageListeners, data);
}, (error) => {
this.call(this.errorListeners, error);
}, () => {
this.call(this.closeListeners);
}), this.shouldRetry ? operatorFunction : (0, operators_1.tap)(), (0, operators_1.catchError)(() => rxjs_1.NEVER))
.subscribe();
}
call(listeners, value) {
for (const l of listeners) {
try {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
l(value);
}
catch (ex) {
console.error(ex);
}
}
}
remove(listeners, value) {
const idx = listeners.indexOf(value);
if (idx !== -1) {
listeners.splice(idx, 1);
}
}
on(type, cb) {
switch (type) {
case 'data':
this.messageListeners.push(cb);
break;
case 'error':
this.errorListeners.push(cb);
break;
case 'close':
this.closeListeners.push(cb);
break;
default:
throw new errors_1.UnsupportedEventError(type);
}
}
off(type, cb) {
switch (type) {
case 'data':
this.remove(this.messageListeners, cb);
break;
case 'error':
this.remove(this.errorListeners, cb);
break;
case 'close':
this.remove(this.closeListeners, cb);
break;
default:
throw new errors_1.UnsupportedEventError(type);
}
}
close() {
this.completed$.next();
}
}
exports.ObservableSubscription = ObservableSubscription;