jazz-tools 0.7.0-alpha.3 → 0.7.0-alpha.30
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +80 -8
- package/CHANGELOG.md +175 -0
- package/dist/coValues/account.js +78 -38
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/coList.js +150 -99
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/coMap.js +178 -162
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coStream.js +195 -70
- package/dist/coValues/coStream.js.map +1 -1
- package/dist/coValues/extensions/imageDef.js +13 -8
- package/dist/coValues/extensions/imageDef.js.map +1 -1
- package/dist/coValues/group.js +40 -36
- package/dist/coValues/group.js.map +1 -1
- package/dist/coValues/interfaces.js +22 -4
- package/dist/coValues/interfaces.js.map +1 -1
- package/dist/implementation/refs.js +26 -12
- package/dist/implementation/refs.js.map +1 -1
- package/dist/implementation/schema.js +38 -1
- package/dist/implementation/schema.js.map +1 -1
- package/dist/implementation/symbols.js +5 -0
- package/dist/implementation/symbols.js.map +1 -0
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/internal.js +1 -0
- package/dist/internal.js.map +1 -1
- package/dist/tests/coList.test.js +20 -24
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +154 -43
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +50 -55
- package/dist/tests/coStream.test.js.map +1 -1
- package/dist/tests/groupsAndAccounts.test.js +86 -0
- package/dist/tests/groupsAndAccounts.test.js.map +1 -0
- package/package.json +5 -4
- package/src/coValues/account.ts +116 -75
- package/src/coValues/coList.ts +189 -125
- package/src/coValues/coMap.ts +238 -295
- package/src/coValues/coStream.ts +282 -124
- package/src/coValues/extensions/imageDef.ts +13 -15
- package/src/coValues/group.ts +85 -85
- package/src/coValues/interfaces.ts +32 -11
- package/src/implementation/refs.ts +42 -25
- package/src/implementation/schema.ts +68 -46
- package/src/implementation/symbols.ts +12 -0
- package/src/index.ts +10 -8
- package/src/internal.ts +1 -0
- package/src/tests/coList.test.ts +20 -24
- package/src/tests/coMap.test.ts +153 -58
- package/src/tests/coStream.test.ts +65 -70
- package/src/tests/groupsAndAccounts.test.ts +96 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
import { expect, describe, test, beforeEach } from "vitest";
|
2
|
+
|
3
|
+
import { webcrypto } from "node:crypto";
|
4
|
+
import { Account, jazzReady, CoMap, co, Group } from "..";
|
5
|
+
|
6
|
+
if (!("crypto" in globalThis)) {
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
8
|
+
(globalThis as any).crypto = webcrypto;
|
9
|
+
}
|
10
|
+
|
11
|
+
beforeEach(async () => {
|
12
|
+
await jazzReady;
|
13
|
+
});
|
14
|
+
|
15
|
+
describe("Custom accounts and groups", async () => {
|
16
|
+
class CustomProfile extends CoMap<CustomProfile> {
|
17
|
+
name = co.string;
|
18
|
+
color = co.string;
|
19
|
+
}
|
20
|
+
|
21
|
+
class CustomAccount extends Account<CustomAccount> {
|
22
|
+
profile = co.ref(CustomProfile);
|
23
|
+
root = co.ref(CoMap);
|
24
|
+
|
25
|
+
migrate(creationProps?: { name: string }) {
|
26
|
+
if (creationProps) {
|
27
|
+
const profileGroup = new Group({ owner: this });
|
28
|
+
profileGroup.addMember("everyone", "reader");
|
29
|
+
this.profile = new CustomProfile(
|
30
|
+
{ name: creationProps.name, color: "blue" },
|
31
|
+
{ owner: this }
|
32
|
+
);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
class CustomGroup extends Group<CustomGroup> {
|
38
|
+
profile = co.null;
|
39
|
+
root = co.null;
|
40
|
+
[co.members] = co.ref(CustomAccount);
|
41
|
+
|
42
|
+
get nMembers() {
|
43
|
+
return this.members.length;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
type T = CustomGroup[typeof co.members];
|
48
|
+
|
49
|
+
test("Custom account and group", async () => {
|
50
|
+
const me = await CustomAccount.create({
|
51
|
+
creationProps: { name: "Hermes Puggington" },
|
52
|
+
});
|
53
|
+
|
54
|
+
expect(me.profile).toBeDefined();
|
55
|
+
expect(me.profile?.name).toBe("Hermes Puggington");
|
56
|
+
expect(me.profile?.color).toBe("blue");
|
57
|
+
|
58
|
+
const group = new CustomGroup({ owner: me });
|
59
|
+
group.addMember("everyone", "reader");
|
60
|
+
|
61
|
+
expect(group.members).toMatchObject([
|
62
|
+
{ id: me.id, role: "admin" },
|
63
|
+
{ id: "everyone", role: "reader" },
|
64
|
+
]);
|
65
|
+
|
66
|
+
expect(group.nMembers).toBe(2);
|
67
|
+
|
68
|
+
await new Promise<void>((resolve) => {
|
69
|
+
group.subscribe((update) => {
|
70
|
+
const meAsMember = update.members.find((member) => {
|
71
|
+
return member.id === me.id && member.account?.profile;
|
72
|
+
});
|
73
|
+
if (meAsMember) {
|
74
|
+
expect(meAsMember.account?.profile?.name).toBe(
|
75
|
+
"Hermes Puggington"
|
76
|
+
);
|
77
|
+
expect(meAsMember.account?.profile?.color).toBe("blue");
|
78
|
+
resolve();
|
79
|
+
}
|
80
|
+
});
|
81
|
+
});
|
82
|
+
|
83
|
+
class MyMap extends CoMap<MyMap> {
|
84
|
+
name = co.string;
|
85
|
+
}
|
86
|
+
|
87
|
+
const map = new MyMap({name: "test"}, {owner: group});
|
88
|
+
|
89
|
+
const meAsCastMember = map._owner.as(CustomGroup).members.find((member) => member.id === me.id);
|
90
|
+
expect(meAsCastMember?.account?.profile?.name).toBe("Hermes Puggington");
|
91
|
+
expect(meAsCastMember?.account?.profile?.color).toBe("blue");
|
92
|
+
|
93
|
+
expect((map._owner as any).nMembers).toBeUndefined();
|
94
|
+
expect(map._owner.as(CustomGroup).nMembers).toBe(2);
|
95
|
+
});
|
96
|
+
});
|