UNPKG

backendless

Version:

Backendless JavaScript SDK for Node.js and the browser

201 lines (200 loc) 8.21 kB
"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 _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 _applyDecoratedDescriptor2 = _interopRequireDefault(require("@babel/runtime/helpers/applyDecoratedDescriptor")); var _rt = require("../../rt"); var _dec, _class; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } var ListenerTypes = { MESSAGE: 'MESSAGE' }; var Channel = (_dec = _rt.RTScopeConnector.connectionRequired(), (_class = /*#__PURE__*/function (_RTScopeConnector) { (0, _inherits2["default"])(Channel, _RTScopeConnector); var _super = _createSuper(Channel); function Channel(options, app) { var _this; (0, _classCallCheck2["default"])(this, Channel); _this = _super.call(this, options); _this.app = app; _this.connect(); return _this; } (0, _createClass2["default"])(Channel, [{ key: "connectSubscriber", get: function get() { return this.app.RT.subscriptions.connectToPubSub; } }, { key: "usersSubscriber", get: function get() { return this.app.RT.subscriptions.onPubSubUserStatus; } }, { key: "commandSubscriber", get: function get() { return this.app.RT.subscriptions.onPubSubCommand; } }, { key: "commandSender", get: function get() { return this.app.RT.methods.sendPubSubCommand; } }, { key: "getScopeOptions", value: function getScopeOptions() { var name = this.options.name; return { channel: name }; } }, { key: "connect", value: function connect() { if (this.app) { return (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "connect", this).call(this); } } }, { key: "publish", value: function publish(message, publishOptions, deliveryTarget) { return this.app.Messaging.publish(this.options.name, message, publishOptions, deliveryTarget); } }, { key: "addMessageListener", value: function addMessageListener(selector, callback, onError) { if (typeof selector === 'function') { onError = callback; callback = selector; selector = undefined; } this.addSubscription(ListenerTypes.MESSAGE, this.app.RT.subscriptions.onPubSubMessage, { callback: callback, onError: onError, params: { selector: selector } }); } }, { key: "removeMessageListener", value: function removeMessageListener(selector, callback) { if (typeof selector === 'function') { callback = selector; selector = undefined; } if (selector && typeof selector !== 'string') { throw new Error('Selector must be a string.'); } if (typeof callback !== 'function') { throw new Error('Callback must be a function.'); } var matcher = function matcher(subscription) { if (selector) { return subscription.params.selector === selector && subscription.callback === callback; } return subscription.callback === callback; }; this.stopSubscription(ListenerTypes.MESSAGE, { matcher: matcher }); } }, { key: "removeMessageListeners", value: function removeMessageListeners(selector) { if (typeof selector !== 'string') { throw new Error('Selector must be a string.'); } var matcher = function matcher(subscription) { return subscription.params.selector === selector; }; this.stopSubscription(ListenerTypes.MESSAGE, { matcher: matcher }); } }, { key: "removeAllMessageListeners", value: function removeAllMessageListeners() { this.stopSubscription(ListenerTypes.MESSAGE, {}); } }, { key: "addCommandListener", value: function addCommandListener(callback, onError) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "addCommandListener", this).call(this, callback, onError); return this; } }, { key: "addUserStatusListener", value: function addUserStatusListener(callback, onError) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "addUserStatusListener", this).call(this, callback, onError); return this; } }, { key: "removeCommandListener", value: function removeCommandListener(callback) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "removeCommandListeners", this).call(this, callback); return this; } }, { key: "removeCommandListeners", value: function removeCommandListeners(callback) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "removeCommandListeners", this).call(this, callback); return this; } }, { key: "removeUserStatusListeners", value: function removeUserStatusListeners(callback) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "removeUserStatusListeners", this).call(this, callback); return this; } }, { key: "addConnectListener", value: function addConnectListener(callback, onError) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "addConnectListener", this).call(this, callback, onError); return this; } }, { key: "removeConnectListeners", value: function removeConnectListeners(callback, onError) { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "removeConnectListeners", this).call(this, callback, onError); return this; } }, { key: "removeAllListeners", value: function removeAllListeners() { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "removeAllListeners", this).call(this); return this; } }, { key: "send", value: function send(type, data) { return (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "send", this).call(this, type, data); } }, { key: "join", value: function join() { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "connect", this).call(this); } }, { key: "leave", value: function leave() { (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "disconnect", this).call(this); } }, { key: "isJoined", value: function isJoined() { return (0, _get2["default"])((0, _getPrototypeOf2["default"])(Channel.prototype), "isConnected", this).call(this); } }]); return Channel; }(_rt.RTScopeConnector), ((0, _applyDecoratedDescriptor2["default"])(_class.prototype, "addMessageListener", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "addMessageListener"), _class.prototype)), _class)); exports["default"] = Channel;