UNPKG

carina

Version:

A NodeJS and Browser compatible client for Mixer.com's constellation socket.

112 lines 4.04 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { EventEmitter } from 'events'; import { ConstellationSocket } from './socket'; import { Subscription } from './subscription'; export { Subscription } from './subscription'; export { State as SocketState } from './socket'; export * from './errors'; export var DEFAULT_MAX_EVENT_LISTENERS = 30; var Carina = /** @class */ (function (_super) { __extends(Carina, _super); function Carina(options) { if (options === void 0) { options = {}; } var _this = _super.call(this) || this; _this.subscriptions = Object.create(null); _this.setMaxListeners(options.maxEventListeners || DEFAULT_MAX_EVENT_LISTENERS); _this.socket = new ConstellationSocket(options); _this.socket.on('error', function (err) { return _this.emit('error', err); }); return _this; } Object.defineProperty(Carina, "WebSocket", { get: function () { return ConstellationSocket.WebSocket; }, /** * Set the websocket implementation. * You will likely not need to set this in a browser environment. * You will not need to set this if WebSocket is globally available. * * @example * Carina.WebSocket = require('ws'); */ set: function (ws) { ConstellationSocket.WebSocket = ws; }, enumerable: true, configurable: true }); /** * Sets the given options on the socket. */ Carina.prototype.setOptions = function (options) { this.socket.setOptions(options); }; /** * Boots the connection to constellation. */ Carina.prototype.open = function () { this.socket.connect(); return this; }; /** * Frees resources associated with the Constellation connection. */ Carina.prototype.close = function () { this.socket.close(); }; /** * @callback onSubscriptionCb * @param {Object} data - The payload for the update. */ /** * Subscribe to a live event * * @param {string} slug * @param {onSubscriptionCb} cb - Called each time we receive an event for this slug. * @returns {Promise.<>} Resolves once subscribed. Any errors will reject. */ Carina.prototype.subscribe = function (slug, cb) { var _this = this; var subscription = this.subscriptions[slug]; if (!subscription) { subscription = this.subscriptions[slug] = new Subscription(this.socket, slug, function (err) { return _this.emit('error', err); }); } subscription.add(cb); return Promise.resolve(); // backwards-compat }; /** * Unsubscribe from a live event. * * @param {string} slug * @returns {Promise.<>} Resolves once unsubscribed. Any errors will reject. */ Carina.prototype.unsubscribe = function (slug, listener) { var subscription = this.subscriptions[slug]; if (!subscription) { return Promise.resolve(); } if (listener) { subscription.remove(listener); } else { subscription.removeAll(); } if (subscription.listenerCount() === 0) { delete this.subscriptions[slug]; } return Promise.resolve(); // backwards-compat }; return Carina; }(EventEmitter)); export { Carina }; //# sourceMappingURL=carina.js.map