UNPKG

backendless-rt-client

Version:

Backendless RT Client for connect to Backendless RT Server

116 lines (115 loc) 4.7 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); 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; } var RTListeners = exports["default"] = /*#__PURE__*/function () { function RTListeners() { (0, _classCallCheck2["default"])(this, RTListeners); this.subscriptions = {}; this.simpleListeners = {}; } return (0, _createClass2["default"])(RTListeners, [{ key: "addSubscription", value: function addSubscription(type, subscriberFn, _ref) { var _this = this; var callback = _ref.callback, onError = _ref.onError, onReady = _ref.onReady, parser = _ref.parser, params = _ref.params; this.subscriptions[type] = this.subscriptions[type] || []; var subscription = subscriberFn(_objectSpread(_objectSpread({}, params), this.getSubscriptionOptions()), { parser: parser, onData: callback, onError: onError, onReady: onReady, onStop: function onStop() { _this.subscriptions[type] = _this.subscriptions[type].filter(function (s) { return s.subscription !== subscription; }); } }); var subscriptionStore = { callback: callback, params: params, subscription: subscription, stop: function stop() { return subscription.stop(); } }; this.subscriptions[type].push(subscriptionStore); return subscriptionStore; } }, { key: "getSubscriptionOptions", value: function getSubscriptionOptions() { return {}; } }, { key: "stopSubscription", value: function stopSubscription(type, _ref2) { var callback = _ref2.callback, matcher = _ref2.matcher; var subscriptionsStack = this.subscriptions[type] = this.subscriptions[type] || []; if (matcher) { subscriptionsStack.forEach(function (subscriptionStore) { if (matcher(subscriptionStore)) { subscriptionStore.subscription.stop(); } }); } else { subscriptionsStack.forEach(function (subscriptionStore) { if (!callback || subscriptionStore.callback === callback) { subscriptionStore.subscription.stop(); } }); } } }, { key: "addSimpleListener", value: function addSimpleListener(type, callback) { var listenersStack = this.simpleListeners[type] = this.simpleListeners[type] || []; listenersStack.push(callback); } }, { key: "removeSimpleListener", value: function removeSimpleListener(type, callback) { if (this.simpleListeners[type]) { this.simpleListeners[type] = callback ? this.simpleListeners[type].filter(function (cb) { return cb !== callback; }) : []; } } }, { key: "runSimpleListeners", value: function runSimpleListeners(type) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } if (this.simpleListeners[type]) { this.simpleListeners[type].forEach(function (callback) { return callback.apply(void 0, args); }); } } }, { key: "removeAllListeners", value: function removeAllListeners() { var _this2 = this; Object.keys(this.subscriptions).forEach(function (listenerType) { _this2.subscriptions[listenerType].forEach(function (_ref3) { var subscription = _ref3.subscription; return subscription.stop(); }); }); this.simpleListeners = {}; } }]); }();