@colyseus/core 0.16.0-preview.8 → 0.16.0
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/README.md +5 -5
- 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 +35 -30
- package/build/MatchMaker.js +150 -100
- package/build/MatchMaker.js.map +2 -2
- package/build/MatchMaker.mjs +154 -107
- package/build/MatchMaker.mjs.map +2 -2
- package/build/Protocol.d.ts +3 -4
- package/build/Protocol.js +33 -19
- package/build/Protocol.js.map +2 -2
- package/build/Protocol.mjs +36 -21
- package/build/Protocol.mjs.map +2 -2
- package/build/Room.d.ts +64 -40
- package/build/Room.js +412 -154
- package/build/Room.js.map +2 -2
- package/build/Room.mjs +416 -161
- package/build/Room.mjs.map +2 -2
- package/build/Server.d.ts +8 -7
- package/build/Server.js +51 -18
- package/build/Server.js.map +2 -2
- package/build/Server.mjs +51 -21
- package/build/Server.mjs.map +3 -3
- package/build/Stats.d.ts +2 -0
- package/build/Stats.js +38 -3
- package/build/Stats.js.map +2 -2
- package/build/Stats.mjs +30 -6
- package/build/Stats.mjs.map +2 -2
- package/build/Transport.d.ts +29 -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/RoomExceptions.d.ts +39 -0
- package/build/errors/RoomExceptions.js +100 -0
- package/build/errors/RoomExceptions.js.map +7 -0
- package/build/errors/RoomExceptions.mjs +71 -0
- package/build/errors/RoomExceptions.mjs.map +7 -0
- 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 +21 -19
- package/build/index.js +47 -20
- package/build/index.js.map +2 -2
- package/build/index.mjs +41 -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 +4 -5
- package/build/matchmaker/controller.js +22 -15
- package/build/matchmaker/controller.js.map +2 -2
- package/build/matchmaker/controller.mjs +19 -13
- package/build/matchmaker/controller.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 +2 -2
- package/build/matchmaker/driver/index.js +2 -2
- package/build/matchmaker/driver/index.js.map +2 -2
- package/build/matchmaker/driver/index.mjs +5 -4
- package/build/matchmaker/driver/index.mjs.map +2 -2
- 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 -6
- package/build/presence/LocalPresence.js +85 -24
- package/build/presence/LocalPresence.js.map +3 -3
- package/build/presence/LocalPresence.mjs +85 -27
- 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 +3 -3
- 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 +16 -15
- package/build/serializer/SchemaSerializer.js +12 -10
- package/build/serializer/SchemaSerializer.js.map +2 -2
- package/build/serializer/SchemaSerializer.mjs +16 -13
- package/build/serializer/SchemaSerializer.mjs.map +2 -2
- package/build/serializer/SchemaSerializerDebug.d.ts +7 -0
- package/build/serializer/SchemaSerializerDebug.js +0 -0
- package/build/serializer/SchemaSerializerDebug.js.map +7 -0
- package/build/serializer/SchemaSerializerDebug.mjs +0 -0
- package/build/serializer/SchemaSerializerDebug.mjs.map +7 -0
- 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 +8 -3
- package/build/utils/Utils.js +41 -17
- package/build/utils/Utils.js.map +2 -2
- package/build/utils/Utils.mjs +40 -21
- package/build/utils/Utils.mjs.map +2 -2
- package/package.json +17 -6
|
@@ -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\n const roomIndex = this.$rooms.indexOf(this);\n if (roomIndex === -1) { return; }\n\n spliceOne(this.$rooms, roomIndex);\n this.$rooms = null;\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;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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; }\n\n const roomIndex = this.$rooms.indexOf(this);\n if (roomIndex === -1) { return; }\n\n spliceOne(this.$rooms, roomIndex);\n this.$rooms = null;\n }\n}\n"],
|
|
5
|
+
"mappings": ";A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|
|
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]>;
|
|
@@ -15,17 +14,15 @@ export declare class LocalPresence implements Presence {
|
|
|
15
14
|
keys: {
|
|
16
15
|
[name: string]: string | number;
|
|
17
16
|
};
|
|
18
|
-
protected subscriptions: {
|
|
19
|
-
[id: string]: Callback[];
|
|
20
|
-
};
|
|
21
17
|
private timeouts;
|
|
22
18
|
constructor();
|
|
23
19
|
subscribe(topic: string, callback: (...args: any[]) => void): this;
|
|
24
20
|
unsubscribe(topic: string, callback?: Callback): this;
|
|
25
21
|
publish(topic: string, data: any): this;
|
|
26
|
-
exists(
|
|
22
|
+
exists(key: string): Promise<boolean>;
|
|
27
23
|
set(key: string, value: string): void;
|
|
28
24
|
setex(key: string, value: string, seconds: number): void;
|
|
25
|
+
expire(key: string, seconds: number): void;
|
|
29
26
|
get(key: string): string | number;
|
|
30
27
|
del(key: string): void;
|
|
31
28
|
sadd(key: string, value: any): void;
|
|
@@ -36,6 +33,7 @@ export declare class LocalPresence implements Presence {
|
|
|
36
33
|
sinter(...keys: string[]): Promise<any[]>;
|
|
37
34
|
hset(key: string, field: string, value: string): void;
|
|
38
35
|
hincrby(key: string, field: string, incrBy: number): number;
|
|
36
|
+
hincrbyex(key: string, field: string, incrBy: number, expireInSeconds: number): number;
|
|
39
37
|
hget(key: string, field: string): Promise<string>;
|
|
40
38
|
hgetall(key: string): Promise<{
|
|
41
39
|
[key: string]: string;
|
|
@@ -44,6 +42,12 @@ export declare class LocalPresence implements Presence {
|
|
|
44
42
|
hlen(key: string): Promise<number>;
|
|
45
43
|
incr(key: string): Promise<number>;
|
|
46
44
|
decr(key: string): Promise<number>;
|
|
45
|
+
llen(key: string): number;
|
|
46
|
+
rpush(key: string, ...values: string[]): number;
|
|
47
|
+
lpush(key: string, ...values: string[]): number;
|
|
48
|
+
lpop(key: string): string;
|
|
49
|
+
rpop(key: string): string;
|
|
50
|
+
brpop(...args: [...keys: string[], timeoutInSeconds: number]): Promise<[string, string] | null>;
|
|
47
51
|
shutdown(): void;
|
|
48
52
|
}
|
|
49
53
|
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() {
|
|
@@ -38,7 +42,6 @@ class LocalPresence {
|
|
|
38
42
|
this.data = {};
|
|
39
43
|
this.hash = {};
|
|
40
44
|
this.keys = {};
|
|
41
|
-
this.subscriptions = {};
|
|
42
45
|
this.timeouts = {};
|
|
43
46
|
if (import_DevMode.isDevMode && import_fs.default.existsSync(DEVMODE_CACHE_FILE_PATH)) {
|
|
44
47
|
const cache = import_fs.default.readFileSync(DEVMODE_CACHE_FILE_PATH).toString("utf-8") || "{}";
|
|
@@ -55,30 +58,14 @@ class LocalPresence {
|
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
60
|
subscribe(topic, callback) {
|
|
58
|
-
if (!this.subscriptions[topic]) {
|
|
59
|
-
this.subscriptions[topic] = [];
|
|
60
|
-
}
|
|
61
|
-
this.subscriptions[topic].push(callback);
|
|
62
61
|
this.channels.on(topic, callback);
|
|
63
62
|
return this;
|
|
64
63
|
}
|
|
65
64
|
unsubscribe(topic, callback) {
|
|
66
|
-
const topicCallbacks = this.subscriptions[topic];
|
|
67
|
-
if (!topicCallbacks) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
65
|
if (callback) {
|
|
71
|
-
|
|
72
|
-
if (idx !== -1) {
|
|
73
|
-
topicCallbacks.splice(idx, 1);
|
|
74
|
-
this.channels.removeListener(topic, callback);
|
|
75
|
-
}
|
|
76
|
-
if (topicCallbacks.length === 0) {
|
|
77
|
-
delete this.subscriptions[topic];
|
|
78
|
-
}
|
|
66
|
+
this.channels.removeListener(topic, callback);
|
|
79
67
|
} else {
|
|
80
|
-
|
|
81
|
-
delete this.subscriptions[topic];
|
|
68
|
+
this.channels.removeAllListeners(topic);
|
|
82
69
|
}
|
|
83
70
|
return this;
|
|
84
71
|
}
|
|
@@ -86,17 +73,20 @@ class LocalPresence {
|
|
|
86
73
|
this.channels.emit(topic, data);
|
|
87
74
|
return this;
|
|
88
75
|
}
|
|
89
|
-
async exists(
|
|
90
|
-
return this.
|
|
76
|
+
async exists(key) {
|
|
77
|
+
return this.keys[key] !== void 0 || this.data[key] !== void 0 || this.hash[key] !== void 0;
|
|
91
78
|
}
|
|
92
79
|
set(key, value) {
|
|
93
80
|
this.keys[key] = value;
|
|
94
81
|
}
|
|
95
82
|
setex(key, value, seconds) {
|
|
83
|
+
this.keys[key] = value;
|
|
84
|
+
this.expire(key, seconds);
|
|
85
|
+
}
|
|
86
|
+
expire(key, seconds) {
|
|
96
87
|
if (this.timeouts[key]) {
|
|
97
88
|
clearTimeout(this.timeouts[key]);
|
|
98
89
|
}
|
|
99
|
-
this.keys[key] = value;
|
|
100
90
|
this.timeouts[key] = setTimeout(() => {
|
|
101
91
|
delete this.keys[key];
|
|
102
92
|
delete this.timeouts[key];
|
|
@@ -164,6 +154,16 @@ class LocalPresence {
|
|
|
164
154
|
this.hash[key][field] = value.toString();
|
|
165
155
|
return value;
|
|
166
156
|
}
|
|
157
|
+
hincrbyex(key, field, incrBy, expireInSeconds) {
|
|
158
|
+
if (!this.hash[key]) {
|
|
159
|
+
this.hash[key] = {};
|
|
160
|
+
}
|
|
161
|
+
let value = Number(this.hash[key][field] || "0");
|
|
162
|
+
value += incrBy;
|
|
163
|
+
this.hash[key][field] = value.toString();
|
|
164
|
+
this.setex(key, field, expireInSeconds);
|
|
165
|
+
return value;
|
|
166
|
+
}
|
|
167
167
|
async hget(key, field) {
|
|
168
168
|
return this.hash[key] && this.hash[key][field];
|
|
169
169
|
}
|
|
@@ -194,6 +194,67 @@ class LocalPresence {
|
|
|
194
194
|
this.keys[key]--;
|
|
195
195
|
return Promise.resolve(this.keys[key]);
|
|
196
196
|
}
|
|
197
|
+
llen(key) {
|
|
198
|
+
return this.data[key] && this.data[key].length || 0;
|
|
199
|
+
}
|
|
200
|
+
rpush(key, ...values) {
|
|
201
|
+
if (!this.data[key]) {
|
|
202
|
+
this.data[key] = [];
|
|
203
|
+
}
|
|
204
|
+
let lastLength = 0;
|
|
205
|
+
values.forEach((value) => {
|
|
206
|
+
lastLength = this.data[key].push(value);
|
|
207
|
+
});
|
|
208
|
+
return lastLength;
|
|
209
|
+
}
|
|
210
|
+
lpush(key, ...values) {
|
|
211
|
+
if (!this.data[key]) {
|
|
212
|
+
this.data[key] = [];
|
|
213
|
+
}
|
|
214
|
+
let lastLength = 0;
|
|
215
|
+
values.forEach((value) => {
|
|
216
|
+
lastLength = this.data[key].unshift(value);
|
|
217
|
+
});
|
|
218
|
+
return lastLength;
|
|
219
|
+
}
|
|
220
|
+
lpop(key) {
|
|
221
|
+
return Array.isArray(this.data[key]) && this.data[key].shift();
|
|
222
|
+
}
|
|
223
|
+
rpop(key) {
|
|
224
|
+
return this.data[key].pop();
|
|
225
|
+
}
|
|
226
|
+
brpop(...args) {
|
|
227
|
+
const keys = args.slice(0, -2);
|
|
228
|
+
const timeoutInSeconds = args[args.length - 1];
|
|
229
|
+
const getFirstPopulated = () => {
|
|
230
|
+
const keyWithValue = keys.find((key) => this.data[key] && this.data[key].length > 0);
|
|
231
|
+
if (keyWithValue) {
|
|
232
|
+
return [keyWithValue, this.data[keyWithValue].pop()];
|
|
233
|
+
} else {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const firstPopulated = getFirstPopulated();
|
|
238
|
+
if (firstPopulated) {
|
|
239
|
+
return Promise.resolve(firstPopulated);
|
|
240
|
+
} else {
|
|
241
|
+
const maxRetries = timeoutInSeconds * 8;
|
|
242
|
+
let tries = 0;
|
|
243
|
+
return new Promise((resolve) => {
|
|
244
|
+
const interval = setInterval(() => {
|
|
245
|
+
tries++;
|
|
246
|
+
const firstPopulated2 = getFirstPopulated();
|
|
247
|
+
if (firstPopulated2) {
|
|
248
|
+
clearInterval(interval);
|
|
249
|
+
return resolve(firstPopulated2);
|
|
250
|
+
} else if (tries >= maxRetries) {
|
|
251
|
+
clearInterval(interval);
|
|
252
|
+
return resolve(void 0);
|
|
253
|
+
}
|
|
254
|
+
}, timeoutInSeconds * 1e3 / maxRetries);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
197
258
|
shutdown() {
|
|
198
259
|
if (import_DevMode.isDevMode) {
|
|
199
260
|
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 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 this.channels.on(topic, callback);\n return this;\n }\n\n public unsubscribe(topic: string, callback?: Callback) {\n if (callback) {\n this.channels.removeListener(topic, callback);\n\n } else {\n this.channels.removeAllListeners(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 (\n this.keys[key] !== undefined ||\n this.data[key] !== undefined ||\n this.hash[key] !== undefined\n );\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,EAU3C,cAAc;AATd,SAAO,WAAW,IAAI,2BAAa;AAEnC,SAAO,OAAuC,CAAC;AAC/C,SAAO,OAAsD,CAAC;AAE9D,SAAO,OAA0C,CAAC;AAElD,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;AAC5C,UAAI,OAAO,MAAM;AAAE,aAAK,OAAO,OAAO;AAAA,MAAM;AAC5C,UAAI,OAAO,MAAM;AAAE,aAAK,OAAO,OAAO;AAAA,MAAM;AAAA,IAC9C;AAAA,EACF;AAAA,EAEO,UAAU,OAAe,UAAoC;AAChE,SAAK,SAAS,GAAG,OAAO,QAAQ;AAChC,WAAO;AAAA,EACX;AAAA,EAEO,YAAY,OAAe,UAAqB;AACnD,QAAI,UAAW;AACX,WAAK,SAAS,eAAe,OAAO,QAAQ;AAAA,IAEhD,OAAO;AACH,WAAK,SAAS,mBAAmB,KAAK;AAAA,IAC1C;AAEA,WAAO;AAAA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cAAc,KAAK,KAAK,YAAY,EAAE,IAAI,CAAC;AAAA,MACrD,OAAO;AACL,eAAO;AAAA,MACT;AAAA,IACF;AAEA,UAAM,iBAAiB,kBAAkB;AAEzC,QAAI,gBAAgB;AAElB,aAAO,QAAQ,QAAQ,cAAc;AAAA,IAEvC,OAAO;AAEL,YAAM,aAAa,mBAAmB;AAEtC,UAAI,QAAQ;AACZ,aAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B,cAAM,WAAW,YAAY,MAAM;AACjC;AAEA,gBAAMC,kBAAiB,kBAAkB;AACzC,cAAIA,iBAAgB;AAClB,0BAAc,QAAQ;AACtB,mBAAO,QAAQA,eAAc;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,16 +1,16 @@
|
|
|
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 = {};
|
|
11
12
|
this.hash = {};
|
|
12
13
|
this.keys = {};
|
|
13
|
-
this.subscriptions = {};
|
|
14
14
|
this.timeouts = {};
|
|
15
15
|
if (isDevMode && fs.existsSync(DEVMODE_CACHE_FILE_PATH)) {
|
|
16
16
|
const cache = fs.readFileSync(DEVMODE_CACHE_FILE_PATH).toString("utf-8") || "{}";
|
|
@@ -27,30 +27,14 @@ class LocalPresence {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
subscribe(topic, callback) {
|
|
30
|
-
if (!this.subscriptions[topic]) {
|
|
31
|
-
this.subscriptions[topic] = [];
|
|
32
|
-
}
|
|
33
|
-
this.subscriptions[topic].push(callback);
|
|
34
30
|
this.channels.on(topic, callback);
|
|
35
31
|
return this;
|
|
36
32
|
}
|
|
37
33
|
unsubscribe(topic, callback) {
|
|
38
|
-
const topicCallbacks = this.subscriptions[topic];
|
|
39
|
-
if (!topicCallbacks) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
34
|
if (callback) {
|
|
43
|
-
|
|
44
|
-
if (idx !== -1) {
|
|
45
|
-
topicCallbacks.splice(idx, 1);
|
|
46
|
-
this.channels.removeListener(topic, callback);
|
|
47
|
-
}
|
|
48
|
-
if (topicCallbacks.length === 0) {
|
|
49
|
-
delete this.subscriptions[topic];
|
|
50
|
-
}
|
|
35
|
+
this.channels.removeListener(topic, callback);
|
|
51
36
|
} else {
|
|
52
|
-
|
|
53
|
-
delete this.subscriptions[topic];
|
|
37
|
+
this.channels.removeAllListeners(topic);
|
|
54
38
|
}
|
|
55
39
|
return this;
|
|
56
40
|
}
|
|
@@ -58,17 +42,20 @@ class LocalPresence {
|
|
|
58
42
|
this.channels.emit(topic, data);
|
|
59
43
|
return this;
|
|
60
44
|
}
|
|
61
|
-
async exists(
|
|
62
|
-
return this.
|
|
45
|
+
async exists(key) {
|
|
46
|
+
return this.keys[key] !== void 0 || this.data[key] !== void 0 || this.hash[key] !== void 0;
|
|
63
47
|
}
|
|
64
48
|
set(key, value) {
|
|
65
49
|
this.keys[key] = value;
|
|
66
50
|
}
|
|
67
51
|
setex(key, value, seconds) {
|
|
52
|
+
this.keys[key] = value;
|
|
53
|
+
this.expire(key, seconds);
|
|
54
|
+
}
|
|
55
|
+
expire(key, seconds) {
|
|
68
56
|
if (this.timeouts[key]) {
|
|
69
57
|
clearTimeout(this.timeouts[key]);
|
|
70
58
|
}
|
|
71
|
-
this.keys[key] = value;
|
|
72
59
|
this.timeouts[key] = setTimeout(() => {
|
|
73
60
|
delete this.keys[key];
|
|
74
61
|
delete this.timeouts[key];
|
|
@@ -136,6 +123,16 @@ class LocalPresence {
|
|
|
136
123
|
this.hash[key][field] = value.toString();
|
|
137
124
|
return value;
|
|
138
125
|
}
|
|
126
|
+
hincrbyex(key, field, incrBy, expireInSeconds) {
|
|
127
|
+
if (!this.hash[key]) {
|
|
128
|
+
this.hash[key] = {};
|
|
129
|
+
}
|
|
130
|
+
let value = Number(this.hash[key][field] || "0");
|
|
131
|
+
value += incrBy;
|
|
132
|
+
this.hash[key][field] = value.toString();
|
|
133
|
+
this.setex(key, field, expireInSeconds);
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
139
136
|
async hget(key, field) {
|
|
140
137
|
return this.hash[key] && this.hash[key][field];
|
|
141
138
|
}
|
|
@@ -166,6 +163,67 @@ class LocalPresence {
|
|
|
166
163
|
this.keys[key]--;
|
|
167
164
|
return Promise.resolve(this.keys[key]);
|
|
168
165
|
}
|
|
166
|
+
llen(key) {
|
|
167
|
+
return this.data[key] && this.data[key].length || 0;
|
|
168
|
+
}
|
|
169
|
+
rpush(key, ...values) {
|
|
170
|
+
if (!this.data[key]) {
|
|
171
|
+
this.data[key] = [];
|
|
172
|
+
}
|
|
173
|
+
let lastLength = 0;
|
|
174
|
+
values.forEach((value) => {
|
|
175
|
+
lastLength = this.data[key].push(value);
|
|
176
|
+
});
|
|
177
|
+
return lastLength;
|
|
178
|
+
}
|
|
179
|
+
lpush(key, ...values) {
|
|
180
|
+
if (!this.data[key]) {
|
|
181
|
+
this.data[key] = [];
|
|
182
|
+
}
|
|
183
|
+
let lastLength = 0;
|
|
184
|
+
values.forEach((value) => {
|
|
185
|
+
lastLength = this.data[key].unshift(value);
|
|
186
|
+
});
|
|
187
|
+
return lastLength;
|
|
188
|
+
}
|
|
189
|
+
lpop(key) {
|
|
190
|
+
return Array.isArray(this.data[key]) && this.data[key].shift();
|
|
191
|
+
}
|
|
192
|
+
rpop(key) {
|
|
193
|
+
return this.data[key].pop();
|
|
194
|
+
}
|
|
195
|
+
brpop(...args) {
|
|
196
|
+
const keys = args.slice(0, -2);
|
|
197
|
+
const timeoutInSeconds = args[args.length - 1];
|
|
198
|
+
const getFirstPopulated = () => {
|
|
199
|
+
const keyWithValue = keys.find((key) => this.data[key] && this.data[key].length > 0);
|
|
200
|
+
if (keyWithValue) {
|
|
201
|
+
return [keyWithValue, this.data[keyWithValue].pop()];
|
|
202
|
+
} else {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
const firstPopulated = getFirstPopulated();
|
|
207
|
+
if (firstPopulated) {
|
|
208
|
+
return Promise.resolve(firstPopulated);
|
|
209
|
+
} else {
|
|
210
|
+
const maxRetries = timeoutInSeconds * 8;
|
|
211
|
+
let tries = 0;
|
|
212
|
+
return new Promise((resolve) => {
|
|
213
|
+
const interval = setInterval(() => {
|
|
214
|
+
tries++;
|
|
215
|
+
const firstPopulated2 = getFirstPopulated();
|
|
216
|
+
if (firstPopulated2) {
|
|
217
|
+
clearInterval(interval);
|
|
218
|
+
return resolve(firstPopulated2);
|
|
219
|
+
} else if (tries >= maxRetries) {
|
|
220
|
+
clearInterval(interval);
|
|
221
|
+
return resolve(void 0);
|
|
222
|
+
}
|
|
223
|
+
}, timeoutInSeconds * 1e3 / maxRetries);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
169
227
|
shutdown() {
|
|
170
228
|
if (isDevMode) {
|
|
171
229
|
const cache = JSON.stringify({
|
|
@@ -176,7 +234,7 @@ class LocalPresence {
|
|
|
176
234
|
fs.writeFileSync(DEVMODE_CACHE_FILE_PATH, cache, { encoding: "utf-8" });
|
|
177
235
|
}
|
|
178
236
|
}
|
|
179
|
-
}
|
|
237
|
+
};
|
|
180
238
|
export {
|
|
181
239
|
LocalPresence
|
|
182
240
|
};
|