jazz-tools 0.8.15 → 0.8.17
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +283 -269
- package/dist/native/coValues/account.js +3 -6
- package/dist/native/coValues/account.js.map +1 -1
- package/dist/native/coValues/coList.js +3 -7
- package/dist/native/coValues/coList.js.map +1 -1
- package/dist/native/coValues/coMap.js +8 -14
- package/dist/native/coValues/coMap.js.map +1 -1
- package/dist/native/coValues/coStream.js +7 -12
- package/dist/native/coValues/coStream.js.map +1 -1
- package/dist/native/coValues/deepLoading.js +6 -3
- package/dist/native/coValues/deepLoading.js.map +1 -1
- package/dist/native/coValues/extensions/imageDef.js.map +1 -1
- package/dist/native/coValues/group.js +3 -6
- package/dist/native/coValues/group.js.map +1 -1
- package/dist/native/coValues/interfaces.js +4 -3
- package/dist/native/coValues/interfaces.js.map +1 -1
- package/dist/native/exports.js +3 -9
- package/dist/native/exports.js.map +1 -1
- package/dist/native/implementation/createContext.js +1 -2
- package/dist/native/implementation/createContext.js.map +1 -1
- package/dist/native/implementation/devtoolsFormatters.js +5 -25
- package/dist/native/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/native/implementation/refs.js +1 -2
- package/dist/native/implementation/refs.js.map +1 -1
- package/dist/native/implementation/schema.js +1 -1
- package/dist/native/implementation/schema.js.map +1 -1
- package/dist/native/implementation/subscriptionScope.js +2 -4
- package/dist/native/implementation/subscriptionScope.js.map +1 -1
- package/dist/native/index.native.js +1 -1
- package/dist/native/index.native.js.map +1 -1
- package/dist/native/lib/cache.js.map +1 -1
- package/dist/native/lib/cache.test.js +1 -1
- package/dist/native/lib/cache.test.js.map +1 -1
- package/dist/web/coValues/account.js +3 -6
- package/dist/web/coValues/account.js.map +1 -1
- package/dist/web/coValues/coList.js +3 -7
- package/dist/web/coValues/coList.js.map +1 -1
- package/dist/web/coValues/coMap.js +8 -14
- package/dist/web/coValues/coMap.js.map +1 -1
- package/dist/web/coValues/coStream.js +7 -12
- package/dist/web/coValues/coStream.js.map +1 -1
- package/dist/web/coValues/deepLoading.js +6 -3
- package/dist/web/coValues/deepLoading.js.map +1 -1
- package/dist/web/coValues/extensions/imageDef.js.map +1 -1
- package/dist/web/coValues/group.js +3 -6
- package/dist/web/coValues/group.js.map +1 -1
- package/dist/web/coValues/interfaces.js +4 -3
- package/dist/web/coValues/interfaces.js.map +1 -1
- package/dist/web/exports.js +3 -9
- package/dist/web/exports.js.map +1 -1
- package/dist/web/implementation/createContext.js +1 -2
- package/dist/web/implementation/createContext.js.map +1 -1
- package/dist/web/implementation/devtoolsFormatters.js +5 -25
- package/dist/web/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/web/implementation/refs.js +1 -2
- package/dist/web/implementation/refs.js.map +1 -1
- package/dist/web/implementation/schema.js +1 -1
- package/dist/web/implementation/schema.js.map +1 -1
- package/dist/web/implementation/subscriptionScope.js +2 -4
- package/dist/web/implementation/subscriptionScope.js.map +1 -1
- package/dist/web/lib/cache.js.map +1 -1
- package/dist/web/lib/cache.test.js +1 -1
- package/dist/web/lib/cache.test.js.map +1 -1
- package/package.json +5 -9
- package/src/coValues/account.ts +330 -339
- package/src/coValues/coList.ts +474 -495
- package/src/coValues/coMap.ts +584 -604
- package/src/coValues/coStream.ts +624 -650
- package/src/coValues/deepLoading.ts +184 -200
- package/src/coValues/extensions/imageDef.ts +44 -44
- package/src/coValues/group.ts +196 -210
- package/src/coValues/interfaces.ts +197 -199
- package/src/exports.ts +38 -26
- package/src/implementation/createContext.ts +206 -213
- package/src/implementation/devtoolsFormatters.ts +80 -100
- package/src/implementation/refs.ts +127 -139
- package/src/implementation/schema.ts +124 -128
- package/src/implementation/subscriptionScope.ts +111 -121
- package/src/index.native.ts +3 -3
- package/src/lib/cache.test.ts +48 -48
- package/src/lib/cache.ts +9 -9
- package/src/tests/coList.test.ts +264 -283
- package/src/tests/coMap.test.ts +741 -761
- package/src/tests/coStream.test.ts +405 -438
- package/src/tests/deepLoading.test.ts +251 -256
- package/src/tests/groupsAndAccounts.test.ts +70 -74
- package/src/tests/schema.test.ts +198 -198
- package/src/tests/subscribe.test.ts +312 -299
- package/tsconfig.json +2 -4
- package/tsconfig.native.json +4 -10
- package/tsconfig.web.json +4 -10
- package/.eslintrc.cjs +0 -24
- package/.prettierrc.js +0 -9
package/src/lib/cache.test.ts
CHANGED
@@ -1,64 +1,64 @@
|
|
1
|
-
import { expect, describe, test } from "vitest";
|
2
|
-
import { coValuesCache } from "./cache.js";
|
3
1
|
import { RawCoValue } from "cojson";
|
2
|
+
import { describe, expect, test } from "vitest";
|
4
3
|
import { CoValue } from "../internal.js";
|
4
|
+
import { coValuesCache } from "./cache.js";
|
5
5
|
|
6
6
|
describe("coValuesCache", () => {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
const result = coValuesCache.get(mockRawValue, () => {
|
13
|
-
computeCalls++;
|
14
|
-
return mockCoValue;
|
15
|
-
});
|
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;
|
16
11
|
|
17
|
-
|
18
|
-
|
12
|
+
const result = coValuesCache.get(mockRawValue, () => {
|
13
|
+
computeCalls++;
|
14
|
+
return mockCoValue;
|
19
15
|
});
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
let computeCalls = 0;
|
17
|
+
expect(result).toBe(mockCoValue);
|
18
|
+
expect(computeCalls).toBe(1);
|
19
|
+
});
|
25
20
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
});
|
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;
|
31
25
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
26
|
+
// First call
|
27
|
+
const result1 = coValuesCache.get(mockRawValue, () => {
|
28
|
+
computeCalls++;
|
29
|
+
return mockCoValue;
|
30
|
+
});
|
37
31
|
|
38
|
-
|
39
|
-
|
40
|
-
|
32
|
+
// Second call with same raw value
|
33
|
+
const result2 = coValuesCache.get(mockRawValue, () => {
|
34
|
+
computeCalls++;
|
35
|
+
return mockCoValue;
|
41
36
|
});
|
42
37
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
const mockCoValue2 = { id: "test2" } as unknown as CoValue;
|
48
|
-
let computeCalls = 0;
|
38
|
+
expect(result1).toBe(mockCoValue);
|
39
|
+
expect(result2).toBe(mockCoValue);
|
40
|
+
expect(computeCalls).toBe(1); // Compute should only be called once
|
41
|
+
});
|
49
42
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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;
|
54
49
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
50
|
+
const result1 = coValuesCache.get(mockRawValue1, () => {
|
51
|
+
computeCalls++;
|
52
|
+
return mockCoValue1;
|
53
|
+
});
|
59
54
|
|
60
|
-
|
61
|
-
|
62
|
-
|
55
|
+
const result2 = coValuesCache.get(mockRawValue2, () => {
|
56
|
+
computeCalls++;
|
57
|
+
return mockCoValue2;
|
63
58
|
});
|
64
|
-
|
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
CHANGED
@@ -4,13 +4,13 @@ import { CoValue } from "../internal.js";
|
|
4
4
|
const weakMap = new WeakMap<RawCoValue, CoValue>();
|
5
5
|
|
6
6
|
export const coValuesCache = {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
16
|
};
|