lemon-model 1.5.1 → 5.0.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 +23 -0
- package/dist/esm/fields/index.js +108 -0
- package/dist/esm/fields/index.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/core-storage.js +87 -17
- package/dist/esm/types/core-storage.js.map +1 -1
- package/dist/fields/index.d.ts +55 -0
- package/dist/fields/index.js +114 -0
- package/dist/fields/index.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/types/core-storage.d.ts +12 -13
- package/dist/types/core-storage.js +90 -19
- package/dist/types/core-storage.js.map +1 -1
- package/package.json +1 -1
- package/dist/esm/sync/network.js +0 -273
- package/dist/esm/sync/network.js.map +0 -1
- package/dist/socket/client/index.d.ts +0 -9
- package/dist/socket/client/index.js +0 -26
- package/dist/socket/client/index.js.map +0 -1
- package/dist/socket/client/keep-alive-controller.d.ts +0 -35
- package/dist/socket/client/keep-alive-controller.js +0 -101
- package/dist/socket/client/keep-alive-controller.js.map +0 -1
- package/dist/socket/client/message-router.d.ts +0 -15
- package/dist/socket/client/message-router.js +0 -52
- package/dist/socket/client/message-router.js.map +0 -1
- package/dist/socket/client/pending-request-store.d.ts +0 -24
- package/dist/socket/client/pending-request-store.js +0 -78
- package/dist/socket/client/pending-request-store.js.map +0 -1
- package/dist/socket/client/reconnect-controller.d.ts +0 -56
- package/dist/socket/client/reconnect-controller.js +0 -177
- package/dist/socket/client/reconnect-controller.js.map +0 -1
- package/dist/socket/client/rotation-controller.d.ts +0 -32
- package/dist/socket/client/rotation-controller.js +0 -76
- package/dist/socket/client/rotation-controller.js.map +0 -1
- package/dist/socket/client/shared-timer-scheduler.d.ts +0 -17
- package/dist/socket/client/shared-timer-scheduler.js +0 -77
- package/dist/socket/client/shared-timer-scheduler.js.map +0 -1
- package/dist/socket/client/socket-client.d.ts +0 -2
- package/dist/socket/client/socket-client.js +0 -219
- package/dist/socket/client/socket-client.js.map +0 -1
- package/dist/socket/client/types.d.ts +0 -101
- package/dist/socket/client/types.js +0 -3
- package/dist/socket/client/types.js.map +0 -1
- package/dist/socket/client/websocket-client-transport.d.ts +0 -23
- package/dist/socket/client/websocket-client-transport.js +0 -152
- package/dist/socket/client/websocket-client-transport.js.map +0 -1
- package/dist/sync/network.d.ts +0 -38
- package/dist/sync/network.js +0 -278
- package/dist/sync/network.js.map +0 -1
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PendingRequestStore = void 0;
|
|
4
|
-
class PendingRequestStore {
|
|
5
|
-
constructor(options = {}) {
|
|
6
|
-
var _a;
|
|
7
|
-
this.items = new Map();
|
|
8
|
-
this.timerScheduler = options.timerScheduler;
|
|
9
|
-
this.timerPrefix = (_a = options.timerPrefix) !== null && _a !== void 0 ? _a : 'pending:';
|
|
10
|
-
}
|
|
11
|
-
size() {
|
|
12
|
-
return this.items.size;
|
|
13
|
-
}
|
|
14
|
-
has(mid) {
|
|
15
|
-
return this.items.has(mid);
|
|
16
|
-
}
|
|
17
|
-
create(mid, options = {}) {
|
|
18
|
-
if (!mid)
|
|
19
|
-
throw new Error(`@mid (string) is required - PendingRequestStore.create()`);
|
|
20
|
-
if (this.items.has(mid))
|
|
21
|
-
throw new Error(`409 CONFLICT - pending request[${mid}] already exists`);
|
|
22
|
-
return new Promise((resolve, reject) => {
|
|
23
|
-
const item = {
|
|
24
|
-
resolve,
|
|
25
|
-
reject,
|
|
26
|
-
timer: options.timeoutMs && options.timeoutMs > 0 && !this.timerScheduler
|
|
27
|
-
? setTimeout(() => this.timeout(mid, options), options.timeoutMs)
|
|
28
|
-
: undefined,
|
|
29
|
-
};
|
|
30
|
-
this.items.set(mid, item);
|
|
31
|
-
if (options.timeoutMs && options.timeoutMs > 0 && this.timerScheduler) {
|
|
32
|
-
this.timerScheduler.schedule(this.toTimerKey(mid), options.timeoutMs, () => this.timeout(mid, options));
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
resolve(mid, value) {
|
|
37
|
-
const item = this.items.get(mid);
|
|
38
|
-
if (!item)
|
|
39
|
-
return false;
|
|
40
|
-
this.remove(mid, item);
|
|
41
|
-
item.resolve(value);
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
reject(mid, error) {
|
|
45
|
-
const item = this.items.get(mid);
|
|
46
|
-
if (!item)
|
|
47
|
-
return false;
|
|
48
|
-
this.remove(mid, item);
|
|
49
|
-
item.reject(error);
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
clear(reason = new Error(`499 CLIENT CLOSED REQUEST`)) {
|
|
53
|
-
const mids = [...this.items.keys()];
|
|
54
|
-
mids.forEach(mid => this.reject(mid, reason));
|
|
55
|
-
return mids.length;
|
|
56
|
-
}
|
|
57
|
-
timeout(mid, options) {
|
|
58
|
-
var _a, _b;
|
|
59
|
-
const item = this.items.get(mid);
|
|
60
|
-
if (!item)
|
|
61
|
-
return;
|
|
62
|
-
this.items.delete(mid);
|
|
63
|
-
const error = (_b = (_a = options.onTimeout) === null || _a === void 0 ? void 0 : _a.call(options, mid)) !== null && _b !== void 0 ? _b : new Error(`408 REQUEST TIMEOUT - pending[${mid}]`);
|
|
64
|
-
item.reject(error);
|
|
65
|
-
}
|
|
66
|
-
remove(mid, item) {
|
|
67
|
-
var _a;
|
|
68
|
-
this.items.delete(mid);
|
|
69
|
-
(_a = this.timerScheduler) === null || _a === void 0 ? void 0 : _a.cancel(this.toTimerKey(mid));
|
|
70
|
-
if (item.timer)
|
|
71
|
-
clearTimeout(item.timer);
|
|
72
|
-
}
|
|
73
|
-
toTimerKey(mid) {
|
|
74
|
-
return `${this.timerPrefix}${mid}`;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
exports.PendingRequestStore = PendingRequestStore;
|
|
78
|
-
//# sourceMappingURL=pending-request-store.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pending-request-store.js","sourceRoot":"","sources":["../../../src/socket/client/pending-request-store.ts"],"names":[],"mappings":";;;AAkBA,MAAa,mBAAmB;IAK5B,YAAmB,UAAsC,EAAE;;QAJ1C,UAAK,GAAG,IAAI,GAAG,EAA0B,CAAC;QAKvD,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;QAC7C,IAAI,CAAC,WAAW,GAAG,MAAA,OAAO,CAAC,WAAW,mCAAI,UAAU,CAAC;IACzD,CAAC;IAEM,IAAI;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;IAC3B,CAAC;IAEM,GAAG,CAAC,GAAW;QAClB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC/B,CAAC;IAEM,MAAM,CAAc,GAAW,EAAE,UAAuC,EAAE;QAC7E,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;QACtF,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,GAAG,kBAAkB,CAAC,CAAC;QAElG,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACtC,MAAM,IAAI,GAAsB;gBAC5B,OAAO;gBACP,MAAM;gBACN,KAAK,EACD,OAAO,CAAC,SAAS,IAAI,OAAO,CAAC,SAAS,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc;oBAC9D,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC;oBACjE,CAAC,CAAC,SAAS;aACtB,CAAC;YACF,IAAI,CAAC,KAAK,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CAAC,CAAC,CAAC;QAC9C,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAEO,OAAO,CAAC,GAAW,EAAE,OAAoC;;QAC7D,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI;YAAE,OAAO;QAClB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACvB,MAAM,KAAK,GAAG,MAAA,MAAA,OAAO,CAAC,SAAS,wDAAG,GAAG,CAAC,mCAAI,IAAI,KAAK,CAAC,iCAAiC,GAAG,GAAG,CAAC,CAAC;QAC7F,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAEO,MAAM,CAAC,GAAW,EAAE,IAAoB;;QAC5C,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACvB,MAAA,IAAI,CAAC,cAAc,0CAAE,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;QAClD,IAAI,IAAI,CAAC,KAAK;YAAE,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7C,CAAC;IAEO,UAAU,CAAC,GAAW;QAC1B,OAAO,GAAG,IAAI,CAAC,WAAW,GAAG,GAAG,EAAE,CAAC;IACvC,CAAC;CACJ;AA7ED,kDA6EC"}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type { SharedTimerScheduler, SocketRuntimePort, SocketUnsubscribe } from './types';
|
|
2
|
-
export interface ReconnectGiveUpEvent {
|
|
3
|
-
attempts: number;
|
|
4
|
-
}
|
|
5
|
-
export interface ReconnectConnectFailedEvent {
|
|
6
|
-
attempt: number;
|
|
7
|
-
error: unknown;
|
|
8
|
-
}
|
|
9
|
-
export interface ReconnectControllerOptions<TMessage> {
|
|
10
|
-
client: SocketRuntimePort<TMessage>;
|
|
11
|
-
minDelayMs?: number;
|
|
12
|
-
maxDelayMs?: number;
|
|
13
|
-
factor?: number;
|
|
14
|
-
jitterRatio?: number;
|
|
15
|
-
jitterMode?: 'equal' | 'full';
|
|
16
|
-
maxAttempts?: number;
|
|
17
|
-
minStableMs?: number;
|
|
18
|
-
timerScheduler?: SharedTimerScheduler;
|
|
19
|
-
timerKey?: string;
|
|
20
|
-
now?: () => number;
|
|
21
|
-
}
|
|
22
|
-
export declare class ReconnectController<TMessage = unknown> {
|
|
23
|
-
private readonly client;
|
|
24
|
-
private readonly minDelayMs;
|
|
25
|
-
private readonly maxDelayMs;
|
|
26
|
-
private readonly factor;
|
|
27
|
-
private readonly jitterRatio;
|
|
28
|
-
private readonly jitterMode;
|
|
29
|
-
private readonly maxAttempts;
|
|
30
|
-
private readonly minStableMs;
|
|
31
|
-
private readonly timerScheduler;
|
|
32
|
-
private readonly timerKey;
|
|
33
|
-
private readonly now;
|
|
34
|
-
private readonly unsubs;
|
|
35
|
-
private readonly giveUpListeners;
|
|
36
|
-
private readonly connectFailedListeners;
|
|
37
|
-
private active;
|
|
38
|
-
private manualStop;
|
|
39
|
-
private restarting;
|
|
40
|
-
private connecting?;
|
|
41
|
-
private attempt;
|
|
42
|
-
private stableSince?;
|
|
43
|
-
constructor(options: ReconnectControllerOptions<TMessage>);
|
|
44
|
-
start(): Promise<void>;
|
|
45
|
-
stop(code?: number, reason?: string): Promise<void>;
|
|
46
|
-
restart(): Promise<void>;
|
|
47
|
-
onGiveUp(listener: (event: ReconnectGiveUpEvent) => void): SocketUnsubscribe;
|
|
48
|
-
onConnectFailed(listener: (event: ReconnectConnectFailedEvent) => void): SocketUnsubscribe;
|
|
49
|
-
destroy(): void;
|
|
50
|
-
private applyStableReset;
|
|
51
|
-
private scheduleReconnect;
|
|
52
|
-
private tryConnect;
|
|
53
|
-
private giveUp;
|
|
54
|
-
private clearTimer;
|
|
55
|
-
private computeBackoffDelay;
|
|
56
|
-
}
|
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.ReconnectController = void 0;
|
|
13
|
-
const shared_timer_scheduler_1 = require("./shared-timer-scheduler");
|
|
14
|
-
class ReconnectController {
|
|
15
|
-
constructor(options) {
|
|
16
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
17
|
-
this.unsubs = [];
|
|
18
|
-
this.giveUpListeners = new Set();
|
|
19
|
-
this.connectFailedListeners = new Set();
|
|
20
|
-
this.active = false;
|
|
21
|
-
this.manualStop = false;
|
|
22
|
-
this.restarting = false;
|
|
23
|
-
this.attempt = 0;
|
|
24
|
-
this.client = options.client;
|
|
25
|
-
this.minDelayMs = (_a = options.minDelayMs) !== null && _a !== void 0 ? _a : 500;
|
|
26
|
-
this.maxDelayMs = (_b = options.maxDelayMs) !== null && _b !== void 0 ? _b : 30000;
|
|
27
|
-
this.factor = (_c = options.factor) !== null && _c !== void 0 ? _c : 2;
|
|
28
|
-
this.jitterRatio = Math.max(0, Math.min(1, (_d = options.jitterRatio) !== null && _d !== void 0 ? _d : 0.3));
|
|
29
|
-
this.jitterMode = (_e = options.jitterMode) !== null && _e !== void 0 ? _e : 'equal';
|
|
30
|
-
this.maxAttempts = Math.max(0, (_f = options.maxAttempts) !== null && _f !== void 0 ? _f : 0);
|
|
31
|
-
this.minStableMs = Math.max(0, (_g = options.minStableMs) !== null && _g !== void 0 ? _g : 5000);
|
|
32
|
-
this.timerScheduler = (_h = options.timerScheduler) !== null && _h !== void 0 ? _h : new shared_timer_scheduler_1.InMemorySharedTimerScheduler();
|
|
33
|
-
this.timerKey = (_j = options.timerKey) !== null && _j !== void 0 ? _j : 'reconnect';
|
|
34
|
-
this.now = (_k = options.now) !== null && _k !== void 0 ? _k : (() => Date.now());
|
|
35
|
-
this.unsubs.push(options.client.onState(event => {
|
|
36
|
-
if (!this.active)
|
|
37
|
-
return;
|
|
38
|
-
if (event.next === 'connected') {
|
|
39
|
-
this.stableSince = this.now();
|
|
40
|
-
this.clearTimer();
|
|
41
|
-
this.connecting = undefined;
|
|
42
|
-
}
|
|
43
|
-
if (event.next === 'closed' && !this.manualStop && !this.restarting) {
|
|
44
|
-
this.applyStableReset();
|
|
45
|
-
this.scheduleReconnect();
|
|
46
|
-
}
|
|
47
|
-
}));
|
|
48
|
-
}
|
|
49
|
-
start() {
|
|
50
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
this.active = true;
|
|
52
|
-
this.manualStop = false;
|
|
53
|
-
this.clearTimer();
|
|
54
|
-
this.attempt = 0;
|
|
55
|
-
this.stableSince = this.client.state === 'connected' ? this.now() : undefined;
|
|
56
|
-
if (this.client.state !== 'connected')
|
|
57
|
-
yield this.tryConnect();
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
stop(code, reason) {
|
|
61
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
-
this.active = false;
|
|
63
|
-
this.manualStop = true;
|
|
64
|
-
this.restarting = false;
|
|
65
|
-
this.clearTimer();
|
|
66
|
-
this.attempt = 0;
|
|
67
|
-
this.stableSince = undefined;
|
|
68
|
-
yield this.client.disconnect(code, reason);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
restart() {
|
|
72
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
-
if (!this.active) {
|
|
74
|
-
yield this.start();
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
this.clearTimer();
|
|
78
|
-
this.attempt = 0;
|
|
79
|
-
this.stableSince = undefined;
|
|
80
|
-
this.restarting = true;
|
|
81
|
-
try {
|
|
82
|
-
if (this.client.state === 'connected' || this.client.state === 'connecting') {
|
|
83
|
-
yield this.client.disconnect(1012, 'service-restart');
|
|
84
|
-
}
|
|
85
|
-
yield this.tryConnect();
|
|
86
|
-
}
|
|
87
|
-
finally {
|
|
88
|
-
this.restarting = false;
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
onGiveUp(listener) {
|
|
93
|
-
this.giveUpListeners.add(listener);
|
|
94
|
-
return () => this.giveUpListeners.delete(listener);
|
|
95
|
-
}
|
|
96
|
-
onConnectFailed(listener) {
|
|
97
|
-
this.connectFailedListeners.add(listener);
|
|
98
|
-
return () => this.connectFailedListeners.delete(listener);
|
|
99
|
-
}
|
|
100
|
-
destroy() {
|
|
101
|
-
this.clearTimer();
|
|
102
|
-
this.giveUpListeners.clear();
|
|
103
|
-
this.connectFailedListeners.clear();
|
|
104
|
-
this.unsubs.splice(0).forEach(unsub => unsub());
|
|
105
|
-
}
|
|
106
|
-
applyStableReset() {
|
|
107
|
-
if (this.stableSince === undefined)
|
|
108
|
-
return;
|
|
109
|
-
const stableMs = this.now() - this.stableSince;
|
|
110
|
-
this.stableSince = undefined;
|
|
111
|
-
if (stableMs >= this.minStableMs)
|
|
112
|
-
this.attempt = 0;
|
|
113
|
-
}
|
|
114
|
-
scheduleReconnect() {
|
|
115
|
-
if (!this.active || this.manualStop || this.connecting)
|
|
116
|
-
return;
|
|
117
|
-
if (this.maxAttempts > 0 && this.attempt >= this.maxAttempts) {
|
|
118
|
-
this.giveUp();
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
const delayMs = this.computeBackoffDelay(this.attempt);
|
|
122
|
-
this.timerScheduler.schedule(this.timerKey, delayMs, () => {
|
|
123
|
-
void this.tryConnect();
|
|
124
|
-
});
|
|
125
|
-
this.attempt += 1;
|
|
126
|
-
}
|
|
127
|
-
tryConnect() {
|
|
128
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
-
if (!this.active || this.manualStop)
|
|
130
|
-
return;
|
|
131
|
-
if (this.connecting)
|
|
132
|
-
return this.connecting;
|
|
133
|
-
const attempt = this.attempt;
|
|
134
|
-
let resolveConnecting;
|
|
135
|
-
this.connecting = new Promise(resolve => {
|
|
136
|
-
resolveConnecting = resolve;
|
|
137
|
-
});
|
|
138
|
-
let connectPromise;
|
|
139
|
-
try {
|
|
140
|
-
connectPromise = Promise.resolve(this.client.connect());
|
|
141
|
-
}
|
|
142
|
-
catch (error) {
|
|
143
|
-
connectPromise = Promise.reject(error);
|
|
144
|
-
}
|
|
145
|
-
connectPromise.then(() => {
|
|
146
|
-
this.connecting = undefined;
|
|
147
|
-
resolveConnecting();
|
|
148
|
-
}, error => {
|
|
149
|
-
this.connecting = undefined;
|
|
150
|
-
this.connectFailedListeners.forEach(listener => listener({ attempt, error }));
|
|
151
|
-
this.scheduleReconnect();
|
|
152
|
-
resolveConnecting();
|
|
153
|
-
});
|
|
154
|
-
return this.connecting;
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
giveUp() {
|
|
158
|
-
const attempts = this.attempt;
|
|
159
|
-
this.active = false;
|
|
160
|
-
this.clearTimer();
|
|
161
|
-
this.giveUpListeners.forEach(listener => listener({ attempts }));
|
|
162
|
-
}
|
|
163
|
-
clearTimer() {
|
|
164
|
-
this.timerScheduler.cancel(this.timerKey);
|
|
165
|
-
}
|
|
166
|
-
computeBackoffDelay(attempt) {
|
|
167
|
-
const expDelay = Math.min(this.minDelayMs * Math.pow(this.factor, attempt), this.maxDelayMs);
|
|
168
|
-
if (this.jitterMode === 'full') {
|
|
169
|
-
const delay = Math.random() * expDelay;
|
|
170
|
-
return Math.max(this.minDelayMs, Math.min(this.maxDelayMs, delay));
|
|
171
|
-
}
|
|
172
|
-
const jitter = this.jitterRatio > 0 ? expDelay * (Math.random() * 2 - 1) * this.jitterRatio : 0;
|
|
173
|
-
return Math.max(this.minDelayMs, Math.min(this.maxDelayMs, expDelay + jitter));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
exports.ReconnectController = ReconnectController;
|
|
177
|
-
//# sourceMappingURL=reconnect-controller.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"reconnect-controller.js","sourceRoot":"","sources":["../../../src/socket/client/reconnect-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qEAAwE;AA0BxE,MAAa,mBAAmB;IAsB5B,YAAmB,OAA6C;;QAV/C,WAAM,GAAwB,EAAE,CAAC;QACjC,oBAAe,GAAG,IAAI,GAAG,EAAyC,CAAC;QACnE,2BAAsB,GAAG,IAAI,GAAG,EAAgD,CAAC;QAC1F,WAAM,GAAG,KAAK,CAAC;QACf,eAAU,GAAG,KAAK,CAAC;QACnB,eAAU,GAAG,KAAK,CAAC;QAEnB,YAAO,GAAG,CAAC,CAAC;QAIhB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,CAAC,UAAU,mCAAI,GAAG,CAAC;QAC5C,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,CAAC,UAAU,mCAAI,KAAK,CAAC;QAC9C,IAAI,CAAC,MAAM,GAAG,MAAA,OAAO,CAAC,MAAM,mCAAI,CAAC,CAAC;QAClC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAA,OAAO,CAAC,WAAW,mCAAI,GAAG,CAAC,CAAC,CAAC;QACxE,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,CAAC,UAAU,mCAAI,OAAO,CAAC;QAChD,IAAI,CAAC,WAAW,GAAG,IAAI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,UAAU,EAAE,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC;IACnF,CAAC;CACJ;AA9KD,kDA8KC"}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { SharedTimerScheduler, SocketRuntimePort } from './types';
|
|
2
|
-
export interface RotationReconnectControl {
|
|
3
|
-
restart(): Promise<void>;
|
|
4
|
-
}
|
|
5
|
-
export interface RotationControllerOptions<TMessage> {
|
|
6
|
-
client: SocketRuntimePort<TMessage>;
|
|
7
|
-
reconnect?: RotationReconnectControl;
|
|
8
|
-
maxLifetimeMs?: number;
|
|
9
|
-
refreshBeforeMs?: number;
|
|
10
|
-
jitterRatio?: number;
|
|
11
|
-
timerScheduler?: SharedTimerScheduler;
|
|
12
|
-
timerKey?: string;
|
|
13
|
-
}
|
|
14
|
-
export declare class RotationController<TMessage = unknown> {
|
|
15
|
-
private readonly client;
|
|
16
|
-
private readonly reconnect;
|
|
17
|
-
private readonly maxLifetimeMs;
|
|
18
|
-
private readonly refreshBeforeMs;
|
|
19
|
-
private readonly jitterRatio;
|
|
20
|
-
private readonly timerScheduler;
|
|
21
|
-
private readonly timerKey;
|
|
22
|
-
private readonly unsubs;
|
|
23
|
-
private active;
|
|
24
|
-
constructor(options: RotationControllerOptions<TMessage>);
|
|
25
|
-
start(): void;
|
|
26
|
-
stop(): void;
|
|
27
|
-
destroy(): void;
|
|
28
|
-
private scheduleRotation;
|
|
29
|
-
private rotate;
|
|
30
|
-
private clearTimer;
|
|
31
|
-
private computeRotationDelay;
|
|
32
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.RotationController = void 0;
|
|
13
|
-
const shared_timer_scheduler_1 = require("./shared-timer-scheduler");
|
|
14
|
-
class RotationController {
|
|
15
|
-
constructor(options) {
|
|
16
|
-
var _a, _b, _c, _d, _e;
|
|
17
|
-
this.unsubs = [];
|
|
18
|
-
this.active = false;
|
|
19
|
-
this.client = options.client;
|
|
20
|
-
this.reconnect = options.reconnect;
|
|
21
|
-
this.maxLifetimeMs = (_a = options.maxLifetimeMs) !== null && _a !== void 0 ? _a : 1000 * 60 * 110;
|
|
22
|
-
this.refreshBeforeMs = (_b = options.refreshBeforeMs) !== null && _b !== void 0 ? _b : 1000 * 60 * 10;
|
|
23
|
-
this.jitterRatio = Math.max(0, Math.min(1, (_c = options.jitterRatio) !== null && _c !== void 0 ? _c : 0.1));
|
|
24
|
-
this.timerScheduler = (_d = options.timerScheduler) !== null && _d !== void 0 ? _d : new shared_timer_scheduler_1.InMemorySharedTimerScheduler();
|
|
25
|
-
this.timerKey = (_e = options.timerKey) !== null && _e !== void 0 ? _e : 'rotation';
|
|
26
|
-
this.unsubs.push(options.client.onState(event => {
|
|
27
|
-
if (!this.active)
|
|
28
|
-
return;
|
|
29
|
-
if (event.next === 'connected')
|
|
30
|
-
this.scheduleRotation();
|
|
31
|
-
if (event.next === 'closing' || event.next === 'closed')
|
|
32
|
-
this.clearTimer();
|
|
33
|
-
}));
|
|
34
|
-
}
|
|
35
|
-
start() {
|
|
36
|
-
this.active = true;
|
|
37
|
-
if (this.client.state === 'connected')
|
|
38
|
-
this.scheduleRotation();
|
|
39
|
-
}
|
|
40
|
-
stop() {
|
|
41
|
-
this.active = false;
|
|
42
|
-
this.clearTimer();
|
|
43
|
-
}
|
|
44
|
-
destroy() {
|
|
45
|
-
this.stop();
|
|
46
|
-
this.unsubs.splice(0).forEach(unsub => unsub());
|
|
47
|
-
}
|
|
48
|
-
scheduleRotation() {
|
|
49
|
-
this.clearTimer();
|
|
50
|
-
if (!this.active)
|
|
51
|
-
return;
|
|
52
|
-
this.timerScheduler.schedule(this.timerKey, this.computeRotationDelay(), () => this.rotate());
|
|
53
|
-
}
|
|
54
|
-
rotate() {
|
|
55
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
-
if (!this.active)
|
|
57
|
-
return;
|
|
58
|
-
if (this.reconnect) {
|
|
59
|
-
yield this.reconnect.restart();
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
yield this.client.disconnect(1012, 'service-restart');
|
|
63
|
-
yield this.client.connect();
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
clearTimer() {
|
|
67
|
-
this.timerScheduler.cancel(this.timerKey);
|
|
68
|
-
}
|
|
69
|
-
computeRotationDelay() {
|
|
70
|
-
const baseDelay = Math.max(0, this.maxLifetimeMs - this.refreshBeforeMs);
|
|
71
|
-
const jitter = this.jitterRatio > 0 ? baseDelay * (Math.random() * 2 - 1) * this.jitterRatio : 0;
|
|
72
|
-
return Math.max(0, baseDelay + jitter);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
exports.RotationController = RotationController;
|
|
76
|
-
//# sourceMappingURL=rotation-controller.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rotation-controller.js","sourceRoot":"","sources":["../../../src/socket/client/rotation-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qEAAwE;AAiBxE,MAAa,kBAAkB;IAW3B,YAAmB,OAA4C;;QAH9C,WAAM,GAAwB,EAAE,CAAC;QAC1C,WAAM,GAAG,KAAK,CAAC;QAGnB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC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GAAG,MAAM,CAAC,CAAC;IAC3C,CAAC;CACJ;AApED,gDAoEC"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { SharedTimerScheduler } from './types';
|
|
2
|
-
export interface SharedTimerSchedulerOptions {
|
|
3
|
-
now?: () => number;
|
|
4
|
-
}
|
|
5
|
-
export declare class InMemorySharedTimerScheduler implements SharedTimerScheduler {
|
|
6
|
-
private readonly tasks;
|
|
7
|
-
private readonly now;
|
|
8
|
-
private timer?;
|
|
9
|
-
constructor(options?: SharedTimerSchedulerOptions);
|
|
10
|
-
schedule(key: string, delayMs: number, task: () => unknown | Promise<unknown>): void;
|
|
11
|
-
cancel(key: string): void;
|
|
12
|
-
cancelAll(prefix?: string): void;
|
|
13
|
-
size(): number;
|
|
14
|
-
private rearm;
|
|
15
|
-
private getNextTask;
|
|
16
|
-
private flush;
|
|
17
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.InMemorySharedTimerScheduler = void 0;
|
|
13
|
-
class InMemorySharedTimerScheduler {
|
|
14
|
-
constructor(options = {}) {
|
|
15
|
-
var _a;
|
|
16
|
-
this.tasks = new Map();
|
|
17
|
-
this.now = (_a = options.now) !== null && _a !== void 0 ? _a : (() => Date.now());
|
|
18
|
-
}
|
|
19
|
-
schedule(key, delayMs, task) {
|
|
20
|
-
const safeDelayMs = Math.max(0, delayMs || 0);
|
|
21
|
-
this.tasks.set(key, {
|
|
22
|
-
key,
|
|
23
|
-
dueAt: this.now() + safeDelayMs,
|
|
24
|
-
task,
|
|
25
|
-
});
|
|
26
|
-
this.rearm();
|
|
27
|
-
}
|
|
28
|
-
cancel(key) {
|
|
29
|
-
this.tasks.delete(key);
|
|
30
|
-
this.rearm();
|
|
31
|
-
}
|
|
32
|
-
cancelAll(prefix) {
|
|
33
|
-
if (!prefix) {
|
|
34
|
-
this.tasks.clear();
|
|
35
|
-
this.rearm();
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
for (const key of [...this.tasks.keys()]) {
|
|
39
|
-
if (key.startsWith(prefix))
|
|
40
|
-
this.tasks.delete(key);
|
|
41
|
-
}
|
|
42
|
-
this.rearm();
|
|
43
|
-
}
|
|
44
|
-
size() {
|
|
45
|
-
return this.tasks.size;
|
|
46
|
-
}
|
|
47
|
-
rearm() {
|
|
48
|
-
if (this.timer)
|
|
49
|
-
clearTimeout(this.timer);
|
|
50
|
-
this.timer = undefined;
|
|
51
|
-
const next = this.getNextTask();
|
|
52
|
-
if (!next)
|
|
53
|
-
return;
|
|
54
|
-
const delayMs = Math.max(0, next.dueAt - this.now());
|
|
55
|
-
this.timer = setTimeout(() => void this.flush(), delayMs);
|
|
56
|
-
}
|
|
57
|
-
getNextTask() {
|
|
58
|
-
let next;
|
|
59
|
-
this.tasks.forEach(task => {
|
|
60
|
-
if (!next || task.dueAt < next.dueAt)
|
|
61
|
-
next = task;
|
|
62
|
-
});
|
|
63
|
-
return next;
|
|
64
|
-
}
|
|
65
|
-
flush() {
|
|
66
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
this.timer = undefined;
|
|
68
|
-
const now = this.now();
|
|
69
|
-
const due = [...this.tasks.values()].filter(task => task.dueAt <= now);
|
|
70
|
-
due.forEach(task => this.tasks.delete(task.key));
|
|
71
|
-
this.rearm();
|
|
72
|
-
yield Promise.all(due.map(task => Promise.resolve(task.task()).catch(() => undefined)));
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
exports.InMemorySharedTimerScheduler = InMemorySharedTimerScheduler;
|
|
77
|
-
//# sourceMappingURL=shared-timer-scheduler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"shared-timer-scheduler.js","sourceRoot":"","sources":["../../../src/socket/client/shared-timer-scheduler.ts"],"names":[],"mappings":";;;;;;;;;;;;AAYA,MAAa,4BAA4B;IAKrC,YAAmB,UAAuC,EAAE;;QAJ3C,UAAK,GAAG,IAAI,GAAG,EAAyB,CAAC;QAKtD,IAAI,CAAC,GAAG,GAAG,MAAA,OAAO,CAAC,GAAG,mCAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IACjD,CAAC;IAEM,QAAQ,CAAC,GAAW,EAAE,OAAe,EAAE,IAAsC;QAChF,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,CAAC;QAC9C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;YAChB,GAAG;YACH,KAAK,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,WAAW;YAC/B,IAAI;SACP,CAAC,CAAC;QACH,IAAI,CAAC,KAAK,EAAE,CAAC;IACjB,CAAC;IAEM,MAAM,CAAC,GAAW;QACrB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACvB,IAAI,CAAC,KAAK,EAAE,CAAC;IACjB,CAAC;IAEM,SAAS,CAAC,MAAe;QAC5B,IAAI,CAAC,MAAM,EAAE;YACT,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;YACnB,IAAI,CAAC,KAAK,EAAE,CAAC;YACb,OAAO;SACV;QACD,KA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