landstrasse
Version:
Strongly typed WAMP Client for browsers
127 lines • 4.93 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import Deferred from '../../../util/deferred';
import { LogLevel } from '../../../util/logger';
class Subscriptions {
constructor(id, uri, unsubscribeCallback, logger) {
Object.defineProperty(this, "logger", {
enumerable: true,
configurable: true,
writable: true,
value: logger
});
Object.defineProperty(this, "_id", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_uri", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_subscriptions", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
Object.defineProperty(this, "_unsubscribeCallback", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_unsubscribed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
Object.defineProperty(this, "unsubscribedDeferred", {
enumerable: true,
configurable: true,
writable: true,
value: new Deferred()
});
this._id = id;
this._uri = uri;
this._unsubscribeCallback = unsubscribeCallback;
this.reinitCatch();
}
get id() {
return this._id;
}
get uri() {
return this._uri;
}
add(requestId, subscription) {
if (this._unsubscribed) {
throw new Error('Subscriptions are already closed.');
}
this._subscriptions.set(requestId, subscription);
}
trigger(args, kwArgs, details) {
if (this._unsubscribed) {
return;
}
this.logger.log(LogLevel.DEBUG, `Event received for subscription "${this.uri}".`, args, kwArgs, details);
this._subscriptions.forEach((subscription) => {
try {
subscription.handler(args, kwArgs, details);
}
catch (error) {
this.logger.log(LogLevel.WARNING, `Error thrown while executing a callback for the subscription "${this.uri}".`, error);
}
});
}
unsubscribe(requestId) {
return __awaiter(this, void 0, void 0, function* () {
if (this._unsubscribed) {
return;
}
const subscription = this._subscriptions.get(requestId);
if (!subscription) {
throw new Error('Unexpected unsubscribe (unable to find the related subscription).');
}
this.logger.log(LogLevel.DEBUG, `Unsubscribing ${requestId} from \`${this.uri}\`.`);
this._subscriptions.delete(requestId);
if (this._subscriptions.size === 0) {
this.unsubscribedDeferred.promise.then(() => { subscription.unsubscribedDeferred.resolve(); }, (err) => { subscription.unsubscribedDeferred.reject(err); });
yield this._unsubscribeCallback(this);
}
else {
subscription.unsubscribedDeferred.resolve();
}
return subscription.unsubscribed;
});
}
//
// - Internal
//
reinitCatch() {
this.unsubscribedDeferred = new Deferred();
this.unsubscribedDeferred.promise.then(() => {
this._unsubscribed = true;
// This can happen in one of two cases, which is why the loop is necessary
// First, when the last subscriber unsubscribes, then this array is empty
// Second: when the router sends actively a UNSUBSCRIBED message to indicate that
// the subscription was revoked.
this._subscriptions.forEach((subscription) => {
subscription.unsubscribedDeferred.resolve();
});
this._subscriptions.clear();
}, () => { this.reinitCatch(); });
}
}
export default Subscriptions;
//# sourceMappingURL=subscriptions.js.map