UNPKG

mysql-live

Version:

Brings the server.publish and client.subscribe for live updates on mysql database. The only one Live Collections.

97 lines (96 loc) 4.16 kB
var Receiver = (function () { function Receiver(handler) { this.events = ['disconnect', 'requestSessionId', 'set passport', 'subscribe', 'unsubscribe', 'requestRefresh', 'requestSaveObject', 'requestRemoveObject', 'method call']; this.handler = handler; this.listen(); } Receiver.prototype.listen = function () { var _this = this; this.handler.nsp.on('connection', function (socket) { _this.bindEventsTo(socket); }); }; Receiver.prototype.capitalizeFirstLetter = function (str) { return str.charAt(0).toUpperCase() + str.slice(1); }; Receiver.prototype.bindEventsTo = function (socket) { var _this = this; this.events.forEach(function (evt) { var funcName = 'on' + _this.capitalizeFirstLetter(evt); if (funcName.indexOf(' ') !== -1) { funcName = funcName.split(' ')[0] + _this.capitalizeFirstLetter(funcName.split(' ')[1]); } socket.on(evt, _this[funcName].bind(_this, socket)); }); }; Receiver.prototype.onDisconnect = function (socket) { this.handler.store.removeAllSubscriptionsBySubscriber(socket.id); }; Receiver.prototype.onRequestSessionId = function (socket, cb) { cb(this.handler.passport.getSessionId(socket)); }; Receiver.prototype.onSetPassport = function (socket, encryptedPassportObj, cb) { this.handler.passport.addPassport(socket, encryptedPassportObj); if (cb) { cb(); } }; Receiver.prototype.onSubscribe = function (socket, publicationName, args, callbackClient) { var socketId = socket.id; var subscriptions = (_a = this.handler.store).registerSubscriptions.apply(_a, [socketId, publicationName].concat(args)); this.handler.dispatcher.emitSubscriptions(subscriptions, callbackClient); var _a; }; Receiver.prototype.onUnsubscribe = function (socket, subscriptionName) { this.handler.store.removeSubscriptionByNameAndSubscriber(socket.id, subscriptionName); }; Receiver.prototype.onRequestRefresh = function (socket, collectionName, cb) { var alreadySubToThisCollection = this.handler.store.getSubscriptionByCollectionAndSubscriber(socket.id, collectionName); if (alreadySubToThisCollection !== undefined) { this.handler.dispatcher.publishFind(alreadySubToThisCollection, cb); } }; Receiver.prototype.onRequestSaveObject = function (socket, evt, cb) { var collectionName = evt.collectionName; var object = evt.object; var _col = this.handler.collections[collectionName]; var _table = this.handler.db.table(_col.table); var primaryKeyValue = _table.getPrimaryKeyValue(object); var methodName = ""; if (!primaryKeyValue || primaryKeyValue <= 0) { methodName = "insert"; } else { methodName = "update"; } var hasRights = this.handler.passport.hasRights(_col, methodName, socket, object); if (hasRights) { _table.save(object).then(function (newObj) { if (cb) { cb(newObj); } }); } }; Receiver.prototype.onRequestRemoveObject = function (socket, evt, cb) { var collectionName = evt.collectionName; var primaryKey = evt.primaryKey; var _col = this.handler.collections[collectionName]; var _table = this.handler.db.table(_col.table); var hasRights = this.handler.passport.hasRights(_col, "remove", socket, primaryKey); if (hasRights) { _table.remove(primaryKey).then(function (deleteAnswer) { if (cb) { cb(deleteAnswer.affectedRows); } }); } }; Receiver.prototype.onMethodCall = function (socket, methodName, args, cb) { this.handler.methods.callMethod(socket, methodName, args, cb); }; return Receiver; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = Receiver;