backendless-rt-client
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -7,4 +7,8 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var isUndefined = function isUndefined(value) { | ||
@@ -23,53 +27,61 @@ return typeof value === 'undefined'; | ||
var RTConfig = { | ||
appId: null, | ||
lookupPath: null, | ||
debugMode: false, | ||
connectQuery: {}, | ||
var RTConfig = function () { | ||
function RTConfig(config) { | ||
_classCallCheck(this, RTConfig); | ||
set: function set(config) { | ||
if (!config) { | ||
return; | ||
} | ||
this.appId = null; | ||
this.lookupPath = null; | ||
this.debugMode = false; | ||
this.connectQuery = {}; | ||
if (!isUndefined(config.appId)) { | ||
if (!isString(config.appId)) { | ||
throw new Error('"appId" must be String.'); | ||
this.set(config); | ||
} | ||
_createClass(RTConfig, [{ | ||
key: 'set', | ||
value: function set(config) { | ||
if (!config) { | ||
return; | ||
} | ||
this.appId = config.appId; | ||
} | ||
if (!isUndefined(config.appId)) { | ||
if (!isString(config.appId)) { | ||
throw new Error('"appId" must be String.'); | ||
} | ||
if (!isUndefined(config.lookupPath)) { | ||
if (!isString(config.lookupPath)) { | ||
throw new Error('"lookupPath" must be String.'); | ||
this.appId = config.appId; | ||
} | ||
this.lookupPath = config.lookupPath; | ||
} | ||
if (!isUndefined(config.lookupPath)) { | ||
if (!isString(config.lookupPath)) { | ||
throw new Error('"lookupPath" must be String.'); | ||
} | ||
if (!isUndefined(config.debugMode)) { | ||
this.debugMode = !!config.debugMode; | ||
} | ||
this.lookupPath = config.lookupPath; | ||
} | ||
if (!isUndefined(config.connectQuery)) { | ||
if (isFunction(config.connectQuery)) { | ||
this.getConnectQuery = config.connectQuery; | ||
} else if (isObject(config.connectQuery)) { | ||
this.connectQuery = config.connectQuery; | ||
} else { | ||
throw new Error('"connectQuery" must be Function or Object.'); | ||
if (!isUndefined(config.debugMode)) { | ||
this.debugMode = !!config.debugMode; | ||
} | ||
if (!isUndefined(config.connectQuery)) { | ||
if (isFunction(config.connectQuery)) { | ||
this.getConnectQuery = config.connectQuery; | ||
} else if (isObject(config.connectQuery)) { | ||
this.connectQuery = config.connectQuery; | ||
} else { | ||
throw new Error('"connectQuery" must be Function or Object.'); | ||
} | ||
} | ||
} | ||
}, | ||
}, { | ||
key: 'getConnectQuery', | ||
value: function getConnectQuery() { | ||
return this.connectQuery; | ||
} | ||
}]); | ||
return RTConfig; | ||
}(); | ||
/** | ||
* @abstract | ||
**/ | ||
getConnectQuery: function getConnectQuery() { | ||
return this.connectQuery; | ||
} | ||
}; | ||
exports.default = RTConfig; |
@@ -6,13 +6,10 @@ 'use strict'; | ||
}); | ||
exports.RTScopeConnector = exports.RTListeners = undefined; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _config = require('./config'); | ||
var _client = require('./client'); | ||
var _config2 = _interopRequireDefault(_config); | ||
var _client2 = _interopRequireDefault(_client); | ||
var _provider = require('./provider'); | ||
var _provider2 = _interopRequireDefault(_provider); | ||
var _listeners = require('./listeners'); | ||
@@ -30,15 +27,13 @@ | ||
var BackendlessRTClient = { | ||
Config: _config2.default, | ||
Provider: _provider2.default, | ||
Listeners: _listeners2.default, | ||
ScopeConnector: _scopeConnector2.default | ||
}; | ||
_client2.default.Listeners = _listeners2.default; | ||
_client2.default.ScopeConnector = _scopeConnector2.default; | ||
if (root) { | ||
root.BackendlessRTClient = BackendlessRTClient; | ||
root.BackendlessRTClient = _client2.default; | ||
} | ||
module.exports = BackendlessRTClient; | ||
module.exports = _client2.default; | ||
exports.default = BackendlessRTClient; | ||
exports.default = _client2.default; | ||
var RTListeners = exports.RTListeners = _listeners2.default; | ||
var RTScopeConnector = exports.RTScopeConnector = _scopeConnector2.default; |
@@ -27,11 +27,15 @@ 'use strict'; | ||
var RTMethods = function () { | ||
function RTMethods(rtProvider) { | ||
function RTMethods(_ref) { | ||
var _this = this; | ||
var onMessage = _ref.onMessage, | ||
emitMessage = _ref.emitMessage, | ||
terminateSocketIfNeeded = _ref.terminateSocketIfNeeded; | ||
_classCallCheck(this, RTMethods); | ||
this.onResponse = function (_ref) { | ||
var id = _ref.id, | ||
error = _ref.error, | ||
result = _ref.result; | ||
this.onResponse = function (_ref2) { | ||
var id = _ref2.id, | ||
error = _ref2.error, | ||
result = _ref2.result; | ||
@@ -48,2 +52,4 @@ if (_this.invocations[id]) { | ||
delete _this.invocations[id]; | ||
_this.terminateSocketIfNeeded(); | ||
} | ||
@@ -60,3 +66,5 @@ }; | ||
this.rtProvider = rtProvider; | ||
this.onMessage = onMessage; | ||
this.emitMessage = emitMessage; | ||
this.terminateSocketIfNeeded = terminateSocketIfNeeded; | ||
@@ -70,3 +78,3 @@ this.invocations = {}; | ||
if (!this.initialized) { | ||
this.rtProvider.on(_constants.RTSocketEvents.MET_RES, this.onResponse); | ||
this.onMessage(_constants.RTSocketEvents.MET_RES, this.onResponse); | ||
@@ -77,2 +85,7 @@ this.initialized = true; | ||
}, { | ||
key: 'terminate', | ||
value: function terminate() { | ||
this.invocations = {}; | ||
} | ||
}, { | ||
key: 'reconnect', | ||
@@ -86,2 +99,7 @@ value: function reconnect() { | ||
}, { | ||
key: 'hasActivity', | ||
value: function hasActivity() { | ||
return !!Object.keys(this.invocations).length; | ||
} | ||
}, { | ||
key: 'send', | ||
@@ -100,3 +118,3 @@ | ||
this.rtProvider.emit(_constants.RTSocketEvents.MET_REQ, methodData); | ||
this.emitMessage(_constants.RTSocketEvents.MET_REQ, methodData); | ||
@@ -103,0 +121,0 @@ return new Promise(function (resolve, reject) { |
@@ -18,6 +18,2 @@ 'use strict'; | ||
var _config = require('./config'); | ||
var _config2 = _interopRequireDefault(_config); | ||
var _constants = require('./constants'); | ||
@@ -32,10 +28,10 @@ | ||
key: 'connect', | ||
value: function connect(onDisconnect) { | ||
if (!_config2.default.lookupPath) { | ||
throw new Error('RTConfig.lookupPath is not configured'); | ||
value: function connect(config, onDisconnect) { | ||
if (!config.lookupPath) { | ||
throw new Error('config.lookupPath is not configured'); | ||
} | ||
return _backendlessRequest2.default.get(_config2.default.lookupPath).then(function (rtServerHost) { | ||
return _backendlessRequest2.default.get(config.lookupPath).then(function (rtServerHost) { | ||
return new Promise(function (resolve, reject) { | ||
var rtSocket = new RTSocket(rtServerHost); | ||
var rtSocket = new RTSocket(config, rtServerHost); | ||
@@ -67,17 +63,19 @@ rtSocket.on(_constants.NativeSocketEvents.CONNECT, onConnect); | ||
function RTSocket(host) { | ||
function RTSocket(config, host) { | ||
_classCallCheck(this, RTSocket); | ||
this.config = config; | ||
this.events = {}; | ||
if (!_config2.default.appId) { | ||
throw new Error('RTConfig.appId is not configured'); | ||
if (!config.appId) { | ||
throw new Error('config.appId is not configured'); | ||
} | ||
this.ioSocket = (0, _socket2.default)(host + '/' + _config2.default.appId, { | ||
this.ioSocket = (0, _socket2.default)(host + '/' + this.config.appId, { | ||
forceNew: true, | ||
autoConnect: false, | ||
reconnection: false, | ||
path: '/' + _config2.default.appId, | ||
query: _config2.default.getConnectQuery() | ||
path: '/' + this.config.appId, | ||
query: this.config.getConnectQuery() | ||
}); | ||
@@ -130,3 +128,5 @@ } | ||
value: function onEvent(event, data) { | ||
logMessage('FROM SERVER', event, data); | ||
if (this.config.debugMode) { | ||
logMessage('FROM SERVER', event, data); | ||
} | ||
@@ -142,3 +142,5 @@ if (this.events[event]) { | ||
value: function emit(event, data) { | ||
logMessage('TO SERVER', event, data); | ||
if (this.config.debugMode) { | ||
logMessage('TO SERVER', event, data); | ||
} | ||
@@ -156,5 +158,3 @@ this.ioSocket.emit(event, data); | ||
function logMessage(type, event, data) { | ||
if (_config2.default.debugMode) { | ||
console.log('[' + type + '] - [event: ' + event + '] - arguments: ' + JSON.stringify(data) + ' '); | ||
} | ||
console.log('[' + type + '] - [event: ' + event + '] - arguments: ' + JSON.stringify(data) + ' '); | ||
} |
@@ -27,3 +27,7 @@ 'use strict'; | ||
var RTSubscriptions = function () { | ||
function RTSubscriptions(rtProvider) { | ||
function RTSubscriptions(_ref) { | ||
var onMessage = _ref.onMessage, | ||
emitMessage = _ref.emitMessage, | ||
terminateSocketIfNeeded = _ref.terminateSocketIfNeeded; | ||
_classCallCheck(this, RTSubscriptions); | ||
@@ -44,3 +48,5 @@ | ||
this.rtProvider = rtProvider; | ||
this.onMessage = onMessage; | ||
this.emitMessage = emitMessage; | ||
this.terminateSocketIfNeeded = terminateSocketIfNeeded; | ||
@@ -56,3 +62,3 @@ this.subscriptions = {}; | ||
if (!this.initialized) { | ||
this.rtProvider.on(_constants.RTSocketEvents.SUB_RES, function (data) { | ||
this.onMessage(_constants.RTSocketEvents.SUB_RES, function (data) { | ||
return _this.onSubscriptionResponse(data); | ||
@@ -65,2 +71,11 @@ }); | ||
}, { | ||
key: 'terminate', | ||
value: function terminate() { | ||
var _this2 = this; | ||
Object.keys(this.subscriptions).forEach(function (subscriptionId) { | ||
return _this2.stopSubscription(subscriptionId); | ||
}); | ||
} | ||
}, { | ||
key: 'reconnect', | ||
@@ -75,15 +90,20 @@ value: function reconnect() { | ||
}, { | ||
key: 'hasActivity', | ||
value: function hasActivity() { | ||
return !!Object.keys(this.subscriptions).length; | ||
} | ||
}, { | ||
key: 'reconnectSubscriptions', | ||
value: function reconnectSubscriptions() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
Object.keys(this.subscriptions).forEach(function (subscriptionId) { | ||
var subscription = _this2.subscriptions[subscriptionId]; | ||
var subscription = _this3.subscriptions[subscriptionId]; | ||
if (subscription.keepAlive === false) { | ||
delete _this2.subscriptions[subscriptionId]; | ||
delete _this3.subscriptions[subscriptionId]; | ||
} else { | ||
subscription.ready = false; | ||
_this2.onSubscription(subscriptionId); | ||
_this3.startSubscription(subscriptionId); | ||
} | ||
@@ -94,11 +114,11 @@ }); | ||
key: 'subscribe', | ||
value: function subscribe(name, options, _ref) { | ||
var _this3 = this; | ||
value: function subscribe(name, options, _ref2) { | ||
var _this4 = this; | ||
var keepAlive = _ref.keepAlive, | ||
parser = _ref.parser, | ||
onData = _ref.onData, | ||
onError = _ref.onError, | ||
onStop = _ref.onStop, | ||
onReady = _ref.onReady; | ||
var keepAlive = _ref2.keepAlive, | ||
parser = _ref2.parser, | ||
onData = _ref2.onData, | ||
onError = _ref2.onError, | ||
onStop = _ref2.onStop, | ||
onReady = _ref2.onReady; | ||
@@ -120,12 +140,12 @@ this.initialize(); | ||
this.onSubscription(subscriptionId); | ||
this.startSubscription(subscriptionId); | ||
return { | ||
isReady: function isReady() { | ||
return !!_this3.subscriptions[subscriptionId] && _this3.subscriptions[subscriptionId].ready; | ||
return !!_this4.subscriptions[subscriptionId] && _this4.subscriptions[subscriptionId].ready; | ||
}, | ||
stop: function stop() { | ||
if (_this3.subscriptions[subscriptionId]) { | ||
_this3.offSubscription(subscriptionId); | ||
if (_this4.subscriptions[subscriptionId]) { | ||
_this4.offSubscription(subscriptionId); | ||
} | ||
@@ -136,16 +156,14 @@ } | ||
}, { | ||
key: 'onSubscription', | ||
value: function onSubscription(subscriptionId) { | ||
key: 'startSubscription', | ||
value: function startSubscription(subscriptionId) { | ||
var subscription = this.subscriptions[subscriptionId]; | ||
this.rtProvider.emit(_constants.RTSocketEvents.SUB_ON, subscription.data); | ||
this.emitMessage(_constants.RTSocketEvents.SUB_ON, subscription.data); | ||
} | ||
}, { | ||
key: 'offSubscription', | ||
value: function offSubscription(subscriptionId) { | ||
key: 'stopSubscription', | ||
value: function stopSubscription(subscriptionId) { | ||
var subscription = this.subscriptions[subscriptionId]; | ||
if (subscription) { | ||
this.rtProvider.emit(_constants.RTSocketEvents.SUB_OFF, { id: subscriptionId }); | ||
if (subscription.onStop) { | ||
@@ -156,5 +174,18 @@ subscription.onStop(); | ||
delete this.subscriptions[subscriptionId]; | ||
this.terminateSocketIfNeeded(); | ||
} | ||
} | ||
}, { | ||
key: 'offSubscription', | ||
value: function offSubscription(subscriptionId) { | ||
var subscription = this.subscriptions[subscriptionId]; | ||
if (subscription) { | ||
this.emitMessage(_constants.RTSocketEvents.SUB_OFF, { id: subscriptionId }); | ||
this.stopSubscription(subscriptionId); | ||
} | ||
} | ||
}, { | ||
key: 'onSubscriptionResponse', | ||
@@ -168,6 +199,6 @@ | ||
/******************************************************************************* **/ | ||
value: function onSubscriptionResponse(_ref2) { | ||
var id = _ref2.id, | ||
data = _ref2.data, | ||
error = _ref2.error; | ||
value: function onSubscriptionResponse(_ref3) { | ||
var id = _ref3.id, | ||
data = _ref3.data, | ||
error = _ref3.error; | ||
@@ -183,7 +214,3 @@ var subscription = this.subscriptions[id]; | ||
if (subscription.onStop) { | ||
subscription.onStop(error); | ||
} | ||
delete this.subscriptions[id]; | ||
this.stopSubscription(id); | ||
} else { | ||
@@ -190,0 +217,0 @@ if (!subscription.ready) { |
@@ -8,3 +8,3 @@ 'use strict'; | ||
var RTUtils = { | ||
var Utils = { | ||
generateUID: function generateUID() { | ||
@@ -19,5 +19,30 @@ //TODO: find a better solution for generate UID | ||
return hash + Date.now(); | ||
}, | ||
deferred: function deferred(timeout) { | ||
return function (target, key, descriptor) { | ||
var lastInvocation = null; | ||
var decorated = descriptor.value; | ||
descriptor.value = function () { | ||
var _this = this, | ||
_arguments = arguments; | ||
if (lastInvocation) { | ||
clearTimeout(lastInvocation); | ||
} | ||
lastInvocation = setTimeout(function () { | ||
decorated.apply(_this, _arguments); | ||
}, timeout || 500); | ||
}; | ||
return descriptor; | ||
}; | ||
} | ||
}; | ||
exports.default = RTUtils; | ||
exports.default = Utils; |
@@ -7,4 +7,8 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var isUndefined = function isUndefined(value) { | ||
@@ -23,53 +27,61 @@ return typeof value === 'undefined'; | ||
var RTConfig = { | ||
appId: null, | ||
lookupPath: null, | ||
debugMode: false, | ||
connectQuery: {}, | ||
var RTConfig = function () { | ||
function RTConfig(config) { | ||
_classCallCheck(this, RTConfig); | ||
set: function set(config) { | ||
if (!config) { | ||
return; | ||
} | ||
this.appId = null; | ||
this.lookupPath = null; | ||
this.debugMode = false; | ||
this.connectQuery = {}; | ||
if (!isUndefined(config.appId)) { | ||
if (!isString(config.appId)) { | ||
throw new Error('"appId" must be String.'); | ||
this.set(config); | ||
} | ||
_createClass(RTConfig, [{ | ||
key: 'set', | ||
value: function set(config) { | ||
if (!config) { | ||
return; | ||
} | ||
this.appId = config.appId; | ||
} | ||
if (!isUndefined(config.appId)) { | ||
if (!isString(config.appId)) { | ||
throw new Error('"appId" must be String.'); | ||
} | ||
if (!isUndefined(config.lookupPath)) { | ||
if (!isString(config.lookupPath)) { | ||
throw new Error('"lookupPath" must be String.'); | ||
this.appId = config.appId; | ||
} | ||
this.lookupPath = config.lookupPath; | ||
} | ||
if (!isUndefined(config.lookupPath)) { | ||
if (!isString(config.lookupPath)) { | ||
throw new Error('"lookupPath" must be String.'); | ||
} | ||
if (!isUndefined(config.debugMode)) { | ||
this.debugMode = !!config.debugMode; | ||
} | ||
this.lookupPath = config.lookupPath; | ||
} | ||
if (!isUndefined(config.connectQuery)) { | ||
if (isFunction(config.connectQuery)) { | ||
this.getConnectQuery = config.connectQuery; | ||
} else if (isObject(config.connectQuery)) { | ||
this.connectQuery = config.connectQuery; | ||
} else { | ||
throw new Error('"connectQuery" must be Function or Object.'); | ||
if (!isUndefined(config.debugMode)) { | ||
this.debugMode = !!config.debugMode; | ||
} | ||
if (!isUndefined(config.connectQuery)) { | ||
if (isFunction(config.connectQuery)) { | ||
this.getConnectQuery = config.connectQuery; | ||
} else if (isObject(config.connectQuery)) { | ||
this.connectQuery = config.connectQuery; | ||
} else { | ||
throw new Error('"connectQuery" must be Function or Object.'); | ||
} | ||
} | ||
} | ||
}, | ||
}, { | ||
key: 'getConnectQuery', | ||
value: function getConnectQuery() { | ||
return this.connectQuery; | ||
} | ||
}]); | ||
return RTConfig; | ||
}(); | ||
/** | ||
* @abstract | ||
**/ | ||
getConnectQuery: function getConnectQuery() { | ||
return this.connectQuery; | ||
} | ||
}; | ||
exports.default = RTConfig; |
@@ -6,13 +6,10 @@ 'use strict'; | ||
}); | ||
exports.RTScopeConnector = exports.RTListeners = undefined; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _config = require('./config'); | ||
var _client = require('./client'); | ||
var _config2 = _interopRequireDefault(_config); | ||
var _client2 = _interopRequireDefault(_client); | ||
var _provider = require('./provider'); | ||
var _provider2 = _interopRequireDefault(_provider); | ||
var _listeners = require('./listeners'); | ||
@@ -30,15 +27,13 @@ | ||
var BackendlessRTClient = { | ||
Config: _config2.default, | ||
Provider: _provider2.default, | ||
Listeners: _listeners2.default, | ||
ScopeConnector: _scopeConnector2.default | ||
}; | ||
_client2.default.Listeners = _listeners2.default; | ||
_client2.default.ScopeConnector = _scopeConnector2.default; | ||
if (root) { | ||
root.BackendlessRTClient = BackendlessRTClient; | ||
root.BackendlessRTClient = _client2.default; | ||
} | ||
module.exports = BackendlessRTClient; | ||
module.exports = _client2.default; | ||
exports.default = BackendlessRTClient; | ||
exports.default = _client2.default; | ||
var RTListeners = exports.RTListeners = _listeners2.default; | ||
var RTScopeConnector = exports.RTScopeConnector = _scopeConnector2.default; |
@@ -27,11 +27,15 @@ 'use strict'; | ||
var RTMethods = function () { | ||
function RTMethods(rtProvider) { | ||
function RTMethods(_ref) { | ||
var _this = this; | ||
var onMessage = _ref.onMessage, | ||
emitMessage = _ref.emitMessage, | ||
terminateSocketIfNeeded = _ref.terminateSocketIfNeeded; | ||
_classCallCheck(this, RTMethods); | ||
this.onResponse = function (_ref) { | ||
var id = _ref.id, | ||
error = _ref.error, | ||
result = _ref.result; | ||
this.onResponse = function (_ref2) { | ||
var id = _ref2.id, | ||
error = _ref2.error, | ||
result = _ref2.result; | ||
@@ -48,2 +52,4 @@ if (_this.invocations[id]) { | ||
delete _this.invocations[id]; | ||
_this.terminateSocketIfNeeded(); | ||
} | ||
@@ -60,3 +66,5 @@ }; | ||
this.rtProvider = rtProvider; | ||
this.onMessage = onMessage; | ||
this.emitMessage = emitMessage; | ||
this.terminateSocketIfNeeded = terminateSocketIfNeeded; | ||
@@ -70,3 +78,3 @@ this.invocations = {}; | ||
if (!this.initialized) { | ||
this.rtProvider.on(_constants.RTSocketEvents.MET_RES, this.onResponse); | ||
this.onMessage(_constants.RTSocketEvents.MET_RES, this.onResponse); | ||
@@ -77,2 +85,7 @@ this.initialized = true; | ||
}, { | ||
key: 'terminate', | ||
value: function terminate() { | ||
this.invocations = {}; | ||
} | ||
}, { | ||
key: 'reconnect', | ||
@@ -86,2 +99,7 @@ value: function reconnect() { | ||
}, { | ||
key: 'hasActivity', | ||
value: function hasActivity() { | ||
return !!Object.keys(this.invocations).length; | ||
} | ||
}, { | ||
key: 'send', | ||
@@ -100,3 +118,3 @@ | ||
this.rtProvider.emit(_constants.RTSocketEvents.MET_REQ, methodData); | ||
this.emitMessage(_constants.RTSocketEvents.MET_REQ, methodData); | ||
@@ -103,0 +121,0 @@ return new Promise(function (resolve, reject) { |
@@ -18,6 +18,2 @@ 'use strict'; | ||
var _config = require('./config'); | ||
var _config2 = _interopRequireDefault(_config); | ||
var _constants = require('./constants'); | ||
@@ -32,10 +28,10 @@ | ||
key: 'connect', | ||
value: function connect(onDisconnect) { | ||
if (!_config2.default.lookupPath) { | ||
throw new Error('RTConfig.lookupPath is not configured'); | ||
value: function connect(config, onDisconnect) { | ||
if (!config.lookupPath) { | ||
throw new Error('config.lookupPath is not configured'); | ||
} | ||
return _backendlessRequest2.default.get(_config2.default.lookupPath).then(function (rtServerHost) { | ||
return _backendlessRequest2.default.get(config.lookupPath).then(function (rtServerHost) { | ||
return new Promise(function (resolve, reject) { | ||
var rtSocket = new RTSocket(rtServerHost); | ||
var rtSocket = new RTSocket(config, rtServerHost); | ||
@@ -67,17 +63,19 @@ rtSocket.on(_constants.NativeSocketEvents.CONNECT, onConnect); | ||
function RTSocket(host) { | ||
function RTSocket(config, host) { | ||
_classCallCheck(this, RTSocket); | ||
this.config = config; | ||
this.events = {}; | ||
if (!_config2.default.appId) { | ||
throw new Error('RTConfig.appId is not configured'); | ||
if (!config.appId) { | ||
throw new Error('config.appId is not configured'); | ||
} | ||
this.ioSocket = (0, _socket2.default)(host + '/' + _config2.default.appId, { | ||
this.ioSocket = (0, _socket2.default)(host + '/' + this.config.appId, { | ||
forceNew: true, | ||
autoConnect: false, | ||
reconnection: false, | ||
path: '/' + _config2.default.appId, | ||
query: _config2.default.getConnectQuery() | ||
path: '/' + this.config.appId, | ||
query: this.config.getConnectQuery() | ||
}); | ||
@@ -130,3 +128,5 @@ } | ||
value: function onEvent(event, data) { | ||
logMessage('FROM SERVER', event, data); | ||
if (this.config.debugMode) { | ||
logMessage('FROM SERVER', event, data); | ||
} | ||
@@ -142,3 +142,5 @@ if (this.events[event]) { | ||
value: function emit(event, data) { | ||
logMessage('TO SERVER', event, data); | ||
if (this.config.debugMode) { | ||
logMessage('TO SERVER', event, data); | ||
} | ||
@@ -156,5 +158,3 @@ this.ioSocket.emit(event, data); | ||
function logMessage(type, event, data) { | ||
if (_config2.default.debugMode) { | ||
console.log('[' + type + '] - [event: ' + event + '] - arguments: ' + JSON.stringify(data) + ' '); | ||
} | ||
console.log('[' + type + '] - [event: ' + event + '] - arguments: ' + JSON.stringify(data) + ' '); | ||
} |
@@ -27,3 +27,7 @@ 'use strict'; | ||
var RTSubscriptions = function () { | ||
function RTSubscriptions(rtProvider) { | ||
function RTSubscriptions(_ref) { | ||
var onMessage = _ref.onMessage, | ||
emitMessage = _ref.emitMessage, | ||
terminateSocketIfNeeded = _ref.terminateSocketIfNeeded; | ||
_classCallCheck(this, RTSubscriptions); | ||
@@ -44,3 +48,5 @@ | ||
this.rtProvider = rtProvider; | ||
this.onMessage = onMessage; | ||
this.emitMessage = emitMessage; | ||
this.terminateSocketIfNeeded = terminateSocketIfNeeded; | ||
@@ -56,3 +62,3 @@ this.subscriptions = {}; | ||
if (!this.initialized) { | ||
this.rtProvider.on(_constants.RTSocketEvents.SUB_RES, function (data) { | ||
this.onMessage(_constants.RTSocketEvents.SUB_RES, function (data) { | ||
return _this.onSubscriptionResponse(data); | ||
@@ -65,2 +71,11 @@ }); | ||
}, { | ||
key: 'terminate', | ||
value: function terminate() { | ||
var _this2 = this; | ||
Object.keys(this.subscriptions).forEach(function (subscriptionId) { | ||
return _this2.stopSubscription(subscriptionId); | ||
}); | ||
} | ||
}, { | ||
key: 'reconnect', | ||
@@ -75,15 +90,20 @@ value: function reconnect() { | ||
}, { | ||
key: 'hasActivity', | ||
value: function hasActivity() { | ||
return !!Object.keys(this.subscriptions).length; | ||
} | ||
}, { | ||
key: 'reconnectSubscriptions', | ||
value: function reconnectSubscriptions() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
Object.keys(this.subscriptions).forEach(function (subscriptionId) { | ||
var subscription = _this2.subscriptions[subscriptionId]; | ||
var subscription = _this3.subscriptions[subscriptionId]; | ||
if (subscription.keepAlive === false) { | ||
delete _this2.subscriptions[subscriptionId]; | ||
delete _this3.subscriptions[subscriptionId]; | ||
} else { | ||
subscription.ready = false; | ||
_this2.onSubscription(subscriptionId); | ||
_this3.startSubscription(subscriptionId); | ||
} | ||
@@ -94,11 +114,11 @@ }); | ||
key: 'subscribe', | ||
value: function subscribe(name, options, _ref) { | ||
var _this3 = this; | ||
value: function subscribe(name, options, _ref2) { | ||
var _this4 = this; | ||
var keepAlive = _ref.keepAlive, | ||
parser = _ref.parser, | ||
onData = _ref.onData, | ||
onError = _ref.onError, | ||
onStop = _ref.onStop, | ||
onReady = _ref.onReady; | ||
var keepAlive = _ref2.keepAlive, | ||
parser = _ref2.parser, | ||
onData = _ref2.onData, | ||
onError = _ref2.onError, | ||
onStop = _ref2.onStop, | ||
onReady = _ref2.onReady; | ||
@@ -120,12 +140,12 @@ this.initialize(); | ||
this.onSubscription(subscriptionId); | ||
this.startSubscription(subscriptionId); | ||
return { | ||
isReady: function isReady() { | ||
return !!_this3.subscriptions[subscriptionId] && _this3.subscriptions[subscriptionId].ready; | ||
return !!_this4.subscriptions[subscriptionId] && _this4.subscriptions[subscriptionId].ready; | ||
}, | ||
stop: function stop() { | ||
if (_this3.subscriptions[subscriptionId]) { | ||
_this3.offSubscription(subscriptionId); | ||
if (_this4.subscriptions[subscriptionId]) { | ||
_this4.offSubscription(subscriptionId); | ||
} | ||
@@ -136,16 +156,14 @@ } | ||
}, { | ||
key: 'onSubscription', | ||
value: function onSubscription(subscriptionId) { | ||
key: 'startSubscription', | ||
value: function startSubscription(subscriptionId) { | ||
var subscription = this.subscriptions[subscriptionId]; | ||
this.rtProvider.emit(_constants.RTSocketEvents.SUB_ON, subscription.data); | ||
this.emitMessage(_constants.RTSocketEvents.SUB_ON, subscription.data); | ||
} | ||
}, { | ||
key: 'offSubscription', | ||
value: function offSubscription(subscriptionId) { | ||
key: 'stopSubscription', | ||
value: function stopSubscription(subscriptionId) { | ||
var subscription = this.subscriptions[subscriptionId]; | ||
if (subscription) { | ||
this.rtProvider.emit(_constants.RTSocketEvents.SUB_OFF, { id: subscriptionId }); | ||
if (subscription.onStop) { | ||
@@ -156,5 +174,18 @@ subscription.onStop(); | ||
delete this.subscriptions[subscriptionId]; | ||
this.terminateSocketIfNeeded(); | ||
} | ||
} | ||
}, { | ||
key: 'offSubscription', | ||
value: function offSubscription(subscriptionId) { | ||
var subscription = this.subscriptions[subscriptionId]; | ||
if (subscription) { | ||
this.emitMessage(_constants.RTSocketEvents.SUB_OFF, { id: subscriptionId }); | ||
this.stopSubscription(subscriptionId); | ||
} | ||
} | ||
}, { | ||
key: 'onSubscriptionResponse', | ||
@@ -168,6 +199,6 @@ | ||
/******************************************************************************* **/ | ||
value: function onSubscriptionResponse(_ref2) { | ||
var id = _ref2.id, | ||
data = _ref2.data, | ||
error = _ref2.error; | ||
value: function onSubscriptionResponse(_ref3) { | ||
var id = _ref3.id, | ||
data = _ref3.data, | ||
error = _ref3.error; | ||
@@ -183,7 +214,3 @@ var subscription = this.subscriptions[id]; | ||
if (subscription.onStop) { | ||
subscription.onStop(error); | ||
} | ||
delete this.subscriptions[id]; | ||
this.stopSubscription(id); | ||
} else { | ||
@@ -190,0 +217,0 @@ if (!subscription.ready) { |
@@ -8,3 +8,3 @@ 'use strict'; | ||
var RTUtils = { | ||
var Utils = { | ||
generateUID: function generateUID() { | ||
@@ -19,5 +19,30 @@ //TODO: find a better solution for generate UID | ||
return hash + Date.now(); | ||
}, | ||
deferred: function deferred(timeout) { | ||
return function (target, key, descriptor) { | ||
var lastInvocation = null; | ||
var decorated = descriptor.value; | ||
descriptor.value = function () { | ||
var _this = this, | ||
_arguments = arguments; | ||
if (lastInvocation) { | ||
clearTimeout(lastInvocation); | ||
} | ||
lastInvocation = setTimeout(function () { | ||
decorated.apply(_this, _arguments); | ||
}, timeout || 500); | ||
}; | ||
return descriptor; | ||
}; | ||
} | ||
}; | ||
exports.default = RTUtils; | ||
exports.default = Utils; |
{ | ||
"name": "backendless-rt-client", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Backendless RT Client for connect to Backendless RT Server", | ||
@@ -5,0 +5,0 @@ "browser": "dist/backendless-rt-client.js", |
@@ -6,8 +6,13 @@ const isUndefined = value => typeof value === 'undefined' | ||
const RTConfig = { | ||
appId : null, | ||
lookupPath : null, | ||
debugMode : false, | ||
connectQuery: {}, | ||
export default class RTConfig { | ||
constructor(config) { | ||
this.appId = null | ||
this.lookupPath = null | ||
this.debugMode = false | ||
this.connectQuery = {} | ||
this.set(config) | ||
} | ||
set(config) { | ||
@@ -49,13 +54,9 @@ if (!config) { | ||
} | ||
}, | ||
} | ||
/** | ||
* @abstract | ||
**/ | ||
getConnectQuery() { | ||
return this.connectQuery | ||
}, | ||
} | ||
} | ||
export default RTConfig |
@@ -1,3 +0,2 @@ | ||
import Config from './config' | ||
import Provider from './provider' | ||
import RTClient from './client' | ||
import Listeners from './listeners' | ||
@@ -9,15 +8,14 @@ import ScopeConnector from './scope-connector' | ||
const BackendlessRTClient = { | ||
Config, | ||
Provider, | ||
Listeners, | ||
ScopeConnector, | ||
} | ||
RTClient.Listeners = Listeners | ||
RTClient.ScopeConnector = ScopeConnector | ||
if (root) { | ||
root.BackendlessRTClient = BackendlessRTClient | ||
root.BackendlessRTClient = RTClient | ||
} | ||
module.exports = BackendlessRTClient | ||
module.exports = RTClient | ||
export default BackendlessRTClient | ||
export default RTClient | ||
export const RTListeners = Listeners | ||
export const RTScopeConnector = ScopeConnector | ||
@@ -10,4 +10,6 @@ import { RTSocketEvents, RTMethodTypes } from './constants' | ||
constructor(rtProvider) { | ||
this.rtProvider = rtProvider | ||
constructor({ onMessage, emitMessage, terminateSocketIfNeeded }) { | ||
this.onMessage = onMessage | ||
this.emitMessage = emitMessage | ||
this.terminateSocketIfNeeded = terminateSocketIfNeeded | ||
@@ -19,3 +21,3 @@ this.invocations = {} | ||
if (!this.initialized) { | ||
this.rtProvider.on(RTSocketEvents.MET_RES, this.onResponse) | ||
this.onMessage(RTSocketEvents.MET_RES, this.onResponse) | ||
@@ -26,2 +28,6 @@ this.initialized = true | ||
terminate() { | ||
this.invocations = {} | ||
} | ||
reconnect() { | ||
@@ -34,2 +40,6 @@ if (this.initialized) { | ||
hasActivity() { | ||
return !!Object.keys(this.invocations).length | ||
} | ||
send(name, options) { | ||
@@ -41,3 +51,3 @@ this.initialize() | ||
this.rtProvider.emit(RTSocketEvents.MET_REQ, methodData) | ||
this.emitMessage(RTSocketEvents.MET_REQ, methodData) | ||
@@ -60,2 +70,4 @@ return new Promise((resolve, reject) => { | ||
delete this.invocations[id] | ||
this.terminateSocketIfNeeded() | ||
} | ||
@@ -62,0 +74,0 @@ } |
import io from 'socket.io-client' | ||
import Request from 'backendless-request' | ||
import Config from './config' | ||
import { NativeSocketEvents } from './constants' | ||
@@ -9,11 +8,11 @@ | ||
static connect(onDisconnect) { | ||
if (!Config.lookupPath) { | ||
throw new Error('RTConfig.lookupPath is not configured') | ||
static connect(config, onDisconnect) { | ||
if (!config.lookupPath) { | ||
throw new Error('config.lookupPath is not configured') | ||
} | ||
return Request.get(Config.lookupPath) | ||
return Request.get(config.lookupPath) | ||
.then(rtServerHost => { | ||
return new Promise((resolve, reject) => { | ||
const rtSocket = new RTSocket(rtServerHost) | ||
const rtSocket = new RTSocket(config, rtServerHost) | ||
@@ -45,15 +44,17 @@ rtSocket.on(NativeSocketEvents.CONNECT, onConnect) | ||
constructor(host) { | ||
constructor(config, host) { | ||
this.config = config | ||
this.events = {} | ||
if (!Config.appId) { | ||
throw new Error('RTConfig.appId is not configured') | ||
if (!config.appId) { | ||
throw new Error('config.appId is not configured') | ||
} | ||
this.ioSocket = io(`${host}/${Config.appId}`, { | ||
this.ioSocket = io(`${host}/${this.config.appId}`, { | ||
forceNew : true, | ||
autoConnect : false, | ||
reconnection: false, | ||
path : `/${Config.appId}`, | ||
query : Config.getConnectQuery() | ||
path : `/${this.config.appId}`, | ||
query : this.config.getConnectQuery() | ||
}) | ||
@@ -96,3 +97,5 @@ } | ||
onEvent(event, data) { | ||
logMessage('FROM SERVER', event, data) | ||
if (this.config.debugMode) { | ||
logMessage('FROM SERVER', event, data) | ||
} | ||
@@ -105,3 +108,5 @@ if (this.events[event]) { | ||
emit(event, data) { | ||
logMessage('TO SERVER', event, data) | ||
if (this.config.debugMode) { | ||
logMessage('TO SERVER', event, data) | ||
} | ||
@@ -114,5 +119,3 @@ this.ioSocket.emit(event, data) | ||
function logMessage(type, event, data) { | ||
if (Config.debugMode) { | ||
console.log(`[${type}] - [event: ${event}] - arguments: ${JSON.stringify(data)} `) | ||
} | ||
console.log(`[${type}] - [event: ${event}] - arguments: ${JSON.stringify(data)} `) | ||
} |
@@ -10,4 +10,6 @@ import { RTSocketEvents, RTSubscriptionTypes } from './constants' | ||
constructor(rtProvider) { | ||
this.rtProvider = rtProvider | ||
constructor({ onMessage, emitMessage, terminateSocketIfNeeded }) { | ||
this.onMessage = onMessage | ||
this.emitMessage = emitMessage | ||
this.terminateSocketIfNeeded = terminateSocketIfNeeded | ||
@@ -19,3 +21,3 @@ this.subscriptions = {} | ||
if (!this.initialized) { | ||
this.rtProvider.on(RTSocketEvents.SUB_RES, data => this.onSubscriptionResponse(data)) | ||
this.onMessage(RTSocketEvents.SUB_RES, data => this.onSubscriptionResponse(data)) | ||
@@ -26,2 +28,8 @@ this.initialized = true | ||
terminate() { | ||
Object | ||
.keys(this.subscriptions) | ||
.forEach(subscriptionId => this.stopSubscription(subscriptionId)) | ||
} | ||
reconnect() { | ||
@@ -35,2 +43,6 @@ if (this.initialized) { | ||
hasActivity() { | ||
return !!Object.keys(this.subscriptions).length | ||
} | ||
reconnectSubscriptions() { | ||
@@ -47,3 +59,3 @@ Object | ||
this.onSubscription(subscriptionId) | ||
this.startSubscription(subscriptionId) | ||
} | ||
@@ -69,3 +81,3 @@ }) | ||
this.onSubscription(subscriptionId) | ||
this.startSubscription(subscriptionId) | ||
@@ -85,14 +97,12 @@ return { | ||
onSubscription(subscriptionId) { | ||
startSubscription(subscriptionId) { | ||
const subscription = this.subscriptions[subscriptionId] | ||
this.rtProvider.emit(RTSocketEvents.SUB_ON, subscription.data) | ||
this.emitMessage(RTSocketEvents.SUB_ON, subscription.data) | ||
} | ||
offSubscription(subscriptionId) { | ||
stopSubscription(subscriptionId) { | ||
const subscription = this.subscriptions[subscriptionId] | ||
if (subscription) { | ||
this.rtProvider.emit(RTSocketEvents.SUB_OFF, { id: subscriptionId }) | ||
if (subscription.onStop) { | ||
@@ -103,5 +113,17 @@ subscription.onStop() | ||
delete this.subscriptions[subscriptionId] | ||
this.terminateSocketIfNeeded() | ||
} | ||
} | ||
offSubscription(subscriptionId) { | ||
const subscription = this.subscriptions[subscriptionId] | ||
if (subscription) { | ||
this.emitMessage(RTSocketEvents.SUB_OFF, { id: subscriptionId }) | ||
this.stopSubscription(subscriptionId) | ||
} | ||
} | ||
onSubscriptionResponse({ id, data, error }) { | ||
@@ -117,8 +139,4 @@ const subscription = this.subscriptions[id] | ||
if (subscription.onStop) { | ||
subscription.onStop(error) | ||
} | ||
this.stopSubscription(id) | ||
delete this.subscriptions[id] | ||
} else { | ||
@@ -125,0 +143,0 @@ if (!subscription.ready) { |
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' | ||
const RTUtils = { | ||
const Utils = { | ||
@@ -14,5 +14,24 @@ generateUID() { | ||
return hash + Date.now() | ||
}, | ||
deferred: timeout => (target, key, descriptor) => { | ||
let lastInvocation = null | ||
const decorated = descriptor.value | ||
descriptor.value = function () { | ||
if (lastInvocation) { | ||
clearTimeout(lastInvocation) | ||
} | ||
lastInvocation = setTimeout(() => { | ||
decorated.apply(this, arguments) | ||
}, timeout || 500) | ||
} | ||
return descriptor | ||
} | ||
} | ||
export default RTUtils | ||
export default Utils |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
950328
6507