UNPKG

backendless

Version:

Backendless JavaScript SDK for Node.js and the browser

230 lines (229 loc) 12.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _applyDecoratedDescriptor2 = _interopRequireDefault(require("@babel/runtime/helpers/applyDecoratedDescriptor")); var _rt = require("../rt"); var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _class; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = (0, _getPrototypeOf2["default"])(t); if (r) { var s = (0, _getPrototypeOf2["default"])(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return (0, _possibleConstructorReturn2["default"])(this, e); }; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } var ListenerTypes = { CHANGES: 'CHANGES', CLEARED: 'CLEARED', INVOKE: 'INVOKE' }; var RemoteSharedObject = (_dec = _rt.RTScopeConnector.connectionRequired(), _dec2 = _rt.RTScopeConnector.connectionRequired(), _dec3 = _rt.RTScopeConnector.connectionRequired(), _dec4 = _rt.RTScopeConnector.connectionRequired(), _dec5 = _rt.RTScopeConnector.connectionRequired(true), _dec6 = _rt.RTScopeConnector.connectionRequired(true), _dec7 = _rt.RTScopeConnector.connectionRequired(true), _dec8 = _rt.RTScopeConnector.connectionRequired(true), _dec9 = _rt.RTScopeConnector.connectionRequired(true), (_class = /*#__PURE__*/function (_RTScopeConnector) { (0, _inherits2["default"])(RemoteSharedObject, _RTScopeConnector); var _super = _createSuper(RemoteSharedObject); function RemoteSharedObject(options, app) { var _this; (0, _classCallCheck2["default"])(this, RemoteSharedObject); _this = _super.call(this, options); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onInvoke", function (_ref) { var _this$invocationTarge; var method = _ref.method, args = _ref.args; checkInvocationTargetMethod(_this.invocationTarget, method); (_this$invocationTarge = _this.invocationTarget)[method].apply(_this$invocationTarge, (0, _toConsumableArray2["default"])(args)); }); _this.app = app; _this.invocationTarget = null; _this.connect(); return _this; } (0, _createClass2["default"])(RemoteSharedObject, [{ key: "connectSubscriber", get: function get() { return this.app.RT.subscriptions.connectToRSO; } }, { key: "usersSubscriber", get: function get() { return this.app.RT.subscriptions.onRSOUserStatus; } }, { key: "commandSubscriber", get: function get() { return this.app.RT.subscriptions.onRSOCommand; } }, { key: "commandSender", get: function get() { return this.app.RT.methods.sendRSOCommand; } }, { key: "setInvocationTarget", value: function setInvocationTarget(invocationTarget) { this.invocationTarget = invocationTarget; } }, { key: "connect", value: function connect() { if (this.app) { return (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "connect", this).call(this); } } }, { key: "onConnect", value: function onConnect() { (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "onConnect", this).apply(this, arguments); this.addSubscription(ListenerTypes.INVOKE, this.app.RT.subscriptions.onRSOInvoke, { callback: this.onInvoke }); } }, { key: "onDisconnect", value: function onDisconnect() { this.stopSubscription(ListenerTypes.INVOKE, { callback: this.onInvoke }); (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "onDisconnect", this).apply(this, arguments); } }, { key: "addChangesListener", value: function addChangesListener(callback, onError) { this.addSubscription(ListenerTypes.CHANGES, this.app.RT.subscriptions.onRSOChanges, { callback: callback, onError: onError }); } }, { key: "removeChangesListeners", value: function removeChangesListeners(callback) { this.stopSubscription(ListenerTypes.CHANGES, { callback: callback }); } }, { key: "addClearListener", value: function addClearListener(callback, onError) { this.addSubscription(ListenerTypes.CLEARED, this.app.RT.subscriptions.onRSOClear, { callback: callback, onError: onError }); } }, { key: "removeClearListeners", value: function removeClearListeners(callback) { this.stopSubscription(ListenerTypes.CLEARED, { callback: callback }); } }, { key: "addCommandListener", value: function addCommandListener(callback, onError) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "addCommandListener", this).call(this, callback, onError); return this; } }, { key: "removeCommandListener", value: function removeCommandListener(callback) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "removeCommandListeners", this).call(this, callback); return this; } }, { key: "removeCommandListeners", value: function removeCommandListeners(callback) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "removeCommandListeners", this).call(this, callback); return this; } }, { key: "addUserStatusListener", value: function addUserStatusListener(callback, onError) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "addUserStatusListener", this).call(this, callback, onError); return this; } }, { key: "removeUserStatusListeners", value: function removeUserStatusListeners(callback) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "removeUserStatusListeners", this).call(this, callback); return this; } }, { key: "addConnectListener", value: function addConnectListener(callback, onError) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "addConnectListener", this).call(this, callback, onError); return this; } }, { key: "removeConnectListeners", value: function removeConnectListeners(callback, onError) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "removeConnectListeners", this).call(this, callback, onError); return this; } }, { key: "removeAllListeners", value: function removeAllListeners() { (0, _get2["default"])((0, _getPrototypeOf2["default"])(RemoteSharedObject.prototype), "removeAllListeners", this).call(this); return this; } }, { key: "get", value: function get(key) { return this.app.RT.methods.getRSO(_objectSpread(_objectSpread({}, this.getScopeOptions()), {}, { key: key })); } }, { key: "set", value: function set(key, data) { return this.app.RT.methods.setRSO(_objectSpread(_objectSpread({}, this.getScopeOptions()), {}, { key: key, data: data })); } }, { key: "clear", value: function clear() { return this.app.RT.methods.clearRSO(this.getScopeOptions()); } }, { key: "invoke", value: function invoke(method) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } return this.invokeOn.apply(this, [method, undefined].concat(args)); } }, { key: "invokeOn", value: function invokeOn(method, targets) { var _this2 = this; for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { args[_key2 - 2] = arguments[_key2]; } return Promise.resolve().then(function () { return checkInvocationTargetMethod(_this2.invocationTarget, method); }).then(function () { return _this2.app.RT.methods.invokeRSOMethod(_objectSpread(_objectSpread({}, _this2.getScopeOptions()), {}, { method: method, targets: targets, args: args })); }); } }]); return RemoteSharedObject; }(_rt.RTScopeConnector), ((0, _applyDecoratedDescriptor2["default"])(_class.prototype, "addChangesListener", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "addChangesListener"), _class.prototype), (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "removeChangesListeners", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "removeChangesListeners"), _class.prototype), (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "addClearListener", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "addClearListener"), _class.prototype), (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "removeClearListeners", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "removeClearListeners"), _class.prototype), (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "get", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "get"), _class.prototype), (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "set", [_dec6], Object.getOwnPropertyDescriptor(_class.prototype, "set"), _class.prototype), (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "clear", [_dec7], Object.getOwnPropertyDescriptor(_class.prototype, "clear"), _class.prototype), (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "invoke", [_dec8], Object.getOwnPropertyDescriptor(_class.prototype, "invoke"), _class.prototype), (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "invokeOn", [_dec9], Object.getOwnPropertyDescriptor(_class.prototype, "invokeOn"), _class.prototype)), _class)); exports["default"] = RemoteSharedObject; function checkInvocationTargetMethod(invocationTarget, method) { if (!invocationTarget) { throw new Error('"invocationTarget" is not specified'); } if (typeof invocationTarget[method] !== 'function') { throw new Error("Method \"".concat(method, "\" of invocationTarget is not function")); } }