@pubby.club/sdk
Version:
Pubby Development Kit
148 lines (147 loc) • 5.86 kB
JavaScript
;
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 __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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.PlaybackModule = void 0;
var module_1 = require("../../module");
var incoming_1 = require("../../websocket/incoming");
var websocket_1 = require("../../websocket/websocket");
var outgoing_1 = require("../../websocket/outgoing");
var media_add_action_event_1 = require("../../websocket/outgoing/media-add-action.event");
var media_dislike_action_event_1 = require("../../websocket/outgoing/media-dislike-action.event");
var media_like_action_event_1 = require("../../websocket/outgoing/media-like-action.event");
var PlaybackModule = /** @class */ (function (_super) {
__extends(PlaybackModule, _super);
function PlaybackModule(client) {
var _this = _super.call(this, client) || this;
client.playback = _this;
// Importa websocket
client.use(websocket_1.WebsocketModule);
_this.state$ = client.ws.listen(incoming_1.PlaybackUpdateResponse);
return _this;
}
Object.defineProperty(PlaybackModule.prototype, "dj", {
get: function () {
return this.pubby.room.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 0;
return Math.max(0, 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);
}
});
};
PlaybackModule.prototype.skip = function () {
this.pubby.ws.add(new outgoing_1.PlaybackSkipRequest());
};
PlaybackModule.prototype.add = function (playlistId) {
this.pubby.ws.add(new media_add_action_event_1.MediaAddActionRequest(playlistId));
};
PlaybackModule.prototype.like = function () {
this.pubby.ws.add(new media_like_action_event_1.MediaLikeActionRequest());
};
PlaybackModule.prototype.dislike = function () {
this.pubby.ws.add(new media_dislike_action_event_1.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;
}(module_1.PubbyModule));
exports.PlaybackModule = PlaybackModule;