@colyseus/core 0.16.0-preview.3 → 0.16.0-preview.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/Debug.js +6 -2
- package/build/Debug.js.map +2 -2
- package/build/Debug.mjs +11 -10
- package/build/Debug.mjs.map +2 -2
- package/build/IPC.d.ts +1 -1
- package/build/IPC.js +3 -3
- package/build/IPC.js.map +2 -2
- package/build/IPC.mjs +4 -3
- package/build/IPC.mjs.map +2 -2
- package/build/Logger.mjs +4 -3
- package/build/Logger.mjs.map +1 -1
- package/build/MatchMaker.d.ts +30 -22
- package/build/MatchMaker.js +110 -87
- package/build/MatchMaker.js.map +2 -2
- package/build/MatchMaker.mjs +114 -94
- package/build/MatchMaker.mjs.map +2 -2
- package/build/Protocol.d.ts +3 -4
- package/build/Protocol.js +28 -19
- package/build/Protocol.js.map +2 -2
- package/build/Protocol.mjs +31 -21
- package/build/Protocol.mjs.map +2 -2
- package/build/Room.d.ts +36 -35
- package/build/Room.js +270 -109
- package/build/Room.js.map +2 -2
- package/build/Room.mjs +274 -116
- package/build/Room.mjs.map +2 -2
- package/build/Server.d.ts +6 -7
- package/build/Server.js +40 -12
- package/build/Server.js.map +2 -2
- package/build/Server.mjs +40 -15
- package/build/Server.mjs.map +3 -3
- package/build/Stats.js +1 -1
- package/build/Stats.js.map +2 -2
- package/build/Stats.mjs +6 -5
- package/build/Stats.mjs.map +2 -2
- package/build/Transport.d.ts +24 -11
- package/build/Transport.js +1 -1
- package/build/Transport.js.map +2 -2
- package/build/Transport.mjs +6 -5
- package/build/Transport.mjs.map +2 -2
- package/build/discovery/index.d.ts +1 -1
- package/build/discovery/index.js.map +2 -2
- package/build/discovery/index.mjs +3 -2
- package/build/discovery/index.mjs.map +2 -2
- package/build/errors/SeatReservationError.mjs +3 -2
- package/build/errors/SeatReservationError.mjs.map +1 -1
- package/build/errors/ServerError.js +1 -1
- package/build/errors/ServerError.js.map +1 -1
- package/build/errors/ServerError.mjs +5 -4
- package/build/errors/ServerError.mjs.map +2 -2
- package/build/index.d.ts +20 -19
- package/build/index.js +28 -20
- package/build/index.js.map +2 -2
- package/build/index.mjs +21 -19
- package/build/index.mjs.map +2 -2
- package/build/matchmaker/Lobby.d.ts +3 -3
- package/build/matchmaker/Lobby.js +6 -3
- package/build/matchmaker/Lobby.js.map +2 -2
- package/build/matchmaker/Lobby.mjs +4 -4
- package/build/matchmaker/Lobby.mjs.map +2 -2
- package/build/matchmaker/RegisteredHandler.d.ts +6 -7
- package/build/matchmaker/RegisteredHandler.js +7 -10
- package/build/matchmaker/RegisteredHandler.js.map +2 -2
- package/build/matchmaker/RegisteredHandler.mjs +11 -13
- package/build/matchmaker/RegisteredHandler.mjs.map +2 -2
- package/build/matchmaker/controller.d.ts +3 -4
- package/build/matchmaker/controller.js +22 -5
- package/build/matchmaker/controller.js.map +2 -2
- package/build/matchmaker/controller.mjs +19 -3
- package/build/matchmaker/controller.mjs.map +2 -2
- package/build/matchmaker/driver/RoomData.d.ts +3 -3
- package/build/matchmaker/driver/RoomData.js +3 -3
- package/build/matchmaker/driver/RoomData.js.map +2 -2
- package/build/matchmaker/driver/RoomData.mjs +2 -2
- package/build/matchmaker/driver/RoomData.mjs.map +2 -2
- package/build/matchmaker/driver/api.d.ts +104 -0
- package/build/matchmaker/driver/api.js +29 -0
- package/build/matchmaker/driver/api.js.map +7 -0
- package/build/matchmaker/driver/api.mjs +7 -0
- package/build/matchmaker/driver/api.mjs.map +7 -0
- package/build/matchmaker/driver/index.d.ts +7 -7
- package/build/matchmaker/driver/index.js +1 -1
- package/build/matchmaker/driver/index.js.map +2 -2
- package/build/matchmaker/driver/index.mjs +2 -2
- package/build/matchmaker/driver/index.mjs.map +2 -2
- package/build/matchmaker/driver/interfaces.d.ts +7 -11
- package/build/matchmaker/driver/interfaces.js.map +1 -1
- package/build/matchmaker/driver/local/LocalDriver.d.ts +13 -0
- package/build/matchmaker/driver/local/LocalDriver.js +65 -0
- package/build/matchmaker/driver/local/LocalDriver.js.map +7 -0
- package/build/matchmaker/driver/local/LocalDriver.mjs +43 -0
- package/build/matchmaker/driver/local/LocalDriver.mjs.map +7 -0
- package/build/matchmaker/driver/local/Query.d.ts +9 -0
- package/build/matchmaker/driver/local/Query.js +78 -0
- package/build/matchmaker/driver/local/Query.js.map +7 -0
- package/build/matchmaker/driver/local/Query.mjs +56 -0
- package/build/matchmaker/driver/local/Query.mjs.map +7 -0
- package/build/matchmaker/driver/local/RoomData.d.ts +19 -0
- package/build/matchmaker/driver/local/RoomData.js +79 -0
- package/build/matchmaker/driver/local/RoomData.js.map +7 -0
- package/build/matchmaker/driver/local/RoomData.mjs +57 -0
- package/build/matchmaker/driver/local/RoomData.mjs.map +7 -0
- package/build/presence/LocalPresence.d.ts +10 -3
- package/build/presence/LocalPresence.js +83 -5
- package/build/presence/LocalPresence.js.map +3 -3
- package/build/presence/LocalPresence.mjs +83 -8
- package/build/presence/LocalPresence.mjs.map +3 -3
- package/build/presence/Presence.d.ts +38 -2
- package/build/presence/Presence.js.map +1 -1
- package/build/rooms/LobbyRoom.d.ts +6 -6
- package/build/rooms/LobbyRoom.js +8 -3
- package/build/rooms/LobbyRoom.js.map +2 -2
- package/build/rooms/LobbyRoom.mjs +7 -5
- package/build/rooms/LobbyRoom.mjs.map +2 -2
- package/build/rooms/RelayRoom.d.ts +2 -2
- package/build/rooms/RelayRoom.js +3 -1
- package/build/rooms/RelayRoom.js.map +2 -2
- package/build/rooms/RelayRoom.mjs +10 -7
- package/build/rooms/RelayRoom.mjs.map +2 -2
- package/build/serializer/NoneSerializer.d.ts +2 -2
- package/build/serializer/NoneSerializer.js.map +1 -1
- package/build/serializer/NoneSerializer.mjs +3 -2
- package/build/serializer/NoneSerializer.mjs.map +2 -2
- package/build/serializer/SchemaSerializer.d.ts +13 -11
- package/build/serializer/SchemaSerializer.js +13 -10
- package/build/serializer/SchemaSerializer.js.map +2 -2
- package/build/serializer/SchemaSerializer.mjs +17 -13
- package/build/serializer/SchemaSerializer.mjs.map +2 -2
- package/build/serializer/Serializer.d.ts +1 -2
- package/build/serializer/Serializer.js.map +1 -1
- package/build/utils/DevMode.d.ts +2 -2
- package/build/utils/DevMode.js +8 -4
- package/build/utils/DevMode.js.map +2 -2
- package/build/utils/DevMode.mjs +7 -6
- package/build/utils/DevMode.mjs.map +2 -2
- package/build/utils/Utils.d.ts +4 -2
- package/build/utils/Utils.js +16 -15
- package/build/utils/Utils.js.map +2 -2
- package/build/utils/Utils.mjs +18 -21
- package/build/utils/Utils.mjs.map +2 -2
- package/package.json +9 -3
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// packages/core/src/matchmaker/driver/local/Query.ts
|
|
2
|
+
var Query = class {
|
|
3
|
+
constructor(rooms, conditions) {
|
|
4
|
+
this.$rooms = rooms.slice(0);
|
|
5
|
+
this.conditions = conditions;
|
|
6
|
+
}
|
|
7
|
+
sort(options) {
|
|
8
|
+
this.$rooms = this.$rooms.sort((room1, room2) => {
|
|
9
|
+
for (const field in options) {
|
|
10
|
+
if (options.hasOwnProperty(field)) {
|
|
11
|
+
const direction = options[field];
|
|
12
|
+
const isAscending = direction === 1 || direction === "asc" || direction === "ascending";
|
|
13
|
+
if (isAscending) {
|
|
14
|
+
if (room1[field] > room2[field]) {
|
|
15
|
+
return 1;
|
|
16
|
+
}
|
|
17
|
+
if (room1[field] < room2[field]) {
|
|
18
|
+
return -1;
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
if (room1[field] > room2[field]) {
|
|
22
|
+
return -1;
|
|
23
|
+
}
|
|
24
|
+
if (room1[field] < room2[field]) {
|
|
25
|
+
return 1;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
filter(conditions) {
|
|
33
|
+
return this.$rooms.filter((room) => {
|
|
34
|
+
for (const field in conditions) {
|
|
35
|
+
if (conditions.hasOwnProperty(field) && room[field] !== conditions[field]) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
then(resolve, reject) {
|
|
43
|
+
const result = this.$rooms.find((room) => {
|
|
44
|
+
for (const field in this.conditions) {
|
|
45
|
+
if (this.conditions.hasOwnProperty(field) && room[field] !== this.conditions[field]) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
});
|
|
51
|
+
return resolve(result);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
export {
|
|
55
|
+
Query
|
|
56
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/matchmaker/driver/local/Query.ts"],
|
|
4
|
+
"sourcesContent": ["import { SortOptions } from '../api.js';\n\nexport class Query<T> {\n private $rooms: T[];\n private conditions: any;\n\n constructor(rooms: any[], conditions) {\n this.$rooms = rooms.slice(0);\n this.conditions = conditions;\n }\n\n public sort(options: SortOptions) {\n this.$rooms = this.$rooms.sort((room1, room2) => {\n for (const field in options) {\n if (options.hasOwnProperty(field)) {\n const direction = options[field];\n const isAscending = (direction === 1 || direction === 'asc' || direction === 'ascending');\n\n if (isAscending) {\n if (room1[field] > room2[field]) { return 1; }\n if (room1[field] < room2[field]) { return -1; }\n\n } else {\n if (room1[field] > room2[field]) { return -1; }\n if (room1[field] < room2[field]) { return 1; }\n }\n }\n }\n });\n }\n\n public filter(conditions: any) {\n return this.$rooms.filter(((room) => {\n for (const field in conditions) {\n if (\n conditions.hasOwnProperty(field) &&\n room[field] !== conditions[field]\n ) {\n return false;\n }\n }\n return true;\n }));\n\n }\n\n public then(resolve, reject) {\n const result: any = this.$rooms.find(((room) => {\n for (const field in this.conditions) {\n if (\n this.conditions.hasOwnProperty(field) &&\n room[field] !== this.conditions[field]\n ) {\n return false;\n }\n }\n return true;\n }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C;AACA,iBAAO;AAAA,QACT;AAAA,MACF;AACA,aAAO;AAAA,IACT,CAAE;AACF,WAAO,QAAQ,MAAM;AAAA,EACvB;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { RoomCache, IRoomCache } from '../api.js';
|
|
2
|
+
export declare class RoomData implements RoomCache {
|
|
3
|
+
clients: number;
|
|
4
|
+
locked: boolean;
|
|
5
|
+
private: boolean;
|
|
6
|
+
maxClients: number;
|
|
7
|
+
metadata: any;
|
|
8
|
+
name: string;
|
|
9
|
+
publicAddress: string;
|
|
10
|
+
processId: string;
|
|
11
|
+
roomId: string;
|
|
12
|
+
createdAt: Date;
|
|
13
|
+
unlisted: boolean;
|
|
14
|
+
private $rooms;
|
|
15
|
+
constructor(initialValues: any, rooms: IRoomCache[]);
|
|
16
|
+
save(): void;
|
|
17
|
+
updateOne(operations: any): void;
|
|
18
|
+
remove(): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var RoomData_exports = {};
|
|
19
|
+
__export(RoomData_exports, {
|
|
20
|
+
RoomData: () => RoomData
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(RoomData_exports);
|
|
23
|
+
var import_Utils = require("../../../utils/Utils.js");
|
|
24
|
+
class RoomData {
|
|
25
|
+
constructor(initialValues, rooms) {
|
|
26
|
+
this.clients = 0;
|
|
27
|
+
this.locked = false;
|
|
28
|
+
this.private = false;
|
|
29
|
+
this.maxClients = Infinity;
|
|
30
|
+
this.unlisted = false;
|
|
31
|
+
this.createdAt = /* @__PURE__ */ new Date();
|
|
32
|
+
for (const field in initialValues) {
|
|
33
|
+
if (initialValues.hasOwnProperty(field)) {
|
|
34
|
+
this[field] = initialValues[field];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
Object.defineProperty(this, "$rooms", {
|
|
38
|
+
value: rooms,
|
|
39
|
+
enumerable: false,
|
|
40
|
+
writable: true
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
save() {
|
|
44
|
+
if (this.$rooms.indexOf(this) === -1) {
|
|
45
|
+
this.$rooms.push(this);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
updateOne(operations) {
|
|
49
|
+
if (operations.$set) {
|
|
50
|
+
for (const field in operations.$set) {
|
|
51
|
+
if (operations.$set.hasOwnProperty(field)) {
|
|
52
|
+
this[field] = operations.$set[field];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (operations.$inc) {
|
|
57
|
+
for (const field in operations.$inc) {
|
|
58
|
+
if (operations.$inc.hasOwnProperty(field)) {
|
|
59
|
+
this[field] += operations.$inc[field];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
remove() {
|
|
65
|
+
if (!this.$rooms) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const roomIndex = this.$rooms.indexOf(this);
|
|
69
|
+
if (roomIndex === -1) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
(0, import_Utils.spliceOne)(this.$rooms, roomIndex);
|
|
73
|
+
this.$rooms = null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
77
|
+
0 && (module.exports = {
|
|
78
|
+
RoomData
|
|
79
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/matchmaker/driver/local/RoomData.ts"],
|
|
4
|
+
"sourcesContent": ["import { spliceOne } from '../../../utils/Utils.js';\nimport { RoomCache, IRoomCache } from '../api.js';\n\nexport class RoomData implements RoomCache {\n public clients: number = 0;\n public locked: boolean = false;\n public private: boolean = false;\n public maxClients: number = Infinity;\n public metadata: any;\n public name: string;\n public publicAddress: string;\n public processId: string;\n public roomId: string;\n public createdAt: Date;\n public unlisted: boolean = false;\n\n private $rooms: RoomCache[];\n\n constructor(initialValues: any, rooms: IRoomCache[]) {\n this.createdAt = new Date();\n\n for (const field in initialValues) {\n if (initialValues.hasOwnProperty(field)) {\n this[field] = initialValues[field];\n }\n }\n\n // make $rooms non-enumerable, so it can be serialized (circular references)\n Object.defineProperty(this, \"$rooms\", {\n value: rooms,\n enumerable: false,\n writable: true,\n });\n }\n\n public save() {\n if (this.$rooms.indexOf(this) === -1) {\n this.$rooms.push(this);\n }\n }\n\n public updateOne(operations: any) {\n if (operations.$set) {\n for (const field in operations.$set) {\n if (operations.$set.hasOwnProperty(field)) {\n this[field] = operations.$set[field];\n }\n }\n }\n\n if (operations.$inc) {\n for (const field in operations.$inc) {\n if (operations.$inc.hasOwnProperty(field)) {\n this[field] += operations.$inc[field];\n }\n }\n }\n }\n\n public remove() {\n //\n // WORKAROUND: prevent calling `.remove()` multiple times\n // Seems to happen during disconnect + dispose: https://github.com/colyseus/colyseus/issues/390\n //\n if (!this.$rooms) { return; }\n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,cAAc,IAAI;AAAE;AAAA,IAAQ;AAEhC,gCAAU,KAAK,QAAQ,SAAS;AAChC,SAAK,SAAS;AAAA,EAChB;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// packages/core/src/matchmaker/driver/local/RoomData.ts
|
|
2
|
+
import { spliceOne } from "../../../utils/Utils.mjs";
|
|
3
|
+
var RoomData = class {
|
|
4
|
+
constructor(initialValues, rooms) {
|
|
5
|
+
this.clients = 0;
|
|
6
|
+
this.locked = false;
|
|
7
|
+
this.private = false;
|
|
8
|
+
this.maxClients = Infinity;
|
|
9
|
+
this.unlisted = false;
|
|
10
|
+
this.createdAt = /* @__PURE__ */ new Date();
|
|
11
|
+
for (const field in initialValues) {
|
|
12
|
+
if (initialValues.hasOwnProperty(field)) {
|
|
13
|
+
this[field] = initialValues[field];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(this, "$rooms", {
|
|
17
|
+
value: rooms,
|
|
18
|
+
enumerable: false,
|
|
19
|
+
writable: true
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
save() {
|
|
23
|
+
if (this.$rooms.indexOf(this) === -1) {
|
|
24
|
+
this.$rooms.push(this);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
updateOne(operations) {
|
|
28
|
+
if (operations.$set) {
|
|
29
|
+
for (const field in operations.$set) {
|
|
30
|
+
if (operations.$set.hasOwnProperty(field)) {
|
|
31
|
+
this[field] = operations.$set[field];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (operations.$inc) {
|
|
36
|
+
for (const field in operations.$inc) {
|
|
37
|
+
if (operations.$inc.hasOwnProperty(field)) {
|
|
38
|
+
this[field] += operations.$inc[field];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
remove() {
|
|
44
|
+
if (!this.$rooms) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const roomIndex = this.$rooms.indexOf(this);
|
|
48
|
+
if (roomIndex === -1) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
spliceOne(this.$rooms, roomIndex);
|
|
52
|
+
this.$rooms = null;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
export {
|
|
56
|
+
RoomData
|
|
57
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/matchmaker/driver/local/RoomData.ts"],
|
|
4
|
+
"sourcesContent": ["import { spliceOne } from '../../../utils/Utils.js';\nimport { RoomCache, IRoomCache } from '../api.js';\n\nexport class RoomData implements RoomCache {\n public clients: number = 0;\n public locked: boolean = false;\n public private: boolean = false;\n public maxClients: number = Infinity;\n public metadata: any;\n public name: string;\n public publicAddress: string;\n public processId: string;\n public roomId: string;\n public createdAt: Date;\n public unlisted: boolean = false;\n\n private $rooms: RoomCache[];\n\n constructor(initialValues: any, rooms: IRoomCache[]) {\n this.createdAt = new Date();\n\n for (const field in initialValues) {\n if (initialValues.hasOwnProperty(field)) {\n this[field] = initialValues[field];\n }\n }\n\n // make $rooms non-enumerable, so it can be serialized (circular references)\n Object.defineProperty(this, \"$rooms\", {\n value: rooms,\n enumerable: false,\n writable: true,\n });\n }\n\n public save() {\n if (this.$rooms.indexOf(this) === -1) {\n this.$rooms.push(this);\n }\n }\n\n public updateOne(operations: any) {\n if (operations.$set) {\n for (const field in operations.$set) {\n if (operations.$set.hasOwnProperty(field)) {\n this[field] = operations.$set[field];\n }\n }\n }\n\n if (operations.$inc) {\n for (const field in operations.$inc) {\n if (operations.$inc.hasOwnProperty(field)) {\n this[field] += operations.$inc[field];\n }\n }\n }\n }\n\n public remove() {\n //\n // WORKAROUND: prevent calling `.remove()` multiple times\n // Seems to happen during disconnect + dispose: https://github.com/colyseus/colyseus/issues/390\n //\n if (!this.$rooms) { return; }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EhC,cAAU,KAAK,QAAQ,SAAS;AAChC,SAAK,SAAS;AAAA,EAChB;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { EventEmitter } from 'events';
|
|
3
|
-
import { Presence } from './Presence';
|
|
2
|
+
import { Presence } from './Presence.js';
|
|
4
3
|
type Callback = (...args: any[]) => void;
|
|
5
4
|
export declare class LocalPresence implements Presence {
|
|
6
5
|
channels: EventEmitter<[never]>;
|
|
@@ -23,9 +22,10 @@ export declare class LocalPresence implements Presence {
|
|
|
23
22
|
subscribe(topic: string, callback: (...args: any[]) => void): this;
|
|
24
23
|
unsubscribe(topic: string, callback?: Callback): this;
|
|
25
24
|
publish(topic: string, data: any): this;
|
|
26
|
-
exists(
|
|
25
|
+
exists(key: string): Promise<boolean>;
|
|
27
26
|
set(key: string, value: string): void;
|
|
28
27
|
setex(key: string, value: string, seconds: number): void;
|
|
28
|
+
expire(key: string, seconds: number): void;
|
|
29
29
|
get(key: string): string | number;
|
|
30
30
|
del(key: string): void;
|
|
31
31
|
sadd(key: string, value: any): void;
|
|
@@ -36,6 +36,7 @@ export declare class LocalPresence implements Presence {
|
|
|
36
36
|
sinter(...keys: string[]): Promise<any[]>;
|
|
37
37
|
hset(key: string, field: string, value: string): void;
|
|
38
38
|
hincrby(key: string, field: string, incrBy: number): number;
|
|
39
|
+
hincrbyex(key: string, field: string, incrBy: number, expireInSeconds: number): number;
|
|
39
40
|
hget(key: string, field: string): Promise<string>;
|
|
40
41
|
hgetall(key: string): Promise<{
|
|
41
42
|
[key: string]: string;
|
|
@@ -44,6 +45,12 @@ export declare class LocalPresence implements Presence {
|
|
|
44
45
|
hlen(key: string): Promise<number>;
|
|
45
46
|
incr(key: string): Promise<number>;
|
|
46
47
|
decr(key: string): Promise<number>;
|
|
48
|
+
llen(key: string): number;
|
|
49
|
+
rpush(key: string, ...values: string[]): number;
|
|
50
|
+
lpush(key: string, ...values: string[]): number;
|
|
51
|
+
lpop(key: string): string;
|
|
52
|
+
rpop(key: string): string;
|
|
53
|
+
brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null>;
|
|
47
54
|
shutdown(): void;
|
|
48
55
|
}
|
|
49
56
|
export {};
|
|
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
25
|
mod
|
|
22
26
|
));
|
|
@@ -29,8 +33,8 @@ module.exports = __toCommonJS(LocalPresence_exports);
|
|
|
29
33
|
var import_fs = __toESM(require("fs"));
|
|
30
34
|
var import_path = __toESM(require("path"));
|
|
31
35
|
var import_events = require("events");
|
|
32
|
-
var import_Utils = require("../utils/Utils");
|
|
33
|
-
var import_DevMode = require("../utils/DevMode");
|
|
36
|
+
var import_Utils = require("../utils/Utils.js");
|
|
37
|
+
var import_DevMode = require("../utils/DevMode.js");
|
|
34
38
|
const DEVMODE_CACHE_FILE_PATH = import_path.default.resolve(".devmode.json");
|
|
35
39
|
class LocalPresence {
|
|
36
40
|
constructor() {
|
|
@@ -86,17 +90,20 @@ class LocalPresence {
|
|
|
86
90
|
this.channels.emit(topic, data);
|
|
87
91
|
return this;
|
|
88
92
|
}
|
|
89
|
-
async exists(
|
|
90
|
-
return this.channels.listenerCount(
|
|
93
|
+
async exists(key) {
|
|
94
|
+
return this.channels.listenerCount(key) > 0;
|
|
91
95
|
}
|
|
92
96
|
set(key, value) {
|
|
93
97
|
this.keys[key] = value;
|
|
94
98
|
}
|
|
95
99
|
setex(key, value, seconds) {
|
|
100
|
+
this.keys[key] = value;
|
|
101
|
+
this.expire(key, seconds);
|
|
102
|
+
}
|
|
103
|
+
expire(key, seconds) {
|
|
96
104
|
if (this.timeouts[key]) {
|
|
97
105
|
clearTimeout(this.timeouts[key]);
|
|
98
106
|
}
|
|
99
|
-
this.keys[key] = value;
|
|
100
107
|
this.timeouts[key] = setTimeout(() => {
|
|
101
108
|
delete this.keys[key];
|
|
102
109
|
delete this.timeouts[key];
|
|
@@ -164,6 +171,16 @@ class LocalPresence {
|
|
|
164
171
|
this.hash[key][field] = value.toString();
|
|
165
172
|
return value;
|
|
166
173
|
}
|
|
174
|
+
hincrbyex(key, field, incrBy, expireInSeconds) {
|
|
175
|
+
if (!this.hash[key]) {
|
|
176
|
+
this.hash[key] = {};
|
|
177
|
+
}
|
|
178
|
+
let value = Number(this.hash[key][field] || "0");
|
|
179
|
+
value += incrBy;
|
|
180
|
+
this.hash[key][field] = value.toString();
|
|
181
|
+
this.setex(key, field, expireInSeconds);
|
|
182
|
+
return value;
|
|
183
|
+
}
|
|
167
184
|
async hget(key, field) {
|
|
168
185
|
return this.hash[key] && this.hash[key][field];
|
|
169
186
|
}
|
|
@@ -194,6 +211,67 @@ class LocalPresence {
|
|
|
194
211
|
this.keys[key]--;
|
|
195
212
|
return Promise.resolve(this.keys[key]);
|
|
196
213
|
}
|
|
214
|
+
llen(key) {
|
|
215
|
+
return this.data[key] && this.data[key].length || 0;
|
|
216
|
+
}
|
|
217
|
+
rpush(key, ...values) {
|
|
218
|
+
if (!this.data[key]) {
|
|
219
|
+
this.data[key] = [];
|
|
220
|
+
}
|
|
221
|
+
let lastLength = 0;
|
|
222
|
+
values.forEach((value) => {
|
|
223
|
+
lastLength = this.data[key].push(value);
|
|
224
|
+
});
|
|
225
|
+
return lastLength;
|
|
226
|
+
}
|
|
227
|
+
lpush(key, ...values) {
|
|
228
|
+
if (!this.data[key]) {
|
|
229
|
+
this.data[key] = [];
|
|
230
|
+
}
|
|
231
|
+
let lastLength = 0;
|
|
232
|
+
values.forEach((value) => {
|
|
233
|
+
lastLength = this.data[key].unshift(value);
|
|
234
|
+
});
|
|
235
|
+
return lastLength;
|
|
236
|
+
}
|
|
237
|
+
lpop(key) {
|
|
238
|
+
return Array.isArray(this.data[key]) && this.data[key].shift();
|
|
239
|
+
}
|
|
240
|
+
rpop(key) {
|
|
241
|
+
return this.data[key].pop();
|
|
242
|
+
}
|
|
243
|
+
brpop(...args) {
|
|
244
|
+
const keys = args.slice(0, -2);
|
|
245
|
+
const timeoutInSeconds = args[args.length - 1];
|
|
246
|
+
const getFirstPopulated = () => {
|
|
247
|
+
const keyWithValue = keys.find((key) => this.data[key] && this.data[key].length > 0);
|
|
248
|
+
if (keyWithValue) {
|
|
249
|
+
return [keyWithValue, this.data[keyWithValue].pop()];
|
|
250
|
+
} else {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
const firstPopulated = getFirstPopulated();
|
|
255
|
+
if (firstPopulated) {
|
|
256
|
+
return Promise.resolve(firstPopulated);
|
|
257
|
+
} else {
|
|
258
|
+
const maxRetries = timeoutInSeconds * 8;
|
|
259
|
+
let tries = 0;
|
|
260
|
+
return new Promise((resolve) => {
|
|
261
|
+
const interval = setInterval(() => {
|
|
262
|
+
tries++;
|
|
263
|
+
const firstPopulated2 = getFirstPopulated();
|
|
264
|
+
if (firstPopulated2) {
|
|
265
|
+
clearInterval(interval);
|
|
266
|
+
return resolve(firstPopulated2);
|
|
267
|
+
} else if (tries >= maxRetries) {
|
|
268
|
+
clearInterval(interval);
|
|
269
|
+
return resolve(void 0);
|
|
270
|
+
}
|
|
271
|
+
}, timeoutInSeconds * 1e3 / maxRetries);
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}
|
|
197
275
|
shutdown() {
|
|
198
276
|
if (import_DevMode.isDevMode) {
|
|
199
277
|
const cache = JSON.stringify({
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/presence/LocalPresence.ts"],
|
|
4
|
-
"sourcesContent": ["import fs from
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["path", "fs"]
|
|
4
|
+
"sourcesContent": ["import fs from 'fs';\nimport path from 'path';\n\nimport { EventEmitter } from 'events';\nimport { spliceOne } from '../utils/Utils.js';\nimport { Presence } from './Presence.js';\n\nimport { isDevMode } from '../utils/DevMode.js';\n\ntype Callback = (...args: any[]) => void;\n\nconst DEVMODE_CACHE_FILE_PATH = path.resolve(\".devmode.json\");\n\nexport class LocalPresence implements Presence {\n public channels = new EventEmitter();\n\n public data: {[roomName: string]: string[]} = {};\n public hash: {[roomName: string]: {[key: string]: string}} = {};\n\n public keys: {[name: string]: string | number} = {};\n\n protected subscriptions: {[id: string]: Callback[]} = {};\n private timeouts: {[name: string]: NodeJS.Timeout} = {};\n\n constructor() {\n //\n // reload from local cache on devMode\n //\n if (\n isDevMode &&\n fs.existsSync(DEVMODE_CACHE_FILE_PATH)\n ) {\n const cache = fs.readFileSync(DEVMODE_CACHE_FILE_PATH).toString('utf-8') || \"{}\";\n const parsed = JSON.parse(cache);\n if (parsed.data) { this.data = parsed.data; }\n if (parsed.hash) { this.hash = parsed.hash; }\n if (parsed.keys) { this.keys = parsed.keys; }\n }\n }\n\n public subscribe(topic: string, callback: (...args: any[]) => void) {\n if (!this.subscriptions[topic]) { this.subscriptions[topic] = []; }\n this.subscriptions[topic].push(callback);\n this.channels.on(topic, callback);\n return this;\n }\n\n public unsubscribe(topic: string, callback?: Callback) {\n const topicCallbacks = this.subscriptions[topic];\n if (!topicCallbacks) { return; }\n\n if (callback) {\n const idx = topicCallbacks.indexOf(callback);\n if (idx !== -1) {\n topicCallbacks.splice(idx, 1);\n this.channels.removeListener(topic, callback);\n }\n\n if (topicCallbacks.length === 0) {\n delete this.subscriptions[topic];\n }\n\n } else {\n topicCallbacks.forEach((cb) =>\n this.channels.removeListener(topic, cb));\n\n delete this.subscriptions[topic];\n }\n\n return this;\n }\n\n public publish(topic: string, data: any) {\n this.channels.emit(topic, data);\n return this;\n }\n\n public async exists(key: string): Promise<boolean> {\n return this.channels.listenerCount(key) > 0;\n }\n\n public set(key: string, value: string) {\n this.keys[key] = value;\n }\n\n public setex(key: string, value: string, seconds: number) {\n this.keys[key] = value;\n this.expire(key, seconds);\n }\n\n public expire(key: string, seconds: number) {\n // ensure previous timeout is clear before setting another one.\n if (this.timeouts[key]) {\n clearTimeout(this.timeouts[key]);\n }\n this.timeouts[key] = setTimeout(() => {\n delete this.keys[key];\n delete this.timeouts[key];\n }, seconds * 1000);\n }\n\n public get(key: string) {\n return this.keys[key];\n }\n\n public del(key: string) {\n delete this.keys[key];\n delete this.data[key];\n delete this.hash[key];\n }\n\n public sadd(key: string, value: any) {\n if (!this.data[key]) {\n this.data[key] = [];\n }\n\n if (this.data[key].indexOf(value) === -1) {\n this.data[key].push(value);\n }\n }\n\n public async smembers(key: string): Promise<string[]> {\n return this.data[key] || [];\n }\n\n public async sismember(key: string, field: string) {\n return this.data[key] && this.data[key].includes(field) ? 1 : 0;\n }\n\n public srem(key: string, value: any) {\n if (this.data[key]) {\n spliceOne(this.data[key], this.data[key].indexOf(value));\n }\n }\n\n public scard(key: string) {\n return (this.data[key] || []).length;\n }\n\n public async sinter(...keys: string[]) {\n const intersection: {[value: string]: number} = {};\n\n for (let i = 0, l = keys.length; i < l; i++) {\n (await this.smembers(keys[i])).forEach((member) => {\n if (!intersection[member]) {\n intersection[member] = 0;\n }\n\n intersection[member]++;\n });\n }\n\n return Object.keys(intersection).reduce((prev, curr) => {\n if (intersection[curr] > 1) {\n prev.push(curr);\n }\n return prev;\n }, []);\n }\n\n public hset(key: string, field: string, value: string) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n this.hash[key][field] = value;\n }\n\n public hincrby(key: string, field: string, incrBy: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n return value;\n }\n\n public hincrbyex(key: string, field: string, incrBy: number, expireInSeconds: number) {\n if (!this.hash[key]) { this.hash[key] = {}; }\n let value = Number(this.hash[key][field] || '0');\n value += incrBy;\n this.hash[key][field] = value.toString();\n this.setex(key, field, expireInSeconds);\n return value;\n }\n\n public async hget(key: string, field: string) {\n return this.hash[key] && this.hash[key][field];\n }\n\n public async hgetall(key: string) {\n return this.hash[key] || {};\n }\n\n public hdel(key: string, field: any) {\n const success = this.hash?.[key]?.[field] !== undefined;\n if (success) {\n delete this.hash[key][field];\n }\n return success;\n }\n\n public async hlen(key: string) {\n return this.hash[key] && Object.keys(this.hash[key]).length || 0;\n }\n\n public async incr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)++;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public async decr(key: string) {\n if (!this.keys[key]) {\n this.keys[key] = 0;\n }\n (this.keys[key] as number)--;\n return Promise.resolve(this.keys[key] as number);\n }\n\n public llen(key: string): number {\n return (this.data[key] && this.data[key].length) || 0;\n }\n\n public rpush(key: string, ...values: string[]): number {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].push(value);\n });\n\n return lastLength;\n }\n\n public lpush(key: string, ...values: string[]): number {\n if (!this.data[key]) { this.data[key] = []; }\n\n let lastLength: number = 0;\n\n values.forEach(value => {\n lastLength = this.data[key].unshift(value);\n });\n\n return lastLength;\n }\n\n\n public lpop(key: string): string {\n return Array.isArray(this.data[key]) && this.data[key].shift();\n }\n\n public rpop(key: string): string {\n return this.data[key].pop();\n }\n\n public brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null> {\n const keys = args.slice(0, -2) as string[];\n const timeoutInSeconds = args[args.length - 1] as number;\n\n const getFirstPopulated = (): [string, string] | null => {\n const keyWithValue = keys.find(key => this.data[key] && this.data[key].length > 0);\n if (keyWithValue) {\n return [keyWithValue, this.data[keyWithValue].pop()];\n } else {\n return null;\n }\n }\n\n const firstPopulated = getFirstPopulated();\n\n if (firstPopulated) {\n // return first populated key + item\n return Promise.resolve(firstPopulated);\n\n } else {\n // 8 retries per second\n const maxRetries = timeoutInSeconds * 8;\n\n let tries = 0;\n return new Promise((resolve) => {\n const interval = setInterval(() => {\n tries++;\n\n const firstPopulated = getFirstPopulated();\n if (firstPopulated) {\n clearInterval(interval);\n return resolve(firstPopulated);\n\n } else if (tries >= maxRetries) {\n clearInterval(interval);\n return resolve(undefined);\n }\n\n }, (timeoutInSeconds * 1000) / maxRetries);\n });\n }\n }\n\n public shutdown() {\n if (isDevMode) {\n const cache = JSON.stringify({\n data: this.data,\n hash: this.hash,\n keys: this.keys\n });\n fs.writeFileSync(DEVMODE_CACHE_FILE_PATH, cache, { encoding: \"utf-8\" });\n }\n }\n\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAAe;AACf,kBAAiB;AAEjB,oBAA6B;AAC7B,mBAA0B;AAG1B,qBAA0B;AAI1B,MAAM,0BAA0B,YAAAA,QAAK,QAAQ,eAAe;AAErD,MAAM,cAAkC;AAAA,EAW3C,cAAc;AAVd,SAAO,WAAW,IAAI,2BAAa;AAEnC,SAAO,OAAuC,CAAC;AAC/C,SAAO,OAAsD,CAAC;AAE9D,SAAO,OAA0C,CAAC;AAElD,SAAU,gBAA4C,CAAC;AACvD,SAAQ,WAA6C,CAAC;AAMpD,QACE,4BACA,UAAAC,QAAG,WAAW,uBAAuB,GACrC;AACA,YAAM,QAAQ,UAAAA,QAAG,aAAa,uBAAuB,EAAE,SAAS,OAAO,KAAK;AAC5E,YAAM,SAAS,KAAK,MAAM,KAAK;AAC/B,UAAI,OAAO,MAAM;AAAE,aAAK,OAAO,OAAO;AAAA,MAAM;AAC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;AAAA,UAE/B,WAAW,SAAS,YAAY;AAC9B,0BAAc,QAAQ;AACtB,mBAAO,QAAQ,MAAS;AAAA,UAC1B;AAAA,QAEF,GAAI,mBAAmB,MAAQ,UAAU;AAAA,MAC3C,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEO,WAAW;AAChB,QAAI,0BAAW;AACb,YAAM,QAAQ,KAAK,UAAU;AAAA,QAC3B,MAAM,KAAK;AAAA,QACX,MAAM,KAAK;AAAA,QACX,MAAM,KAAK;AAAA,MACb,CAAC;AACD,gBAAAD,QAAG,cAAc,yBAAyB,OAAO,EAAE,UAAU,QAAQ,CAAC;AAAA,IACxE;AAAA,EACF;AAEJ;",
|
|
6
|
+
"names": ["path", "fs", "firstPopulated"]
|
|
7
7
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
// packages/core/src/presence/LocalPresence.ts
|
|
1
2
|
import fs from "fs";
|
|
2
3
|
import path from "path";
|
|
3
4
|
import { EventEmitter } from "events";
|
|
4
|
-
import { spliceOne } from "../utils/Utils";
|
|
5
|
-
import { isDevMode } from "../utils/DevMode";
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import { spliceOne } from "../utils/Utils.mjs";
|
|
6
|
+
import { isDevMode } from "../utils/DevMode.mjs";
|
|
7
|
+
var DEVMODE_CACHE_FILE_PATH = path.resolve(".devmode.json");
|
|
8
|
+
var LocalPresence = class {
|
|
8
9
|
constructor() {
|
|
9
10
|
this.channels = new EventEmitter();
|
|
10
11
|
this.data = {};
|
|
@@ -58,17 +59,20 @@ class LocalPresence {
|
|
|
58
59
|
this.channels.emit(topic, data);
|
|
59
60
|
return this;
|
|
60
61
|
}
|
|
61
|
-
async exists(
|
|
62
|
-
return this.channels.listenerCount(
|
|
62
|
+
async exists(key) {
|
|
63
|
+
return this.channels.listenerCount(key) > 0;
|
|
63
64
|
}
|
|
64
65
|
set(key, value) {
|
|
65
66
|
this.keys[key] = value;
|
|
66
67
|
}
|
|
67
68
|
setex(key, value, seconds) {
|
|
69
|
+
this.keys[key] = value;
|
|
70
|
+
this.expire(key, seconds);
|
|
71
|
+
}
|
|
72
|
+
expire(key, seconds) {
|
|
68
73
|
if (this.timeouts[key]) {
|
|
69
74
|
clearTimeout(this.timeouts[key]);
|
|
70
75
|
}
|
|
71
|
-
this.keys[key] = value;
|
|
72
76
|
this.timeouts[key] = setTimeout(() => {
|
|
73
77
|
delete this.keys[key];
|
|
74
78
|
delete this.timeouts[key];
|
|
@@ -136,6 +140,16 @@ class LocalPresence {
|
|
|
136
140
|
this.hash[key][field] = value.toString();
|
|
137
141
|
return value;
|
|
138
142
|
}
|
|
143
|
+
hincrbyex(key, field, incrBy, expireInSeconds) {
|
|
144
|
+
if (!this.hash[key]) {
|
|
145
|
+
this.hash[key] = {};
|
|
146
|
+
}
|
|
147
|
+
let value = Number(this.hash[key][field] || "0");
|
|
148
|
+
value += incrBy;
|
|
149
|
+
this.hash[key][field] = value.toString();
|
|
150
|
+
this.setex(key, field, expireInSeconds);
|
|
151
|
+
return value;
|
|
152
|
+
}
|
|
139
153
|
async hget(key, field) {
|
|
140
154
|
return this.hash[key] && this.hash[key][field];
|
|
141
155
|
}
|
|
@@ -166,6 +180,67 @@ class LocalPresence {
|
|
|
166
180
|
this.keys[key]--;
|
|
167
181
|
return Promise.resolve(this.keys[key]);
|
|
168
182
|
}
|
|
183
|
+
llen(key) {
|
|
184
|
+
return this.data[key] && this.data[key].length || 0;
|
|
185
|
+
}
|
|
186
|
+
rpush(key, ...values) {
|
|
187
|
+
if (!this.data[key]) {
|
|
188
|
+
this.data[key] = [];
|
|
189
|
+
}
|
|
190
|
+
let lastLength = 0;
|
|
191
|
+
values.forEach((value) => {
|
|
192
|
+
lastLength = this.data[key].push(value);
|
|
193
|
+
});
|
|
194
|
+
return lastLength;
|
|
195
|
+
}
|
|
196
|
+
lpush(key, ...values) {
|
|
197
|
+
if (!this.data[key]) {
|
|
198
|
+
this.data[key] = [];
|
|
199
|
+
}
|
|
200
|
+
let lastLength = 0;
|
|
201
|
+
values.forEach((value) => {
|
|
202
|
+
lastLength = this.data[key].unshift(value);
|
|
203
|
+
});
|
|
204
|
+
return lastLength;
|
|
205
|
+
}
|
|
206
|
+
lpop(key) {
|
|
207
|
+
return Array.isArray(this.data[key]) && this.data[key].shift();
|
|
208
|
+
}
|
|
209
|
+
rpop(key) {
|
|
210
|
+
return this.data[key].pop();
|
|
211
|
+
}
|
|
212
|
+
brpop(...args) {
|
|
213
|
+
const keys = args.slice(0, -2);
|
|
214
|
+
const timeoutInSeconds = args[args.length - 1];
|
|
215
|
+
const getFirstPopulated = () => {
|
|
216
|
+
const keyWithValue = keys.find((key) => this.data[key] && this.data[key].length > 0);
|
|
217
|
+
if (keyWithValue) {
|
|
218
|
+
return [keyWithValue, this.data[keyWithValue].pop()];
|
|
219
|
+
} else {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
const firstPopulated = getFirstPopulated();
|
|
224
|
+
if (firstPopulated) {
|
|
225
|
+
return Promise.resolve(firstPopulated);
|
|
226
|
+
} else {
|
|
227
|
+
const maxRetries = timeoutInSeconds * 8;
|
|
228
|
+
let tries = 0;
|
|
229
|
+
return new Promise((resolve) => {
|
|
230
|
+
const interval = setInterval(() => {
|
|
231
|
+
tries++;
|
|
232
|
+
const firstPopulated2 = getFirstPopulated();
|
|
233
|
+
if (firstPopulated2) {
|
|
234
|
+
clearInterval(interval);
|
|
235
|
+
return resolve(firstPopulated2);
|
|
236
|
+
} else if (tries >= maxRetries) {
|
|
237
|
+
clearInterval(interval);
|
|
238
|
+
return resolve(void 0);
|
|
239
|
+
}
|
|
240
|
+
}, timeoutInSeconds * 1e3 / maxRetries);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
169
244
|
shutdown() {
|
|
170
245
|
if (isDevMode) {
|
|
171
246
|
const cache = JSON.stringify({
|
|
@@ -176,7 +251,7 @@ class LocalPresence {
|
|
|
176
251
|
fs.writeFileSync(DEVMODE_CACHE_FILE_PATH, cache, { encoding: "utf-8" });
|
|
177
252
|
}
|
|
178
253
|
}
|
|
179
|
-
}
|
|
254
|
+
};
|
|
180
255
|
export {
|
|
181
256
|
LocalPresence
|
|
182
257
|
};
|