@coasys/ad4m 0.13.1-staging.13 → 0.13.1-staging.14
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/lib/index.cjs +548 -477
- package/lib/index.js +548 -473
- package/lib/src/agent/AgentClient.d.ts +9 -8
- package/lib/src/agent/AgentClient.js +46 -67
- package/lib/src/cache/KeyedCache.d.ts +51 -0
- package/lib/src/cache/KeyedCache.js +142 -0
- package/lib/src/expression/ExpressionClient.d.ts +14 -3
- package/lib/src/expression/ExpressionClient.js +49 -111
- package/lib/src/generated/api/ExpressionLookupError.d.ts +7 -0
- package/lib/src/generated/api/ExpressionLookupError.js +2 -0
- package/lib/src/generated/api/ExpressionManyEntry.d.ts +6 -0
- package/lib/src/generated/api/ExpressionManyEntry.js +1 -0
- package/lib/src/generated/api/FlowFiredEntry.d.ts +11 -0
- package/lib/src/generated/api/FlowFiredEntry.js +2 -0
- package/lib/src/generated/api/PerspectiveFlowFiredParams.d.ts +3 -3
- package/lib/src/generated/api/RpcMethods.d.ts +4 -2
- package/lib/src/model/Ad4mModel.d.ts +26 -2
- package/lib/src/model/Ad4mModel.js +125 -82
- package/lib/src/model/ModelQueryBuilder.d.ts +3 -0
- package/lib/src/model/ModelQueryBuilder.js +42 -14
- package/lib/src/model/index.d.ts +0 -1
- package/lib/src/model/index.js +0 -1
- package/lib/src/model/query-common.d.ts +8 -0
- package/lib/src/model/query-common.js +12 -0
- package/lib/src/model/types.d.ts +12 -3
- package/lib/src/perspectives/FlowModels.js +1 -1
- package/lib/src/perspectives/LiveQuery.d.ts +1 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +17 -6
- package/lib/src/perspectives/PerspectiveClient.js +83 -44
- package/lib/src/perspectives/PerspectiveProxy.d.ts +19 -10
- package/lib/src/perspectives/PerspectiveProxy.js +31 -69
- package/package.json +2 -2
- package/src/Ad4mClient.test.ts +17 -6
- package/src/agent/AgentClient.ts +46 -74
- package/src/cache/KeyedCache.test.ts +162 -0
- package/src/cache/KeyedCache.ts +179 -0
- package/src/cache/cache.test.ts +294 -71
- package/src/expression/ExpressionClient.ts +46 -131
- package/src/generated/api/ExpressionLookupError.ts +6 -0
- package/src/generated/api/ExpressionManyEntry.ts +8 -0
- package/src/generated/api/FlowFiredEntry.ts +11 -0
- package/src/generated/api/PerspectiveFlowFiredParams.ts +3 -3
- package/src/generated/api/RpcMethods.ts +4 -2
- package/src/model/Ad4mModel.test.ts +237 -43
- package/src/model/Ad4mModel.ts +132 -98
- package/src/model/ModelQueryBuilder.ts +41 -15
- package/src/model/count-result-type.test.ts +72 -0
- package/src/model/index.ts +0 -1
- package/src/model/query-common.ts +13 -0
- package/src/model/relation-filtering.test.ts +3 -5
- package/src/model/types.ts +9 -3
- package/src/model/write-round-trips.test.ts +30 -6
- package/src/perspectives/FlowModels.ts +1 -1
- package/src/perspectives/LiveQuery.ts +1 -1
- package/src/perspectives/PerspectiveClient.test.ts +287 -0
- package/src/perspectives/PerspectiveClient.ts +101 -66
- package/src/perspectives/PerspectiveProxy.test.ts +6 -14
- package/src/perspectives/PerspectiveProxy.ts +33 -67
- package/src/perspectives/TextContent.test.ts +0 -16
- package/src/perspectives/shacl-rpc.test.ts +1 -1
- package/lib/src/model/query-cache.d.ts +0 -30
- package/lib/src/model/query-cache.js +0 -66
- package/src/model/query-cache.test.ts +0 -54
- package/src/model/query-cache.ts +0 -79
|
@@ -14,7 +14,6 @@ import { Link } from "../links/Links";
|
|
|
14
14
|
import { LinkQuery } from "./LinkQuery";
|
|
15
15
|
import { Literal } from "../Literal";
|
|
16
16
|
import { getPropertiesMetadata, getRelationsMetadata } from "../model/decorators";
|
|
17
|
-
import { getCachedResult, setCachedResult, invalidatePerspectiveCache } from "../model/query-cache";
|
|
18
17
|
import { LiveQuery } from "./LiveQuery";
|
|
19
18
|
import { SHACLShape } from "../shacl/SHACLShape";
|
|
20
19
|
import { SHACLFlow } from "../shacl/SHACLFlow";
|
|
@@ -285,9 +284,7 @@ export class PerspectiveProxy {
|
|
|
285
284
|
* @param batchId - Optional batch ID to group this operation with others
|
|
286
285
|
*/
|
|
287
286
|
async executeAction(actions, expression, parameters, batchId) {
|
|
288
|
-
|
|
289
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
290
|
-
return result;
|
|
287
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").executeCommands(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, JSON.stringify(actions), expression, JSON.stringify(parameters), batchId);
|
|
291
288
|
}
|
|
292
289
|
/**
|
|
293
290
|
* Retrieves links from the perspective that match the given query.
|
|
@@ -515,12 +512,15 @@ export class PerspectiveProxy {
|
|
|
515
512
|
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").flowStates(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, instances);
|
|
516
513
|
}
|
|
517
514
|
/**
|
|
518
|
-
*
|
|
519
|
-
* agent's executor recorded them — the flow's history,
|
|
515
|
+
* For each of `instances`, in order, the proposals whose votes settled an
|
|
516
|
+
* edge, as this agent's executor recorded them — the flow's history,
|
|
517
|
+
* sorted by URI (`fired: []` when none). One call covers up to 200
|
|
518
|
+
* instances (the executor refuses more with a 400); split a longer list
|
|
519
|
+
* into batches of 200.
|
|
520
520
|
* Replaces the `resolvedAs` property proposals used to carry.
|
|
521
521
|
*/
|
|
522
|
-
async flowFired(
|
|
523
|
-
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").flowFired(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid,
|
|
522
|
+
async flowFired(instances) {
|
|
523
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").flowFired(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, instances);
|
|
524
524
|
}
|
|
525
525
|
/**
|
|
526
526
|
* Called whenever a flow instance on this perspective changes state on
|
|
@@ -584,20 +584,18 @@ export class PerspectiveProxy {
|
|
|
584
584
|
* keep its blocking thread busy until it completes; what's saved is the
|
|
585
585
|
* serialise + network + client-deserialise tax on the result. Aborted
|
|
586
586
|
* calls reject with `DOMException('Aborted', 'AbortError')` — match
|
|
587
|
-
* `fetch()` for handling.
|
|
588
|
-
*
|
|
587
|
+
* `fetch()` for handling. A call with `options` makes its own RPC.
|
|
588
|
+
*
|
|
589
|
+
* **Sharing:** concurrent identical calls without `options` share one RPC;
|
|
590
|
+
* each caller gets its own result object. Nothing is cached once the
|
|
591
|
+
* call settles: use a live query for repeated reads of the same data.
|
|
589
592
|
*
|
|
590
593
|
* @param query - SPARQL query string (read-only operations only)
|
|
591
594
|
* @param options - Optional call options (e.g. AbortSignal for cancellation)
|
|
592
595
|
* @returns Query results as parsed JSON
|
|
593
596
|
*/
|
|
594
597
|
async querySparql(query, options) {
|
|
595
|
-
|
|
596
|
-
if (cached !== undefined)
|
|
597
|
-
return cached;
|
|
598
|
-
const result = await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").querySparql(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, query, options);
|
|
599
|
-
setCachedResult(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, query, result);
|
|
600
|
-
return result;
|
|
598
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").querySparql(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, query, options);
|
|
601
599
|
}
|
|
602
600
|
/**
|
|
603
601
|
* Execute a model query — the executor-side replacement for SPARQL query building + JS hydration.
|
|
@@ -610,7 +608,7 @@ export class PerspectiveProxy {
|
|
|
610
608
|
* query over their union, whose rows carry `__subjectClass` and
|
|
611
609
|
* `__subjectClasses` (#1238)
|
|
612
610
|
* @param queryJson - Structured query as JSON string
|
|
613
|
-
* @returns Object with `instances` array and `totalCount`
|
|
611
|
+
* @returns Object with `instances` array, and `totalCount` when the query sets `count: true`
|
|
614
612
|
*/
|
|
615
613
|
async modelQuery(className, queryJson, options) {
|
|
616
614
|
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").modelQuery(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, className, queryJson, options);
|
|
@@ -704,9 +702,7 @@ export class PerspectiveProxy {
|
|
|
704
702
|
* ```
|
|
705
703
|
*/
|
|
706
704
|
async add(link, status = 'shared', batchId) {
|
|
707
|
-
|
|
708
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
709
|
-
return result;
|
|
705
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addLink(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, link, status, batchId);
|
|
710
706
|
}
|
|
711
707
|
/**
|
|
712
708
|
* Adds multiple links to the perspective in a single operation.
|
|
@@ -718,9 +714,7 @@ export class PerspectiveProxy {
|
|
|
718
714
|
* @returns Array of created LinkExpressions
|
|
719
715
|
*/
|
|
720
716
|
async addLinks(links, status = 'shared', batchId) {
|
|
721
|
-
|
|
722
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
723
|
-
return result;
|
|
717
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addLinks(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, links, status, batchId);
|
|
724
718
|
}
|
|
725
719
|
/**
|
|
726
720
|
* Removes multiple links from the perspective.
|
|
@@ -730,9 +724,7 @@ export class PerspectiveProxy {
|
|
|
730
724
|
* @returns Array of removed LinkExpressions
|
|
731
725
|
*/
|
|
732
726
|
async removeLinks(links, batchId) {
|
|
733
|
-
|
|
734
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
735
|
-
return result;
|
|
727
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").removeLinks(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, links, batchId);
|
|
736
728
|
}
|
|
737
729
|
/**
|
|
738
730
|
* Applies a set of link mutations (adds and removes) in a single operation.
|
|
@@ -743,9 +735,7 @@ export class PerspectiveProxy {
|
|
|
743
735
|
* @returns Object containing results of the mutations
|
|
744
736
|
*/
|
|
745
737
|
async linkMutations(mutations, status = 'shared') {
|
|
746
|
-
|
|
747
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
748
|
-
return result;
|
|
738
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").linkMutations(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, mutations, status);
|
|
749
739
|
}
|
|
750
740
|
/**
|
|
751
741
|
* Adds a pre-signed LinkExpression to the perspective.
|
|
@@ -772,9 +762,7 @@ export class PerspectiveProxy {
|
|
|
772
762
|
* @param batchId - Optional batch ID to group this operation with others
|
|
773
763
|
*/
|
|
774
764
|
async update(oldLink, newLink, batchId) {
|
|
775
|
-
|
|
776
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
777
|
-
return result;
|
|
765
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").updateLink(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, oldLink, newLink, batchId);
|
|
778
766
|
}
|
|
779
767
|
/**
|
|
780
768
|
* Removes a link from the perspective.
|
|
@@ -820,9 +808,7 @@ export class PerspectiveProxy {
|
|
|
820
808
|
else {
|
|
821
809
|
resolvedLink = link;
|
|
822
810
|
}
|
|
823
|
-
|
|
824
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
825
|
-
return result;
|
|
811
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").removeLink(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, resolvedLink, batchId);
|
|
826
812
|
}
|
|
827
813
|
/** Creates a new batch for grouping operations */
|
|
828
814
|
async createBatch() {
|
|
@@ -830,9 +816,7 @@ export class PerspectiveProxy {
|
|
|
830
816
|
}
|
|
831
817
|
/** Commits a batch of operations */
|
|
832
818
|
async commitBatch(batchId) {
|
|
833
|
-
|
|
834
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
835
|
-
return result;
|
|
819
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").commitBatch(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, batchId);
|
|
836
820
|
}
|
|
837
821
|
/**
|
|
838
822
|
* Drops an open batch without committing it — the recovery for a failed batched call:
|
|
@@ -1200,13 +1184,13 @@ export class PerspectiveProxy {
|
|
|
1200
1184
|
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").getShaclTargetClass(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, name);
|
|
1201
1185
|
}
|
|
1202
1186
|
/**
|
|
1203
|
-
* Get
|
|
1204
|
-
*
|
|
1187
|
+
* Get the SHACL shapes stored in this Perspective in one RPC call: all of
|
|
1188
|
+
* them, or only those named in `names` (unknown names are left out).
|
|
1205
1189
|
* A shape that fails to decode is skipped with a `console.warn`, so it
|
|
1206
1190
|
* does not hide the others.
|
|
1207
1191
|
*/
|
|
1208
|
-
async getAllShacl() {
|
|
1209
|
-
const entries = await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").getAllShacl(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
1192
|
+
async getAllShacl(options) {
|
|
1193
|
+
const entries = await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").getAllShacl(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, options?.names);
|
|
1210
1194
|
const shapes = [];
|
|
1211
1195
|
for (const { name, shapeUri, links } of entries) {
|
|
1212
1196
|
try {
|
|
@@ -1447,7 +1431,6 @@ export class PerspectiveProxy {
|
|
|
1447
1431
|
* record that the abstract class reads, under its own name. */
|
|
1448
1432
|
async updateSubject(className, exprAddr, values, batchId) {
|
|
1449
1433
|
await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").updateSubject(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, className, exprAddr, JSON.stringify(values), batchId);
|
|
1450
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
1451
1434
|
}
|
|
1452
1435
|
/**
|
|
1453
1436
|
* Creates an instance of a subject class, validated by the executor against the class's
|
|
@@ -1466,42 +1449,23 @@ export class PerspectiveProxy {
|
|
|
1466
1449
|
* const { baseUri } = await perspective.instanceCreate({ className: "Task", properties: { title: "Write" } });
|
|
1467
1450
|
*/
|
|
1468
1451
|
async instanceCreate(body) {
|
|
1469
|
-
|
|
1470
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
1471
|
-
return created;
|
|
1452
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").instanceCreate(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, body);
|
|
1472
1453
|
}
|
|
1473
1454
|
/** Sets single-valued properties of an instance, validated; the others stay. See {@link instanceCreate}. */
|
|
1474
1455
|
async instanceUpdate(body) {
|
|
1475
|
-
|
|
1476
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
1477
|
-
return updated;
|
|
1456
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").instanceUpdate(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, body);
|
|
1478
1457
|
}
|
|
1479
1458
|
/** Removes an instance and every link to and from it. See {@link instanceCreate}. */
|
|
1480
1459
|
async instanceRemove(body) {
|
|
1481
|
-
|
|
1482
|
-
// come after some are gone: invalidate either way.
|
|
1483
|
-
try {
|
|
1484
|
-
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").instanceRemove(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, body);
|
|
1485
|
-
}
|
|
1486
|
-
finally {
|
|
1487
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
1488
|
-
}
|
|
1460
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").instanceRemove(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, body);
|
|
1489
1461
|
}
|
|
1490
1462
|
/** Adds an item to a collection of an instance, validated. See {@link instanceCreate}. */
|
|
1491
1463
|
async instanceAddToCollection(body) {
|
|
1492
|
-
|
|
1493
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
1494
|
-
return added;
|
|
1464
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").instanceAddToCollection(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, body);
|
|
1495
1465
|
}
|
|
1496
1466
|
/** Removes an item from a collection of an instance. See {@link instanceCreate}. */
|
|
1497
1467
|
async instanceRemoveFromCollection(body) {
|
|
1498
|
-
|
|
1499
|
-
try {
|
|
1500
|
-
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").instanceRemoveFromCollection(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, body);
|
|
1501
|
-
}
|
|
1502
|
-
finally {
|
|
1503
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
1504
|
-
}
|
|
1468
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").instanceRemoveFromCollection(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, body);
|
|
1505
1469
|
}
|
|
1506
1470
|
async getSubjectData(subjectClass, exprAddr) {
|
|
1507
1471
|
if (typeof subjectClass === "string") {
|
|
@@ -1565,7 +1529,6 @@ export class PerspectiveProxy {
|
|
|
1565
1529
|
const ctor = subjectClass?.constructor;
|
|
1566
1530
|
if (typeof subjectClass === "object" && ctor && Object.prototype.hasOwnProperty.call(ctor, 'builtinUri')) {
|
|
1567
1531
|
await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").removeSubject(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, ctor.builtinUri, exprAddr, batchId);
|
|
1568
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
1569
1532
|
return;
|
|
1570
1533
|
}
|
|
1571
1534
|
let className = await this.stringOrTemplateObjectToSubjectClassName(subjectClass);
|
|
@@ -1575,7 +1538,6 @@ export class PerspectiveProxy {
|
|
|
1575
1538
|
const markers = await this.get(new LinkQuery({ predicate: "ad4m://abstract" }));
|
|
1576
1539
|
if (markers.some((link) => shapePattern.test(link.data.source))) {
|
|
1577
1540
|
await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").removeSubject(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, className, exprAddr, batchId);
|
|
1578
|
-
invalidatePerspectiveCache(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
1579
1541
|
return;
|
|
1580
1542
|
}
|
|
1581
1543
|
// Get destructor actions from SHACL links (Prolog-free)
|
package/package.json
CHANGED
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
"ws": "8.13.0"
|
|
54
54
|
},
|
|
55
55
|
"patchedDependencies": {},
|
|
56
|
-
"version": "0.13.1-staging.
|
|
57
|
-
"gitHead": "
|
|
56
|
+
"version": "0.13.1-staging.14",
|
|
57
|
+
"gitHead": "5b5bcb2f224e7fc1f3416f517099531e53a3622c",
|
|
58
58
|
"scripts": {
|
|
59
59
|
"build": "patch-package && tsc && pnpm run bundle",
|
|
60
60
|
"bundle": "rollup -c rollup.config.js",
|
package/src/Ad4mClient.test.ts
CHANGED
|
@@ -57,7 +57,7 @@ const MOCK_RESPONSES: Record<string, RpcHandler> = {
|
|
|
57
57
|
'agent.lock': { did: 'did:test:123', isInitialized: true, isUnlocked: false },
|
|
58
58
|
'agent.unlock': { did: 'did:test:123', isInitialized: true, isUnlocked: true },
|
|
59
59
|
'agent.import': { did: 'did:test:imported', isInitialized: true, isUnlocked: true },
|
|
60
|
-
'agent.
|
|
60
|
+
'agent.byDIDs': (p: Record<string, unknown>) => (p.dids as string[]).map(did => ({ did, perspective: new Perspective() })),
|
|
61
61
|
'agent.updateProfile': { did: 'did:test:123', perspective: new Perspective(), directMessageLanguage: 'lang://dm' },
|
|
62
62
|
'agent.sign': 'signed-message-data',
|
|
63
63
|
'agent.isLocked': false,
|
|
@@ -199,10 +199,10 @@ const MOCK_RESPONSES: Record<string, RpcHandler> = {
|
|
|
199
199
|
};
|
|
200
200
|
},
|
|
201
201
|
'expression.getMany': (p: Record<string, unknown>) => {
|
|
202
|
-
//
|
|
202
|
+
// One entry per URL, in order: what `expression.get` answers for it.
|
|
203
203
|
const urls = (p.urls || []) as string[];
|
|
204
|
-
return urls.map((
|
|
205
|
-
author: 'did:test:
|
|
204
|
+
return urls.map(() => ({
|
|
205
|
+
author: 'did:test:123', timestamp: '2024-01-01', data: '{"content":"hello"}',
|
|
206
206
|
language: { address: 'lang://test' }, proof: { valid: true },
|
|
207
207
|
}));
|
|
208
208
|
},
|
|
@@ -513,6 +513,7 @@ describe('AgentClient', () => {
|
|
|
513
513
|
|
|
514
514
|
test('byDID() fetches agent by DID', async () => {
|
|
515
515
|
const agent = await ad4m.agent.byDID('did:test:other');
|
|
516
|
+
expect(lastRpcCall!.params.dids).toEqual(['did:test:other']);
|
|
516
517
|
expect(agent.did).toBe('did:test:other');
|
|
517
518
|
});
|
|
518
519
|
|
|
@@ -1159,8 +1160,18 @@ describe('ExpressionClient', () => {
|
|
|
1159
1160
|
test('getMany() returns multiple expressions', async () => {
|
|
1160
1161
|
const exprs = await ad4m.expression.getMany(['url1', 'url2']);
|
|
1161
1162
|
expect(exprs).toHaveLength(2);
|
|
1162
|
-
expect(exprs[0].author).toBe('did:test:
|
|
1163
|
-
expect(exprs[1].author).toBe('did:test:
|
|
1163
|
+
expect(exprs[0].author).toBe('did:test:123');
|
|
1164
|
+
expect(exprs[1].author).toBe('did:test:123');
|
|
1165
|
+
});
|
|
1166
|
+
|
|
1167
|
+
test('getMany() sends at most 200 URLs per expression.getMany call', async () => {
|
|
1168
|
+
const freshClient = newClient();
|
|
1169
|
+
const urls = Array.from({ length: 201 }, (_, i) => `lang://test/Qm${i}`);
|
|
1170
|
+
const exprs = await freshClient.expression.getMany(urls);
|
|
1171
|
+
expect(exprs).toHaveLength(201);
|
|
1172
|
+
const calls = lastOf(MockWebSocket.instances).rpc.filter(c => c.type === 'expression.getMany');
|
|
1173
|
+
expect(calls.map(c => (c.params.urls as string[]).length)).toEqual([200, 1]);
|
|
1174
|
+
expect(calls.flatMap(c => c.params.urls as string[])).toEqual(urls);
|
|
1164
1175
|
});
|
|
1165
1176
|
|
|
1166
1177
|
test('getRaw() returns raw expression', async () => {
|
package/src/agent/AgentClient.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {ApiClient, CallOptions } from '../apiClient';
|
|
1
|
+
import {ApiClient, CallOptions, RpcError } from '../apiClient';
|
|
2
2
|
import { PerspectiveInput } from "../perspectives/Perspective";
|
|
3
3
|
import {
|
|
4
4
|
Agent,
|
|
@@ -13,7 +13,8 @@ import { AgentStatus } from "./AgentStatus";
|
|
|
13
13
|
import { AGENT_KIND } from "./AgentKind";
|
|
14
14
|
import { LinkMutations, LinkExpression, LinkInput, linkEqual } from "../links/Links";
|
|
15
15
|
import { VerificationRequestResult } from "../runtime/RuntimeTypes";
|
|
16
|
-
import {
|
|
16
|
+
import { createPersistentCache } from "../cache/PersistentCache";
|
|
17
|
+
import { KeyedCache, StoredEntry } from "../cache/KeyedCache";
|
|
17
18
|
import type { Agent as AgentData } from "../generated/api/Agent";
|
|
18
19
|
import type { AgentSignature } from "../generated/api/AgentSignature";
|
|
19
20
|
import type { AgentWithIdentity } from "../generated/api/AgentWithIdentity";
|
|
@@ -29,38 +30,35 @@ function toAgent(data: AgentData | AgentWithIdentity | null): Agent | null {
|
|
|
29
30
|
return data ? Agent.fromWire(data) : null;
|
|
30
31
|
}
|
|
31
32
|
|
|
33
|
+
/** Most DIDs the executor takes in one `agent.byDIDs` call. */
|
|
34
|
+
const MAX_DIDS_PER_CALL = 200;
|
|
35
|
+
|
|
32
36
|
export class AgentClient {
|
|
33
37
|
#apiClient: ApiClient;
|
|
34
38
|
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
/** TTL for remote (non-self) agent profiles in L1 cache (ms). */
|
|
39
|
+
// byDID cache: concurrent misses go out in one `agent.byDIDs` call.
|
|
40
|
+
#agents: KeyedCache<Agent>;
|
|
41
|
+
/**
|
|
42
|
+
* TTL of every cached profile, the own one too, in memory and in the
|
|
43
|
+
* persistent cache (ms). `agent-updated` only comes from this executor, so
|
|
44
|
+
* a profile changed through another executor of the same agent shows up
|
|
45
|
+
* when the entry expires.
|
|
46
|
+
*/
|
|
44
47
|
static REMOTE_AGENT_TTL_MS = 5 * 60_000; // 5 minutes
|
|
45
|
-
/** TTL for remote agent profiles in the L2 (IndexedDB) cache (ms).
|
|
46
|
-
* After this window any restart triggers a fresh network fetch. */
|
|
47
|
-
static REMOTE_AGENT_TTL_L2_MS = 5 * 60_000; // 5 minutes
|
|
48
48
|
|
|
49
49
|
constructor(baseUrl: string, token?: string, sharedApiClient?: ApiClient) {
|
|
50
50
|
this.#apiClient = sharedApiClient || new ApiClient(baseUrl, token);
|
|
51
|
-
this.#
|
|
51
|
+
this.#agents = new KeyedCache<Agent>({
|
|
52
|
+
loadMany: (dids) => this.#loadAgents(dids),
|
|
53
|
+
maxBatch: MAX_DIDS_PER_CALL,
|
|
54
|
+
persistent: createPersistentCache<StoredEntry<Agent>>('ad4m-agent-cache', 'agents'),
|
|
55
|
+
maxAgeMs: () => AgentClient.REMOTE_AGENT_TTL_MS,
|
|
56
|
+
persistentMaxAgeMs: () => AgentClient.REMOTE_AGENT_TTL_MS,
|
|
57
|
+
});
|
|
52
58
|
}
|
|
53
59
|
|
|
54
60
|
async me(): Promise<Agent> {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
// Auto-set selfDid so event-driven cache invalidation works
|
|
58
|
-
// without requiring apps to call setSelfDid() manually
|
|
59
|
-
if (agentObject.did && !this.#selfDid) {
|
|
60
|
-
this.#selfDid = agentObject.did;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return agentObject;
|
|
61
|
+
return toAgent(await this.#apiClient.call('agent.get', {}));
|
|
64
62
|
}
|
|
65
63
|
|
|
66
64
|
async status(): Promise<AgentStatus> {
|
|
@@ -91,69 +89,43 @@ export class AgentClient {
|
|
|
91
89
|
return new AgentStatus(result);
|
|
92
90
|
}
|
|
93
91
|
|
|
92
|
+
/**
|
|
93
|
+
* The agent with this DID, or `null` when it is unknown. Concurrent calls
|
|
94
|
+
* for any DIDs share one `agent.byDIDs` request.
|
|
95
|
+
*/
|
|
94
96
|
async byDID(did: string): Promise<Agent> {
|
|
95
|
-
// agent-updated events
|
|
97
|
+
// agent-updated events refresh cached entries before their TTL runs out.
|
|
96
98
|
this.#listen();
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (cached) {
|
|
101
|
-
const isSelf = did === this.#selfDid;
|
|
102
|
-
// Self-DID: L1 always valid (event-driven invalidation)
|
|
103
|
-
// Remote DID: L1 valid within TTL
|
|
104
|
-
if (isSelf || (now - cached.ts) < AgentClient.REMOTE_AGENT_TTL_MS) {
|
|
105
|
-
return cached.promise;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Deduplicate: store the promise immediately so concurrent calls share one RPC
|
|
110
|
-
const promise = (async () => {
|
|
111
|
-
// L2: check persistent cache before network
|
|
112
|
-
const entry = await this.#persistent.get(did);
|
|
113
|
-
// entry.agent may be undefined for pre-TTL cache entries (migration) — treat as expired
|
|
114
|
-
if (entry?.agent && (now - (entry.ts ?? 0)) < AgentClient.REMOTE_AGENT_TTL_L2_MS) {
|
|
115
|
-
return entry.agent;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// L3: network fetch
|
|
119
|
-
const result = toAgent(await this.#apiClient.call('agent.byDid', { did }));
|
|
120
|
-
this.#persistent.put(did, { agent: result, ts: Date.now() }); // fire-and-forget write to L2
|
|
121
|
-
return result;
|
|
122
|
-
})();
|
|
123
|
-
|
|
124
|
-
this.#memCache.set(did, { promise, ts: now });
|
|
125
|
-
|
|
126
|
-
// Clean up on failure so next call retries
|
|
127
|
-
promise.catch(() => {
|
|
128
|
-
if (this.#memCache.get(did)?.promise === promise) {
|
|
129
|
-
this.#memCache.delete(did);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
99
|
+
return this.#agents.get(did);
|
|
100
|
+
}
|
|
132
101
|
|
|
133
|
-
|
|
102
|
+
/**
|
|
103
|
+
* One `agent.byDIDs` call for the misses of one tick. The executor fails
|
|
104
|
+
* that call when one DID's lookup fails (500), so the DIDs of a failed
|
|
105
|
+
* batch are then read alone: each caller gets what a lone `byDID` gives.
|
|
106
|
+
*/
|
|
107
|
+
async #loadAgents(dids: string[]): Promise<Array<Agent | null | Error>> {
|
|
108
|
+
try {
|
|
109
|
+
return (await this.#apiClient.call('agent.byDIDs', { dids })).map(toAgent);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (dids.length === 1 || !(error instanceof RpcError) || error.status !== 500) throw error;
|
|
112
|
+
return Promise.all(dids.map(did =>
|
|
113
|
+
this.#apiClient.call('agent.byDid', { did }).then(toAgent, (failure) =>
|
|
114
|
+
failure instanceof Error ? failure : new Error(String(failure)))));
|
|
115
|
+
}
|
|
134
116
|
}
|
|
135
117
|
|
|
136
118
|
#cacheAgent(agent: Agent): void {
|
|
137
119
|
if (!agent.did) return;
|
|
138
|
-
this.#
|
|
139
|
-
this.#persistent.put(agent.did, { agent, ts: Date.now() }); // fire-and-forget
|
|
120
|
+
this.#agents.set(agent.did, agent);
|
|
140
121
|
this.#listen();
|
|
141
122
|
}
|
|
142
123
|
|
|
143
|
-
/**
|
|
144
|
-
* Set the current agent's own DID.
|
|
145
|
-
* The self-DID receives event-driven invalidation and is never TTL-expired.
|
|
146
|
-
*/
|
|
147
|
-
setSelfDid(did: string): void {
|
|
148
|
-
this.#selfDid = did;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
124
|
/**
|
|
152
125
|
* Invalidate a specific DID's cache entry in both L1 and L2.
|
|
153
126
|
*/
|
|
154
127
|
invalidateByDid(did: string): void {
|
|
155
|
-
this.#
|
|
156
|
-
this.#persistent.delete(did); // fire-and-forget
|
|
128
|
+
this.#agents.delete(did);
|
|
157
129
|
}
|
|
158
130
|
|
|
159
131
|
/**
|
|
@@ -161,7 +133,7 @@ export class AgentClient {
|
|
|
161
133
|
* L2 (IndexedDB) is preserved — agent data remains valid across sessions.
|
|
162
134
|
*/
|
|
163
135
|
clearByDidCache(): void {
|
|
164
|
-
this.#
|
|
136
|
+
this.#agents.clear();
|
|
165
137
|
}
|
|
166
138
|
|
|
167
139
|
async updatePublicPerspective(perspective: PerspectiveInput): Promise<Agent> {
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { KeyedCache, StoredEntry } from './KeyedCache';
|
|
2
|
+
import { PersistentCache } from './PersistentCache';
|
|
3
|
+
|
|
4
|
+
function deferred<T>() {
|
|
5
|
+
let resolve!: (v: T) => void;
|
|
6
|
+
const promise = new Promise<T>(r => { resolve = r; });
|
|
7
|
+
return { promise, resolve };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
class MapStore<V> implements PersistentCache<StoredEntry<V>> {
|
|
11
|
+
entries = new Map<string, StoredEntry<V>>();
|
|
12
|
+
puts: string[] = [];
|
|
13
|
+
async get(key: string) { return this.entries.get(key); }
|
|
14
|
+
async put(key: string, value: StoredEntry<V>) { this.puts.push(key); this.entries.set(key, value); }
|
|
15
|
+
async delete(key: string) { this.entries.delete(key); }
|
|
16
|
+
async clear() { this.entries.clear(); }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe('KeyedCache', () => {
|
|
20
|
+
test('get() and getMany() misses of one tick share one loadMany; a later read is a hit', async () => {
|
|
21
|
+
const loadMany = jest.fn(async (keys: string[]) => keys.map(k => k.toUpperCase()));
|
|
22
|
+
const cache = new KeyedCache<string>({ loadMany });
|
|
23
|
+
const [a, many] = await Promise.all([cache.get('a'), cache.getMany(['b', 'a'])]);
|
|
24
|
+
expect(a).toBe('A');
|
|
25
|
+
expect(many).toEqual(['B', 'A']);
|
|
26
|
+
expect(loadMany.mock.calls).toEqual([[['a', 'b']]]);
|
|
27
|
+
expect(await cache.get('b')).toBe('B');
|
|
28
|
+
expect(loadMany).toHaveBeenCalledTimes(1);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('with load, a get() miss is read alone and settles without waiting for a slower key', async () => {
|
|
32
|
+
const slow = deferred<string>();
|
|
33
|
+
const load = jest.fn((key: string) => key === 'slow' ? slow.promise : Promise.resolve(key.toUpperCase()));
|
|
34
|
+
const loadMany = jest.fn(async (keys: string[]) => keys.map(k => k.toUpperCase()));
|
|
35
|
+
const cache = new KeyedCache<string>({ load, loadMany });
|
|
36
|
+
const slowRead = cache.get('slow');
|
|
37
|
+
expect(await cache.get('fast')).toBe('FAST');
|
|
38
|
+
// A getMany() shares the get() in flight, and only its other misses go to loadMany.
|
|
39
|
+
const many = cache.getMany(['fast', 'slow', 'b']);
|
|
40
|
+
slow.resolve('SLOW');
|
|
41
|
+
expect(await slowRead).toBe('SLOW');
|
|
42
|
+
expect(await many).toEqual(['FAST', 'SLOW', 'B']);
|
|
43
|
+
expect(load.mock.calls).toEqual([['slow'], ['fast']]);
|
|
44
|
+
expect(loadMany.mock.calls).toEqual([[['b']]]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('with load, a get() miss is served from the persistent store before load', async () => {
|
|
48
|
+
const persistent = new MapStore<string>();
|
|
49
|
+
persistent.entries.set('a', { value: 'stored', ts: Date.now() });
|
|
50
|
+
const load = jest.fn(async (key: string) => key);
|
|
51
|
+
const cache = new KeyedCache<string>({ load, loadMany: async keys => keys, persistent });
|
|
52
|
+
expect(await cache.get('a')).toBe('stored');
|
|
53
|
+
expect(load).not.toHaveBeenCalled();
|
|
54
|
+
expect(persistent.puts).toEqual([]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('with load, a failed get() rejects that read only, and the next get() retries', async () => {
|
|
58
|
+
const load = jest.fn().mockRejectedValueOnce(new Error('boom')).mockResolvedValue('ok');
|
|
59
|
+
const cache = new KeyedCache<string>({ load, loadMany: async keys => keys });
|
|
60
|
+
await expect(cache.get('a')).rejects.toThrow('boom');
|
|
61
|
+
expect(await cache.get('a')).toBe('ok');
|
|
62
|
+
expect(load).toHaveBeenCalledTimes(2);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('maxBatch splits one tick of misses into calls of at most that size', async () => {
|
|
66
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
67
|
+
const cache = new KeyedCache<string>({ loadMany, maxBatch: 2 });
|
|
68
|
+
expect(await cache.getMany(['a', 'b', 'c', 'd', 'e'])).toEqual(['a', 'b', 'c', 'd', 'e']);
|
|
69
|
+
expect(loadMany.mock.calls.map(([keys]) => keys)).toEqual([['a', 'b'], ['c', 'd'], ['e']]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('null (not found) is returned but not kept', async () => {
|
|
73
|
+
const loadMany = jest.fn(async (keys: string[]) => keys.map(() => null));
|
|
74
|
+
const cache = new KeyedCache<string>({ loadMany });
|
|
75
|
+
expect(await cache.get('x')).toBeNull();
|
|
76
|
+
expect(await cache.get('x')).toBeNull();
|
|
77
|
+
expect(loadMany).toHaveBeenCalledTimes(2);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('a set() while a read is in flight wins over the read', async () => {
|
|
81
|
+
const reply = deferred<string[]>();
|
|
82
|
+
const cache = new KeyedCache<string>({ loadMany: () => reply.promise });
|
|
83
|
+
const reading = cache.get('a');
|
|
84
|
+
await Promise.resolve();
|
|
85
|
+
cache.set('a', 'fresh');
|
|
86
|
+
reply.resolve(['stale']);
|
|
87
|
+
expect(await reading).toBe('stale');
|
|
88
|
+
expect(await cache.get('a')).toBe('fresh');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('a delete() while a read is in flight keeps the read out of the cache', async () => {
|
|
92
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
93
|
+
const cache = new KeyedCache<string>({ loadMany });
|
|
94
|
+
const reading = cache.get('a');
|
|
95
|
+
cache.delete('a');
|
|
96
|
+
await reading;
|
|
97
|
+
await cache.get('a');
|
|
98
|
+
expect(loadMany).toHaveBeenCalledTimes(2);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('a clear() while a read is in flight keeps the read out of the cache', async () => {
|
|
102
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
103
|
+
const cache = new KeyedCache<string>({ loadMany });
|
|
104
|
+
const reading = cache.get('a');
|
|
105
|
+
cache.clear();
|
|
106
|
+
await reading;
|
|
107
|
+
await cache.get('a');
|
|
108
|
+
expect(loadMany).toHaveBeenCalledTimes(2);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('maxAgeMs expires a value per key', async () => {
|
|
112
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
113
|
+
const cache = new KeyedCache<string>({ loadMany, maxAgeMs: k => (k === 'keep' ? Infinity : 0) });
|
|
114
|
+
await cache.getMany(['keep', 'drop']);
|
|
115
|
+
await cache.getMany(['keep', 'drop']);
|
|
116
|
+
expect(loadMany.mock.calls.map(([keys]) => keys)).toEqual([['keep', 'drop'], ['drop']]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('limits evict the oldest values first, by count and by JSON size', async () => {
|
|
120
|
+
let limits = { entries: 2, bytes: Infinity };
|
|
121
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
122
|
+
const cache = new KeyedCache<string>({ loadMany, limits: () => limits });
|
|
123
|
+
await cache.getMany(['a', 'b']);
|
|
124
|
+
await cache.get('c'); // evicts a
|
|
125
|
+
await cache.getMany(['b', 'c']);
|
|
126
|
+
expect(loadMany).toHaveBeenCalledTimes(2);
|
|
127
|
+
await cache.get('a');
|
|
128
|
+
expect(loadMany).toHaveBeenCalledTimes(3);
|
|
129
|
+
|
|
130
|
+
limits = { entries: 100, bytes: 6 }; // each value is 3 bytes of JSON ("x")
|
|
131
|
+
cache.clear();
|
|
132
|
+
await cache.getMany(['x', 'y']);
|
|
133
|
+
await cache.get('z'); // evicts x
|
|
134
|
+
await cache.get('x');
|
|
135
|
+
expect(loadMany.mock.calls.slice(3).map(([keys]) => keys)).toEqual([['x', 'y'], ['z'], ['x']]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('the persistent tier serves fresh entries without a load and is not rewritten by them', async () => {
|
|
139
|
+
const store = new MapStore<string>();
|
|
140
|
+
store.entries.set('fresh', { value: 'stored', ts: Date.now() });
|
|
141
|
+
store.entries.set('stale', { value: 'old', ts: Date.now() - 10_000 });
|
|
142
|
+
store.entries.set('legacy', { agent: 'old format' } as any);
|
|
143
|
+
const loadMany = jest.fn(async (keys: string[]) => keys.map(k => `loaded:${k}`));
|
|
144
|
+
const cache = new KeyedCache<string>({ loadMany, persistent: store, persistentMaxAgeMs: () => 5_000 });
|
|
145
|
+
|
|
146
|
+
expect(await cache.getMany(['fresh', 'stale', 'legacy'])).toEqual(['stored', 'loaded:stale', 'loaded:legacy']);
|
|
147
|
+
expect(loadMany.mock.calls).toEqual([[['stale', 'legacy']]]);
|
|
148
|
+
expect(store.puts.sort()).toEqual(['legacy', 'stale']);
|
|
149
|
+
expect(store.entries.get('stale')!.value).toBe('loaded:stale');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('clear() keeps the persistent tier unless asked', async () => {
|
|
153
|
+
const store = new MapStore<string>();
|
|
154
|
+
const cache = new KeyedCache<string>({ loadMany: async keys => keys, persistent: store });
|
|
155
|
+
await cache.get('a');
|
|
156
|
+
await new Promise(r => setTimeout(r, 0));
|
|
157
|
+
cache.clear();
|
|
158
|
+
expect(store.entries.has('a')).toBe(true);
|
|
159
|
+
cache.clear(true);
|
|
160
|
+
expect(store.entries.has('a')).toBe(false);
|
|
161
|
+
});
|
|
162
|
+
});
|