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.
Files changed (51) hide show
  1. package/.turbo/turbo-build.log +80 -8
  2. package/CHANGELOG.md +175 -0
  3. package/dist/coValues/account.js +78 -38
  4. package/dist/coValues/account.js.map +1 -1
  5. package/dist/coValues/coList.js +150 -99
  6. package/dist/coValues/coList.js.map +1 -1
  7. package/dist/coValues/coMap.js +178 -162
  8. package/dist/coValues/coMap.js.map +1 -1
  9. package/dist/coValues/coStream.js +195 -70
  10. package/dist/coValues/coStream.js.map +1 -1
  11. package/dist/coValues/extensions/imageDef.js +13 -8
  12. package/dist/coValues/extensions/imageDef.js.map +1 -1
  13. package/dist/coValues/group.js +40 -36
  14. package/dist/coValues/group.js.map +1 -1
  15. package/dist/coValues/interfaces.js +22 -4
  16. package/dist/coValues/interfaces.js.map +1 -1
  17. package/dist/implementation/refs.js +26 -12
  18. package/dist/implementation/refs.js.map +1 -1
  19. package/dist/implementation/schema.js +38 -1
  20. package/dist/implementation/schema.js.map +1 -1
  21. package/dist/implementation/symbols.js +5 -0
  22. package/dist/implementation/symbols.js.map +1 -0
  23. package/dist/index.js +4 -3
  24. package/dist/index.js.map +1 -1
  25. package/dist/internal.js +1 -0
  26. package/dist/internal.js.map +1 -1
  27. package/dist/tests/coList.test.js +20 -24
  28. package/dist/tests/coList.test.js.map +1 -1
  29. package/dist/tests/coMap.test.js +154 -43
  30. package/dist/tests/coMap.test.js.map +1 -1
  31. package/dist/tests/coStream.test.js +50 -55
  32. package/dist/tests/coStream.test.js.map +1 -1
  33. package/dist/tests/groupsAndAccounts.test.js +86 -0
  34. package/dist/tests/groupsAndAccounts.test.js.map +1 -0
  35. package/package.json +5 -4
  36. package/src/coValues/account.ts +116 -75
  37. package/src/coValues/coList.ts +189 -125
  38. package/src/coValues/coMap.ts +238 -295
  39. package/src/coValues/coStream.ts +282 -124
  40. package/src/coValues/extensions/imageDef.ts +13 -15
  41. package/src/coValues/group.ts +85 -85
  42. package/src/coValues/interfaces.ts +32 -11
  43. package/src/implementation/refs.ts +42 -25
  44. package/src/implementation/schema.ts +68 -46
  45. package/src/implementation/symbols.ts +12 -0
  46. package/src/index.ts +10 -8
  47. package/src/internal.ts +1 -0
  48. package/src/tests/coList.test.ts +20 -24
  49. package/src/tests/coMap.test.ts +153 -58
  50. package/src/tests/coStream.test.ts +65 -70
  51. package/src/tests/groupsAndAccounts.test.ts +96 -0
@@ -15,14 +15,22 @@ import type {
15
15
  CoMap,
16
16
  CoValue,
17
17
  CoValueClass,
18
- FieldDescriptor,
19
- Group,
18
+ Schema,
20
19
  ID,
21
- RefField,
20
+ RefEncoded,
22
21
  SubclassedConstructor,
23
22
  UnavailableError,
24
23
  } from "../internal.js";
25
- import { CoValueBase, Profile, ValueRef, inspect } from "../internal.js";
24
+ import {
25
+ Group,
26
+ CoValueBase,
27
+ MembersSym,
28
+ Profile,
29
+ Ref,
30
+ SchemaInit,
31
+ inspect,
32
+ subscriptionsScopes,
33
+ } from "../internal.js";
26
34
  import type { Stream } from "effect/Stream";
27
35
 
28
36
  export class Account<
@@ -34,21 +42,21 @@ export class Account<
34
42
  extends CoValueBase
35
43
  implements CoValue<"Account", RawAccount | RawControlledAccount>
