@cratis/arc 22.148.0 → 22.148.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@cratis/arc",
4
- "version": "22.148.0",
4
+ "version": "22.148.1",
5
5
  "description": "TypeScript command, query, validation, identity, and messaging runtime for Cratis Arc — the CQRS application framework for ASP.NET Core — used by generated proxies.",
6
6
  "keywords": [
7
7
  "cratis",
@@ -50,13 +50,13 @@ export class ObservableQueryDiagnostics implements IObservableQueryDiagnostics {
50
50
  /** @inheritdoc */
51
51
  beginTracking(cacheKey: string, owner: string): void {
52
52
  this._ownersByKey.set(cacheKey, owner);
53
- this._publishSnapshot(this._createSnapshot());
53
+ this._publishSnapshotWhenObserved();
54
54
  }
55
55
 
56
56
  /** @inheritdoc */
57
57
  endTracking(cacheKey: string): void {
58
58
  this._ownersByKey.delete(cacheKey);
59
- this._publishSnapshot(this._createSnapshot());
59
+ this._publishSnapshotWhenObserved();
60
60
  }
61
61
 
62
62
  private _buildMultiplexerDiagnostics(): MultiplexerDiagnostics {
@@ -117,6 +117,12 @@ export class ObservableQueryDiagnostics implements IObservableQueryDiagnostics {
117
117
  };
118
118
  }
119
119
 
120
+ private _publishSnapshotWhenObserved(): void {
121
+ if (this._snapshots.observed) {
122
+ this._publishSnapshot(this._createSnapshot());
123
+ }
124
+ }
125
+
120
126
  private _publishSnapshot(snapshot: ObservableQueryDiagnosticsSnapshot): void {
121
127
  this._snapshots.next(snapshot);
122
128
  }
@@ -0,0 +1,69 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { describe, it } from 'vitest';
5
+ import { ObservableQueryDiagnostics, QueryInstanceCache } from '../../queries/index.js';
6
+
7
+ function createDiagnostics() {
8
+ const cache = new QueryInstanceCache(0);
9
+ let scans = 0;
10
+ const original = cache.getDiagnosticsSnapshot.bind(cache);
11
+ cache.getDiagnosticsSnapshot = () => {
12
+ scans++;
13
+ return original();
14
+ };
15
+ const diagnostics = new ObservableQueryDiagnostics(
16
+ cache,
17
+ () => undefined,
18
+ () => ({ queryTransportMethod: 'ServerSentEvents', queryDirectMode: false })
19
+ );
20
+ return { diagnostics, scans: () => scans };
21
+ }
22
+
23
+ describe('when tracking query owners without observers', () => {
24
+ it('should not scan the cache when beginning and ending tracking', () => {
25
+ const { diagnostics, scans } = createDiagnostics();
26
+
27
+ diagnostics.beginTracking('cache-key', 'OrdersPage');
28
+ diagnostics.endTracking('cache-key');
29
+
30
+ scans().should.equal(0);
31
+ });
32
+
33
+ it('should scan the cache and publish when there is an observer', () => {
34
+ const { diagnostics, scans } = createDiagnostics();
35
+ let published = 0;
36
+ diagnostics.snapshots$.subscribe(() => published++);
37
+
38
+ diagnostics.beginTracking('cache-key', 'OrdersPage');
39
+ diagnostics.endTracking('cache-key');
40
+
41
+ scans().should.equal(2);
42
+ published.should.equal(2);
43
+ });
44
+
45
+ it('should not scan the cache again after the observer unsubscribes', () => {
46
+ const { diagnostics, scans } = createDiagnostics();
47
+ const subscription = diagnostics.snapshots$.subscribe();
48
+ diagnostics.beginTracking('cache-key', 'OrdersPage');
49
+ subscription.unsubscribe();
50
+ const scansBefore = scans();
51
+
52
+ diagnostics.endTracking('cache-key');
53
+ diagnostics.beginTracking('other-key', 'OrdersPage');
54
+
55
+ scans().should.equal(scansBefore);
56
+ });
57
+
58
+ it('should report current ownership from an explicit snapshot after untracked tracking', () => {
59
+ const { diagnostics } = createDiagnostics();
60
+
61
+ diagnostics.beginTracking('cache-key', 'OrdersPage');
62
+ diagnostics.beginTracking('other-key', 'OrdersPage');
63
+ diagnostics.endTracking('other-key');
64
+
65
+ const ownership = diagnostics.getSnapshot().ownership;
66
+ ownership.ownersByQueryKey.should.deep.equal({ 'cache-key': 'OrdersPage' });
67
+ ownership.queriesByOwner.should.deep.equal({ OrdersPage: ['cache-key'] });
68
+ });
69
+ });