jazz-tools 0.8.15 → 0.8.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. package/CHANGELOG.md +276 -269
  2. package/dist/native/coValues/account.js +3 -6
  3. package/dist/native/coValues/account.js.map +1 -1
  4. package/dist/native/coValues/coList.js +3 -7
  5. package/dist/native/coValues/coList.js.map +1 -1
  6. package/dist/native/coValues/coMap.js +8 -14
  7. package/dist/native/coValues/coMap.js.map +1 -1
  8. package/dist/native/coValues/coStream.js +7 -12
  9. package/dist/native/coValues/coStream.js.map +1 -1
  10. package/dist/native/coValues/deepLoading.js +6 -3
  11. package/dist/native/coValues/deepLoading.js.map +1 -1
  12. package/dist/native/coValues/extensions/imageDef.js.map +1 -1
  13. package/dist/native/coValues/group.js +3 -6
  14. package/dist/native/coValues/group.js.map +1 -1
  15. package/dist/native/coValues/interfaces.js +4 -3
  16. package/dist/native/coValues/interfaces.js.map +1 -1
  17. package/dist/native/exports.js +3 -9
  18. package/dist/native/exports.js.map +1 -1
  19. package/dist/native/implementation/createContext.js +1 -2
  20. package/dist/native/implementation/createContext.js.map +1 -1
  21. package/dist/native/implementation/devtoolsFormatters.js +5 -25
  22. package/dist/native/implementation/devtoolsFormatters.js.map +1 -1
  23. package/dist/native/implementation/refs.js +1 -2
  24. package/dist/native/implementation/refs.js.map +1 -1
  25. package/dist/native/implementation/schema.js +1 -1
  26. package/dist/native/implementation/schema.js.map +1 -1
  27. package/dist/native/implementation/subscriptionScope.js +2 -4
  28. package/dist/native/implementation/subscriptionScope.js.map +1 -1
  29. package/dist/native/index.native.js +1 -1
  30. package/dist/native/index.native.js.map +1 -1
  31. package/dist/native/lib/cache.js.map +1 -1
  32. package/dist/native/lib/cache.test.js +1 -1
  33. package/dist/native/lib/cache.test.js.map +1 -1
  34. package/dist/web/coValues/account.js +3 -6
  35. package/dist/web/coValues/account.js.map +1 -1
  36. package/dist/web/coValues/coList.js +3 -7
  37. package/dist/web/coValues/coList.js.map +1 -1
  38. package/dist/web/coValues/coMap.js +8 -14
  39. package/dist/web/coValues/coMap.js.map +1 -1
  40. package/dist/web/coValues/coStream.js +7 -12
  41. package/dist/web/coValues/coStream.js.map +1 -1
  42. package/dist/web/coValues/deepLoading.js +6 -3
  43. package/dist/web/coValues/deepLoading.js.map +1 -1
  44. package/dist/web/coValues/extensions/imageDef.js.map +1 -1
  45. package/dist/web/coValues/group.js +3 -6
  46. package/dist/web/coValues/group.js.map +1 -1
  47. package/dist/web/coValues/interfaces.js +4 -3
  48. package/dist/web/coValues/interfaces.js.map +1 -1
  49. package/dist/web/exports.js +3 -9
  50. package/dist/web/exports.js.map +1 -1
  51. package/dist/web/implementation/createContext.js +1 -2
  52. package/dist/web/implementation/createContext.js.map +1 -1
  53. package/dist/web/implementation/devtoolsFormatters.js +5 -25
  54. package/dist/web/implementation/devtoolsFormatters.js.map +1 -1
  55. package/dist/web/implementation/refs.js +1 -2
  56. package/dist/web/implementation/refs.js.map +1 -1
  57. package/dist/web/implementation/schema.js +1 -1
  58. package/dist/web/implementation/schema.js.map +1 -1
  59. package/dist/web/implementation/subscriptionScope.js +2 -4
  60. package/dist/web/implementation/subscriptionScope.js.map +1 -1
  61. package/dist/web/lib/cache.js.map +1 -1
  62. package/dist/web/lib/cache.test.js +1 -1
  63. package/dist/web/lib/cache.test.js.map +1 -1
  64. package/package.json +5 -9
  65. package/src/coValues/account.ts +330 -339
  66. package/src/coValues/coList.ts +474 -495
  67. package/src/coValues/coMap.ts +584 -604
  68. package/src/coValues/coStream.ts +624 -650
  69. package/src/coValues/deepLoading.ts +184 -200
  70. package/src/coValues/extensions/imageDef.ts +44 -44
  71. package/src/coValues/group.ts +196 -210
  72. package/src/coValues/interfaces.ts +197 -199
  73. package/src/exports.ts +38 -26
  74. package/src/implementation/createContext.ts +206 -213
  75. package/src/implementation/devtoolsFormatters.ts +80 -100
  76. package/src/implementation/refs.ts +127 -139
  77. package/src/implementation/schema.ts +124 -128
  78. package/src/implementation/subscriptionScope.ts +111 -121
  79. package/src/index.native.ts +3 -3
  80. package/src/lib/cache.test.ts +48 -48
  81. package/src/lib/cache.ts +9 -9
  82. package/src/tests/coList.test.ts +264 -283
  83. package/src/tests/coMap.test.ts +741 -761
  84. package/src/tests/coStream.test.ts +405 -438
  85. package/src/tests/deepLoading.test.ts +251 -256
  86. package/src/tests/groupsAndAccounts.test.ts +70 -74
  87. package/src/tests/schema.test.ts +198 -198
  88. package/src/tests/subscribe.test.ts +312 -299
  89. package/tsconfig.json +2 -4
  90. package/tsconfig.native.json +4 -10
  91. package/tsconfig.web.json +4 -10
  92. package/.eslintrc.cjs +0 -24
  93. package/.prettierrc.js +0 -9
