UNPKG

@pubby/sdk

Version:
123 lines (120 loc) 4.95 kB
import { __extends, __assign, __spread } from 'tslib'; import { PubbyModule } from '../../module.js'; import '../../websocket/incoming/index.js'; import '../../websocket/outgoing/index.js'; import { MediaAddActionRequest } from '../../websocket/outgoing/media-add-action.event.js'; import { MediaDislikeActionRequest } from '../../websocket/outgoing/media-dislike-action.event.js'; import { MediaLikeActionRequest } from '../../websocket/outgoing/media-like-action.event.js'; import { WebsocketModule } from '../../websocket/websocket.js'; import { PlaybackUpdateResponse } from '../../websocket/incoming/playback-update.event.js'; import { MediaActionsUpdateResponse } from '../../websocket/incoming/media-actions-update.event.js'; import { PlaybackSkipRequest } from '../../websocket/outgoing/playback-skip.event.js'; var PlaybackModule = /** @class */ (function (_super) { __extends(PlaybackModule, _super); function PlaybackModule(client) { var _this = _super.call(this, client) || this; _this.actions = { likes: new Set(), dislikes: new Set(), adds: new Set(), }; client.playback = _this; // Importa websocket client.use(WebsocketModule); _this.state$ = client.ws.listen(PlaybackUpdateResponse); _this.actions$ = client.ws.listen(MediaActionsUpdateResponse); return _this; } Object.defineProperty(PlaybackModule.prototype, "dj", { get: function () { return this.pubby.room.state.users.get(this.state.djId); }, enumerable: false, configurable: true }); Object.defineProperty(PlaybackModule.prototype, "duration", { get: function () { var _a; return Math.max(0, ((_a = this.state) === null || _a === void 0 ? void 0 : _a.duration) || this.currentTime); }, enumerable: false, configurable: true }); Object.defineProperty(PlaybackModule.prototype, "currentTime", { get: function () { var _a; var timestamp = (_a = this.state) === null || _a === void 0 ? void 0 : _a.timestamp; if (!timestamp) { return null; } return Math.min(this.duration, (this.pubby.ws.now - timestamp) / 1000); }, enumerable: false, configurable: true }); Object.defineProperty(PlaybackModule.prototype, "progress", { get: function () { return Math.max(0, this.currentTime / this.duration); }, enumerable: false, configurable: true }); PlaybackModule.prototype.init = function () { var _this = this; this.state$.subscribe(function (_a) { var _b, _c; var state = _a.state; // load event if (!_this.state) { _this.state = state; _this.emit("load", state); return; } var old = __assign({}, _this.state); _this.state = state; // update event _this.emit("update", __assign(__assign({}, state), { dj: _this.dj })); // dj-update event if (old.djId !== state.djId) { _this.emit("dj-update", _this.dj); } // media-update event if (((_b = old.media) === null || _b === void 0 ? void 0 : _b.id) !== ((_c = state.media) === null || _c === void 0 ? void 0 : _c.id)) { _this.emit("media-update", state.media); } }); this.actions$.subscribe(function (_a) { var state = _a.state; _this.actions = { adds: new Set(state.adds), likes: new Set(state.likes), dislikes: new Set(state.dislikes), }; _this.emit("actions-update", _this.actions); }); }; PlaybackModule.prototype.skip = function () { this.pubby.ws.add(new PlaybackSkipRequest()); }; PlaybackModule.prototype.add = function (playlistId) { this.pubby.ws.add(new MediaAddActionRequest(playlistId)); }; PlaybackModule.prototype.like = function () { this.pubby.ws.add(new MediaLikeActionRequest()); }; PlaybackModule.prototype.dislike = function () { this.pubby.ws.add(new MediaDislikeActionRequest()); }; PlaybackModule.prototype.isDj = function (userId) { return this.state.djId === userId; }; PlaybackModule.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 PlaybackModule; }(PubbyModule)); export { PlaybackModule };