jazz-tools 0.8.12 → 0.8.13
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +6 -0
- package/dist/native/coValues/account.js +4 -2
- package/dist/native/coValues/account.js.map +1 -1
- package/dist/native/implementation/refs.js +2 -10
- package/dist/native/implementation/refs.js.map +1 -1
- package/dist/native/lib/cache.js +13 -0
- package/dist/native/lib/cache.js.map +1 -0
- package/dist/native/lib/cache.test.js +52 -0
- package/dist/native/lib/cache.test.js.map +1 -0
- package/dist/web/coValues/account.js +4 -2
- package/dist/web/coValues/account.js.map +1 -1
- package/dist/web/implementation/refs.js +2 -10
- package/dist/web/implementation/refs.js.map +1 -1
- package/dist/web/lib/cache.js +13 -0
- package/dist/web/lib/cache.js.map +1 -0
- package/dist/web/lib/cache.test.js +52 -0
- package/dist/web/lib/cache.test.js.map +1 -0
- package/package.json +1 -1
- package/src/coValues/account.ts +6 -2
- package/src/implementation/refs.ts +4 -10
- package/src/lib/cache.test.ts +64 -0
- package/src/lib/cache.ts +16 -0
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,7 @@ import { LocalNode, cojsonInternals } from "cojson";
|
|
2
2
|
import { RawAccount, } from "cojson";
|
3
3
|
import { CoMap, AnonymousJazzAgent, } from "../internal.js";
|
4
4
|
import { Group, CoValueBase, MembersSym, Profile, Ref, SchemaInit, inspect, subscriptionsScopes, loadCoValue, subscribeToCoValue, ensureCoValueLoaded, subscribeToExistingCoValue, } from "../internal.js";
|
5
|
+
import { coValuesCache } from "../lib/cache.js";
|
5
6
|
/** @category Identity & Permissions */
|
6
7
|
export class Account extends CoValueBase {
|
7
8
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
@@ -27,8 +28,9 @@ export class Account extends CoValueBase {
|
|
27
28
|
get _loadedAs() {
|
28
29
|
if (this.isMe)
|
29
30
|
return this;
|
30
|
-
|
31
|
-
|
31
|
+
const rawAccount = this._raw.core.node.account;
|
32
|
+
if (rawAccount instanceof RawAccount) {
|
33
|
+
return coValuesCache.get(rawAccount, () => Account.fromRaw(rawAccount));
|
32
34
|
}
|
33
35
|
return new AnonymousJazzAgent(this._raw.core.node);
|
34
36
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"account.js","sourceRoot":"","sources":["../../../src/coValues/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACpD,OAAO,EAMH,UAAU,GAKb,MAAM,QAAQ,CAAC;AAChB,OAAO,EACH,KAAK,EASL,kBAAkB,GACrB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACH,KAAK,EACL,WAAW,EACX,UAAU,EACV,OAAO,EACP,GAAG,EACH,UAAU,EACV,OAAO,EACP,mBAAmB,EACnB,WAAW,EACX,kBAAkB,EAClB,mBAAmB,EACnB,0BAA0B,GAC7B,MAAM,gBAAgB,CAAC;
|
1
|
+
{"version":3,"file":"account.js","sourceRoot":"","sources":["../../../src/coValues/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACpD,OAAO,EAMH,UAAU,GAKb,MAAM,QAAQ,CAAC;AAChB,OAAO,EACH,KAAK,EASL,kBAAkB,GACrB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACH,KAAK,EACL,WAAW,EACX,UAAU,EACV,OAAO,EACP,GAAG,EACH,UAAU,EACV,OAAO,EACP,mBAAmB,EACnB,WAAW,EACX,kBAAkB,EAClB,mBAAmB,EACnB,0BAA0B,GAC7B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,uCAAuC;AACvC,MAAM,OAAO,OAAQ,SAAQ,WAAW;IAKpC,8DAA8D;IAC9D,MAAM,CAAC,OAAO,CAAM;IACpB,IAAI,OAAO;QAIP,OAAQ,IAAI,CAAC,WAA8B,CAAC,OAAO,CAAC;IACxD,CAAC;IACD;QACI,IAAI,CAAC,OAAO,GAA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1,5 @@
|
|
1
1
|
import { instantiateRefEncoded, isRefEncoded, subscriptionsScopes, } from "../internal.js";
|
2
|
-
|
2
|
+
import { coValuesCache } from "../lib/cache.js";
|
3
3
|
const TRACE_ACCESSES = false;
|
4
4
|
export class Ref {
|
5
5
|
id;
|
@@ -19,15 +19,7 @@ export class Ref {
|
|
19
19
|
: this.controlledAccount._raw.core.node;
|
20
20
|
const raw = node.getLoaded(this.id);
|
21
21
|
if (raw) {
|
22
|
-
|
23
|
-
if (value) {
|
24
|
-
// console.log("Using cached value for " + this.id);
|
25
|
-
}
|
26
|
-
else {
|
27
|
-
value = instantiateRefEncoded(this.schema, raw);
|
28
|
-
refCache.set(raw, value);
|
29
|
-
}
|
30
|
-
return value;
|
22
|
+
return coValuesCache.get(raw, () => instantiateRefEncoded(this.schema, raw));
|
31
23
|
}
|
32
24
|
else {
|
33
25
|
return null;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"refs.js","sourceRoot":"","sources":["../../../src/implementation/refs.ts"],"names":[],"mappings":"AASA,OAAO,EACH,qBAAqB,EACrB,YAAY,EACZ,mBAAmB,GACtB,MAAM,gBAAgB,CAAC;
|
1
|
+
{"version":3,"file":"refs.js","sourceRoot":"","sources":["../../../src/implementation/refs.ts"],"names":[],"mappings":"AASA,OAAO,EACH,qBAAqB,EACrB,YAAY,EACZ,mBAAmB,GACtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,MAAM,cAAc,GAAG,KAAK,CAAC;AAE7B,MAAM,OAAO,GAAG;IAEC;IACA;IACA;IAHb,YACa,EAAS,EACT,iBAA+C,EAC/C,MAAqB;QAFrB,OAAE,GAAF,EAAE,CAAO;QACT,sBAAiB,GAAjB,iBAAiB,CAA8B;QAC/C,WAAM,GAAN,MAAM,CAAe;QAE9B,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,2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C;gBACL,OAAO;oBACH,UAAU,EAAE,IAAI;oBAChB,YAAY,EAAE,IAAI;oBAClB,QAAQ,EAAE,IAAI;iBACjB,CAAC;YACN,CAAC;iBAAM,CAAC;gBACJ,OAAO,OAAO,CAAC,wBAAwB,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;YACzD,CAAC;QACL,CAAC;KACJ,CAAC,CAAC;AACP,CAAC"}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
const weakMap = new WeakMap();
|
2
|
+
export const coValuesCache = {
|
3
|
+
get: (raw, compute) => {
|
4
|
+
const cached = weakMap.get(raw);
|
5
|
+
if (cached) {
|
6
|
+
return cached;
|
7
|
+
}
|
8
|
+
const computed = compute();
|
9
|
+
weakMap.set(raw, computed);
|
10
|
+
return computed;
|
11
|
+
},
|
12
|
+
};
|
13
|
+
//# sourceMappingURL=cache.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../../src/lib/cache.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,GAAG,IAAI,OAAO,EAAuB,CAAC;AAEnD,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,GAAG,EAAE,CAAoB,GAAe,EAAE,OAAgB,EAAE,EAAE;QAC1D,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,MAAM,EAAE,CAAC;YACT,OAAO,MAAW,CAAC;QACvB,CAAC;QACD,MAAM,QAAQ,GAAG,OAAO,EAAE,CAAC;QAC3B,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;QAC3B,OAAO,QAAQ,CAAC;IACpB,CAAC;CACJ,CAAC"}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import { expect, describe, test } from "vitest";
|
2
|
+
import { coValuesCache } from "./cache.js";
|
3
|
+
describe("coValuesCache", () => {
|
4
|
+
test("should return computed value when not cached", () => {
|
5
|
+
const mockRawValue = { type: "comap" };
|
6
|
+
const mockCoValue = { id: "test" };
|
7
|
+
let computeCalls = 0;
|
8
|
+
const result = coValuesCache.get(mockRawValue, () => {
|
9
|
+
computeCalls++;
|
10
|
+
return mockCoValue;
|
11
|
+
});
|
12
|
+
expect(result).toBe(mockCoValue);
|
13
|
+
expect(computeCalls).toBe(1);
|
14
|
+
});
|
15
|
+
test("should return cached value on subsequent calls", () => {
|
16
|
+
const mockRawValue = { type: "comap" };
|
17
|
+
const mockCoValue = { id: "test" };
|
18
|
+
let computeCalls = 0;
|
19
|
+
// First call
|
20
|
+
const result1 = coValuesCache.get(mockRawValue, () => {
|
21
|
+
computeCalls++;
|
22
|
+
return mockCoValue;
|
23
|
+
});
|
24
|
+
// Second call with same raw value
|
25
|
+
const result2 = coValuesCache.get(mockRawValue, () => {
|
26
|
+
computeCalls++;
|
27
|
+
return mockCoValue;
|
28
|
+
});
|
29
|
+
expect(result1).toBe(mockCoValue);
|
30
|
+
expect(result2).toBe(mockCoValue);
|
31
|
+
expect(computeCalls).toBe(1); // Compute should only be called once
|
32
|
+
});
|
33
|
+
test("should cache different values for different raw values", () => {
|
34
|
+
const mockRawValue1 = { type: "comap" };
|
35
|
+
const mockRawValue2 = { type: "colist" };
|
36
|
+
const mockCoValue1 = { id: "test1" };
|
37
|
+
const mockCoValue2 = { id: "test2" };
|
38
|
+
let computeCalls = 0;
|
39
|
+
const result1 = coValuesCache.get(mockRawValue1, () => {
|
40
|
+
computeCalls++;
|
41
|
+
return mockCoValue1;
|
42
|
+
});
|
43
|
+
const result2 = coValuesCache.get(mockRawValue2, () => {
|
44
|
+
computeCalls++;
|
45
|
+
return mockCoValue2;
|
46
|
+
});
|
47
|
+
expect(result1).toBe(mockCoValue1);
|
48
|
+
expect(result2).toBe(mockCoValue2);
|
49
|
+
expect(computeCalls).toBe(2); // Should compute once for each unique raw value
|
50
|
+
});
|
51
|
+
});
|
52
|
+
//# sourceMappingURL=cache.test.js.map
|
@@ -0,0 +1 @@
|
|
1
|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
import { CoMap, AnonymousJazzAgent, } from "../internal.js";
|
4
4
|
import { Group, CoValueBase, MembersSym, Profile, Ref, SchemaInit, inspect, subscriptionsScopes, loadCoValue, subscribeToCoValue, ensureCoValueLoaded, subscribeToExistingCoValue, } from "../internal.js";
|
5
|
+
import { coValuesCache } from "../lib/cache.js";
|
5
6
|
/** @category Identity & Permissions */
|
6
7
|
export class Account extends CoValueBase {
|
7
8
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
@@ -27,8 +28,9 @@ export class Account extends CoValueBase {
|
|
27
28
|
get _loadedAs() {
|
28
29
|
if (this.isMe)
|
29
30
|
return this;
|
30
|
-
|
31
|
-
|
31
|
+
const rawAccount = this._raw.core.node.account;
|
32
|
+
if (rawAccount instanceof RawAccount) {
|
33
|
+
return coValuesCache.get(rawAccount, () => Account.fromRaw(rawAccount));
|
32
34
|
}
|
33
35
|
return new AnonymousJazzAgent(this._raw.core.node);
|
34
36
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"account.js","sourceRoot":"","sources":["../../../src/coValues/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACpD,OAAO,EAMH,UAAU,GAKb,MAAM,QAAQ,CAAC;AAChB,OAAO,EACH,KAAK,EASL,kBAAkB,GACrB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACH,KAAK,EACL,WAAW,EACX,UAAU,EACV,OAAO,EACP,GAAG,EACH,UAAU,EACV,OAAO,EACP,mBAAmB,EACnB,WAAW,EACX,kBAAkB,EAClB,mBAAmB,EACnB,0BAA0B,GAC7B,MAAM,gBAAgB,CAAC;
|
1
|
+
{"version":3,"file":"account.js","sourceRoot":"","sources":["../../../src/coValues/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACpD,OAAO,EAMH,UAAU,GAKb,MAAM,QAAQ,CAAC;AAChB,O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AAK,QAAQ;YACpC,UAAU,IAAI,UAAU,CAAC,KAAK,EAChC,CAAC;YACE,MAAM,CAAC,WAA4B,CAAC,OAAO,KAAK,EAAE,CAAC;YACnD,MAAM,CAAC,WAA4B,CAAC,OAAO,CAAC,GAAG,CAAC;gBAC7C,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;YACjC,OAAO,IAAI,CAAC;QAChB,CAAC;aAAM,CAAC;YACJ,OAAO,OAAO,CAAC,cAAc,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QAC3D,CAAC;IACL,CAAC;CACJ,CAAC;AAEF,uCAAuC;AACvC,MAAM,UAAU,mBAAmB,CAAC,OAAgB;IAKhD,OAAO,OAAO,CAAC,IAAI,CAAC;AACxB,CAAC"}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { instantiateRefEncoded, isRefEncoded, subscriptionsScopes, } from "../internal.js";
|
2
|
-
|
2
|
+
import { coValuesCache } from "../lib/cache.js";
|
3
3
|
const TRACE_ACCESSES = false;
|
4
4
|
export class Ref {
|
5
5
|
id;
|
@@ -19,15 +19,7 @@ export class Ref {
|
|
19
19
|
: this.controlledAccount._raw.core.node;
|
20
20
|
const raw = node.getLoaded(this.id);
|
21
21
|
if (raw) {
|
22
|
-
|
23
|
-
if (value) {
|
24
|
-
// console.log("Using cached value for " + this.id);
|
25
|
-
}
|
26
|
-
else {
|
27
|
-
value = instantiateRefEncoded(this.schema, raw);
|
28
|
-
refCache.set(raw, value);
|
29
|
-
}
|
30
|
-
return value;
|
22
|
+
return coValuesCache.get(raw, () => instantiateRefEncoded(this.schema, raw));
|
31
23
|
}
|
32
24
|
else {
|
33
25
|
return null;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"refs.js","sourceRoot":"","sources":["../../../src/implementation/refs.ts"],"names":[],"mappings":"AASA,OAAO,EACH,qBAAqB,EACrB,YAAY,EACZ,mBAAmB,GACtB,MAAM,gBAAgB,CAAC;
|
1
|
+
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,cAAc,EAAE,CAAC,MAAM,CAAC;YACnC,CAAC;YACD,MAAM,EAAE,GAAG,WAAW,CAAC,GAAW,CAAC,CAAC;YACpC,IAAI,CAAC,EAAE;gBAAE,OAAO,SAAS,CAAC;YAC1B,OAAO,IAAI,GAAG,CACV,EAAiB,EACjB,iBAAiB,EACjB,eAAe,CAAC,GAAW,CAAC,CAC/B,CAAC;QACN,CAAC;QACD,OAAO;YACH,OAAO,cAAc,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;QACzD,CAAC;QACD,wBAAwB,CAAC,MAAM,EAAE,GAAG;YAChC,MAAM,EAAE,GAAG,WAAW,CAAC,GAAW,CAAC,CAAC;YACpC,IAAI,EAAE,EAAE,CAAC;gBACL,OAAO;oBACH,UAAU,EAAE,IAAI;oBAChB,YAAY,EAAE,IAAI;oBAClB,QAAQ,EAAE,IAAI;iBACjB,CAAC;YACN,CAAC;iBAAM,CAAC;gBACJ,OAAO,OAAO,CAAC,wBAAwB,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;YACzD,CAAC;QACL,CAAC;KACJ,CAAC,CAAC;AACP,CAAC"}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
const weakMap = new WeakMap();
|
2
|
+
export const coValuesCache = {
|
3
|
+
get: (raw, compute) => {
|
4
|
+
const cached = weakMap.get(raw);
|
5
|
+
if (cached) {
|
6
|
+
return cached;
|
7
|
+
}
|
8
|
+
const computed = compute();
|
9
|
+
weakMap.set(raw, computed);
|
10
|
+
return computed;
|
11
|
+
},
|
12
|
+
};
|
13
|
+
//# sourceMappingURL=cache.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../../src/lib/cache.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,GAAG,IAAI,OAAO,EAAuB,CAAC;AAEnD,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,GAAG,EAAE,CAAoB,GAAe,EAAE,OAAgB,EAAE,EAAE;QAC1D,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,MAAM,EAAE,CAAC;YACT,OAAO,MAAW,CAAC;QACvB,CAAC;QACD,MAAM,QAAQ,GAAG,OAAO,EAAE,CAAC;QAC3B,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;QAC3B,OAAO,QAAQ,CAAC;IACpB,CAAC;CACJ,CAAC"}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import { expect, describe, test } from "vitest";
|
2
|
+
import { coValuesCache } from "./cache.js";
|
3
|
+
describe("coValuesCache", () => {
|
4
|
+
test("should return computed value when not cached", () => {
|
5
|
+
const mockRawValue = { type: "comap" };
|
6
|
+
const mockCoValue = { id: "test" };
|
7
|
+
let computeCalls = 0;
|
8
|
+
const result = coValuesCache.get(mockRawValue, () => {
|
9
|
+
computeCalls++;
|
10
|
+
return mockCoValue;
|
11
|
+
});
|
12
|
+
expect(result).toBe(mockCoValue);
|
13
|
+
expect(computeCalls).toBe(1);
|
14
|
+
});
|
15
|
+
test("should return cached value on subsequent calls", () => {
|
16
|
+
const mockRawValue = { type: "comap" };
|
17
|
+
const mockCoValue = { id: "test" };
|
18
|
+
let computeCalls = 0;
|
19
|
+
// First call
|
20
|
+
const result1 = coValuesCache.get(mockRawValue, () => {
|
21
|
+
computeCalls++;
|
22
|
+
return mockCoValue;
|
23
|
+
});
|
24
|
+
// Second call with same raw value
|
25
|
+
const result2 = coValuesCache.get(mockRawValue, () => {
|
26
|
+
computeCalls++;
|
27
|
+
return mockCoValue;
|
28
|
+
});
|
29
|
+
expect(result1).toBe(mockCoValue);
|
30
|
+
expect(result2).toBe(mockCoValue);
|
31
|
+
expect(computeCalls).toBe(1); // Compute should only be called once
|
32
|
+
});
|
33
|
+
test("should cache different values for different raw values", () => {
|
34
|
+
const mockRawValue1 = { type: "comap" };
|
35
|
+
const mockRawValue2 = { type: "colist" };
|
36
|
+
const mockCoValue1 = { id: "test1" };
|
37
|
+
const mockCoValue2 = { id: "test2" };
|
38
|
+
let computeCalls = 0;
|
39
|
+
const result1 = coValuesCache.get(mockRawValue1, () => {
|
40
|
+
computeCalls++;
|
41
|
+
return mockCoValue1;
|
42
|
+
});
|
43
|
+
const result2 = coValuesCache.get(mockRawValue2, () => {
|
44
|
+
computeCalls++;
|
45
|
+
return mockCoValue2;
|
46
|
+
});
|
47
|
+
expect(result1).toBe(mockCoValue1);
|
48
|
+
expect(result2).toBe(mockCoValue2);
|
49
|
+
expect(computeCalls).toBe(2); // Should compute once for each unique raw value
|
50
|
+
});
|
51
|
+
});
|
52
|
+
//# sourceMappingURL=cache.test.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"cache.test.js","sourceRoot":"","sources":["../../../src/lib/cache.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAI3C,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC3B,IAAI,CAAC,8CAA8C,EAAE,GAAG,EAAE;QACtD,MAAM,YAAY,GAAG,EAAE,IAAI,EAAE,OAAO,EAAgB,CAAC;QACrD,MAAM,WAAW,GAAG,EAAE,EAAE,EAAE,MAAM,EAAwB,CAAC;QACzD,IAAI,YAAY,GAAG,CAAC,CAAC;QAErB,MAAM,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,YAAY,EAAE,GAAG,EAAE;YAChD,YAAY,EAAE,CAAC;YACf,OAAO,WAAW,CAAC;QACvB,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QACjC,MAAM,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACjC,CAA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,CAAC;QAEH,MAAM,OAAO,GAAG,aAAa,CAAC,GAAG,CAAC,aAAa,EAAE,GAAG,EAAE;YAClD,YAAY,EAAE,CAAC;YACf,OAAO,YAAY,CAAC;QACxB,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QACnC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QACnC,MAAM,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,gDAAgD;IAClF,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/coValues/account.ts
CHANGED
@@ -37,6 +37,7 @@ import {
|
|
37
37
|
ensureCoValueLoaded,
|
38
38
|
subscribeToExistingCoValue,
|
39
39
|
} from "../internal.js";
|
40
|
+
import { coValuesCache } from "../lib/cache.js";
|
40
41
|
|
41
42
|
/** @category Identity & Permissions */
|
42
43
|
export class Account extends CoValueBase implements CoValue {
|
@@ -71,9 +72,12 @@ export class Account extends CoValueBase implements CoValue {
|
|
71
72
|
get _loadedAs(): Account | AnonymousJazzAgent {
|
72
73
|
if (this.isMe) return this;
|
73
74
|
|
74
|
-
|
75
|
-
|
75
|
+
const rawAccount = this._raw.core.node.account;
|
76
|
+
|
77
|
+
if (rawAccount instanceof RawAccount) {
|
78
|
+
return coValuesCache.get(rawAccount, () => Account.fromRaw(rawAccount)) ;
|
76
79
|
}
|
80
|
+
|
77
81
|
return new AnonymousJazzAgent(this._raw.core.node);
|
78
82
|
}
|
79
83
|
|
@@ -12,8 +12,7 @@ import {
|
|
12
12
|
isRefEncoded,
|
13
13
|
subscriptionsScopes,
|
14
14
|
} from "../internal.js";
|
15
|
-
|
16
|
-
const refCache = new WeakMap<RawCoValue, CoValue>();
|
15
|
+
import { coValuesCache } from "../lib/cache.js";
|
17
16
|
|
18
17
|
const TRACE_ACCESSES = false;
|
19
18
|
|
@@ -35,14 +34,9 @@ export class Ref<out V extends CoValue> {
|
|
35
34
|
: this.controlledAccount._raw.core.node;
|
36
35
|
const raw = node.getLoaded(this.id as unknown as CoID<RawCoValue>);
|
37
36
|
if (raw) {
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
} else {
|
42
|
-
value = instantiateRefEncoded(this.schema, raw);
|
43
|
-
refCache.set(raw, value);
|
44
|
-
}
|
45
|
-
return value as V;
|
37
|
+
return coValuesCache.get(raw, () =>
|
38
|
+
instantiateRefEncoded(this.schema, raw),
|
39
|
+
);
|
46
40
|
} else {
|
47
41
|
return null;
|
48
42
|
}
|
@@ -0,0 +1,64 @@
|
|
1
|
+
import { expect, describe, test } from "vitest";
|
2
|
+
import { coValuesCache } from "./cache.js";
|
3
|
+
import { RawCoValue } from "cojson";
|
4
|
+
import { CoValue } from "../internal.js";
|
5
|
+
|
6
|
+
describe("coValuesCache", () => {
|
7
|
+
test("should return computed value when not cached", () => {
|
8
|
+
const mockRawValue = { type: "comap" } as RawCoValue;
|
9
|
+
const mockCoValue = { id: "test" } as unknown as CoValue;
|
10
|
+
let computeCalls = 0;
|
11
|
+
|
12
|
+
const result = coValuesCache.get(mockRawValue, () => {
|
13
|
+
computeCalls++;
|
14
|
+
return mockCoValue;
|
15
|
+
});
|
16
|
+
|
17
|
+
expect(result).toBe(mockCoValue);
|
18
|
+
expect(computeCalls).toBe(1);
|
19
|
+
});
|
20
|
+
|
21
|
+
test("should return cached value on subsequent calls", () => {
|
22
|
+
const mockRawValue = { type: "comap" } as RawCoValue;
|
23
|
+
const mockCoValue = { id: "test" } as unknown as CoValue;
|
24
|
+
let computeCalls = 0;
|
25
|
+
|
26
|
+
// First call
|
27
|
+
const result1 = coValuesCache.get(mockRawValue, () => {
|
28
|
+
computeCalls++;
|
29
|
+
return mockCoValue;
|
30
|
+
});
|
31
|
+
|
32
|
+
// Second call with same raw value
|
33
|
+
const result2 = coValuesCache.get(mockRawValue, () => {
|
34
|
+
computeCalls++;
|
35
|
+
return mockCoValue;
|
36
|
+
});
|
37
|
+
|
38
|
+
expect(result1).toBe(mockCoValue);
|
39
|
+
expect(result2).toBe(mockCoValue);
|
40
|
+
expect(computeCalls).toBe(1); // Compute should only be called once
|
41
|
+
});
|
42
|
+
|
43
|
+
test("should cache different values for different raw values", () => {
|
44
|
+
const mockRawValue1 = { type: "comap" } as RawCoValue;
|
45
|
+
const mockRawValue2 = { type: "colist" } as RawCoValue;
|
46
|
+
const mockCoValue1 = { id: "test1" } as unknown as CoValue;
|
47
|
+
const mockCoValue2 = { id: "test2" } as unknown as CoValue;
|
48
|
+
let computeCalls = 0;
|
49
|
+
|
50
|
+
const result1 = coValuesCache.get(mockRawValue1, () => {
|
51
|
+
computeCalls++;
|
52
|
+
return mockCoValue1;
|
53
|
+
});
|
54
|
+
|
55
|
+
const result2 = coValuesCache.get(mockRawValue2, () => {
|
56
|
+
computeCalls++;
|
57
|
+
return mockCoValue2;
|
58
|
+
});
|
59
|
+
|
60
|
+
expect(result1).toBe(mockCoValue1);
|
61
|
+
expect(result2).toBe(mockCoValue2);
|
62
|
+
expect(computeCalls).toBe(2); // Should compute once for each unique raw value
|
63
|
+
});
|
64
|
+
});
|
package/src/lib/cache.ts
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
import { RawCoValue } from "cojson";
|
2
|
+
import { CoValue } from "../internal.js";
|
3
|
+
|
4
|
+
const weakMap = new WeakMap<RawCoValue, CoValue>();
|
5
|
+
|
6
|
+
export const coValuesCache = {
|
7
|
+
get: <V extends CoValue>(raw: RawCoValue, compute: () => V) => {
|
8
|
+
const cached = weakMap.get(raw);
|
9
|
+
if (cached) {
|
10
|
+
return cached as V;
|
11
|
+
}
|
12
|
+
const computed = compute();
|
13
|
+
weakMap.set(raw, computed);
|
14
|
+
return computed;
|
15
|
+
},
|
16
|
+
};
|