@@ -1,375 +1,366 @@
1
- import { LocalNode, cojsonInternals } from "cojson";
2
1
  import {
3
- AgentSecret,
4
- CoID,
5
- CryptoProvider,
6
- InviteSecret,
7
- Peer,
8
- RawAccount,
9
- RawCoMap,
10
- RawCoValue,
11
- RawControlledAccount,
12
- SessionID,
2
+ AgentSecret,
3
+ CoID,
4
+ CryptoProvider,
5
+ InviteSecret,
6
+ LocalNode,
7
+ Peer,
8
+ RawAccount,
9
+ RawCoMap,
10
+ RawCoValue,
11
+ RawControlledAccount,
12
+ SessionID,
13
+ cojsonInternals,
13
14
  } from "cojson";
14
15
  import {
15
- CoMap,
16
- CoValue,
17
- CoValueClass,
18
- Schema,
19
- ID,
20
- RefEncoded,
21
- RefIfCoValue,
22
- DeeplyLoaded,
23
- DepthsIn,
24
- AnonymousJazzAgent,
25
- } from "../internal.js";
26
- import {
27
- Group,
28
- CoValueBase,
29
- MembersSym,
30
- Profile,
31
- Ref,
32
- SchemaInit,
33
- inspect,
34
- subscriptionsScopes,
35
- loadCoValue,
36
- subscribeToCoValue,
37
- ensureCoValueLoaded,
38
- subscribeToExistingCoValue,
16
+ AnonymousJazzAgent,
17
+ CoMap,
18
+ type CoValue,
19
+ CoValueBase,
20
+ CoValueClass,
21
+ DeeplyLoaded,
22
+ DepthsIn,
23
+ Group,
24
+ ID,
25
+ MembersSym,
26
+ Profile,
27
+ Ref,
28
+ type RefEncoded,
29
+ RefIfCoValue,
30
+ type Schema,
31
+ SchemaInit,
32
+ ensureCoValueLoaded,
33
+ inspect,
34
+ loadCoValue,
35
+ subscribeToCoValue,
36
+ subscribeToExistingCoValue,
37
+ subscriptionsScopes,
39
38
  } from "../internal.js";
40
39
  import { coValuesCache } from "../lib/cache.js";
41
40
 
42
41
  /** @category Identity & Permissions */
