UNPKG

kuzzle-sdk

Version:
88 lines 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Room { /** * @param controller * @param index * @param collection * @param body * @param callback * @param options */ constructor(controller, index, collection, body, callback, options = {}) { this.id = null; this.channel = null; Reflect.defineProperty(this, "_kuzzle", { value: controller.kuzzle, }); this.controller = controller; this.index = index; this.collection = collection; this.callback = callback; this.options = options; this.id = null; this.channel = null; this.request = { action: "subscribe", body, collection, controller: "realtime", index, scope: this.options.scope, state: this.options.state, users: this.options.users, volatile: this.options.volatile, }; this.autoResubscribe = typeof options.autoResubscribe === "boolean" ? options.autoResubscribe : this.kuzzle.autoResubscribe; this.subscribeToSelf = typeof options.subscribeToSelf === "boolean" ? options.subscribeToSelf : true; this._channelListener = this._channelListener.bind(this); } get kuzzle() { return this._kuzzle; } subscribe() { return this.kuzzle .query(this.request, this.options) .then((response) => { this.id = response.result.roomId; this.channel = response.result.channel; this.kuzzle.protocol.on(this.channel, this._channelListener); return response; }); } removeListeners() { if (this.channel) { this.kuzzle.protocol.removeListener(this.channel, this._channelListener); } } _channelListener(data) { if (data.type === "TokenExpired") { return this.kuzzle.tokenExpired(); } const fromSelf = data.volatile && data.volatile.sdkInstanceId === this.kuzzle.protocol.id; if (this.subscribeToSelf || !fromSelf) { try { const callbackResponse = this.callback(data); if (callbackResponse !== null && typeof callbackResponse === "object" && typeof callbackResponse.then === "function" && typeof callbackResponse.catch === "function") { callbackResponse.catch((error) => { this.kuzzle.emit("callbackError", { error, notification: data }); }); } } catch (error) { this.kuzzle.emit("callbackError", { error, notification: data }); } } } } exports.default = Room; //# sourceMappingURL=Room.js.map