@pubby/sdk
Version:
Pubby Development Kit
127 lines (122 loc) • 5.18 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib = require('tslib');
var module$1 = require('../../module.js');
require('../../websocket/incoming/index.js');
require('../../websocket/outgoing/index.js');
var mediaAddAction_event = require('../../websocket/outgoing/media-add-action.event.js');
var mediaDislikeAction_event = require('../../websocket/outgoing/media-dislike-action.event.js');
var mediaLikeAction_event = require('../../websocket/outgoing/media-like-action.event.js');
var websocket = require('../../websocket/websocket.js');
var playbackUpdate_event = require('../../websocket/incoming/playback-update.event.js');
var mediaActionsUpdate_event = require('../../websocket/incoming/media-actions-update.event.js');
var playbackSkip_event = require('../../websocket/outgoing/playback-skip.event.js');
var PlaybackModule = /** @class */ (function (_super) {
tslib.__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(websocket.WebsocketModule);
_this.state$ = client.ws.listen(playbackUpdate_event.PlaybackUpdateResponse);
_this.actions$ = client.ws.listen(mediaActionsUpdate_event.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 = tslib.__assign({}, _this.state);
_this.state = state;
// update event
_this.emit("update", tslib.__assign(tslib.__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 playbackSkip_event.PlaybackSkipRequest());
};
PlaybackModule.prototype.add = function (playlistId) {
this.pubby.ws.add(new mediaAddAction_event.MediaAddActionRequest(playlistId));
};
PlaybackModule.prototype.like = function () {
this.pubby.ws.add(new mediaLikeAction_event.MediaLikeActionRequest());
};
PlaybackModule.prototype.dislike = function () {
this.pubby.ws.add(new mediaDislikeAction_event.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, tslib.__spread([event], listeners));
};
return PlaybackModule;
}(module$1.PubbyModule));
exports.PlaybackModule = PlaybackModule;