36
44
  {
37
- id!: ID<this>;
38
- _type!: "Account";
39
- _raw!: RawAccount | RawControlledAccount;
45
+ declare id: ID<this>;
46
+ declare _type: "Account";
47
+ declare _raw: RawAccount | RawControlledAccount;
40
48
 
41
- static _encoding: any;
42
- get _encoding(): {
43
- profile: FieldDescriptor;
44
- root: FieldDescriptor;
49
+ static _schema: any;
50
+ get _schema(): {
51
+ profile: Schema;
52
+ root: Schema;
45
53
  } {
46
- return (this.constructor as typeof Account)._encoding;
54
+ return (this.constructor as typeof Account)._schema;
47
55
  }
48
56
  static {
49
- this._encoding = {
50
- profile: { ref: () => Profile },
51
- root: { json: true },
57
+ this._schema = {
58
+ profile: () => Profile satisfies Schema,
59
+ root: "json" satisfies Schema,
52
60
  } as any;
53
61
  }
54
62
 
@@ -66,36 +74,36 @@ export class Account<
66
74
 
67
75
  get _refs(): {
68
76
  profile: NonNullable<Def["profile"]> extends Profile
69
- ? ValueRef<NonNullable<Def["profile"]>> | null
77
+ ? Ref<NonNullable<Def["profile"]>> | null
70
78
  : null;
71
79
  root: NonNullable<Def["root"]> extends CoMap
72
- ? ValueRef<NonNullable<Def["root"]>> | null
80
+ ? Ref<NonNullable<Def["root"]>> | null
73
81
  : null;
74
82
  } {
75
- const profileID = this._raw.get("profile") as unknown as ID<
76
- NonNullable<Def["profile"]>
77
- > | undefined;
83
+ const profileID = this._raw.get("profile") as unknown as
84
+ | ID<NonNullable<Def["profile"]>>
85
+ | undefined;
78
86
  const rootID = this._raw.get("root") as unknown as
79
87
  | ID<NonNullable<Def["root"]>>
80
88
  | undefined;
81
89
  return {
82
- profile: profileID && (new ValueRef(
83
- profileID,
84
- this._loadedAs,
85
- (
86
- this._encoding.profile as RefField<
90
+ profile:
91
+ profileID &&
92
+ (new Ref(
93
+ profileID,
94
+ this._loadedAs,
95
+ this._schema.profile as RefEncoded<
87
96
  NonNullable<Def["profile"]> & CoValue
88
97
  >
89
- ).ref()
90
- )) as any,
98
+ ) as any),
91
99
  root:
92
100
  rootID &&
93
- (new ValueRef(
101
+ (new Ref(
94
102
  rootID,
95
103
  this._loadedAs,
96
- (
97
- this._encoding.root as RefField<NonNullable<Def["root"]> & CoValue>
98
- ).ref()
104
+ this._schema.root as RefEncoded<
105
+ NonNullable<Def["root"]> & CoValue
106
+ >
99
107
  ) as any),
100
108
  };
101
109
  }
@@ -109,7 +117,7 @@ export class Account<
109
117
  options: { fromRaw: RawAccount | RawControlledAccount }
110
118
  );
111
119
  constructor(
112
- init: undefined,
120
+ _init: undefined,
113
121
  options:
114
122
  | { fromRaw: RawAccount | RawControlledAccount }
115
123
  | { owner: Group | Account }
@@ -128,40 +136,17 @@ export class Account<
128
136
  enumerable: false,
129
137
  },
130
138
  _raw: { value: options.fromRaw, enumerable: false },
131
- profile: {
132
- get: () => {
133
- const ref = this._refs.profile;
134
- return ref ? ref.accessFrom(this) : (undefined as any);
135
- },
136
- set: (value: Def["profile"] | null) => {
137
- if (value) {
138
- this._raw.set(
139
- "profile",
140
- value.id as unknown as CoID<RawCoMap>
141
- );
142
- }
143
- },
144
- },
145
- root: {
146
- get: () => {
147
- const ref = this._refs.root;
148
- return ref ? ref.accessFrom(this) : (undefined as any);
149
- },
150
- set: (value: Def["root"] | null) => {
151
- if (value) {
152
- this._raw.set(
153
- "root",
154
- value.id as unknown as CoID<RawCoMap>
155
- );
156
- }
157
- },
158
- },
159
139
  });
160
140
 
161
141
  if (this.isMe) {
162
142
  (this as Account & Me).sessionID =
163
143
  options.fromRaw.core.node.currentSessionID;
164
144
  }
145
+
146
+ return new Proxy(
147
+ this,
148
+ AccountAndGroupProxyHandler as ProxyHandler<this>
149
+ );
165
150
  }
166
151
 
167
152
  myRole(): "admin" | undefined {
@@ -198,19 +183,19 @@ export class Account<
198
183
  static async create<A extends Account>(
199
184
  this: SubclassedConstructor<A> & typeof Account,
200
185
  options: {
201
- name: string;
186
+ creationProps: { name: string };
202
187
  initialAgentSecret?: AgentSecret;
203
188
  peersToLoadFrom?: Peer[];
204
189
  }
205
190
  ): Promise<A & Me> {
206
191
  const { node } = await LocalNode.withNewlyCreatedAccount({
207
192
  ...options,
208
- migration: async (rawAccount) => {
193
+ migration: async (rawAccount, _node, creationProps) => {
209
194
  const account = new this(undefined, {
210
195
  fromRaw: rawAccount,
211
196
  }) as A & Me;
212
197
 
213
- await account.migrate?.();
198
+ await account.migrate?.(creationProps);
214
199
  },
215
200
  });
216
201
 
@@ -231,12 +216,12 @@ export class Account<
231
216
  accountSecret: options.accountSecret,
232
217
  sessionID: options.sessionID,
233
218
  peersToLoadFrom: options.peersToLoadFrom,
234
- migration: async (rawAccount) => {
219
+ migration: async (rawAccount, _node, creationProps) => {
235
220
  const account = new this(undefined, {
236
221
  fromRaw: rawAccount,
237
222
  }) as A & Me;
238
223
 
239
- await account.migrate?.();
224
+ await account.migrate?.(creationProps);
240
225
  },
241
226
  });
242
227
 
@@ -263,20 +248,76 @@ export class Account<
263
248
  return this.toJSON();
264
249
  }
265
250
 
266
- static encoding<V extends Account>(
267
- this: { new (...args: any[]): V } & CoValueClass<V> & { _encoding: any },
268
- fields: {
269
- profile: V["_encoding"]["profile"];
270
- root: V["_encoding"]["root"];
251
+ migrate(creationProps?: { name: string }): void | Promise<void> {
252
+ if (creationProps) {
253
+ const profileGroup = new Group({ owner: this });
254
+ profileGroup.addMember("everyone", "reader");
255
+ this.profile = new Profile(
256
+ { name: creationProps.name },
257
+ { owner: profileGroup }
258
+ );
271
259
  }
272
- ) {
273
- this._encoding ||= {};
274
- Object.assign(this._encoding, fields);
275
260
  }
276
-
277
- migrate: (() => void | Promise<void>) | undefined;
278
261
  }
279
262
 
263
+ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
264
+ get(target, key, receiver) {
265
+ if (key === "profile") {
266
+ const ref = target._refs.profile;
267
+ return ref ? ref.accessFrom(receiver) : (undefined as any);
268
+ } else if (key === "root") {
269
+ const ref = target._refs.root;
270
+ return ref ? ref.accessFrom(receiver) : (undefined as any);
271
+ } else {
272
+ return Reflect.get(target, key, receiver);
273
+ }
274
+ },
275
+ set(target, key, value, receiver) {
276
+ if (
277
+ (key === "profile" || key === "root" || key === MembersSym) &&
278
+ typeof value === "object" &&
279
+ SchemaInit in value
280
+ ) {
281
+ (target.constructor as typeof CoMap)._schema ||= {};
282
+ (target.constructor as typeof CoMap)._schema[key] =
283
+ value[SchemaInit];
284
+ return true;
285
+ } else if (key === "profile") {
286
+ if (value) {
287
+ target._raw.set(
288
+ "profile",
289
+ value.id as unknown as CoID<RawCoMap>,
290
+ "trusting"
291
+ );
292
+ }
293
+ subscriptionsScopes.get(receiver)?.onRefAccessedOrSet(value.id);
294
+ return true;
295
+ } else if (key === "root") {
296
+ if (value) {
297
+ target._raw.set("root", value.id as unknown as CoID<RawCoMap>);
298
+ }
299
+ subscriptionsScopes.get(receiver)?.onRefAccessedOrSet(value.id);
300
+ return true;
301
+ } else {
302
+ return Reflect.set(target, key, value, receiver);
303
+ }
304
+ },
305
+ defineProperty(target, key, descriptor) {
306
+ if (
307
+ (key === "profile" || key === "root" || key === MembersSym) &&
308
+ typeof descriptor.value === "object" &&
309
+ SchemaInit in descriptor.value
310
+ ) {
311
+ (target.constructor as typeof CoMap)._schema ||= {};
312
+ (target.constructor as typeof CoMap)._schema[key] =
313
+ descriptor.value[SchemaInit];
314
+ return true;
315
+ } else {
316
+ return Reflect.defineProperty(target, key, descriptor);
317
+ }
318
+ },
319
+ };
320
+
280
321
  export interface Me {
281
322
  id: ID<any>;
282
323
  isMe: true;