mysql-live-client
Version:
The client side script of mysql-live package.
110 lines (109 loc) • 5.07 kB
JavaScript
var Helper_1 = require("./Helper");
var _ = require("lodash");
var Receiver = (function () {
function Receiver(handler) {
this.waitingPublicationReceive = [];
this.events = ['onPublicationReceive', 'onInsertObject', 'onUpdateObject', 'onRemoveObject'];
this.isListening = false;
this.handler = handler;
this.listen();
}
Receiver.prototype.listen = function () {
var _this = this;
this.events.forEach(function (evt) {
_this.handler.socket.on(evt, _this[evt].bind(_this));
});
this.isListening = true;
};
Receiver.prototype.onPublicationReceive = function (publication) {
var col = this.handler.collections[publication.collectionName];
if (col === undefined) {
this.waitingPublicationReceive.push(publication);
}
else {
this.doCollectionInitialReceive(publication);
}
};
Receiver.prototype.doCollectionInitialReceive = function (publication) {
var col = this.handler.collections[publication.collectionName];
if (col === undefined)
return;
if (publication.items.length > 0 && col.items.length > 0 && col.items.length === publication.items.length &&
col.items[col.items.length - 1][col.primaryKeyColumn] === publication.items[publication.items.length - 1][col.primaryKeyColumn] &&
col.items[0][col.primaryKeyColumn] === publication.items[0][col.primaryKeyColumn]) {
return;
}
else if (publication.items.length === col.items.length && col.hasReceived === true) {
if (publication.items.length > 0 && publication.items[0][col.primaryKeyColumn] === col.items[0][col.primaryKeyColumn]) {
return;
}
}
col.primaryKeyColumn = publication.primaryKeyColumn;
col.items = publication.items;
col.hasReceived = true;
var evt;
if (publication.__single_item_collection__) {
col.__single_item_collection__ = publication.__single_item_collection__;
evt = { name: 'receive', item: (col.items && col.items.length > 0 ? col.items[0] : undefined) };
}
else {
evt = { name: 'receive', items: col.items };
}
this.handler.subscriptionAcceptedFromServer(publication.name, publication.collectionName);
var indexOfWaitingPub = _.indexOf(this.waitingPublicationReceive, function (waitingPub) { return waitingPub.collectionName === publication.collectionName; });
if (indexOfWaitingPub !== -1) {
this.waitingPublicationReceive.splice(indexOfWaitingPub, 1);
}
col.fireChange(evt);
};
Receiver.prototype.getWaitingPublication = function (collectionName) {
return _.find(this.waitingPublicationReceive, function (waitingPub) { return waitingPub.collectionName === collectionName; });
};
Receiver.prototype.onInsertObject = function (evt) {
var col = this.handler.collections[evt.collectionName];
if (col.__single_item_collection__ === false) {
if (col === undefined) {
console.error(evt.collectionName + ':onInsertObject Error# Please put Mysql.connect(...) method before your the Mysql.Collection calls, otherwise you will still get errors, the package will work but maybe you have problems later.');
}
else {
var event_1 = { name: 'insert', newItem: evt.newItem };
col.items.push(evt.newItem);
col.fireChange(event_1);
}
}
};
Receiver.prototype.onUpdateObject = function (evt) {
var col = this.handler.collections[evt.collectionName];
if (col.__single_item_collection__ && col.item !== undefined) {
if (evt.selector[col.primaryKeyColumn] === col.item[col.primaryKeyColumn]) {
this.updateObject(col, evt);
}
}
else {
this.updateObject(col, evt);
}
};
Receiver.prototype.updateObject = function (col, evt) {
var newItem = Helper_1.default._findAndUpdate(col.items, evt.selector, evt.newItem);
if (newItem !== undefined) {
var event_2 = { name: 'update', newItem: newItem, selector: evt.selector };
col.fireChange(event_2);
}
};
Receiver.prototype.onRemoveObject = function (evt) {
var col = this.handler.collections[evt.collectionName];
if (col.__single_item_collection__ && col.item !== undefined) {
if (evt.selector[col.primaryKeyColumn] === col.item[col.primaryKeyColumn]) {
col.items.splice(0, 1);
col.fireChange({ name: 'remove', selector: evt.selector });
}
}
else {
_.remove(col.items, evt.selector);
col.fireChange({ name: 'remove', selector: evt.selector });
}
};
return Receiver;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Receiver;