pubsub-ts
Version:
PubSub Messaging Pattern in Typescript
111 lines • 4.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var PubSub;
(function (PubSub) {
var NotificationType;
(function (NotificationType) {
NotificationType[NotificationType["standard"] = 0] = "standard";
NotificationType[NotificationType["priority"] = 1] = "priority";
NotificationType[NotificationType["urgent"] = 2] = "urgent";
})(NotificationType = PubSub.NotificationType || (PubSub.NotificationType = {}));
var Subscriber = /** @class */ (function () {
function Subscriber(key) {
if (key === void 0) { key = '' + new Date().getTime(); }
this.standardQueue = [];
this.priorityQueue = [];
this.interests = new Map();
this.shouldPost = false;
this.key = key;
}
Subscriber.prototype.on = function (eventName, callback) {
this.interests.set(eventName, callback);
};
Subscriber.prototype.off = function (eventName) {
this.interests.delete(eventName);
};
Subscriber.prototype.getKey = function () {
return this.key;
};
Subscriber.prototype.start = function () {
this.shouldPost = true;
this.processNotifications();
};
Subscriber.prototype.pause = function () {
this.shouldPost = false;
};
Subscriber.prototype.post = function (notification) {
switch (notification.type) {
case NotificationType.standard:
this.standardQueue.unshift(notification);
break;
case NotificationType.priority:
this.priorityQueue.unshift(notification);
break;
case NotificationType.urgent:
this.postCallback(notification);
break;
}
this.processNotifications();
};
Subscriber.prototype.processNotifications = function () {
if (!this.shouldPost) {
return;
}
this.postNotifications(this.priorityQueue);
this.postNotifications(this.standardQueue);
};
Subscriber.prototype.postNotifications = function (queue) {
var i = queue.length;
while (i--) {
var notification = queue[i];
this.postCallback(notification);
}
};
Subscriber.prototype.postCallback = function (notification) {
var callback = this.interests.get(notification.name);
if (callback) {
callback.call(this, notification);
}
};
return Subscriber;
}());
PubSub.Subscriber = Subscriber;
var Publisher = /** @class */ (function () {
function Publisher() {
this.subscribers = new Map();
}
Publisher.prototype.add = function (subscriber) {
var key = subscriber.getKey();
if (!this.has(key)) {
this.subscribers.set(key, subscriber);
}
};
Publisher.prototype.delete = function (subscriber) {
this.subscribers.delete(subscriber.getKey());
};
Publisher.prototype.has = function (key) {
return this.subscribers.has(key);
};
Publisher.prototype.notify = function (eventName, data) {
this.sendNotification(eventName, data, NotificationType.standard);
};
Publisher.prototype.notifyPriority = function (eventName, data) {
this.sendNotification(eventName, data, NotificationType.priority);
};
Publisher.prototype.notifyUrgent = function (eventName, data) {
this.sendNotification(eventName, data, NotificationType.urgent);
};
Publisher.prototype.sendNotification = function (eventName, data, type) {
this.subscribers.forEach(function (subscriber) {
subscriber.post({
name: eventName,
body: data,
type: type
});
});
};
return Publisher;
}());
PubSub.Publisher = Publisher;
})(PubSub = exports.PubSub || (exports.PubSub = {}));
//# sourceMappingURL=index.js.map