mysql-live-client
Version:
The client side script of mysql-live package.
55 lines (54 loc) • 3.01 kB
JavaScript
/* Handler deals with socket, Receiver and Dispatcher. */
/* Receiver Accepts from server and do client side actions. */
/* Dispatcher Accepts from client, and push to the server.*/
/* The Handler is a middleware between receiver and dispatcher. */
/* Both Receiver and Dispatcher will have reference to the Handler class because:
The Handler is the only one class will have the socket reference (socket.on,socket.emit).*/
/* Handler is also the only class which keep the collections dictionary, just that, nothing here to do with collections. */
/* Handler initializes the Receiver, and the Dispatcher. */
/* Handler is initialized by the MysqlLiveClient. */
/* The only difference from the server-side style code is that the Collection here,
should make emits/send actions to the server, so we pass the Dispatcher to the Collection Object as an argument on constructor.*/
/// <reference path="../../typings/socket.io-client/socket.io-client.d.ts" />
var Collection_1 = require("./Collection");
var Dispatcher_1 = require("./Dispatcher");
var Receiver_1 = require("./Receiver");
var _ = require("lodash");
var Handler = (function () {
function Handler(socket) {
this.collections = {};
this.subscriptions = [];
this.socket = socket;
this.dispatcher = new Dispatcher_1.default(this);
this.receiver = new Receiver_1.default(this);
}
Handler.prototype.addCollection = function (name) {
var col = new Collection_1.default(this, name);
this.collections[name] = col;
var waitingPublicationForThisCollection = this.receiver.getWaitingPublication(name);
if (waitingPublicationForThisCollection !== undefined) {
this.receiver.doCollectionInitialReceive(waitingPublicationForThisCollection);
}
return col;
};
Handler.prototype.subscriptionAcceptedFromServer = function (subscriptionName, toCollectionName) {
var alreadySubscribed = _.findLast(this.subscriptions, function (sub) { return sub.name === subscriptionName && sub.collectionName === toCollectionName; });
if (alreadySubscribed === undefined) {
alreadySubscribed = { name: subscriptionName, collectionName: toCollectionName };
}
return alreadySubscribed;
};
Handler.prototype.getSubscriptionsByCollection = function (collectionName) {
return _.filter(this.subscriptions, function (sub) { return sub.collectionName === collectionName; });
};
Handler.prototype.getSubscriptionsByName = function (subscriptionName) {
return _.filter(this.subscriptions, function (sub) { return sub.name === subscriptionName; });
};
Handler.prototype.removeSubscriptionsByName = function (subscriptionName) {
var removedSubs = _.remove(this.subscriptions, function (sub) { return sub.name === subscriptionName; });
return removedSubs;
};
return Handler;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Handler;