@pubby.club/sdk
Version:
Pubby Development Kit
140 lines (139 loc) • 5.35 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 __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 __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
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.QueueModule = 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 QueueModule = /** @class */ (function (_super) {
__extends(QueueModule, _super);
function QueueModule(client) {
var _this = _super.call(this, client) || this;
_this.state = new Set();
// Importa websocket
client.use(room_1.RoomModule);
client.queue = _this;
_this.stream$ = client.ws.listen(incoming_1.QueueUpdateResponse);
return _this;
}
Object.defineProperty(QueueModule.prototype, "users", {
get: function () {
var _this = this;
return Array.from(this.state).map(function (userId) {
return _this.pubby.room.users.get(userId);
});
},
enumerable: false,
configurable: true
});
Object.defineProperty(QueueModule.prototype, "next", {
get: function () {
var _a = __read(Array.from(this.state), 1), userId = _a[0];
return this.pubby.room.users.get(userId);
},
enumerable: false,
configurable: true
});
QueueModule.prototype.init = function () {
var _this = this;
this.stream$.subscribe(function (_a) {
var e_1, _b, e_2, _c;
var queue = _a.queue;
var old = new Set(_this.state);
_this.state = new Set(queue);
_this.emit("update", _this.users);
try {
for (var _d = __values(_this.state), _e = _d.next(); !_e.done; _e = _d.next()) {
var userId = _e.value;
if (!old.has(userId)) {
_this.emit("user-join", _this.pubby.room.users.get(userId));
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
}
finally { if (e_1) throw e_1.error; }
}
try {
for (var old_1 = __values(old), old_1_1 = old_1.next(); !old_1_1.done; old_1_1 = old_1.next()) {
var userId = old_1_1.value;
if (!_this.state.has(userId)) {
_this.emit("user-leave", _this.pubby.room.users.get(userId));
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (old_1_1 && !old_1_1.done && (_c = old_1.return)) _c.call(old_1);
}
finally { if (e_2) throw e_2.error; }
}
});
};
QueueModule.prototype.move = function (userId, position) {
var user = this.users[position - 1];
if (user !== undefined && user.id !== userId) {
this.pubby.ws.add(new outgoing_1.QueueMoveRequest(userId, user.id));
}
};
QueueModule.prototype.has = function (userId) {
return this.state.has(userId);
};
QueueModule.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 QueueModule;
}(module_1.PubbyModule));
exports.QueueModule = QueueModule;