backendless-rt-client
Version:
Backendless RT Client for connect to Backendless RT Server
112 lines (110 loc) • 4.22 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _constants = require("./constants");
var _utils = _interopRequireDefault(require("./utils"));
var method = function method(type) {
return function (data) {
return this.send(type, data);
};
};
var RTMethods = exports["default"] = /*#__PURE__*/function () {
function RTMethods(_ref) {
var _this = this;
var onMessage = _ref.onMessage,
emitMessage = _ref.emitMessage;
(0, _classCallCheck2["default"])(this, RTMethods);
(0, _defineProperty2["default"])(this, "sendRequest", function (invocationId) {
if (_this.invocations[invocationId]) {
_this.emitMessage(_constants.RTSocketEvents.MET_REQ, _this.invocations[invocationId].data);
}
});
(0, _defineProperty2["default"])(this, "onResponse", function (_ref2) {
var id = _ref2.id,
error = _ref2.error,
result = _ref2.result;
if (_this.invocations[id]) {
var invocation = _this.invocations[id];
if (error) {
invocation.reject(error);
} else {
invocation.resolve(result);
}
delete _this.invocations[id];
}
});
//---------------------------------//
//----------- AUTH METHODS --------//
(0, _defineProperty2["default"])(this, "setUserToken", method(_constants.RTMethodTypes.SET_USER_TOKEN).bind(this));
//----------- AUTH METHODS --------//
//---------------------------------//
//---------------------------------//
//-------- PUB_SUB METHODS --------//
(0, _defineProperty2["default"])(this, "sendPubSubCommand", method(_constants.RTMethodTypes.PUB_SUB_COMMAND).bind(this));
//-------- PUB_SUB METHODS --------//
//---------------------------------//
//---------------------------------//
//----------- RSO METHODS ---------//
(0, _defineProperty2["default"])(this, "getRSO", method(_constants.RTMethodTypes.RSO_GET).bind(this));
(0, _defineProperty2["default"])(this, "setRSO", method(_constants.RTMethodTypes.RSO_SET).bind(this));
(0, _defineProperty2["default"])(this, "clearRSO", method(_constants.RTMethodTypes.RSO_CLEAR).bind(this));
(0, _defineProperty2["default"])(this, "sendRSOCommand", method(_constants.RTMethodTypes.RSO_COMMAND).bind(this));
(0, _defineProperty2["default"])(this, "invokeRSOMethod", method(_constants.RTMethodTypes.RSO_INVOKE).bind(this));
this.onMessage = onMessage;
this.emitMessage = emitMessage;
this.invocations = {};
}
return (0, _createClass2["default"])(RTMethods, [{
key: "initialize",
value: function initialize() {
this.onMessage(_constants.RTSocketEvents.MET_RES, this.onResponse);
}
}, {
key: "restore",
value: function restore() {
var _this2 = this;
Object.keys(this.invocations).forEach(function (invocationId) {
_this2.sendRequest(invocationId);
});
}
}, {
key: "stop",
value: function stop() {}
}, {
key: "reset",
value: function reset() {
this.invocations = {};
}
}, {
key: "hasActivity",
value: function hasActivity() {
return !!Object.keys(this.invocations).length;
}
}, {
key: "send",
value: function send(name, options) {
var _this3 = this;
var invocationId = _utils["default"].generateUID();
this.invocations[invocationId] = {
data: {
id: invocationId,
name: name,
options: options
}
};
this.sendRequest(invocationId);
return new Promise(function (resolve, reject) {
_this3.invocations[invocationId].resolve = resolve;
_this3.invocations[invocationId].reject = reject;
});
}
//----------- RSO METHODS ---------//
//---------------------------------//
}]);
}();