@sprucelabs/mercury-client 42.0.30 → 42.0.32
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.
|
@@ -13,6 +13,7 @@ export default class MercurySocketIoClient<Contract extends EventContract> imple
|
|
|
13
13
|
private proxyToken;
|
|
14
14
|
private emitTimeoutMs;
|
|
15
15
|
private reconnectDelayMs;
|
|
16
|
+
private listenerMap;
|
|
16
17
|
private isReAuthing;
|
|
17
18
|
private reconnectPromise;
|
|
18
19
|
protected lastAuthOptions?: {
|
|
@@ -53,6 +54,7 @@ export default class MercurySocketIoClient<Contract extends EventContract> imple
|
|
|
53
54
|
private reRegisterAllListeners;
|
|
54
55
|
private mapSocketErrorToSpruceError;
|
|
55
56
|
emit<Name extends EventName<Contract>, IEventSignature extends EventSignature = Contract['eventSignatures'][EventName], EmitSchema extends Schema = IEventSignature['emitPayloadSchema'] extends Schema ? IEventSignature['emitPayloadSchema'] : never, ResponseSchema extends Schema = IEventSignature['responsePayloadSchema'] extends Schema ? IEventSignature['responsePayloadSchema'] : never, ResponsePayload = ResponseSchema extends Schema ? SchemaValues<ResponseSchema> : never>(eventName: Name, targetAndPayload?: (EmitSchema extends Schema ? SchemaValues<EmitSchema> : never) | EmitCallback<Contract, Name>, cb?: EmitCallback<Contract, Name>): Promise<MercuryAggregateResponse<ResponsePayload>>;
|
|
57
|
+
private handleLocalEmit;
|
|
56
58
|
emitAndFlattenResponses<Name extends EventName<Contract>, IEventSignature extends EventSignature = Contract['eventSignatures'][EventName], EmitSchema extends Schema = IEventSignature['emitPayloadSchema'] extends Schema ? IEventSignature['emitPayloadSchema'] : never, ResponseSchema extends Schema = IEventSignature['responsePayloadSchema'] extends Schema ? IEventSignature['responsePayloadSchema'] : never, ResponsePayload = ResponseSchema extends Schema ? SchemaValues<ResponseSchema> : never>(eventName: Name, payload?: (EmitSchema extends Schema ? SchemaValues<EmitSchema> : never) | EmitCallback<Contract, Name>, cb?: EmitCallback<Contract, Name>): Promise<ResponsePayload[]>;
|
|
57
59
|
private _emit;
|
|
58
60
|
protected assertValidEmitTargetAndPayload<Name extends EventName<Contract>>(eventName: Name, payload: any): void;
|
|
@@ -61,7 +63,8 @@ export default class MercurySocketIoClient<Contract extends EventContract> imple
|
|
|
61
63
|
setShouldAutoRegisterListeners(should: boolean): void;
|
|
62
64
|
on<Name extends EventName<Contract>, IEventSignature extends EventSignature = Contract['eventSignatures'][EventName], EmitSchema extends Schema = IEventSignature['emitPayloadSchema'] extends Schema ? IEventSignature['emitPayloadSchema'] : never>(eventName: Name, cb: (payload: EmitSchema extends Schema ? SchemaValues<EmitSchema> : never) => IEventSignature['responsePayloadSchema'] extends Schema ? Promise<SchemaValues<IEventSignature['responsePayloadSchema']>> | SchemaValues<IEventSignature['responsePayloadSchema']> : Promise<void> | void): Promise<void>;
|
|
63
65
|
private isEventLocal;
|
|
64
|
-
off(eventName: EventName<Contract
|
|
66
|
+
off(eventName: EventName<Contract>, cb?: () => void): Promise<number>;
|
|
67
|
+
private removeLocalListener;
|
|
65
68
|
getId(): string;
|
|
66
69
|
disconnect(): Promise<void>;
|
|
67
70
|
authenticate(options: AuthenticateOptions): Promise<{
|
|
@@ -19,6 +19,7 @@ class MercurySocketIoClient {
|
|
|
19
19
|
}
|
|
20
20
|
constructor(options) {
|
|
21
21
|
this.proxyToken = null;
|
|
22
|
+
this.listenerMap = new WeakMap();
|
|
22
23
|
this.isReAuthing = false;
|
|
23
24
|
this.reconnectPromise = null;
|
|
24
25
|
this.connectionRetriesRemaining = 5;
|
|
@@ -231,23 +232,26 @@ class MercurySocketIoClient {
|
|
|
231
232
|
async emit(eventName, targetAndPayload, cb) {
|
|
232
233
|
const isLocalEvent = this.isEventLocal(eventName);
|
|
233
234
|
if (isLocalEvent) {
|
|
234
|
-
|
|
235
|
-
for (const listener of listeners) {
|
|
236
|
-
const cb = listener === null || listener === void 0 ? void 0 : listener[1];
|
|
237
|
-
cb === null || cb === void 0 ? void 0 : cb({
|
|
238
|
-
//@ts-ignore
|
|
239
|
-
payload: targetAndPayload === null || targetAndPayload === void 0 ? void 0 : targetAndPayload.payload,
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
return {
|
|
243
|
-
responses: [],
|
|
244
|
-
totalContracts: 0,
|
|
245
|
-
totalErrors: 0,
|
|
246
|
-
totalResponses: 0,
|
|
247
|
-
};
|
|
235
|
+
return this.handleLocalEmit(eventName, targetAndPayload);
|
|
248
236
|
}
|
|
249
237
|
return this._emit(this.maxEmitRetries, eventName, targetAndPayload, cb);
|
|
250
238
|
}
|
|
239
|
+
handleLocalEmit(eventName, targetAndPayload) {
|
|
240
|
+
const listeners = this.registeredListeners.filter((r) => r[0] === eventName);
|
|
241
|
+
for (const listener of listeners) {
|
|
242
|
+
const cb = listener === null || listener === void 0 ? void 0 : listener[1];
|
|
243
|
+
cb === null || cb === void 0 ? void 0 : cb({
|
|
244
|
+
//@ts-ignore
|
|
245
|
+
payload: targetAndPayload === null || targetAndPayload === void 0 ? void 0 : targetAndPayload.payload,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return {
|
|
249
|
+
responses: [],
|
|
250
|
+
totalContracts: 0,
|
|
251
|
+
totalErrors: 0,
|
|
252
|
+
totalResponses: 0,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
251
255
|
async emitAndFlattenResponses(eventName, payload, cb) {
|
|
252
256
|
const results = await this.emit(eventName, payload, cb);
|
|
253
257
|
const { payloads, errors } = spruce_event_utils_1.eventResponseUtil.getAllResponsePayloadsAndErrors(results, SpruceError_1.default);
|
|
@@ -418,9 +422,7 @@ class MercurySocketIoClient {
|
|
|
418
422
|
throw error_1.default.parse(options, SpruceError_1.default);
|
|
419
423
|
}
|
|
420
424
|
}
|
|
421
|
-
|
|
422
|
-
//@ts-ignore
|
|
423
|
-
async (targetAndPayload, ioCallback) => {
|
|
425
|
+
const listener = async (targetAndPayload, ioCallback) => {
|
|
424
426
|
if (cb) {
|
|
425
427
|
try {
|
|
426
428
|
const results = await cb(targetAndPayload);
|
|
@@ -444,12 +446,17 @@ class MercurySocketIoClient {
|
|
|
444
446
|
}
|
|
445
447
|
}
|
|
446
448
|
}
|
|
447
|
-
}
|
|
449
|
+
};
|
|
450
|
+
this.listenerMap.set(cb, listener);
|
|
451
|
+
(_c = this.socket) === null || _c === void 0 ? void 0 : _c.on(eventName,
|
|
452
|
+
//@ts-ignore
|
|
453
|
+
listener);
|
|
448
454
|
}
|
|
449
455
|
isEventLocal(eventName) {
|
|
450
456
|
return eventName === 'connection-status-change';
|
|
451
457
|
}
|
|
452
|
-
async off(eventName) {
|
|
458
|
+
async off(eventName, cb) {
|
|
459
|
+
this.removeLocalListener(cb, eventName);
|
|
453
460
|
return new Promise((resolve, reject) => {
|
|
454
461
|
var _a;
|
|
455
462
|
if (!this.socket || !this.auth || this.isEventLocal(eventName)) {
|
|
@@ -471,6 +478,17 @@ class MercurySocketIoClient {
|
|
|
471
478
|
});
|
|
472
479
|
});
|
|
473
480
|
}
|
|
481
|
+
removeLocalListener(cb, eventName) {
|
|
482
|
+
var _a, _b;
|
|
483
|
+
const listener = this.listenerMap.get(cb);
|
|
484
|
+
if (listener) {
|
|
485
|
+
this.listenerMap.delete(cb);
|
|
486
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.off(eventName, listener);
|
|
487
|
+
}
|
|
488
|
+
else {
|
|
489
|
+
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.removeAllListeners(eventName);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
474
492
|
getId() {
|
|
475
493
|
return this.id;
|
|
476
494
|
}
|
|
@@ -13,6 +13,7 @@ export default class MercurySocketIoClient<Contract extends EventContract> imple
|
|
|
13
13
|
private proxyToken;
|
|
14
14
|
private emitTimeoutMs;
|
|
15
15
|
private reconnectDelayMs;
|
|
16
|
+
private listenerMap;
|
|
16
17
|
private isReAuthing;
|
|
17
18
|
private reconnectPromise;
|
|
18
19
|
protected lastAuthOptions?: {
|
|
@@ -53,6 +54,7 @@ export default class MercurySocketIoClient<Contract extends EventContract> imple
|
|
|
53
54
|
private reRegisterAllListeners;
|
|
54
55
|
private mapSocketErrorToSpruceError;
|
|
55
56
|
emit<Name extends EventName<Contract>, IEventSignature extends EventSignature = Contract['eventSignatures'][EventName], EmitSchema extends Schema = IEventSignature['emitPayloadSchema'] extends Schema ? IEventSignature['emitPayloadSchema'] : never, ResponseSchema extends Schema = IEventSignature['responsePayloadSchema'] extends Schema ? IEventSignature['responsePayloadSchema'] : never, ResponsePayload = ResponseSchema extends Schema ? SchemaValues<ResponseSchema> : never>(eventName: Name, targetAndPayload?: (EmitSchema extends Schema ? SchemaValues<EmitSchema> : never) | EmitCallback<Contract, Name>, cb?: EmitCallback<Contract, Name>): Promise<MercuryAggregateResponse<ResponsePayload>>;
|
|
57
|
+
private handleLocalEmit;
|
|
56
58
|
emitAndFlattenResponses<Name extends EventName<Contract>, IEventSignature extends EventSignature = Contract['eventSignatures'][EventName], EmitSchema extends Schema = IEventSignature['emitPayloadSchema'] extends Schema ? IEventSignature['emitPayloadSchema'] : never, ResponseSchema extends Schema = IEventSignature['responsePayloadSchema'] extends Schema ? IEventSignature['responsePayloadSchema'] : never, ResponsePayload = ResponseSchema extends Schema ? SchemaValues<ResponseSchema> : never>(eventName: Name, payload?: (EmitSchema extends Schema ? SchemaValues<EmitSchema> : never) | EmitCallback<Contract, Name>, cb?: EmitCallback<Contract, Name>): Promise<ResponsePayload[]>;
|
|
57
59
|
private _emit;
|
|
58
60
|
protected assertValidEmitTargetAndPayload<Name extends EventName<Contract>>(eventName: Name, payload: any): void;
|
|
@@ -61,7 +63,8 @@ export default class MercurySocketIoClient<Contract extends EventContract> imple
|
|
|
61
63
|
setShouldAutoRegisterListeners(should: boolean): void;
|
|
62
64
|
on<Name extends EventName<Contract>, IEventSignature extends EventSignature = Contract['eventSignatures'][EventName], EmitSchema extends Schema = IEventSignature['emitPayloadSchema'] extends Schema ? IEventSignature['emitPayloadSchema'] : never>(eventName: Name, cb: (payload: EmitSchema extends Schema ? SchemaValues<EmitSchema> : never) => IEventSignature['responsePayloadSchema'] extends Schema ? Promise<SchemaValues<IEventSignature['responsePayloadSchema']>> | SchemaValues<IEventSignature['responsePayloadSchema']> : Promise<void> | void): Promise<void>;
|
|
63
65
|
private isEventLocal;
|
|
64
|
-
off(eventName: EventName<Contract
|
|
66
|
+
off(eventName: EventName<Contract>, cb?: () => void): Promise<number>;
|
|
67
|
+
private removeLocalListener;
|
|
65
68
|
getId(): string;
|
|
66
69
|
disconnect(): Promise<void>;
|
|
67
70
|
authenticate(options: AuthenticateOptions): Promise<{
|
|
@@ -33,6 +33,7 @@ class MercurySocketIoClient {
|
|
|
33
33
|
}
|
|
34
34
|
constructor(options) {
|
|
35
35
|
this.proxyToken = null;
|
|
36
|
+
this.listenerMap = new WeakMap();
|
|
36
37
|
this.isReAuthing = false;
|
|
37
38
|
this.reconnectPromise = null;
|
|
38
39
|
this.connectionRetriesRemaining = 5;
|
|
@@ -256,24 +257,27 @@ class MercurySocketIoClient {
|
|
|
256
257
|
return __awaiter(this, void 0, void 0, function* () {
|
|
257
258
|
const isLocalEvent = this.isEventLocal(eventName);
|
|
258
259
|
if (isLocalEvent) {
|
|
259
|
-
|
|
260
|
-
for (const listener of listeners) {
|
|
261
|
-
const cb = listener === null || listener === void 0 ? void 0 : listener[1];
|
|
262
|
-
cb === null || cb === void 0 ? void 0 : cb({
|
|
263
|
-
//@ts-ignore
|
|
264
|
-
payload: targetAndPayload === null || targetAndPayload === void 0 ? void 0 : targetAndPayload.payload,
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
return {
|
|
268
|
-
responses: [],
|
|
269
|
-
totalContracts: 0,
|
|
270
|
-
totalErrors: 0,
|
|
271
|
-
totalResponses: 0,
|
|
272
|
-
};
|
|
260
|
+
return this.handleLocalEmit(eventName, targetAndPayload);
|
|
273
261
|
}
|
|
274
262
|
return this._emit(this.maxEmitRetries, eventName, targetAndPayload, cb);
|
|
275
263
|
});
|
|
276
264
|
}
|
|
265
|
+
handleLocalEmit(eventName, targetAndPayload) {
|
|
266
|
+
const listeners = this.registeredListeners.filter((r) => r[0] === eventName);
|
|
267
|
+
for (const listener of listeners) {
|
|
268
|
+
const cb = listener === null || listener === void 0 ? void 0 : listener[1];
|
|
269
|
+
cb === null || cb === void 0 ? void 0 : cb({
|
|
270
|
+
//@ts-ignore
|
|
271
|
+
payload: targetAndPayload === null || targetAndPayload === void 0 ? void 0 : targetAndPayload.payload,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return {
|
|
275
|
+
responses: [],
|
|
276
|
+
totalContracts: 0,
|
|
277
|
+
totalErrors: 0,
|
|
278
|
+
totalResponses: 0,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
277
281
|
emitAndFlattenResponses(eventName, payload, cb) {
|
|
278
282
|
return __awaiter(this, void 0, void 0, function* () {
|
|
279
283
|
const results = yield this.emit(eventName, payload, cb);
|
|
@@ -447,9 +451,7 @@ class MercurySocketIoClient {
|
|
|
447
451
|
throw AbstractSpruceError.parse(options, SpruceError);
|
|
448
452
|
}
|
|
449
453
|
}
|
|
450
|
-
|
|
451
|
-
//@ts-ignore
|
|
452
|
-
(targetAndPayload, ioCallback) => __awaiter(this, void 0, void 0, function* () {
|
|
454
|
+
const listener = (targetAndPayload, ioCallback) => __awaiter(this, void 0, void 0, function* () {
|
|
453
455
|
if (cb) {
|
|
454
456
|
try {
|
|
455
457
|
const results = yield cb(targetAndPayload);
|
|
@@ -473,14 +475,19 @@ class MercurySocketIoClient {
|
|
|
473
475
|
}
|
|
474
476
|
}
|
|
475
477
|
}
|
|
476
|
-
})
|
|
478
|
+
});
|
|
479
|
+
this.listenerMap.set(cb, listener);
|
|
480
|
+
(_c = this.socket) === null || _c === void 0 ? void 0 : _c.on(eventName,
|
|
481
|
+
//@ts-ignore
|
|
482
|
+
listener);
|
|
477
483
|
});
|
|
478
484
|
}
|
|
479
485
|
isEventLocal(eventName) {
|
|
480
486
|
return eventName === 'connection-status-change';
|
|
481
487
|
}
|
|
482
|
-
off(eventName) {
|
|
488
|
+
off(eventName, cb) {
|
|
483
489
|
return __awaiter(this, void 0, void 0, function* () {
|
|
490
|
+
this.removeLocalListener(cb, eventName);
|
|
484
491
|
return new Promise((resolve, reject) => {
|
|
485
492
|
var _a;
|
|
486
493
|
if (!this.socket || !this.auth || this.isEventLocal(eventName)) {
|
|
@@ -503,6 +510,17 @@ class MercurySocketIoClient {
|
|
|
503
510
|
});
|
|
504
511
|
});
|
|
505
512
|
}
|
|
513
|
+
removeLocalListener(cb, eventName) {
|
|
514
|
+
var _a, _b;
|
|
515
|
+
const listener = this.listenerMap.get(cb);
|
|
516
|
+
if (listener) {
|
|
517
|
+
this.listenerMap.delete(cb);
|
|
518
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.off(eventName, listener);
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.removeAllListeners(eventName);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
506
524
|
getId() {
|
|
507
525
|
return this.id;
|
|
508
526
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "42.0.
|
|
6
|
+
"version": "42.0.32",
|
|
7
7
|
"files": [
|
|
8
8
|
"build"
|
|
9
9
|
],
|
|
@@ -63,10 +63,10 @@
|
|
|
63
63
|
"watch.tsc": "tsc -w"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"@sprucelabs/error": "^6.0.
|
|
67
|
-
"@sprucelabs/schema": "^30.0.
|
|
68
|
-
"@sprucelabs/spruce-core-schemas": "^40.0.
|
|
69
|
-
"@sprucelabs/spruce-event-utils": "^40.0.
|
|
66
|
+
"@sprucelabs/error": "^6.0.16",
|
|
67
|
+
"@sprucelabs/schema": "^30.0.26",
|
|
68
|
+
"@sprucelabs/spruce-core-schemas": "^40.0.21",
|
|
69
|
+
"@sprucelabs/spruce-event-utils": "^40.0.24",
|
|
70
70
|
"dotenv": "^16.4.5",
|
|
71
71
|
"just-clone": "^6.2.0",
|
|
72
72
|
"socket.io-client": "^4.7.5"
|
|
@@ -74,13 +74,13 @@
|
|
|
74
74
|
"devDependencies": {
|
|
75
75
|
"@sprucelabs/esm-postbuild": "^6.0.14",
|
|
76
76
|
"@sprucelabs/jest-json-reporter": "^8.0.18",
|
|
77
|
-
"@sprucelabs/mercury-core-events": "^24.0.
|
|
78
|
-
"@sprucelabs/mercury-event-emitter": "^42.0.
|
|
79
|
-
"@sprucelabs/mercury-types": "^47.0.
|
|
77
|
+
"@sprucelabs/mercury-core-events": "^24.0.26",
|
|
78
|
+
"@sprucelabs/mercury-event-emitter": "^42.0.32",
|
|
79
|
+
"@sprucelabs/mercury-types": "^47.0.21",
|
|
80
80
|
"@sprucelabs/resolve-path-aliases": "^2.0.15",
|
|
81
81
|
"@sprucelabs/semantic-release": "^5.0.1",
|
|
82
82
|
"@sprucelabs/test": "^9.0.11",
|
|
83
|
-
"@sprucelabs/test-utils": "^5.0.
|
|
83
|
+
"@sprucelabs/test-utils": "^5.0.18",
|
|
84
84
|
"@types/node": "^20.12.8",
|
|
85
85
|
"chokidar-cli": "^3.0.0",
|
|
86
86
|
"eslint": "^9.1.1",
|
|
@@ -111,5 +111,5 @@
|
|
|
111
111
|
"^#spruce/(.*)$": "<rootDir>/build/.spruce/$1"
|
|
112
112
|
}
|
|
113
113
|
},
|
|
114
|
-
"gitHead": "
|
|
114
|
+
"gitHead": "06e65c56c2654ca2d9c93722d04eaf50c3b53067"
|
|
115
115
|
}
|