UNPKG

@pubby.club/sdk

Version:
91 lines (90 loc) 3.4 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChatModule = void 0; var module_1 = require("../../module"); var incoming_1 = require("../../websocket/incoming"); var outgoing_1 = require("../../websocket/outgoing"); var room_1 = require("../room/room"); var message_1 = require("./message"); var ChatModule = /** @class */ (function (_super) { __extends(ChatModule, _super); function ChatModule(pubby) { var _this = _super.call(this, pubby) || this; // Importa módulo de sala pubby.use(room_1.RoomModule); // Exporta o módulo de chat pubby.chat = _this; return _this; } ChatModule.prototype.init = function () { var _this = this; this.onMessage$ = this.pubby.ws.listen(incoming_1.ChatMessageResponse); // Message Event this.onMessage$.createEvent("message", this, function (data) { return _this.createMessage(data.message); }); }; ChatModule.prototype.createMessage = function (message) { return new message_1.PubbyMessage(message, this); }; ChatModule.prototype.send = function (messageOrText) { var _this = this; return new Promise(function (resolve) { var text; var replyId; if (messageOrText instanceof message_1.PubbyMessage) { text = messageOrText.text; replyId = messageOrText.replyId; } else { text = messageOrText; } _this.pubby.ws .addAsync(new outgoing_1.ChatMessageRequest(text, replyId)) .then(function (msg) { return _this.createMessage(msg); }) .then(resolve); }); }; ChatModule.prototype.on = function (event) { var listeners = []; for (var _i = 1; _i < arguments.length; _i++) { listeners[_i - 1] = arguments[_i]; } return _super.prototype.on.apply(this, __spread([event], listeners)); }; return ChatModule; }(module_1.PubbyModule)); exports.ChatModule = ChatModule;