mysql-live-client
Version:
The client side script of mysql-live package.
112 lines (111 loc) • 4.14 kB
JavaScript
var io = require("socket.io-client");
var Handler_1 = require("./Handler");
var MysqlLiveClient = (function () {
function MysqlLiveClient() {
this.NAMESPACE = "/mysql";
this.PROTOCOL = "http";
this.DOMAIN = "localhost";
this.PORT = 8080;
}
Object.defineProperty(MysqlLiveClient.prototype, "Endpoint", {
get: function () {
return this.PROTOCOL + "://" + this.DOMAIN + (this.PORT > 0 ? (":" + this.PORT) : "") + this.NAMESPACE;
},
enumerable: true,
configurable: true
});
MysqlLiveClient.prototype.getCurrentLocationHref = function () {
var _thisLoc = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
return _thisLoc;
};
MysqlLiveClient.prototype.setEndpoint = function (domain, protocol, port) {
if (protocol !== undefined) {
this.PROTOCOL = protocol;
}
var protocolIndexOnDomain = domain.indexOf("://");
if (protocolIndexOnDomain > 0) {
this.PROTOCOL = domain.substr(0, protocolIndexOnDomain);
this.DOMAIN = domain.substr(protocolIndexOnDomain + 1);
}
else {
this.DOMAIN = domain;
}
this.PORT = port !== undefined ? port : 80;
return this.Endpoint;
};
MysqlLiveClient.prototype.connect = function (_endpoint) {
var host = this.Endpoint;
if (_endpoint !== undefined) {
if (_endpoint.lastIndexOf('/') === _endpoint.length - 1)
_endpoint = _endpoint.substr(0, _endpoint.length - 1);
host = _endpoint + this.NAMESPACE;
}
else {
host = this.getCurrentLocationHref() + this.NAMESPACE;
}
var socket = io.connect(host);
this.handler = new Handler_1.default(socket);
return this.handler.dispatcher;
};
Object.defineProperty(MysqlLiveClient.prototype, "isListening", {
get: function () {
return this.handler !== undefined && this.handler.socket !== undefined && this.handler.receiver.isListening;
},
enumerable: true,
configurable: true
});
MysqlLiveClient.prototype.subscribe = function (publicationName) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
if (this.isListening === false) {
this.connect();
}
(_a = this.handler.dispatcher).subscribe.apply(_a, [publicationName].concat(args));
var _a;
};
MysqlLiveClient.prototype.Collection = function (collectionName) {
var col = null;
if (this.isListening) {
col = this.handler.collections[collectionName];
}
if (col === null || col === undefined) {
if (this.isListening === false) {
this.connect();
}
col = this.handler.dispatcher.register(collectionName);
}
return col;
};
MysqlLiveClient.prototype.Object = function (objectName) {
return this.Collection(objectName);
};
MysqlLiveClient.prototype.unsubscribe = function (collectionName) {
if (this.isListening) {
return this.handler.dispatcher.unsubscribe(collectionName);
}
return false;
};
MysqlLiveClient.prototype.setPassport = function (passport, cb) {
this.handler.dispatcher.setPassport(passport, cb);
};
MysqlLiveClient.prototype.call = function (methodName) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
this.handler.dispatcher.callMethod(methodName, args);
};
Object.defineProperty(MysqlLiveClient.prototype, "socket", {
get: function () {
return this.handler.socket;
},
enumerable: true,
configurable: true
});
return MysqlLiveClient;
})();
exports.MysqlLiveClient = MysqlLiveClient;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = new MysqlLiveClient();