@cratis/arc 20.27.2 → 20.33.1
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/dist/cjs/queries/IObservableQueryDiagnostics.d.ts +9 -0
- package/dist/cjs/queries/IObservableQueryDiagnostics.d.ts.map +1 -0
- package/dist/cjs/queries/IObservableQueryHubConnection.d.ts +1 -0
- package/dist/cjs/queries/IObservableQueryHubConnection.d.ts.map +1 -1
- package/dist/cjs/queries/ObservableQueryDiagnostics.d.ts +21 -0
- package/dist/cjs/queries/ObservableQueryDiagnostics.d.ts.map +1 -0
- package/dist/cjs/queries/ObservableQueryDiagnostics.js +87 -0
- package/dist/cjs/queries/ObservableQueryDiagnostics.js.map +1 -0
- package/dist/cjs/queries/ObservableQueryDiagnosticsSnapshot.d.ts +48 -0
- package/dist/cjs/queries/ObservableQueryDiagnosticsSnapshot.d.ts.map +1 -0
- package/dist/cjs/queries/ObservableQueryMultiplexer.d.ts +3 -0
- package/dist/cjs/queries/ObservableQueryMultiplexer.d.ts.map +1 -1
- package/dist/cjs/queries/ObservableQueryMultiplexer.js +11 -0
- package/dist/cjs/queries/ObservableQueryMultiplexer.js.map +1 -1
- package/dist/cjs/queries/QueryInstanceCache.d.ts +2 -0
- package/dist/cjs/queries/QueryInstanceCache.d.ts.map +1 -1
- package/dist/cjs/queries/QueryInstanceCache.js +36 -0
- package/dist/cjs/queries/QueryInstanceCache.js.map +1 -1
- package/dist/cjs/queries/ServerSentEventHubConnection.d.ts +1 -0
- package/dist/cjs/queries/ServerSentEventHubConnection.d.ts.map +1 -1
- package/dist/cjs/queries/ServerSentEventHubConnection.js +3 -0
- package/dist/cjs/queries/ServerSentEventHubConnection.js.map +1 -1
- package/dist/cjs/queries/WebSocketHubConnection.d.ts +1 -0
- package/dist/cjs/queries/WebSocketHubConnection.d.ts.map +1 -1
- package/dist/cjs/queries/WebSocketHubConnection.js +3 -0
- package/dist/cjs/queries/WebSocketHubConnection.js.map +1 -1
- package/dist/cjs/queries/for_ObservableQueryDiagnostics/when_tracking_owners.d.ts +2 -0
- package/dist/cjs/queries/for_ObservableQueryDiagnostics/when_tracking_owners.d.ts.map +1 -0
- package/dist/cjs/queries/index.d.ts +3 -0
- package/dist/cjs/queries/index.d.ts.map +1 -1
- package/dist/cjs/queries/index.js +3 -0
- package/dist/cjs/queries/index.js.map +1 -1
- package/dist/esm/queries/IObservableQueryDiagnostics.d.ts +9 -0
- package/dist/esm/queries/IObservableQueryDiagnostics.d.ts.map +1 -0
- package/dist/esm/queries/IObservableQueryDiagnostics.js +2 -0
- package/dist/esm/queries/IObservableQueryDiagnostics.js.map +1 -0
- package/dist/esm/queries/IObservableQueryHubConnection.d.ts +1 -0
- package/dist/esm/queries/IObservableQueryHubConnection.d.ts.map +1 -1
- package/dist/esm/queries/ObservableQueryDiagnostics.d.ts +21 -0
- package/dist/esm/queries/ObservableQueryDiagnostics.d.ts.map +1 -0
- package/dist/esm/queries/ObservableQueryDiagnostics.js +85 -0
- package/dist/esm/queries/ObservableQueryDiagnostics.js.map +1 -0
- package/dist/esm/queries/ObservableQueryDiagnosticsSnapshot.d.ts +48 -0
- package/dist/esm/queries/ObservableQueryDiagnosticsSnapshot.d.ts.map +1 -0
- package/dist/esm/queries/ObservableQueryDiagnosticsSnapshot.js +2 -0
- package/dist/esm/queries/ObservableQueryDiagnosticsSnapshot.js.map +1 -0
- package/dist/esm/queries/ObservableQueryMultiplexer.d.ts +3 -0
- package/dist/esm/queries/ObservableQueryMultiplexer.d.ts.map +1 -1
- package/dist/esm/queries/ObservableQueryMultiplexer.js +11 -1
- package/dist/esm/queries/ObservableQueryMultiplexer.js.map +1 -1
- package/dist/esm/queries/QueryInstanceCache.d.ts +2 -0
- package/dist/esm/queries/QueryInstanceCache.d.ts.map +1 -1
- package/dist/esm/queries/QueryInstanceCache.js +36 -0
- package/dist/esm/queries/QueryInstanceCache.js.map +1 -1
- package/dist/esm/queries/ServerSentEventHubConnection.d.ts +1 -0
- package/dist/esm/queries/ServerSentEventHubConnection.d.ts.map +1 -1
- package/dist/esm/queries/ServerSentEventHubConnection.js +3 -0
- package/dist/esm/queries/ServerSentEventHubConnection.js.map +1 -1
- package/dist/esm/queries/WebSocketHubConnection.d.ts +1 -0
- package/dist/esm/queries/WebSocketHubConnection.d.ts.map +1 -1
- package/dist/esm/queries/WebSocketHubConnection.js +3 -0
- package/dist/esm/queries/WebSocketHubConnection.js.map +1 -1
- package/dist/esm/queries/for_ObservableQueryDiagnostics/when_tracking_owners.d.ts +2 -0
- package/dist/esm/queries/for_ObservableQueryDiagnostics/when_tracking_owners.d.ts.map +1 -0
- package/dist/esm/queries/for_ObservableQueryDiagnostics/when_tracking_owners.js +33 -0
- package/dist/esm/queries/for_ObservableQueryDiagnostics/when_tracking_owners.js.map +1 -0
- package/dist/esm/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_a_changed_cache_key.js +1 -0
- package/dist/esm/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_a_changed_cache_key.js.map +1 -1
- package/dist/esm/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_a_new_cache_key.js +1 -1
- package/dist/esm/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_a_new_cache_key.js.map +1 -1
- package/dist/esm/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_an_explicit_size.js +1 -1
- package/dist/esm/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_an_explicit_size.js.map +1 -1
- package/dist/esm/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_the_same_cache_key.js +1 -1
- package/dist/esm/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_the_same_cache_key.js.map +1 -1
- package/dist/esm/queries/index.d.ts +3 -0
- package/dist/esm/queries/index.d.ts.map +1 -1
- package/dist/esm/queries/index.js +2 -1
- package/dist/esm/queries/index.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/queries/IObservableQueryDiagnostics.ts +40 -0
- package/queries/IObservableQueryHubConnection.ts +5 -0
- package/queries/ObservableQueryDiagnostics.ts +123 -0
- package/queries/ObservableQueryDiagnosticsSnapshot.ts +112 -0
- package/queries/ObservableQueryMultiplexer.ts +20 -0
- package/queries/QueryInstanceCache.ts +47 -0
- package/queries/ServerSentEventHubConnection.ts +5 -0
- package/queries/WebSocketHubConnection.ts +7 -0
- package/queries/for_ObservableQueryDiagnostics/when_tracking_owners.ts +49 -0
- package/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_a_changed_cache_key.ts +2 -1
- package/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_a_new_cache_key.ts +1 -1
- package/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_an_explicit_size.ts +1 -1
- package/queries/for_ObservableQueryMultiplexer/when_getting_or_creating/with_the_same_cache_key.ts +1 -1
- package/queries/index.ts +3 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { ObservableQueryDiagnosticsSnapshot } from './ObservableQueryDiagnosticsSnapshot';
|
|
3
|
+
export interface IObservableQueryDiagnostics {
|
|
4
|
+
getSnapshot(): ObservableQueryDiagnosticsSnapshot;
|
|
5
|
+
readonly snapshots$: Observable<ObservableQueryDiagnosticsSnapshot>;
|
|
6
|
+
beginTracking(cacheKey: string, owner: string): void;
|
|
7
|
+
endTracking(cacheKey: string): void;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=IObservableQueryDiagnostics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IObservableQueryDiagnostics.d.ts","sourceRoot":"","sources":["../../../queries/IObservableQueryDiagnostics.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,kCAAkC,EAAE,MAAM,sCAAsC,CAAC;AAS1F,MAAM,WAAW,2BAA2B;IAIxC,WAAW,IAAI,kCAAkC,CAAC;IAKlD,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,kCAAkC,CAAC,CAAC;IASpE,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAOrD,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACvC"}
|
|
@@ -2,6 +2,7 @@ import { DataReceived } from './ObservableQueryConnection';
|
|
|
2
2
|
import { SubscriptionRequest } from './WebSocketHubConnection';
|
|
3
3
|
export interface IObservableQueryHubConnection {
|
|
4
4
|
readonly queryCount: number;
|
|
5
|
+
readonly isConnected: boolean;
|
|
5
6
|
readonly lastPingLatency: number;
|
|
6
7
|
readonly averageLatency: number;
|
|
7
8
|
subscribe(queryId: string, request: SubscriptionRequest, callback: DataReceived<any>): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IObservableQueryHubConnection.d.ts","sourceRoot":"","sources":["../../../queries/IObservableQueryHubConnection.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAW/D,MAAM,WAAW,6BAA6B;IAI1C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAK5B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IAKjC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAQhC,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,YAAY,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAM5F,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAKnC,OAAO,IAAI,IAAI,CAAC;CACnB"}
|
|
1
|
+
{"version":3,"file":"IObservableQueryHubConnection.d.ts","sourceRoot":"","sources":["../../../queries/IObservableQueryHubConnection.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAW/D,MAAM,WAAW,6BAA6B;IAI1C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAK5B,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAK9B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IAKjC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAQhC,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,YAAY,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAM5F,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAKnC,OAAO,IAAI,IAAI,CAAC;CACnB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { QueryInstanceCache } from './QueryInstanceCache';
|
|
3
|
+
import { ObservableQueryMultiplexer } from './ObservableQueryMultiplexer';
|
|
4
|
+
import { IObservableQueryDiagnostics } from './IObservableQueryDiagnostics';
|
|
5
|
+
import { ObservableQueryDiagnosticsSnapshot, TransportDiagnostics } from './ObservableQueryDiagnosticsSnapshot';
|
|
6
|
+
export declare class ObservableQueryDiagnostics implements IObservableQueryDiagnostics {
|
|
7
|
+
private readonly _cache;
|
|
8
|
+
private readonly _getMultiplexer;
|
|
9
|
+
private readonly _getTransportConfig;
|
|
10
|
+
private readonly _snapshots;
|
|
11
|
+
private readonly _ownersByKey;
|
|
12
|
+
constructor(_cache: QueryInstanceCache, _getMultiplexer: () => ObservableQueryMultiplexer | undefined, _getTransportConfig: () => TransportDiagnostics);
|
|
13
|
+
get snapshots$(): Observable<ObservableQueryDiagnosticsSnapshot>;
|
|
14
|
+
getSnapshot(): ObservableQueryDiagnosticsSnapshot;
|
|
15
|
+
beginTracking(cacheKey: string, owner: string): void;
|
|
16
|
+
endTracking(cacheKey: string): void;
|
|
17
|
+
private _buildMultiplexerDiagnostics;
|
|
18
|
+
private _createSnapshot;
|
|
19
|
+
private _publishSnapshot;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=ObservableQueryDiagnostics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObservableQueryDiagnostics.d.ts","sourceRoot":"","sources":["../../../queries/ObservableQueryDiagnostics.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAW,MAAM,MAAM,CAAC;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAC;AAC5E,OAAO,EACH,kCAAkC,EAElC,oBAAoB,EAGvB,MAAM,sCAAsC,CAAC;AAM9C,qBAAa,0BAA2B,YAAW,2BAA2B;IAWtE,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,eAAe;IAChC,OAAO,CAAC,QAAQ,CAAC,mBAAmB;IAZxC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAqD;IAChF,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA6B;gBASrC,MAAM,EAAE,kBAAkB,EAC1B,eAAe,EAAE,MAAM,0BAA0B,GAAG,SAAS,EAC7D,mBAAmB,EAAE,MAAM,oBAAoB;IAMpE,IAAI,UAAU,IAAI,UAAU,CAAC,kCAAkC,CAAC,CAE/D;IAGD,WAAW,IAAI,kCAAkC;IAOjD,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAMpD,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAKnC,OAAO,CAAC,4BAA4B;IAwBpC,OAAO,CAAC,eAAe;IAkCvB,OAAO,CAAC,gBAAgB;CAG3B"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rxjs = require('rxjs');
|
|
4
|
+
|
|
5
|
+
class ObservableQueryDiagnostics {
|
|
6
|
+
_cache;
|
|
7
|
+
_getMultiplexer;
|
|
8
|
+
_getTransportConfig;
|
|
9
|
+
_snapshots = new rxjs.Subject();
|
|
10
|
+
_ownersByKey = new Map();
|
|
11
|
+
constructor(_cache, _getMultiplexer, _getTransportConfig) {
|
|
12
|
+
this._cache = _cache;
|
|
13
|
+
this._getMultiplexer = _getMultiplexer;
|
|
14
|
+
this._getTransportConfig = _getTransportConfig;
|
|
15
|
+
}
|
|
16
|
+
get snapshots$() {
|
|
17
|
+
return this._snapshots.asObservable();
|
|
18
|
+
}
|
|
19
|
+
getSnapshot() {
|
|
20
|
+
const snapshot = this._createSnapshot();
|
|
21
|
+
this._publishSnapshot(snapshot);
|
|
22
|
+
return snapshot;
|
|
23
|
+
}
|
|
24
|
+
beginTracking(cacheKey, owner) {
|
|
25
|
+
this._ownersByKey.set(cacheKey, owner);
|
|
26
|
+
this._publishSnapshot(this._createSnapshot());
|
|
27
|
+
}
|
|
28
|
+
endTracking(cacheKey) {
|
|
29
|
+
this._ownersByKey.delete(cacheKey);
|
|
30
|
+
this._publishSnapshot(this._createSnapshot());
|
|
31
|
+
}
|
|
32
|
+
_buildMultiplexerDiagnostics() {
|
|
33
|
+
const mux = this._getMultiplexer();
|
|
34
|
+
if (!mux) {
|
|
35
|
+
return {
|
|
36
|
+
isConnected: false,
|
|
37
|
+
configuredConnectionCount: 0,
|
|
38
|
+
effectiveConnectionCount: 0,
|
|
39
|
+
activeConnectionCount: 0,
|
|
40
|
+
connections: [],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const connections = mux.getConnectionsSnapshot();
|
|
44
|
+
const activeConnectionCount = connections.filter(c => c.isConnected).length;
|
|
45
|
+
return {
|
|
46
|
+
isConnected: activeConnectionCount > 0,
|
|
47
|
+
configuredConnectionCount: connections.length,
|
|
48
|
+
effectiveConnectionCount: connections.length,
|
|
49
|
+
activeConnectionCount,
|
|
50
|
+
connections,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
_createSnapshot() {
|
|
54
|
+
const transport = this._getTransportConfig();
|
|
55
|
+
const multiplexer = this._buildMultiplexerDiagnostics();
|
|
56
|
+
const cache = this._cache.getDiagnosticsSnapshot();
|
|
57
|
+
const ownersByQueryKey = {};
|
|
58
|
+
const queriesByOwner = {};
|
|
59
|
+
for (const [key, owner] of this._ownersByKey) {
|
|
60
|
+
ownersByQueryKey[key] = owner;
|
|
61
|
+
if (!queriesByOwner[owner]) {
|
|
62
|
+
queriesByOwner[owner] = [];
|
|
63
|
+
}
|
|
64
|
+
queriesByOwner[owner].push(key);
|
|
65
|
+
}
|
|
66
|
+
const ownership = { ownersByQueryKey, queriesByOwner };
|
|
67
|
+
const disconnectedQueryCount = multiplexer.connections.filter(c => !c.isConnected).length;
|
|
68
|
+
const health = {
|
|
69
|
+
allQueriesConnected: multiplexer.isConnected && cache.healthy,
|
|
70
|
+
disconnectedQueryCount,
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
health,
|
|
74
|
+
transport,
|
|
75
|
+
multiplexer,
|
|
76
|
+
cache,
|
|
77
|
+
ownership,
|
|
78
|
+
timestamp: new Date().toISOString(),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
_publishSnapshot(snapshot) {
|
|
82
|
+
this._snapshots.next(snapshot);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
exports.ObservableQueryDiagnostics = ObservableQueryDiagnostics;
|
|
87
|
+
//# sourceMappingURL=ObservableQueryDiagnostics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObservableQueryDiagnostics.js","sources":["../../../queries/ObservableQueryDiagnostics.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { Observable, Subject } from 'rxjs';\nimport { QueryInstanceCache } from './QueryInstanceCache';\nimport { ObservableQueryMultiplexer } from './ObservableQueryMultiplexer';\nimport { IObservableQueryDiagnostics } from './IObservableQueryDiagnostics';\nimport {\n ObservableQueryDiagnosticsSnapshot,\n MultiplexerDiagnostics,\n TransportDiagnostics,\n HealthDiagnostics,\n OwnershipDiagnostics,\n} from './ObservableQueryDiagnosticsSnapshot';\n\n/**\n * Implements the {@link IObservableQueryDiagnostics} contract by collecting live state from\n * the {@link QueryInstanceCache} and the shared {@link ObservableQueryMultiplexer}.\n */\nexport class ObservableQueryDiagnostics implements IObservableQueryDiagnostics {\n private readonly _snapshots = new Subject<ObservableQueryDiagnosticsSnapshot>();\n private readonly _ownersByKey = new Map<string, string>();\n\n /**\n * Initializes a new instance of {@link ObservableQueryDiagnostics}.\n * @param _cache The {@link QueryInstanceCache} whose state will be reflected.\n * @param _getMultiplexer Factory that returns the current shared multiplexer, or {@code undefined}.\n * @param _getTransportConfig Factory that returns the current transport diagnostics.\n */\n constructor(\n private readonly _cache: QueryInstanceCache,\n private readonly _getMultiplexer: () => ObservableQueryMultiplexer | undefined,\n private readonly _getTransportConfig: () => TransportDiagnostics,\n ) {}\n\n /**\n * Stream of diagnostics snapshots.\n */\n get snapshots$(): Observable<ObservableQueryDiagnosticsSnapshot> {\n return this._snapshots.asObservable();\n }\n\n /** @inheritdoc */\n getSnapshot(): ObservableQueryDiagnosticsSnapshot {\n const snapshot = this._createSnapshot();\n this._publishSnapshot(snapshot);\n return snapshot;\n }\n\n /** @inheritdoc */\n beginTracking(cacheKey: string, owner: string): void {\n this._ownersByKey.set(cacheKey, owner);\n this._publishSnapshot(this._createSnapshot());\n }\n\n /** @inheritdoc */\n endTracking(cacheKey: string): void {\n this._ownersByKey.delete(cacheKey);\n this._publishSnapshot(this._createSnapshot());\n }\n\n private _buildMultiplexerDiagnostics(): MultiplexerDiagnostics {\n const mux = this._getMultiplexer();\n if (!mux) {\n return {\n isConnected: false,\n configuredConnectionCount: 0,\n effectiveConnectionCount: 0,\n activeConnectionCount: 0,\n connections: [],\n };\n }\n\n const connections = mux.getConnectionsSnapshot();\n const activeConnectionCount = connections.filter(c => c.isConnected).length;\n\n return {\n isConnected: activeConnectionCount > 0,\n configuredConnectionCount: connections.length,\n effectiveConnectionCount: connections.length,\n activeConnectionCount,\n connections,\n };\n }\n\n private _createSnapshot(): ObservableQueryDiagnosticsSnapshot {\n const transport = this._getTransportConfig();\n const multiplexer = this._buildMultiplexerDiagnostics();\n const cache = this._cache.getDiagnosticsSnapshot();\n\n const ownersByQueryKey: Record<string, string> = {};\n const queriesByOwner: Record<string, string[]> = {};\n\n for (const [key, owner] of this._ownersByKey) {\n ownersByQueryKey[key] = owner;\n if (!queriesByOwner[owner]) {\n queriesByOwner[owner] = [];\n }\n queriesByOwner[owner].push(key);\n }\n\n const ownership: OwnershipDiagnostics = { ownersByQueryKey, queriesByOwner };\n\n const disconnectedQueryCount = multiplexer.connections.filter(c => !c.isConnected).length;\n const health: HealthDiagnostics = {\n allQueriesConnected: multiplexer.isConnected && cache.healthy,\n disconnectedQueryCount,\n };\n\n return {\n health,\n transport,\n multiplexer,\n cache,\n ownership,\n timestamp: new Date().toISOString(),\n };\n }\n\n private _publishSnapshot(snapshot: ObservableQueryDiagnosticsSnapshot): void {\n this._snapshots.next(snapshot);\n }\n}\n"],"names":["Subject"],"mappings":";;;;MAmBa,0BAA0B,CAAA;AAWd,IAAA,MAAA;AACA,IAAA,eAAA;AACA,IAAA,mBAAA;AAZJ,IAAA,UAAU,GAAG,IAAIA,YAAO,EAAsC;AAC9D,IAAA,YAAY,GAAG,IAAI,GAAG,EAAkB;AAQzD,IAAA,WAAA,CACqB,MAA0B,EAC1B,eAA6D,EAC7D,mBAA+C,EAAA;QAF/C,IAAA,CAAA,MAAM,GAAN,MAAM;QACN,IAAA,CAAA,eAAe,GAAf,eAAe;QACf,IAAA,CAAA,mBAAmB,GAAn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
|
4
|
+
queryCount: number;
|
|
5
|
+
}
|
|
6
|
+
export interface MultiplexerDiagnostics {
|
|
7
|
+
isConnected: boolean;
|
|
8
|
+
configuredConnectionCount: number;
|
|
9
|
+
effectiveConnectionCount: number;
|
|
10
|
+
activeConnectionCount: number;
|
|
11
|
+
connections: MultiplexerConnectionState[];
|
|
12
|
+
}
|
|
13
|
+
export interface CacheEntryDiagnostics {
|
|
14
|
+
key: string;
|
|
15
|
+
queryName: string;
|
|
16
|
+
subscriberCount: number;
|
|
17
|
+
listenerCount: number;
|
|
18
|
+
subscribed: boolean;
|
|
19
|
+
hasResult: boolean;
|
|
20
|
+
estimatedBytes: number;
|
|
21
|
+
}
|
|
22
|
+
export interface CacheDiagnostics {
|
|
23
|
+
healthy: boolean;
|
|
24
|
+
entryCount: number;
|
|
25
|
+
estimatedBytes: number;
|
|
26
|
+
entries: CacheEntryDiagnostics[];
|
|
27
|
+
}
|
|
28
|
+
export interface TransportDiagnostics {
|
|
29
|
+
queryTransportMethod: string;
|
|
30
|
+
queryDirectMode: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface HealthDiagnostics {
|
|
33
|
+
allQueriesConnected: boolean;
|
|
34
|
+
disconnectedQueryCount: number;
|
|
35
|
+
}
|
|
36
|
+
export interface OwnershipDiagnostics {
|
|
37
|
+
ownersByQueryKey: Record<string, string>;
|
|
38
|
+
queriesByOwner: Record<string, string[]>;
|
|
39
|
+
}
|
|
40
|
+
export interface ObservableQueryDiagnosticsSnapshot {
|
|
41
|
+
health: HealthDiagnostics;
|
|
42
|
+
transport: TransportDiagnostics;
|
|
43
|
+
multiplexer: MultiplexerDiagnostics;
|
|
44
|
+
cache: CacheDiagnostics;
|
|
45
|
+
ownership: OwnershipDiagnostics;
|
|
46
|
+
timestamp: string;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=ObservableQueryDiagnosticsSnapshot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObservableQue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,EAAE,iBAAiB,CAAC;IAE1B,SAAS,EAAE,oBAAoB,CAAC;IAEhC,WAAW,EAAE,sBAAsB,CAAC;IAEpC,KAAK,EAAE,gBAAgB,CAAC;IAExB,SAAS,EAAE,oBAAoB,CAAC;IAEhC,SAAS,EAAE,MAAM,CAAC;CACrB"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { IObservableQueryConnection } from './IObservableQueryConnection';
|
|
2
2
|
import { IObservableQueryHubConnection } from './IObservableQueryHubConnection';
|
|
3
3
|
import { DataReceived } from './ObservableQueryConnection';
|
|
4
|
+
import { MultiplexerConnectionState } from './ObservableQueryDiagnosticsSnapshot';
|
|
4
5
|
export declare const WS_HUB_ROUTE = "/.cratis/queries/ws";
|
|
5
6
|
export declare class ObservableQueryMultiplexer {
|
|
6
7
|
private readonly _connections;
|
|
@@ -14,6 +15,7 @@ export declare class ObservableQueryMultiplexer {
|
|
|
14
15
|
private leastLoaded;
|
|
15
16
|
private generateQueryId;
|
|
16
17
|
private buildSubscriptionRequest;
|
|
18
|
+
getConnectionsSnapshot(): MultiplexerConnectionState[];
|
|
17
19
|
}
|
|
18
20
|
export declare class MultiplexedObservableQueryConnection<TDataType> implements IObservableQueryConnection<TDataType> {
|
|
19
21
|
private readonly _pool;
|
|
@@ -27,4 +29,5 @@ export declare class MultiplexedObservableQueryConnection<TDataType> implements
|
|
|
27
29
|
}
|
|
28
30
|
export declare function getOrCreateMultiplexer(connectionFactory: () => IObservableQueryHubConnection, cacheKey: string, size?: number): ObservableQueryMultiplexer;
|
|
29
31
|
export declare function resetSharedMultiplexer(): void;
|
|
32
|
+
export declare function getSharedMultiplexer(): ObservableQueryMultiplexer | undefined;
|
|
30
33
|
//# sourceMappingURL=ObservableQueryMultiplexer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ObservableQueryMultiplexer.d.ts","sourceRoot":"","sources":["../../../queries/ObservableQueryMultiplexer.ts"],"names":[],"mappings":"AAGA,OA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AAC,QAAQ,CAAC,CAAa;gBAQT,KAAK,EAAE,0BAA0B,EACjC,UAAU,EAAE,MAAM;IAKvC,IAAI,eAAe,IAAI,MAAM,CAE5B;IAGD,IAAI,cAAc,IAAI,MAAM,CAE3B;IAGD,OAAO,CAAC,YAAY,EAAE,YAAY,CAAC,SAAS,CAAC,EAAE,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI;IAS7E,UAAU,IAAI,IAAI;CAIrB;AAeD,wBAAgB,sBAAsB,CAAC,iBAAiB,EAAE,MAAM,6BAA6B,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAE,MAAqC,GAAG,0BAA0B,CASxL;AAMD,wBAAgB,sBAAsB,IAAI,IAAI,CAI7C;AAKD,wBAAgB,oBAAoB,IAAI,0BAA0B,GAAG,SAAS,CAE7E"}
|
|
@@ -75,6 +75,13 @@ class ObservableQueryMultiplexer {
|
|
|
75
75
|
}
|
|
76
76
|
return request;
|
|
77
77
|
}
|
|
78
|
+
getConnectionsSnapshot() {
|
|
79
|
+
return this._connections.map((conn, index) => ({
|
|
80
|
+
index,
|
|
81
|
+
isConnected: conn.isConnected,
|
|
82
|
+
queryCount: conn.queryCount,
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
78
85
|
}
|
|
79
86
|
class MultiplexedObservableQueryConnection {
|
|
80
87
|
_pool;
|
|
@@ -114,10 +121,14 @@ function resetSharedMultiplexer() {
|
|
|
114
121
|
_sharedMultiplexer = undefined;
|
|
115
122
|
_sharedMultiplexerKey = '';
|
|
116
123
|
}
|
|
124
|
+
function getSharedMultiplexer() {
|
|
125
|
+
return _sharedMultiplexer;
|
|
126
|
+
}
|
|
117
127
|
|
|
118
128
|
exports.MultiplexedObservableQueryConnection = MultiplexedObservableQueryConnection;
|
|
119
129
|
exports.ObservableQueryMultiplexer = ObservableQueryMultiplexer;
|
|
120
130
|
exports.WS_HUB_ROUTE = WS_HUB_ROUTE;
|
|
121
131
|
exports.getOrCreateMultiplexer = getOrCreateMultiplexer;
|
|
132
|
+
exports.getSharedMultiplexer = getSharedMultiplexer;
|
|
122
133
|
exports.resetSharedMultiplexer = resetSharedMultiplexer;
|
|
123
134
|
//# sourceMappingURL=ObservableQueryMultiplexer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ObservableQueryMultiplexer.js","sources":["../../../queries/ObservableQueryMultiplexer.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { IObservableQueryConnection } from './IObservableQueryConnection';\nimport { IObservableQueryHubConnection } from './IObservableQueryHubConnection';\nimport { DataReceived } from './ObservableQueryConnection';\nimport { SubscriptionRequest } from './WebSocketHubConnection';\nimport { Globals } from '../Globals';\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/**\n * The WebSocket demultiplexer route used when connecting through the multiplexed observable query endpoint.\n */\nexport const WS_HUB_ROUTE = '/.cratis/queries/ws';\n\n/**\n * Multiplexes multiple observable query subscriptions across a bounded pool of physical\n * connections (WebSocket or SSE) to the backend demultiplexer.\n *\n * Each pool slot is a single multiplexed connection. When a new subscription is requested,\n * the slot with the fewest active queries is chosen. This balances load across N physical\n * connections while keeping the total connection count bounded.\n */\nexport class ObservableQueryMultiplexer {\n private readonly _connections: IObservableQueryHubConnection[];\n private readonly _size: number;\n\n /**\n * Initializes a new {@link ObservableQueryMultiplexer}.\n * @param {number} size Number of physical connections (pool slots).\n * @param {() => IObservableQueryHubConnection} connectionFactory Factory function to create each connection.\n */\n constructor(size: number, connectionFactory: () => IObservableQueryHubConnection) {\n this._size = Math.max(1, size);\n this._connections = Array.from({ length: this._size }, () => connectionFactory());\n }\n\n /**\n * Gets the pool size.\n */\n get size(): number {\n return this._size;\n }\n\n /**\n * Gets the best available ping latency across all connections.\n */\n get lastPingLatency(): number {\n return this._connections.reduce((min, c) =>\n c.lastPingLatency > 0 && c.lastPingLatency < min ? c.lastPingLatency : min,\n Number.MAX_SAFE_INTEGER\n );\n }\n\n /**\n * Gets the average latency across all connections.\n */\n get averageLatency(): number {\n const active = this._connections.filter(c => c.averageLatency > 0);\n if (active.length === 0) return 0;\n return active.reduce((sum, c) => sum + c.averageLatency, 0) / active.length;\n }\n\n /**\n * Subscribe to a query through the multiplexer. Picks the least-loaded connection\n * and sends a Subscribe message on it.\n * @param {string} queryName Fully qualified backend query name.\n * @param {object} queryArguments Flat query arguments (incl. page, pageSize, sortBy, sortDirection).\n * @param {DataReceived<any>} callback Callback invoked for each result.\n * @returns A cleanup function that unsubscribes from the query.\n */\n subscribe(queryName: string, queryArguments: object | undefined, callback: DataReceived<any>): () => void {\n const request = this.buildSubscriptionRequest(queryName, queryArguments);\n const conn = this.leastLoaded();\n const queryId = this.generateQueryId();\n\n conn.subscribe(queryId, request, callback);\n\n return () => {\n conn.unsubscribe(queryId);\n };\n }\n\n /**\n * Dispose all connections in the multiplexer.\n */\n dispose(): void {\n for (const conn of this._connections) {\n conn.dispose();\n }\n }\n\n private leastLoaded(): IObservableQueryHubConnection {\n return this._connections.reduce((min, c) =>\n c.queryCount < min.queryCount ? c : min, this._connections[0]);\n }\n\n private generateQueryId(): string {\n // Use crypto.randomUUID when available, fall back to timestamp + random\n if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {\n return crypto.randomUUID();\n }\n return `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;\n }\n\n private buildSubscriptionRequest(queryName: string, args?: object): SubscriptionRequest {\n const request: SubscriptionRequest = { queryName };\n\n // Always include the transfer mode so the server knows which emission strategy to use.\n request.transferMode = Globals.observableQueryTransferMode;\n\n if (!args) return request;\n\n const a = args as Record<string, any>;\n const pagingAndSortingKeys = new Set(['page', 'pageSize', 'sortBy', 'sortDirection']);\n\n if (a.page !== undefined && a.page !== null) request.page = Number(a.page);\n if (a.pageSize !== undefined && a.pageSize !== null) request.pageSize = Number(a.pageSize);\n if (a.sortBy !== undefined && a.sortBy !== null) request.sortBy = String(a.sortBy);\n if (a.sortDirection !== undefined && a.sortDirection !== null) request.sortDirection = String(a.sortDirection);\n\n // Everything else goes into arguments as string key-value pairs\n const remaining: Record<string, string | null> = {};\n let hasRemaining = false;\n\n for (const [key, value] of Object.entries(a)) {\n if (pagingAndSortingKeys.has(key)) continue;\n if (value === undefined || value === null) continue;\n remaining[key] = String(value);\n hasRemaining = true;\n }\n\n if (hasRemaining) {\n request.arguments = remaining;\n }\n\n return request;\n }\n}\n\n/**\n * Wraps an {@link ObservableQueryMultiplexer} subscription as an {@link IObservableQueryConnection},\n * allowing the multiplexed transport to plug into the existing query subscription pipeline\n * without changes to callers.\n */\nexport class MultiplexedObservableQueryConnection<TDataType> implements IObservableQueryConnection<TDataType> {\n private _cleanup?: () => void;\n\n /**\n * Initializes a new {@link MultiplexedObservableQueryConnection}.\n * @param {ObservableQueryMultiplexer} multiplexer The shared multiplexer.\n * @param {string} queryName The fully qualified backend query name.\n */\n constructor(\n private readonly _pool: ObservableQueryMultiplexer,\n private readonly _queryName: string,\n ) {\n }\n\n /** @inheritdoc */\n get lastPingLatency(): number {\n return this._pool.lastPingLatency;\n }\n\n /** @inheritdoc */\n get averageLatency(): number {\n return this._pool.averageLatency;\n }\n\n /** @inheritdoc */\n connect(dataReceived: DataReceived<TDataType>, queryArguments?: object): void {\n this._cleanup = this._pool.subscribe(\n this._queryName,\n queryArguments,\n dataReceived as DataReceived<any>,\n );\n }\n\n /** @inheritdoc */\n disconnect(): void {\n this._cleanup?.();\n this._cleanup = undefined;\n }\n}\n\n// ----- Shared pool singleton management -----\n\nlet _sharedMultiplexer: ObservableQueryMultiplexer | undefined;\nlet _sharedMultiplexerKey = '';\n\n/**\n * Returns the shared {@link ObservableQueryMultiplexer}, creating or re-creating it when the\n * configuration (connection count, origin, base path, microservice, transport) changes.\n * @param {() => IObservableQueryHubConnection} connectionFactory Factory to create individual connections.\n * @param {string} cacheKey A string that identifies the current configuration for invalidation.\n * @param {number} size Number of physical connections to create. Defaults to {@link Globals.queryConnectionCount}.\n * @returns The shared multiplexer instance.\n */\nexport function getOrCreateMultiplexer(connectionFactory: () => IObservableQueryHubConnection, cacheKey: string, size: number = Globals.queryConnectionCount): ObservableQueryMultiplexer {\n if (_sharedMultiplexer && _sharedMultiplexerKey === cacheKey) {\n return _sharedMultiplexer;\n }\n\n _sharedMultiplexer?.dispose();\n _sharedMultiplexer = new ObservableQueryMultiplexer(size, connectionFactory);\n _sharedMultiplexerKey = cacheKey;\n return _sharedMultiplexer;\n}\n\n/**\n * Disposes and clears the shared multiplexer singleton.\n * Intended for use in test teardown to prevent state leakage across tests.\n */\nexport function resetSharedMultiplexer(): void {\n _sharedMultiplexer?.dispose();\n _sharedMultiplexer = undefined;\n _sharedMultiplexerKey = '';\n}\n"],"names":["Globals"],"mappings":";;;;AAcO,MAAM,YAAY,GAAG;MAUf,0BAA0B,CAAA;AAClB,IAAA,YAAY;AACZ,IAAA,KAAK;IAOtB,WAAA,CAAY,IAAY,EAAE,iBAAsD,EAAA;QAC5E,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC;QAC9B,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,EAAE,EAAE,MAAM,iBAAiB,EAAE,CAAC;IACrF;AAKA,IAAA,IAAI,IAAI,GAAA;QACJ,OAAO,IAAI,CAAC,KAAK;IACrB;AAKA,IAAA,IAAI,eAAe,GAAA;AACf,QAAA,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,KACnC,CAAC,CAAC,eAAe,GAAG,CAAC,IAAI,CAAC,CAAC,eAAe,GAAG,GAAG,GAAG,CAAC,CAAC,eAAe,GAAG,GAAG,EAC1E,MAAM,CAAC,gBAAgB,CAC1B;IACL;AAKA,IAAA,IAAI,cAAc,GAAA;AACd,QAAA,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM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A,kBAAkB,EAAE,OAAO,EAAE;IAC7B,kBAAkB,GAAG,IAAI,0BAA0B,CAAC,IAAI,EAAE,iBAAiB,CAAC;IAC5E,qBAAqB,GAAG,QAAQ;AAChC,IAAA,OAAO,kBAAkB;AAC7B;SAMgB,sBAAsB,GAAA;IAClC,kBAAkB,EAAE,OAAO,EAAE;IAC7B,kBAAkB,GAAG,SAAS;IAC9B,qBAAqB,GAAG,EAAE;AAC9B;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"ObservableQueryMultiplexer.js","sources":["../../../queries/ObservableQueryMultiplexer.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { IObservableQueryConnection } from './IObservableQueryConnection';\nimport { IObservableQueryHubConnection } from './IObservableQueryHubConnection';\nimport { DataReceived } from './ObservableQueryConnection';\nimport { SubscriptionRequest } from './WebSocketHubConnection';\nimport { Globals } from '../Globals';\nimport { MultiplexerConnectionState } from './ObservableQueryDiagnosticsSnapshot';\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/**\n * The WebSocket demultiplexer route used when connecting through the multiplexed observable query endpoint.\n */\nexport const WS_HUB_ROUTE = '/.cratis/queries/ws';\n\n/**\n * Multiplexes multiple observable query subscriptions across a bounded pool of physical\n * connections (WebSocket or SSE) to the backend demultiplexer.\n *\n * Each pool slot is a single multiplexed connection. When a new subscription is requested,\n * the slot with the fewest active queries is chosen. This balances load across N physical\n * connections while keeping the total connection count bounded.\n */\nexport class ObservableQueryMultiplexer {\n private readonly _connections: IObservableQueryHubConnection[];\n private readonly _size: number;\n\n /**\n * Initializes a new {@link ObservableQueryMultiplexer}.\n * @param {number} size Number of physical connections (pool slots).\n * @param {() => IObservableQueryHubConnection} connectionFactory Factory function to create each connection.\n */\n constructor(size: number, connectionFactory: () => IObservableQueryHubConnection) {\n this._size = Math.max(1, size);\n this._connections = Array.from({ length: this._size }, () => connectionFactory());\n }\n\n /**\n * Gets the pool size.\n */\n get size(): number {\n return this._size;\n }\n\n /**\n * Gets the best available ping latency across all connections.\n */\n get lastPingLatency(): number {\n return this._connections.reduce((min, c) =>\n c.lastPingLatency > 0 && c.lastPingLatency < min ? c.lastPingLatency : min,\n Number.MAX_SAFE_INTEGER\n );\n }\n\n /**\n * Gets the average latency across all connections.\n */\n get averageLatency(): number {\n const active = this._connections.filter(c => c.averageLatency > 0);\n if (active.length === 0) return 0;\n return active.reduce((sum, c) => sum + c.averageLatency, 0) / active.length;\n }\n\n /**\n * Subscribe to a query through the multiplexer. Picks the least-loaded connection\n * and sends a Subscribe message on it.\n * @param {string} queryName Fully qualified backend query name.\n * @param {object} queryArguments Flat query arguments (incl. page, pageSize, sortBy, sortDirection).\n * @param {DataReceived<any>} callback Callback invoked for each result.\n * @returns A cleanup function that unsubscribes from the query.\n */\n subscribe(queryName: string, queryArguments: object | undefined, callback: DataReceived<any>): () => void {\n const request = this.buildSubscriptionRequest(queryName, queryArguments);\n const conn = this.leastLoaded();\n const queryId = this.generateQueryId();\n\n conn.subscribe(queryId, request, callback);\n\n return () => {\n conn.unsubscribe(queryId);\n };\n }\n\n /**\n * Dispose all connections in the multiplexer.\n */\n dispose(): void {\n for (const conn of this._connections) {\n conn.dispose();\n }\n }\n\n private leastLoaded(): IObservableQueryHubConnection {\n return this._connections.reduce((min, c) =>\n c.queryCount < min.queryCount ? c : min, this._connections[0]);\n }\n\n private generateQueryId(): string {\n // Use crypto.randomUUID when available, fall back to timestamp + random\n if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {\n return crypto.randomUUID();\n }\n return `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;\n }\n\n private buildSubscriptionRequest(queryName: string, args?: object): SubscriptionRequest {\n const request: SubscriptionRequest = { queryName };\n\n // Always include the transfer mode so the server knows which emission strategy to use.\n request.transferMode = Globals.observableQueryTransferMode;\n\n if (!args) return request;\n\n const a = args as Record<string, any>;\n const pagingAndSortingKeys = new Set(['page', 'pageSize', 'sortBy', 'sortDirection']);\n\n if (a.page !== undefined && a.page !== null) request.page = Number(a.page);\n if (a.pageSize !== undefined && a.pageSize !== null) request.pageSize = Number(a.pageSize);\n if (a.sortBy !== undefined && a.sortBy !== null) request.sortBy = String(a.sortBy);\n if (a.sortDirection !== undefined && a.sortDirection !== null) request.sortDirection = String(a.sortDirection);\n\n // Everything else goes into arguments as string key-value pairs\n const remaining: Record<string, string | null> = {};\n let hasRemaining = false;\n\n for (const [key, value] of Object.entries(a)) {\n if (pagingAndSortingKeys.has(key)) continue;\n if (value === undefined || value === null) continue;\n remaining[key] = String(value);\n hasRemaining = true;\n }\n\n if (hasRemaining) {\n request.arguments = remaining;\n }\n\n return request;\n }\n\n /**\n * Returns a snapshot of the per-connection state for diagnostics.\n * @returns An array describing each slot in the connection pool.\n */\n getConnectionsSnapshot(): MultiplexerConnectionState[] {\n return this._connections.map((conn, index) => ({\n index,\n isConnected: conn.isConnected,\n queryCount: conn.queryCount,\n }));\n }\n}\n\n/**\n * Wraps an {@link ObservableQueryMultiplexer} subscription as an {@link IObservableQueryConnection},\n * allowing the multiplexed transport to plug into the existing query subscription pipeline\n * without changes to callers.\n */\nexport class MultiplexedObservableQueryConnection<TDataType> implements IObservableQueryConnection<TDataType> {\n private _cleanup?: () => void;\n\n /**\n * Initializes a new {@link MultiplexedObservableQueryConnection}.\n * @param {ObservableQueryMultiplexer} multiplexer The shared multiplexer.\n * @param {string} queryName The fully qualified backend query name.\n */\n constructor(\n private readonly _pool: ObservableQueryMultiplexer,\n private readonly _queryName: string,\n ) {\n }\n\n /** @inheritdoc */\n get lastPingLatency(): number {\n return this._pool.lastPingLatency;\n }\n\n /** @inheritdoc */\n get averageLatency(): number {\n return this._pool.averageLatency;\n }\n\n /** @inheritdoc */\n connect(dataReceived: DataReceived<TDataType>, queryArguments?: object): void {\n this._cleanup = this._pool.subscribe(\n this._queryName,\n queryArguments,\n dataReceived as DataReceived<any>,\n );\n }\n\n /** @inheritdoc */\n disconnect(): void {\n this._cleanup?.();\n this._cleanup = undefined;\n }\n}\n\n// ----- Shared pool singleton management -----\n\nlet _sharedMultiplexer: ObservableQueryMultiplexer | undefined;\nlet _sharedMultiplexerKey = '';\n\n/**\n * Returns the shared {@link ObservableQueryMultiplexer}, creating or re-creating it when the\n * configuration (connection count, origin, base path, microservice, transport) changes.\n * @param {() => IObservableQueryHubConnection} connectionFactory Factory to create individual connections.\n * @param {string} cacheKey A string that identifies the current configuration for invalidation.\n * @param {number} size Number of physical connections to create. Defaults to {@link Globals.queryConnectionCount}.\n * @returns The shared multiplexer instance.\n */\nexport function getOrCreateMultiplexer(connectionFactory: () => IObservableQueryHubConnection, cacheKey: string, size: number = Globals.queryConnectionCount): ObservableQueryMultiplexer {\n if (_sharedMultiplexer && _sharedMultiplexerKey === cacheKey) {\n return _sharedMultiplexer;\n }\n\n _sharedMultiplexer?.dispose();\n _sharedMultiplexer = new ObservableQueryMultiplexer(size, connectionFactory);\n _sharedMultiplexerKey = cacheKey;\n return _sharedMultiplexer;\n}\n\n/**\n * Disposes and clears the shared multiplexer singleton.\n * Intended for use in test teardown to prevent state leakage across tests.\n */\nexport function resetSharedMultiplexer(): void {\n _sharedMultiplexer?.dispose();\n _sharedMultiplexer = undefined;\n _sharedMultiplexerKey = '';\n}\n\n/**\n * Returns the current shared multiplexer instance, or {@code undefined} if none has been created yet.\n */\nexport function getSharedMultiplexer(): ObservableQueryMultiplexer | undefined {\n return _sharedMultiplexer;\n}\n"],"names":["Globals"],"mappings":";;;;AAeO,MAAM,YAAY,GAAG;MAUf,0BAA0B,CAAA;AAClB,IAAA,YAAY;AACZ,IAAA,KAAK;IAOtB,WAAA,CAAY,IAAY,EAAE,iBAAsD,EAAA;QAC5E,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC;QAC9B,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,KAA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,eAAe,GAAA;AACf,QAAA,OAAO,IAAI,CAAC,KAAK,CAAC,eAAe;IACrC;AAGA,IAAA,IAAI,cAAc,GAAA;AACd,QAAA,OAAO,IAAI,CAAC,KAAK,CAAC,cAAc;IACpC;IAGA,OAAO,CAAC,YAAqC,EAAE,cAAuB,EAAA;AAClE,QAAA,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAChC,IAAI,CAAC,UAAU,EACf,cAAc,EACd,YAAiC,CACpC;IACL;IAGA,UAAU,GAAA;AACN,QAAA,IAAI,CAAC,QAAQ,IAAI;AACjB,QAAA,IAAI,CAAC,QAAQ,GAAG,SAAS;IAC7B;AACH;AAID,IAAI,kBAA0D;AAC9D,IAAI,qBAAqB,GAAG,EAAE;AAUxB,SAAU,sBAAsB,CAAC,iBAAsD,EAAE,QAAgB,EAAE,IAAA,GAAeA,eAAO,CAAC,oBAAoB,EAAA;AACxJ,IAAA,IAAI,kBAAkB,IAAI,qBAAqB,KAAK,QAAQ,EAAE;AAC1D,QAAA,OAAO,kBAAkB;IAC7B;IAEA,kBAAkB,EAAE,OAAO,EAAE;IAC7B,kBAAkB,GAAG,IAAI,0BAA0B,CAAC,IAAI,EAAE,iBAAiB,CAAC;IAC5E,qBAAqB,GAAG,QAAQ;AAChC,IAAA,OAAO,kBAAkB;AAC7B;SAMgB,sBAAsB,GAAA;IAClC,kBAAkB,EAAE,OAAO,EAAE;IAC7B,kBAAkB,GAAG,SAAS;IAC9B,qBAAqB,GAAG,EAAE;AAC9B;SAKgB,oBAAoB,GAAA;AAChC,IAAA,OAAO,kBAAkB;AAC7B;;;;;;;;;"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { QueryResultWithState } from './QueryResultWithState';
|
|
2
|
+
import { CacheDiagnostics } from './ObservableQueryDiagnosticsSnapshot';
|
|
2
3
|
export type QueryCacheKey = string;
|
|
3
4
|
export type QueryCacheListener<TDataType> = (result: QueryResultWithState<TDataType>) => void;
|
|
4
5
|
export interface QueryCacheEntry<TDataType> {
|
|
@@ -33,5 +34,6 @@ export declare class QueryInstanceCache {
|
|
|
33
34
|
dispose(): void;
|
|
34
35
|
deferDispose(): void;
|
|
35
36
|
cancelPendingDispose(): void;
|
|
37
|
+
getDiagnosticsSnapshot(): CacheDiagnostics;
|
|
36
38
|
}
|
|
37
39
|
//# sourceMappingURL=QueryInstanceCache.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"QueryInstanceCache.d.ts","sourceRoot":"","sources":["../../../queries/QueryInstanceCache.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"QueryInstanceCache.d.ts","sourceRoot":"","sources":["../../../queries/QueryInstanceCache.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAyB,MAAM,sCAAsC,CAAC;AAK/F,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC;AAKnC,MAAM,MAAM,kBAAkB,CAAC,SAAS,IAAI,CAAC,MAAM,EAAE,oBAAoB,CAAC,SAAS,CAAC,KAAK,IAAI,CAAC;AAM9F,MAAM,WAAW,eAAe,CAAC,SAAS;IAItC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAK3B,UAAU,CAAC,EAAE,oBAAoB,CAAC,SAAS,CAAC,CAAC;IAK7C,eAAe,EAAE,MAAM,CAAC;IAKxB,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC,CAAC;IAMvD,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;IAKtB,UAAU,EAAE,OAAO,CAAC;IAOpB,cAAc,CAAC,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;CAClD;AAUD,qBAAa,kBAAkB;IAWf,OAAO,CAAC,QAAQ,CAAC,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{
|
|
|
143
143
|
this._pendingDispose = undefined;
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
+
getDiagnosticsSnapshot() {
|
|
147
|
+
const entries = [];
|
|
148
|
+
let totalBytes = 0;
|
|
149
|
+
let unhealthyCount = 0;
|
|
150
|
+
for (const [key, entry] of this._entries) {
|
|
151
|
+
const colonIndex = key.indexOf('::');
|
|
152
|
+
const queryName = colonIndex >= 0 ? key.substring(0, colonIndex) : key;
|
|
153
|
+
let estimatedBytes = 0;
|
|
154
|
+
try {
|
|
155
|
+
if (entry.lastResult !== undefined) {
|
|
156
|
+
estimatedBytes = JSON.stringify(entry.lastResult).length;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
}
|
|
161
|
+
if (entry.subscriberCount > 0 && !entry.subscribed) {
|
|
162
|
+
unhealthyCount++;
|
|
163
|
+
}
|
|
164
|
+
totalBytes += estimatedBytes;
|
|
165
|
+
entries.push({
|
|
166
|
+
key,
|
|
167
|
+
queryName,
|
|
168
|
+
subscriberCount: entry.subscriberCount,
|
|
169
|
+
listenerCount: entry.listeners.size,
|
|
170
|
+
subscribed: entry.subscribed,
|
|
171
|
+
hasResult: entry.lastResult !== undefined,
|
|
172
|
+
estimatedBytes,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
healthy: unhealthyCount === 0,
|
|
177
|
+
entryCount: this._entries.size,
|
|
178
|
+
estimatedBytes: totalBytes,
|
|
179
|
+
entries,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
146
182
|
}
|
|
147
183
|
|
|
148
184
|
exports.QueryInstanceCache = QueryInstanceCache;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"QueryInstanceCache.js","sources":["../../../queries/QueryInstanceCache.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { QueryResultWithState } from './QueryResultWithState';\n\n/**\n * Represents a key that uniquely identifies a query instance in the cache, based on the query type name and its serialized arguments.\n */\nexport type QueryCacheKey = string;\n\n/**\n * Callback invoked when the cached result for an entry changes.\n */\nexport type QueryCacheListener<TDataType> = (result: QueryResultWithState<TDataType>) => void;\n\n/**\n * Represents a single entry in the {@link QueryInstanceCache}.\n * @template TDataType The type of data returned by the query.\n */\nexport interface QueryCacheEntry<TDataType> {\n /**\n * The cached query instance.\n */\n readonly instance: unknown;\n\n /**\n * The last result received from the query, if any.\n */\n lastResult?: QueryResultWithState<TDataType>;\n\n /**\n * The number of active subscribers holding a reference to this entry.\n */\n subscriberCount: number;\n\n /**\n * Set of listener callbacks that are notified when {@link lastResult} changes.\n */\n readonly listeners: Set<QueryCacheListener<TDataType>>;\n\n /**\n * Cleanup function returned by the first subscriber that starts the query connection.\n * Called when the last subscriber releases the entry.\n */\n teardown?: () => void;\n\n /**\n * Whether an active subscription has been established for this entry.\n */\n subscribed: boolean;\n\n /**\n * Timer handle for deferred cleanup. Allows React StrictMode re-mounts (in any build\n * environment) to cancel the pending teardown so the connection is reused instead of\n * torn down and recreated.\n */\n pendingCleanup?: ReturnType<typeof setTimeout>;\n}\n\n/**\n * Provides a cache for query instances, keyed by query type and serialized arguments.\n *\n * Two callers requesting the same query type with identical arguments receive the same\n * cached instance and immediately see the last known result — without an additional\n * round trip to the server. When the last subscriber releases its reference the entry\n * is evicted.\n */\nexport class QueryInstanceCache {\n private readonly _entries = new Map<QueryCacheKey, QueryCacheEntry<unknown>>();\n private _pendingDispose?: ReturnType<typeof setTimeout>;\n\n /**\n * Initializes a new instance of {@link QueryInstanceCache}.\n * @param retentionMs How long in milliseconds to keep a cache entry alive after the last\n * subscriber releases it before evicting the subscription and cached data. A non-zero\n * value lets users navigate away and back without losing cached data. Defaults to\n * 30 000 ms. Pass 0 for immediate eviction.\n */\n constructor(private readonly _retentionMs: number = 30_000) {\n }\n\n /**\n * Builds the cache key for a query.\n * @param queryTypeName The stable type name for the query. Use the instance's {@link queryName}\n * (a hardcoded fully-qualified string in generated proxies) rather than {@link Function.name},\n * which is unstable under minification.\n * @param args Optional arguments supplied to the query.\n * @returns A stable string key.\n */\n buildKey(queryTypeName: string, args?: object): QueryCacheKey {\n if (!args || Object.keys(args).length === 0) {\n return `${queryTypeName}::`;\n }\n\n const sorted = Object.keys(args)\n .sort()\n .reduce<Record<string, unknown>>((accumulator, key) => {\n accumulator[key] = (args as Record<string, unknown>)[key];\n return accumulator;\n }, {});\n\n return `${queryTypeName}::${JSON.stringify(sorted)}`;\n }\n\n /**\n * Returns a cached instance for the given key, or creates a new one using the provided factory.\n * The subscriber count of the entry is incremented.\n * @template TInstance The type of the query instance.\n * @param key The cache key produced by {@link buildKey}.\n * @param factory A zero-argument factory that creates a fresh query instance when one is not yet cached.\n * @returns The cached (or newly created) instance and whether it was newly created.\n */\n getOrCreate<TInstance>(\n key: QueryCacheKey,\n factory: () => TInstance\n ): { instance: TInstance; isNew: boolean } {\n if (!this._entries.has(key)) {\n const entry: QueryCacheEntry<unknown> = {\n instance: factory(),\n lastResult: undefined,\n subscriberCount: 0,\n listeners: new Set(),\n subscribed: false,\n };\n\n this._entries.set(key, entry);\n return { instance: entry.instance as TInstance, isNew: true };\n }\n\n const entry = this._entries.get(key)!;\n return { instance: entry.instance as TInstance, isNew: false };\n }\n\n /**\n * Increments the active subscriber count for the given key.\n * If a deferred cleanup was pending (from a recent {@link release}),\n * it is cancelled so the existing subscription is reused.\n * Call from `useEffect` setup to pair with {@link release} in the cleanup.\n * @param key The cache key produced by {@link buildKey}.\n */\n acquire(key: QueryCacheKey): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n if (entry.pendingCleanup !== undefined) {\n clearTimeout(entry.pendingCleanup);\n entry.pendingCleanup = undefined;\n }\n\n entry.subscriberCount++;\n }\n }\n\n /**\n * Returns the last cached result for the given key, or `undefined` if no result has been stored yet.\n * @template TDataType The type of data returned by the query.\n * @param key The cache key produced by {@link buildKey}.\n * @returns The last {@link QueryResultWithState}, or `undefined`.\n */\n getLastResult<TDataType>(key: QueryCacheKey): QueryResultWithState<TDataType> | undefined {\n return this._entries.get(key)?.lastResult as QueryResultWithState<TDataType> | undefined;\n }\n\n /**\n * Stores the most recent result for the given key and notifies all registered listeners.\n * @template TDataType The type of data returned by the query.\n * @param key The cache key produced by {@link buildKey}.\n * @param result The result to store.\n */\n setLastResult<TDataType>(key: QueryCacheKey, result: QueryResultWithState<TDataType>): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n const previousResult = entry.lastResult as QueryResultWithState<TDataType> | undefined;\n entry.lastResult = result as QueryResultWithState<unknown>;\n\n // Suppress re-renders when the server re-sends identical data after a reconnect.\n // We only compare `data` and `isSuccess` — other fields (e.g. changeSet) are\n // ephemeral and do not affect what the user sees.\n if (\n previousResult !== undefined &&\n previousResult.isSuccess === result.isSuccess &&\n JSON.stringify(previousResult.data) === JSON.stringify(result.data)\n ) {\n return;\n }\n\n for (const listener of entry.listeners) {\n (listener as QueryCacheListener<TDataType>)(result);\n }\n }\n }\n\n /**\n * Registers a listener that is invoked whenever the cached result for the given key changes.\n * @template TDataType The type of data returned by the query.\n * @param key The cache key produced by {@link buildKey}.\n * @param listener The callback to register.\n */\n addListener<TDataType>(key: QueryCacheKey, listener: QueryCacheListener<TDataType>): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n entry.listeners.add(listener as QueryCacheListener<unknown>);\n }\n }\n\n /**\n * Removes a previously registered listener.\n * @template TDataType The type of data returned by the query.\n * @param key The cache key produced by {@link buildKey}.\n * @param listener The callback to remove.\n */\n removeListener<TDataType>(key: QueryCacheKey, listener: QueryCacheListener<TDataType>): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n entry.listeners.delete(listener as QueryCacheListener<unknown>);\n }\n }\n\n /**\n * Stores a teardown function for the given key and marks the entry as subscribed.\n * Called automatically when the last subscriber releases the entry.\n * @param key The cache key produced by {@link buildKey}.\n * @param teardown Cleanup function that disconnects the underlying query subscription.\n */\n setTeardown(key: QueryCacheKey, teardown: () => void): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n entry.teardown = teardown;\n entry.subscribed = true;\n }\n }\n\n /**\n * Returns whether an active subscription exists for the given key.\n * @param key The cache key to check.\n * @returns `true` if a subscription has been established; `false` otherwise.\n */\n isSubscribed(key: QueryCacheKey): boolean {\n return this._entries.get(key)?.subscribed ?? false;\n }\n\n /**\n * Decrements the subscriber count for the given key. When the count reaches zero, both teardown\n * and eviction are deferred by one microtask so that React StrictMode re-mounts — in any build\n * environment — can re-acquire the entry and cancel the cleanup before the timeout fires. This\n * prevents an unnecessary disconnect/reconnect cycle during the synthetic unmount/remount that\n * StrictMode performs.\n * @param key The cache key produced by {@link buildKey}.\n */\n release(key: QueryCacheKey): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n entry.subscriberCount--;\n\n if (entry.subscriberCount <= 0) {\n // Defer both teardown and eviction. React StrictMode re-mounts can cancel by\n // calling acquire() before the timeout fires. A non-zero _retentionMs keeps the\n // entry alive so users navigating back quickly see cached data immediately.\n entry.pendingCleanup = setTimeout(() => {\n const current = this._entries.get(key);\n\n if (current && current.subscriberCount <= 0) {\n current.subscribed = false;\n current.teardown?.();\n current.teardown = undefined;\n current.pendingCleanup = undefined;\n this._entries.delete(key);\n }\n }, this._retentionMs);\n }\n }\n }\n\n /**\n * Returns whether an entry exists for the given key.\n * @param key The cache key to check.\n * @returns `true` if an entry exists; `false` otherwise.\n */\n has(key: QueryCacheKey): boolean {\n return this._entries.has(key);\n }\n\n /**\n * Tears down all active subscriptions and marks every entry as not subscribed,\n * but preserves entries, subscriber counts, and listeners. This allows\n * hooks whose effects re-run afterward to detect that the entry is no longer\n * subscribed and re-establish a fresh connection.\n *\n * Use this when the underlying transport must be replaced (e.g. after an\n * authentication change that requires new WebSocket connections with updated\n * credentials).\n */\n teardownAllSubscriptions(): void {\n for (const [, entry] of this._entries) {\n if (entry.pendingCleanup !== undefined) {\n clearTimeout(entry.pendingCleanup);\n entry.pendingCleanup = undefined;\n }\n\n entry.subscribed = false;\n entry.teardown?.();\n entry.teardown = undefined;\n }\n }\n\n /**\n * Immediately tears down all subscriptions, cancels any pending deferred cleanups,\n * and evicts all entries. Call when the owning component (e.g. the {@link Arc} provider)\n * unmounts permanently so that all query connections are closed synchronously.\n */\n dispose(): void {\n for (const [, entry] of this._entries) {\n if (entry.pendingCleanup !== undefined) {\n clearTimeout(entry.pendingCleanup);\n entry.pendingCleanup = undefined;\n }\n\n entry.subscribed = false;\n entry.teardown?.();\n entry.teardown = undefined;\n }\n\n this._entries.clear();\n }\n\n /**\n * Schedules a deferred {@link dispose} using {@code setTimeout(0)}.\n *\n * This allows React StrictMode re-mounts to call {@link cancelPendingDispose}\n * before the dispose fires, avoiding the destruction of cache entries that child\n * effects are about to re-acquire.\n *\n * If a deferred dispose is already pending, it is replaced.\n */\n deferDispose(): void {\n if (this._pendingDispose !== undefined) {\n clearTimeout(this._pendingDispose);\n }\n\n this._pendingDispose = setTimeout(() => {\n this._pendingDispose = undefined;\n this.dispose();\n }, 0);\n }\n\n /**\n * Cancels a pending deferred dispose scheduled by {@link deferDispose}.\n *\n * Call from the {@code useEffect} setup phase so that a StrictMode re-mount\n * prevents the synthetic unmount's deferred dispose from firing.\n */\n cancelPendingDispose(): void {\n if (this._pendingDispose !== undefined) {\n clearTimeout(this._pendingDispose);\n this._pendingDispose = undefined;\n }\n }\n}\n"],"names":[],"mappings":";;MAmEa,kBAAkB,CAAA;AAWE,IAAA,YAAA;AAVZ,IAAA,QAAQ,GAAG,IAAI,GAAG,EAA2C;AACtE,IAAA,eAAe;AASvB,IAAA,WAAA,CAA6B,eAAuB,MAAM,EAAA;QAA7B,IAAA,CAAA,YAAY,GAAZ,YAAY;IACzC;IAUA,QAAQ,CAAC,aAAqB,EAAE,IAAa,EAAA;AACzC,QAAA,IAAI,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE;YACzC,OAAO,CAAA,EAAG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IACJ;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"QueryInstanceCache.js","sources":["../../../queries/QueryInstanceCache.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { QueryResultWithState } from './QueryResultWithState';\nimport { CacheDiagnostics, CacheEntryDiagnostics } from './ObservableQueryDiagnosticsSnapshot';\n\n/**\n * Represents a key that uniquely identifies a query instance in the cache, based on the query type name and its serialized arguments.\n */\nexport type QueryCacheKey = string;\n\n/**\n * Callback invoked when the cached result for an entry changes.\n */\nexport type QueryCacheListener<TDataType> = (result: QueryResultWithState<TDataType>) => void;\n\n/**\n * Represents a single entry in the {@link QueryInstanceCache}.\n * @template TDataType The type of data returned by the query.\n */\nexport interface QueryCacheEntry<TDataType> {\n /**\n * The cached query instance.\n */\n readonly instance: unknown;\n\n /**\n * The last result received from the query, if any.\n */\n lastResult?: QueryResultWithState<TDataType>;\n\n /**\n * The number of active subscribers holding a reference to this entry.\n */\n subscriberCount: number;\n\n /**\n * Set of listener callbacks that are notified when {@link lastResult} changes.\n */\n readonly listeners: Set<QueryCacheListener<TDataType>>;\n\n /**\n * Cleanup function returned by the first subscriber that starts the query connection.\n * Called when the last subscriber releases the entry.\n */\n teardown?: () => void;\n\n /**\n * Whether an active subscription has been established for this entry.\n */\n subscribed: boolean;\n\n /**\n * Timer handle for deferred cleanup. Allows React StrictMode re-mounts (in any build\n * environment) to cancel the pending teardown so the connection is reused instead of\n * torn down and recreated.\n */\n pendingCleanup?: ReturnType<typeof setTimeout>;\n}\n\n/**\n * Provides a cache for query instances, keyed by query type and serialized arguments.\n *\n * Two callers requesting the same query type with identical arguments receive the same\n * cached instance and immediately see the last known result — without an additional\n * round trip to the server. When the last subscriber releases its reference the entry\n * is evicted.\n */\nexport class QueryInstanceCache {\n private readonly _entries = new Map<QueryCacheKey, QueryCacheEntry<unknown>>();\n private _pendingDispose?: ReturnType<typeof setTimeout>;\n\n /**\n * Initializes a new instance of {@link QueryInstanceCache}.\n * @param retentionMs How long in milliseconds to keep a cache entry alive after the last\n * subscriber releases it before evicting the subscription and cached data. A non-zero\n * value lets users navigate away and back without losing cached data. Defaults to\n * 30 000 ms. Pass 0 for immediate eviction.\n */\n constructor(private readonly _retentionMs: number = 30_000) {\n }\n\n /**\n * Builds the cache key for a query.\n * @param queryTypeName The stable type name for the query. Use the instance's {@link queryName}\n * (a hardcoded fully-qualified string in generated proxies) rather than {@link Function.name},\n * which is unstable under minification.\n * @param args Optional arguments supplied to the query.\n * @returns A stable string key.\n */\n buildKey(queryTypeName: string, args?: object): QueryCacheKey {\n if (!args || Object.keys(args).length === 0) {\n return `${queryTypeName}::`;\n }\n\n const sorted = Object.keys(args)\n .sort()\n .reduce<Record<string, unknown>>((accumulator, key) => {\n accumulator[key] = (args as Record<string, unknown>)[key];\n return accumulator;\n }, {});\n\n return `${queryTypeName}::${JSON.stringify(sorted)}`;\n }\n\n /**\n * Returns a cached instance for the given key, or creates a new one using the provided factory.\n * The subscriber count of the entry is incremented.\n * @template TInstance The type of the query instance.\n * @param key The cache key produced by {@link buildKey}.\n * @param factory A zero-argument factory that creates a fresh query instance when one is not yet cached.\n * @returns The cached (or newly created) instance and whether it was newly created.\n */\n getOrCreate<TInstance>(\n key: QueryCacheKey,\n factory: () => TInstance\n ): { instance: TInstance; isNew: boolean } {\n if (!this._entries.has(key)) {\n const entry: QueryCacheEntry<unknown> = {\n instance: factory(),\n lastResult: undefined,\n subscriberCount: 0,\n listeners: new Set(),\n subscribed: false,\n };\n\n this._entries.set(key, entry);\n return { instance: entry.instance as TInstance, isNew: true };\n }\n\n const entry = this._entries.get(key)!;\n return { instance: entry.instance as TInstance, isNew: false };\n }\n\n /**\n * Increments the active subscriber count for the given key.\n * If a deferred cleanup was pending (from a recent {@link release}),\n * it is cancelled so the existing subscription is reused.\n * Call from `useEffect` setup to pair with {@link release} in the cleanup.\n * @param key The cache key produced by {@link buildKey}.\n */\n acquire(key: QueryCacheKey): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n if (entry.pendingCleanup !== undefined) {\n clearTimeout(entry.pendingCleanup);\n entry.pendingCleanup = undefined;\n }\n\n entry.subscriberCount++;\n }\n }\n\n /**\n * Returns the last cached result for the given key, or `undefined` if no result has been stored yet.\n * @template TDataType The type of data returned by the query.\n * @param key The cache key produced by {@link buildKey}.\n * @returns The last {@link QueryResultWithState}, or `undefined`.\n */\n getLastResult<TDataType>(key: QueryCacheKey): QueryResultWithState<TDataType> | undefined {\n return this._entries.get(key)?.lastResult as QueryResultWithState<TDataType> | undefined;\n }\n\n /**\n * Stores the most recent result for the given key and notifies all registered listeners.\n * @template TDataType The type of data returned by the query.\n * @param key The cache key produced by {@link buildKey}.\n * @param result The result to store.\n */\n setLastResult<TDataType>(key: QueryCacheKey, result: QueryResultWithState<TDataType>): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n const previousResult = entry.lastResult as QueryResultWithState<TDataType> | undefined;\n entry.lastResult = result as QueryResultWithState<unknown>;\n\n // Suppress re-renders when the server re-sends identical data after a reconnect.\n // We only compare `data` and `isSuccess` — other fields (e.g. changeSet) are\n // ephemeral and do not affect what the user sees.\n if (\n previousResult !== undefined &&\n previousResult.isSuccess === result.isSuccess &&\n JSON.stringify(previousResult.data) === JSON.stringify(result.data)\n ) {\n return;\n }\n\n for (const listener of entry.listeners) {\n (listener as QueryCacheListener<TDataType>)(result);\n }\n }\n }\n\n /**\n * Registers a listener that is invoked whenever the cached result for the given key changes.\n * @template TDataType The type of data returned by the query.\n * @param key The cache key produced by {@link buildKey}.\n * @param listener The callback to register.\n */\n addListener<TDataType>(key: QueryCacheKey, listener: QueryCacheListener<TDataType>): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n entry.listeners.add(listener as QueryCacheListener<unknown>);\n }\n }\n\n /**\n * Removes a previously registered listener.\n * @template TDataType The type of data returned by the query.\n * @param key The cache key produced by {@link buildKey}.\n * @param listener The callback to remove.\n */\n removeListener<TDataType>(key: QueryCacheKey, listener: QueryCacheListener<TDataType>): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n entry.listeners.delete(listener as QueryCacheListener<unknown>);\n }\n }\n\n /**\n * Stores a teardown function for the given key and marks the entry as subscribed.\n * Called automatically when the last subscriber releases the entry.\n * @param key The cache key produced by {@link buildKey}.\n * @param teardown Cleanup function that disconnects the underlying query subscription.\n */\n setTeardown(key: QueryCacheKey, teardown: () => void): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n entry.teardown = teardown;\n entry.subscribed = true;\n }\n }\n\n /**\n * Returns whether an active subscription exists for the given key.\n * @param key The cache key to check.\n * @returns `true` if a subscription has been established; `false` otherwise.\n */\n isSubscribed(key: QueryCacheKey): boolean {\n return this._entries.get(key)?.subscribed ?? false;\n }\n\n /**\n * Decrements the subscriber count for the given key. When the count reaches zero, both teardown\n * and eviction are deferred by one microtask so that React StrictMode re-mounts — in any build\n * environment — can re-acquire the entry and cancel the cleanup before the timeout fires. This\n * prevents an unnecessary disconnect/reconnect cycle during the synthetic unmount/remount that\n * StrictMode performs.\n * @param key The cache key produced by {@link buildKey}.\n */\n release(key: QueryCacheKey): void {\n const entry = this._entries.get(key);\n\n if (entry) {\n entry.subscriberCount--;\n\n if (entry.subscriberCount <= 0) {\n // Defer both teardown and eviction. React StrictMode re-mounts can cancel by\n // calling acquire() before the timeout fires. A non-zero _retentionMs keeps the\n // entry alive so users navigating back quickly see cached data immediately.\n entry.pendingCleanup = setTimeout(() => {\n const current = this._entries.get(key);\n\n if (current && current.subscriberCount <= 0) {\n current.subscribed = false;\n current.teardown?.();\n current.teardown = undefined;\n current.pendingCleanup = undefined;\n this._entries.delete(key);\n }\n }, this._retentionMs);\n }\n }\n }\n\n /**\n * Returns whether an entry exists for the given key.\n * @param key The cache key to check.\n * @returns `true` if an entry exists; `false` otherwise.\n */\n has(key: QueryCacheKey): boolean {\n return this._entries.has(key);\n }\n\n /**\n * Tears down all active subscriptions and marks every entry as not subscribed,\n * but preserves entries, subscriber counts, and listeners. This allows\n * hooks whose effects re-run afterward to detect that the entry is no longer\n * subscribed and re-establish a fresh connection.\n *\n * Use this when the underlying transport must be replaced (e.g. after an\n * authentication change that requires new WebSocket connections with updated\n * credentials).\n */\n teardownAllSubscriptions(): void {\n for (const [, entry] of this._entries) {\n if (entry.pendingCleanup !== undefined) {\n clearTimeout(entry.pendingCleanup);\n entry.pendingCleanup = undefined;\n }\n\n entry.subscribed = false;\n entry.teardown?.();\n entry.teardown = undefined;\n }\n }\n\n /**\n * Immediately tears down all subscriptions, cancels any pending deferred cleanups,\n * and evicts all entries. Call when the owning component (e.g. the {@link Arc} provider)\n * unmounts permanently so that all query connections are closed synchronously.\n */\n dispose(): void {\n for (const [, entry] of this._entries) {\n if (entry.pendingCleanup !== undefined) {\n clearTimeout(entry.pendingCleanup);\n entry.pendingCleanup = undefined;\n }\n\n entry.subscribed = false;\n entry.teardown?.();\n entry.teardown = undefined;\n }\n\n this._entries.clear();\n }\n\n /**\n * Schedules a deferred {@link dispose} using {@code setTimeout(0)}.\n *\n * This allows React StrictMode re-mounts to call {@link cancelPendingDispose}\n * before the dispose fires, avoiding the destruction of cache entries that child\n * effects are about to re-acquire.\n *\n * If a deferred dispose is already pending, it is replaced.\n */\n deferDispose(): void {\n if (this._pendingDispose !== undefined) {\n clearTimeout(this._pendingDispose);\n }\n\n this._pendingDispose = setTimeout(() => {\n this._pendingDispose = undefined;\n this.dispose();\n }, 0);\n }\n\n /**\n * Cancels a pending deferred dispose scheduled by {@link deferDispose}.\n *\n * Call from the {@code useEffect} setup phase so that a StrictMode re-mount\n * prevents the synthetic unmount's deferred dispose from firing.\n */\n cancelPendingDispose(): void {\n if (this._pendingDispose !== undefined) {\n clearTimeout(this._pendingDispose);\n this._pendingDispose = undefined;\n }\n }\n\n /**\n * Returns a diagnostics snapshot of the current cache state.\n * @returns A {@link CacheDiagnostics} describing all entries.\n */\n getDiagnosticsSnapshot(): CacheDiagnostics {\n const entries: CacheEntryDiagnostics[] = [];\n let totalBytes = 0;\n let unhealthyCount = 0;\n\n for (const [key, entry] of this._entries) {\n const colonIndex = key.indexOf('::');\n const queryName = colonIndex >= 0 ? key.substring(0, colonIndex) : key;\n\n let estimatedBytes = 0;\n try {\n if (entry.lastResult !== undefined) {\n estimatedBytes = JSON.stringify(entry.lastResult).length;\n }\n } catch {\n // Ignore serialization errors\n }\n\n if (entry.subscriberCount > 0 && !entry.subscribed) {\n unhealthyCount++;\n }\n\n totalBytes += estimatedBytes;\n entries.push({\n key,\n queryName,\n subscriberCount: entry.subscriberCount,\n listenerCount: entry.listeners.size,\n subscribed: entry.subscribed,\n hasResult: entry.lastResult !== undefined,\n estimatedBytes,\n });\n }\n\n return {\n healthy: unhealthyCount === 0,\n entryCount: this._entries.size,\n estimatedBytes: totalBytes,\n entries,\n };\n }\n}\n"],"names":[],"mappings":";;MAoEa,kBAAkB,CAAA;AAWE,IAAA,YAAA;AAVZ,IAAA,QAAQ,GAAG,IAAI,GAAG,EAA2C;AACtE,IAAA,eAAe;AASvB,IAAA,WAAA,CAA6B,eAAuB,MAAM,EAAA;QAA7B,IAAA,CAAA,YAAY,GAAZ,YAAY;IACzC;IAUA,QAAQ,CAAC,aAAqB,EAAE,IAAa,EAAA;AACzC,QAAA,IAAI,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE;YACzC,OAAO,CAAA,EAAG,aAAa,CAAA,EAAA,CAAI;QAC/B;AAEA,QAAA,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI;AAC1B,aAAA,IAAI;AACJ,aAAA,MAAM,CAA0B,CAAC,WAAW,EAAE,GAAG,KAAI;YAClD,WAAW,CAAC,GAAG,CAAC,GAAI,IAAgC,CAAC,GAAG,CAAC;AACzD,YAAA,OAAO,WAAW;QACtB,CAAC,EAAE,EAAE,CAAC;QAEV,OAAO,CAAA,EAAG,aAAa,CAAA,EAAA,EAAK,IAAI,CA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AAE,UAAU,CAAC,GAAG,GAAG;YAEtE,IAAI,cAAc,GAAG,CAAC;AACtB,YAAA,IAAI;AACA,gBAAA,IAAI,KAAK,CAAC,UAAU,KAAK,SAAS,EAAE;oBAChC,cAAc,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,MAAM;gBAC5D;YACJ;AAAE,YAAA,MAAM;YAER;YAEA,IAAI,KAAK,CAAC,eAAe,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE;AAChD,gBAAA,cAAc,EAAE;YACpB;YAEA,UAAU,IAAI,cAAc;YAC5B,OAAO,CAAC,IAAI,CAAC;gBACT,GAAG;gBACH,SAAS;gBACT,eAAe,EAAE,KAAK,CAAC,eAAe;AACtC,gBAAA,aAAa,EAAE,KAAK,CAAC,SAAS,CAAC,IAAI;gBACnC,UAAU,EAAE,KAAK,CAAC,UAAU;AAC5B,gBAAA,SAAS,EAAE,KAAK,CAAC,UAAU,KAAK,SAAS;gBACzC,cAAc;AACjB,aAAA,CAAC;QACN;QAEA,OAAO;YACH,OAAO,EAAE,cAAc,KAAK,CAAC;AAC7B,YAAA,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI;AAC9B,YAAA,cAAc,EAAE,UAAU;YAC1B,OAAO;SACV;IACL;AACH;;;;"}
|
|
@@ -20,6 +20,7 @@ export declare class ServerSentEventHubConnection implements IObservableQueryHub
|
|
|
20
20
|
private readonly _keepAlive;
|
|
21
21
|
constructor(_sseUrl: string, _subscribeUrl: string, _unsubscribeUrl: string, _microservice: string, keepAliveIntervalMs?: number, _connectTimeoutMs?: number, _policy?: IReconnectPolicy);
|
|
22
22
|
get queryCount(): number;
|
|
23
|
+
get isConnected(): boolean;
|
|
23
24
|
get lastPingLatency(): number;
|
|
24
25
|
get averageLatency(): number;
|
|
25
26
|
subscribe(queryId: string, request: SubscriptionRequest, callback: DataReceived<any>): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"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;IACrC,OAAO,CAAC,eAAe,CAAgB;IACvC,OAAO,CAAC,oBAAoB,CAAC,CAAgC;IAC7D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAyB;gBAe/B,OAAO,EAAE,MAAM,EACf,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,MAAM,EACvB,aAAa,EAAE,MAAM,EACtC,mBAAmB,GAAE,MAAc,EAClB,iBAAiB,GAAE,MAAc,EACjC,OAAO,GAAE,gBAAwC;IAqBtE,IAAI,UAAU,IAAI,MAAM,CAEvB;IAGD,IAAI,WAAW,IAAI,OAAO,CAEzB;IAGD,IAAI,eAAe,IAAI,MAAM,CAE5B;IAGD,IAAI,cAAc,IAAI,MAAM,CAG3B;IAGD,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,YAAY,CAAC,GAAG,CAAC,GAAG,IAAI;IAe3F,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAclC,OAAO,IAAI,IAAI;IAYf,OAAO,CAAC,eAAe;IAYvB,OAAO,CAAC,KAAK;IAmBb,OAAO,CAAC,eAAe;IAwCvB,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,mBAAmB;IAO3B,OAAO,CAAC,aAAa;IA2BrB,OAAO,CAAC,eAAe;IAcvB,OAAO,CAAC,iBAAiB;IAUzB,OAAO,CAAC,kBAAkB;IAW1B,OAAO,CAAC,aAAa;IA+CrB,OAAO,CAAC,eAAe;CAkB1B"}
|
|
@@ -40,6 +40,9 @@ class ServerSentEventHubConnection {
|
|
|
40
40
|
get queryCount() {
|
|
41
41
|
return this._subscriptions.size;
|
|
42
42
|
}
|
|
43
|
+
get isConnected() {
|
|
44
|
+
return this._connectionId !== undefined && this._eventSource?.readyState === EventSource.OPEN;
|
|
45
|
+
}
|
|
43
46
|
get lastPingLatency() {
|
|
44
47
|
return this._lastPongLatency;
|
|
45
48
|
}
|