43
42
  export class Account extends CoValueBase implements CoValue {
44
- declare id: ID<this>;
45
- declare _type: "Account";
46
- declare _raw: RawAccount | RawControlledAccount;
47
-
48
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
- static _schema: any;
50
- get _schema(): {
51
- profile: Schema;
52
- root: Schema;
53
- } {
54
- return (this.constructor as typeof Account)._schema;
55
- }
56
- static {
57
- this._schema = {
58
- profile: {
59
- ref: () => Profile,
60
- optional: false,
61
- } satisfies RefEncoded<Profile>,
62
- root: {
63
- ref: () => CoMap,
64
- optional: true,
65
- } satisfies RefEncoded<CoMap>,
66
- };
67
- }
68
-
69
- get _owner(): Account {
70
- return this as Account;
43
+ declare id: ID<this>;
44
+ declare _type: "Account";
45
+ declare _raw: RawAccount | RawControlledAccount;
46
+
47
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ static _schema: any;
49
+ get _schema(): {
50
+ profile: Schema;
51
+ root: Schema;
52
+ } {
53
+ return (this.constructor as typeof Account)._schema;
54
+ }
55
+ static {
56
+ this._schema = {
57
+ profile: {
58
+ ref: () => Profile,
59
+ optional: false,
60
+ } satisfies RefEncoded<Profile>,
61
+ root: {
62
+ ref: () => CoMap,
63
+ optional: true,
64
+ } satisfies RefEncoded<CoMap>,
65
+ };
66
+ }
67
+
68
+ get _owner(): Account {
69
+ return this as Account;
70
+ }
71
+ get _loadedAs(): Account | AnonymousJazzAgent {
72
+ if (this.isMe) return this;
73
+
74
+ const rawAccount = this._raw.core.node.account;
75
+
76
+ if (rawAccount instanceof RawAccount) {
77
+ return coValuesCache.get(rawAccount, () => Account.fromRaw(rawAccount));
71
78
  }
72
- get _loadedAs(): Account | AnonymousJazzAgent {
73
- if (this.isMe) return this;
74
-
75
- const rawAccount = this._raw.core.node.account;
76
-
77
- if (rawAccount instanceof RawAccount) {
78
- return coValuesCache.get(rawAccount, () => Account.fromRaw(rawAccount)) ;
79
- }
80
79
 
81
- return new AnonymousJazzAgent(this._raw.core.node);
80
+ return new AnonymousJazzAgent(this._raw.core.node);
81
+ }
82
+
83
+ declare profile: Profile | null;
84
+ declare root: CoMap | null;
85
+
86
+ get _refs(): {
87
+ profile: RefIfCoValue<Profile> | undefined;
88
+ root: RefIfCoValue<CoMap> | undefined;
89
+ } {
90
+ const profileID = this._raw.get("profile") as unknown as
91
+ | ID<NonNullable<this["profile"]>>
92
+ | undefined;
93
+ const rootID = this._raw.get("root") as unknown as
94
+ | ID<NonNullable<this["root"]>>
95
+ | undefined;
96
+
97
+ return {
98
+ profile:
99
+ profileID &&
100
+ (new Ref(
101
+ profileID,
102
+ this._loadedAs,
103
+ this._schema.profile as RefEncoded<
104
+ NonNullable<this["profile"]> & CoValue
105
+ >,
106
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
107
+ ) as any as RefIfCoValue<this["profile"]>),
108
+ root:
109
+ rootID &&
110
+ (new Ref(
111
+ rootID,
112
+ this._loadedAs,
113
+ this._schema.root as RefEncoded<NonNullable<this["root"]> & CoValue>,
114
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
115
+ ) as any as RefIfCoValue<this["root"]>),
116
+ };
117
+ }
118
+
119
+ isMe: boolean;
120
+ sessionID: SessionID | undefined;
121
+
122
+ constructor(options: { fromRaw: RawAccount | RawControlledAccount }) {
123
+ super();
124
+ if (!("fromRaw" in options)) {
125
+ throw new Error("Can only construct account from raw or with .create()");
82
126
  }
83
-
84
- declare profile: Profile | null;
85
- declare root: CoMap | null;
86
-
87
- get _refs(): {
88
- profile: RefIfCoValue<Profile> | undefined;
89
- root: RefIfCoValue<CoMap> | undefined;
90
- } {
91
- const profileID = this._raw.get("profile") as unknown as
92
- | ID<NonNullable<this["profile"]>>
93
- | undefined;
94
- const rootID = this._raw.get("root") as unknown as
95
- | ID<NonNullable<this["root"]>>
96
- | undefined;
97
-
98
- return {
99
- profile:
100
- profileID &&
101
- (new Ref(
102
- profileID,
103
- this._loadedAs,
104
- this._schema.profile as RefEncoded<
105
- NonNullable<this["profile"]> & CoValue
106
- >,
107
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
108
- ) as any as RefIfCoValue<this["profile"]>),
109
- root:
110
- rootID &&
111
- (new Ref(
112
- rootID,
113
- this._loadedAs,
114
- this._schema.root as RefEncoded<
115
- NonNullable<this["root"]> & CoValue
116
- >,
117
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
118
- ) as any as RefIfCoValue<this["root"]>),
119
- };
127
+ this.isMe = options.fromRaw.id == options.fromRaw.core.node.account.id;
128
+
129
+ Object.defineProperties(this, {
130
+ id: {
131
+ value: options.fromRaw.id,
132
+ enumerable: false,
133
+ },
134
+ _raw: { value: options.fromRaw, enumerable: false },
135
+ _type: { value: "Account", enumerable: false },
136
+ });
137
+
138
+ if (this.isMe) {
139
+ this.sessionID = options.fromRaw.core.node.currentSessionID;
120
140
  }
121
141
 
122
- isMe: boolean;
123
- sessionID: SessionID | undefined;
124
-
125
- constructor(options: { fromRaw: RawAccount | RawControlledAccount }) {
126
- super();
127
- if (!("fromRaw" in options)) {
128
- throw new Error(
129
- "Can only construct account from raw or with .create()",
130
- );
131
- }
132
- this.isMe = options.fromRaw.id == options.fromRaw.core.node.account.id;
133
-
134
- Object.defineProperties(this, {
135
- id: {
136
- value: options.fromRaw.id,
137
- enumerable: false,
138
- },
139
- _raw: { value: options.fromRaw, enumerable: false },
140
- _type: { value: "Account", enumerable: false },
141
- });
142
-
143
- if (this.isMe) {
144
- this.sessionID = options.fromRaw.core.node.currentSessionID;
145
- }
146
-
147
- return new Proxy(
148
- this,
149
- AccountAndGroupProxyHandler as ProxyHandler<this>,
150
- );
151
- }
142
+ return new Proxy(this, AccountAndGroupProxyHandler as ProxyHandler<this>);
143
+ }
152
144
 
153
- myRole(): "admin" | undefined {
154
- if (this.isMe) {
155
- return "admin";
156
- }
145
+ myRole(): "admin" | undefined {
146
+ if (this.isMe) {
147
+ return "admin";
157
148
  }
158
-
159
- async acceptInvite<V extends CoValue>(
160
- valueID: ID<V>,
161
- inviteSecret: InviteSecret,
162
- coValueClass: CoValueClass<V>,
163
- ) {
164
- if (!this.isMe) {
165
- throw new Error("Only a controlled account can accept invites");
166
- }
167
-
168
- await (this._raw as RawControlledAccount).acceptInvite(
169
- valueID as unknown as CoID<RawCoValue>,
170
- inviteSecret,
171
- );
172
-
173
- return loadCoValue(coValueClass, valueID, this as Account, []);
174
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
175
- }
176
-
177
- /** @private */
178
- static async create<A extends Account>(
179
- this: CoValueClass<A> & typeof Account,
180
- options: {
181
- creationProps: { name: string };
182
- initialAgentSecret?: AgentSecret;
183
- peersToLoadFrom?: Peer[];
184
- crypto: CryptoProvider;
185
- },
186
- ): Promise<A> {
187
- const { node } = await LocalNode.withNewlyCreatedAccount({
188
- ...options,
189
- migration: async (rawAccount, _node, creationProps) => {
190
- const account = new this({
191
- fromRaw: rawAccount,
192
- }) as A;
193
-
194
- await account.migrate?.(creationProps);
195
- },
196
- });
197
-
198
- return this.fromNode(node) as A;
199
- }
200
-
201
- static async createAs<A extends Account>(
202
- this: CoValueClass<A> & typeof Account,
203
- as: Account,
204
- options: {
205
- creationProps: { name: string };
206
- },
207
- ) {
208
- // TODO: is there a cleaner way to do this?
209
- const connectedPeers = cojsonInternals.connectedPeers(
210
- "creatingAccount",
211
- "createdAccount",
212
- { peer1role: "server", peer2role: "client" },
213
- );
214
-
215
- as._raw.core.node.syncManager.addPeer(connectedPeers[1]);
216
-
217
- return this.create<A>({
218
- creationProps: options.creationProps,
219
- crypto: as._raw.core.node.crypto,
220
- peersToLoadFrom: [connectedPeers[0]],
221
- });
149
+ }
150
+
151
+ async acceptInvite<V extends CoValue>(
152
+ valueID: ID<V>,
153
+ inviteSecret: InviteSecret,
154
+ coValueClass: CoValueClass<V>,
155
+ ) {
156
+ if (!this.isMe) {
157
+ throw new Error("Only a controlled account can accept invites");
222
158
  }
223
159
 
224
- static fromNode<A extends Account>(
225
- this: CoValueClass<A>,
226
- node: LocalNode,
227
- ): A {
228
- return new this({
229
- fromRaw: node.account as RawControlledAccount,
230
- }) as A;
231
- }
160
+ await (this._raw as RawControlledAccount).acceptInvite(
161
+ valueID as unknown as CoID<RawCoValue>,
162
+ inviteSecret,
163
+ );
232
164
 
165
+ return loadCoValue(coValueClass, valueID, this as Account, []);
233
166
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
234
- toJSON(): object | any[] {
235
- return {
236
- id: this.id,
237
- _type: this._type,
238
- };
239
- }
167
+ }
168
+
169
+ /** @private */
170
+ static async create<A extends Account>(
171
+ this: CoValueClass<A> & typeof Account,
172
+ options: {
173
+ creationProps: { name: string };
174
+ initialAgentSecret?: AgentSecret;
175
+ peersToLoadFrom?: Peer[];
176
+ crypto: CryptoProvider;
177
+ },
178
+ ): Promise<A> {
179
+ const { node } = await LocalNode.withNewlyCreatedAccount({
180
+ ...options,
181
+ migration: async (rawAccount, _node, creationProps) => {
182
+ const account = new this({
183
+ fromRaw: rawAccount,
184
+ }) as A;
240
185
 
241
- [inspect]() {
242
- return this.toJSON();
243
- }
186
+ await account.migrate?.(creationProps);
187
+ },
188
+ });
244
189
 
245
- migrate(
246
- this: Account,
247
- creationProps?: { name: string },
248
- ): void | Promise<void> {
249
- if (creationProps) {
250
- const profileGroup = Group.create({ owner: this });
251
- profileGroup.addMember("everyone", "reader");
252
- this.profile = Profile.create(
253
- { name: creationProps.name },
254
- { owner: profileGroup },
255
- );
256
- }
257
- }
190
+ return this.fromNode(node) as A;
191
+ }
258
192
 
259
- /** @category Subscription & Loading */
260
- static load<A extends Account, Depth>(
261
- this: CoValueClass<A>,
262
- id: ID<A>,
263
- as: Account,
264
- depth: Depth & DepthsIn<A>,
265
- ): Promise<DeeplyLoaded<A, Depth> | undefined> {
266
- return loadCoValue(this, id, as, depth);
193
+ static async createAs<A extends Account>(
194
+ this: CoValueClass<A> & typeof Account,
195
+ as: Account,
196
+ options: {
197
+ creationProps: { name: string };
198
+ },
199
+ ) {
200
+ // TODO: is there a cleaner way to do this?
201
+ const connectedPeers = cojsonInternals.connectedPeers(
202
+ "creatingAccount",
203
+ "createdAccount",
204
+ { peer1role: "server", peer2role: "client" },
205
+ );
206
+
207
+ as._raw.core.node.syncManager.addPeer(connectedPeers[1]);
208
+
209
+ return this.create<A>({
210
+ creationProps: options.creationProps,
211
+ crypto: as._raw.core.node.crypto,
212
+ peersToLoadFrom: [connectedPeers[0]],
213
+ });
214
+ }
215
+
216
+ static fromNode<A extends Account>(
217
+ this: CoValueClass<A>,
218
+ node: LocalNode,
219
+ ): A {
220
+ return new this({
221
+ fromRaw: node.account as RawControlledAccount,
222
+ }) as A;
223
+ }
224
+
225
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
226
+ toJSON(): object | any[] {
227
+ return {
228
+ id: this.id,
229
+ _type: this._type,
230
+ };
231
+ }
232
+
233
+ [inspect]() {
234
+ return this.toJSON();
235
+ }
236
+
237
+ migrate(
238
+ this: Account,
239
+ creationProps?: { name: string },
240
+ ): void | Promise<void> {
241
+ if (creationProps) {
242
+ const profileGroup = Group.create({ owner: this });
243
+ profileGroup.addMember("everyone", "reader");
244
+ this.profile = Profile.create(
245
+ { name: creationProps.name },
246
+ { owner: profileGroup },
247
+ );
267
248
  }
249
+ }
250
+
251
+ /** @category Subscription & Loading */
252
+ static load<A extends Account, Depth>(
253
+ this: CoValueClass<A>,
254
+ id: ID<A>,
255
+ as: Account,
256
+ depth: Depth & DepthsIn<A>,
257
+ ): Promise<DeeplyLoaded<A, Depth> | undefined> {
258
+ return loadCoValue(this, id, as, depth);
259
+ }
260
+
261
+ /** @category Subscription & Loading */
262
+ static subscribe<A extends Account, Depth>(
263
+ this: CoValueClass<A>,
264
+ id: ID<A>,
265
+ as: Account,
266
+ depth: Depth & DepthsIn<A>,
267
+ listener: (value: DeeplyLoaded<A, Depth>) => void,
268
+ ): () => void {
269
+ return subscribeToCoValue<A, Depth>(this, id, as, depth, listener);
270
+ }
271
+
272
+ /** @category Subscription & Loading */
273
+ ensureLoaded<A extends Account, Depth>(
274
+ this: A,
275
+ depth: Depth & DepthsIn<A>,
276
+ ): Promise<DeeplyLoaded<A, Depth> | undefined> {
277
+ return ensureCoValueLoaded(this, depth);
278
+ }
279
+
280
+ /** @category Subscription & Loading */
281
+ subscribe<A extends Account, Depth>(
282
+ this: A,
283
+ depth: Depth & DepthsIn<A>,
284
+ listener: (value: DeeplyLoaded<A, Depth>) => void,
285
+ ): () => void {
286
+ return subscribeToExistingCoValue(this, depth, listener);
287
+ }
288
+ }
268
289
 
269
- /** @category Subscription & Loading */
270
- static subscribe<A extends Account, Depth>(
271
- this: CoValueClass<A>,
272
- id: ID<A>,
273
- as: Account,
274
- depth: Depth & DepthsIn<A>,
275
- listener: (value: DeeplyLoaded<A, Depth>) => void,
276
- ): () => void {
277
- return subscribeToCoValue<A, Depth>(this, id, as, depth, listener);
290
+ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
291
+ get(target, key, receiver) {
292
+ if (key === "profile") {
293
+ const ref = target._refs.profile;
294
+ return ref
295
+ ? ref.accessFrom(receiver, "profile")
296
+ : // eslint-disable-next-line @typescript-eslint/no-explicit-any
297
+ (undefined as any);
298
+ } else if (key === "root") {
299
+ const ref = target._refs.root;
300
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
301
+ return ref ? ref.accessFrom(receiver, "root") : (undefined as any);
302
+ } else {
303
+ return Reflect.get(target, key, receiver);
278
304
  }
279
-
280
- /** @category Subscription & Loading */
281
- ensureLoaded<A extends Account, Depth>(
282
- this: A,
283
- depth: Depth & DepthsIn<A>,
284
- ): Promise<DeeplyLoaded<A, Depth> | undefined> {
285
- return ensureCoValueLoaded(this, depth);
305
+ },
306
+ set(target, key, value, receiver) {
307
+ if (
308
+ (key === "profile" || key === "root" || key === MembersSym) &&
309
+ typeof value === "object" &&
310
+ SchemaInit in value
311
+ ) {
312
+ (target.constructor as typeof CoMap)._schema ||= {};
313
+ (target.constructor as typeof CoMap)._schema[key] = value[SchemaInit];
314
+ return true;
315
+ } else if (key === "profile") {
316
+ if (value) {
317
+ target._raw.set(
318
+ "profile",
319
+ value.id as unknown as CoID<RawCoMap>,
320
+ "trusting",
321
+ );
322
+ }
323
+ subscriptionsScopes
324
+ .get(receiver)
325
+ ?.onRefAccessedOrSet(target.id, value.id);
326
+ return true;
327
+ } else if (key === "root") {
328
+ if (value) {
329
+ target._raw.set("root", value.id as unknown as CoID<RawCoMap>);
330
+ }
331
+ subscriptionsScopes
332
+ .get(receiver)
333
+ ?.onRefAccessedOrSet(target.id, value.id);
334
+ return true;
335
+ } else {
336
+ return Reflect.set(target, key, value, receiver);
286
337
  }
287
-
288
- /** @category Subscription & Loading */
289
- subscribe<A extends Account, Depth>(
290
- this: A,
291
- depth: Depth & DepthsIn<A>,
292
- listener: (value: DeeplyLoaded<A, Depth>) => void,
293
- ): () => void {
294
- return subscribeToExistingCoValue(this, depth, listener);
338
+ },
339
+ defineProperty(target, key, descriptor) {
340
+ if (
341
+ (key === "profile" || key === "root" || key === MembersSym) &&
342
+ typeof descriptor.value === "object" &&
343
+ SchemaInit in descriptor.value
344
+ ) {
345
+ (target.constructor as typeof CoMap)._schema ||= {};
346
+ (target.constructor as typeof CoMap)._schema[key] =
347
+ descriptor.value[SchemaInit];
348
+ return true;
349
+ } else {
350
+ return Reflect.defineProperty(target, key, descriptor);
295
351
  }
296
- }
297
-
298
- export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
299
- get(target, key, receiver) {
300
- if (key === "profile") {
301
- const ref = target._refs.profile;
302
- return ref
303
- ? ref.accessFrom(receiver, "profile")
304
- : // eslint-disable-next-line @typescript-eslint/no-explicit-any
305
- (undefined as any);
306
- } else if (key === "root") {
307
- const ref = target._refs.root;
308
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
309
- return ref ? ref.accessFrom(receiver, "root") : (undefined as any);
310
- } else {
311
- return Reflect.get(target, key, receiver);
312
- }
313
- },
314
- set(target, key, value, receiver) {
315
- if (
316
- (key === "profile" || key === "root" || key === MembersSym) &&
317
- typeof value === "object" &&
318
- SchemaInit in value
319
- ) {
320
- (target.constructor as typeof CoMap)._schema ||= {};
321
- (target.constructor as typeof CoMap)._schema[key] =
322
- value[SchemaInit];
323
- return true;
324
- } else if (key === "profile") {
325
- if (value) {
326
- target._raw.set(
327
- "profile",
328
- value.id as unknown as CoID<RawCoMap>,
329
- "trusting",
330
- );
331
- }
332
- subscriptionsScopes
333
- .get(receiver)
334
- ?.onRefAccessedOrSet(target.id, value.id);
335
- return true;
336
- } else if (key === "root") {
337
- if (value) {
338
- target._raw.set("root", value.id as unknown as CoID<RawCoMap>);
339
- }
340
- subscriptionsScopes
341
- .get(receiver)
342
- ?.onRefAccessedOrSet(target.id, value.id);
343
- return true;
344
- } else {
345
- return Reflect.set(target, key, value, receiver);
346
- }
347
- },
348
- defineProperty(target, key, descriptor) {
349
- if (
350
- (key === "profile" || key === "root" || key === MembersSym) &&
351
- typeof descriptor.value === "object" &&
352
- SchemaInit in descriptor.value
353
- ) {
354
- (target.constructor as typeof CoMap)._schema ||= {};
355
- (target.constructor as typeof CoMap)._schema[key] =
356
- descriptor.value[SchemaInit];
357
- return true;
358
- } else {
359
- return Reflect.defineProperty(target, key, descriptor);
360
- }
361
- },
352
+ },
362
353
  };
363
354
 
364
355
  /** @category Identity & Permissions */
365
356
  export function isControlledAccount(account: Account): account is Account & {
366
- isMe: true;
367
- sessionID: SessionID;
368
- _raw: RawControlledAccount;
357
+ isMe: true;
358
+ sessionID: SessionID;
359
+ _raw: RawControlledAccount;
369
360
  } {
370
- return account.isMe;
361
+ return account.isMe;
371
362
  }
372
363
 
373
364
  export type AccountClass<Acc extends Account> = CoValueClass<Acc> & {
374
- fromNode: (typeof Account)["fromNode"];
365
+ fromNode: (typeof Account)["fromNode"];
375
366
  };