jazz-tools 0.7.0-alpha.3 → 0.7.0-alpha.31

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 +78 -8
  2. package/CHANGELOG.md +181 -0
  3. package/dist/coValues/account.js +81 -41
  4. package/dist/coValues/account.js.map +1 -1
  5. package/dist/coValues/coList.js +156 -104
  6. package/dist/coValues/coList.js.map +1 -1
  7. package/dist/coValues/coMap.js +182 -163
  8. package/dist/coValues/coMap.js.map +1 -1
  9. package/dist/coValues/coStream.js +202 -71
  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 +46 -38
  14. package/dist/coValues/group.js.map +1 -1
  15. package/dist/coValues/interfaces.js +23 -5
  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 +32 -36
  28. package/dist/tests/coList.test.js.map +1 -1
  29. package/dist/tests/coMap.test.js +170 -59
  30. package/dist/tests/coMap.test.js.map +1 -1
  31. package/dist/tests/coStream.test.js +59 -64
  32. package/dist/tests/coStream.test.js.map +1 -1
  33. package/dist/tests/groupsAndAccounts.test.js +88 -0
  34. package/dist/tests/groupsAndAccounts.test.js.map +1 -0
  35. package/package.json +5 -4
  36. package/src/coValues/account.ts +129 -105
  37. package/src/coValues/coList.ts +200 -131
  38. package/src/coValues/coMap.ts +243 -305
  39. package/src/coValues/coStream.ts +300 -127
  40. package/src/coValues/extensions/imageDef.ts +14 -16
  41. package/src/coValues/group.ts +90 -106
  42. package/src/coValues/interfaces.ts +33 -12
  43. package/src/implementation/refs.ts +42 -25
  44. package/src/implementation/schema.ts +69 -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 +35 -39
  49. package/src/tests/coMap.test.ts +176 -81
  50. package/src/tests/coStream.test.ts +76 -81
  51. package/src/tests/groupsAndAccounts.test.ts +100 -0
@@ -1,148 +1,110 @@
1
1
  import type { JsonValue, RawCoMap } from "cojson";
2
2
  import type { Simplify } from "effect/Types";
3
- import { Schema } from "@effect/schema";
3
+ import { encodeSync, decodeSync } from "@effect/schema/Schema";
4
4
  import type {
5
5
  CoValue,
6
- Encoder,
7
- FieldDescriptor,
8
- FieldDescriptorFor,
6
+ Schema,
9
7
  Group,
10
8
  ID,
11
- RefField,
12
- EnsureCoValueNullable,
13
- CoValueClass,
9
+ RefEncoded,
10
+ IfCo,
11
+ RefIfCoValue,
12
+ SubclassedConstructor,
14
13
  } from "../internal.js";
15
14
  import {
16
15
  Account,
17
16
  CoValueBase,
18
- ValueRef,
17
+ Ref,
18
+ SchemaInit,
19
19
  inspect,
20
20
  makeRefs,
21
21
  subscriptionsScopes,
22
- indexSignature,
22
+ ItemsSym,
23
+ InitValues,
24
+ isRefEncoded,
23
25
  } from "../internal.js";
24
26
 
25
- type EnsureValid<
26
- Fields extends { [key: string]: any; [indexSignature]?: any },
27
- > = {
28
- [Key in OwnKeys<Fields> as IfOptionalKey<
29
- Key,
30
- Fields
31
- >]?: EnsureCoValueNullable<Fields[Key], Key>;
32
- } & {
33
- [Key in OwnKeys<Fields> as IfRequiredKey<
34
- Key,
35
- Fields
36
- >]: EnsureCoValueNullable<Fields[Key], Key>;
37
- } & {
38
- [Key in indexSignature]?: EnsureCoValueNullable<
39
- Fields[indexSignature],
40
- Key
41
- >;
27
+ type CoMapEdit<V> = {
28
+ value?: V;
29
+ ref?: RefIfCoValue<V>;
30
+ by?: Account;
31
+ madeAt: Date;
42
32
  };
43
33
 
44
- type IfOptionalKey<Key extends keyof Obj, Obj> = Pick<
45
- Partial<Obj>,
46
- Key
47
- > extends Pick<Obj, Key>
48
- ? Key
49
- : never;
50
- type IfRequiredKey<Key extends keyof Obj, Obj> = Pick<
51
- Partial<Obj>,
52
- Key
53
- > extends Pick<Obj, Key>
54
- ? never
55
- : Key;
56
-
57
- type DefaultFields = {
58
- [key: string]: any;
59
- [indexSignature]?: any;
34
+ type InitValuesFor<C extends CoMap> = {
35
+ init: Simplify<CoMapInit<C>>;
36
+ owner: Account | Group;
60
37
  };
61
38
 
62
- export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
63
- extends CoValueBase
64
- implements CoValue<"CoMap", RawCoMap>
65
- {
66
- id!: ID<this>;
67
- _type!: "CoMap";
39
+ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
40
+ declare id: ID<this>;
41
+ declare _type: "CoMap";
68
42
  static {
69
43
  this.prototype._type = "CoMap";
70
44
  }
71
- _raw!: RawCoMap;
72
-
73
- static _encoding: any;
74
- get _encoding(): {
75
- [Key in OwnKeys<Fields>]: FieldDescriptorFor<Fields[Key]>;
76
- } & {
77
- [indexSignature]: indexSignature extends keyof Fields
78
- ? FieldDescriptorFor<Fields[indexSignature]>
79
- : never;
80
- } {
81
- return (this.constructor as typeof CoMap)._encoding;
45
+ declare _raw: RawCoMap;
46
+
47
+ static _schema: any;
48
+ get _schema() {
49
+ return (this.constructor as typeof CoMap)._schema as {
50
+ [key: string]: Schema;
51
+ } & { [ItemsSym]?: Schema };
82
52
  }
83
53
 
84
54
  get _refs(): {
85
- [Key in OwnKeys<Fields> as NonNullable<Fields[Key]> extends CoValue
86
- ? Key
87
- : never]: NonNullable<Fields[Key]> extends CoValue
88
- ? ValueRef<NonNullable<Fields[Key]>>
89
- : never;
55
+ [Key in CoKeys<this>]: IfCo<this[Key], RefIfCoValue<this[Key]>>;
90
56
  } {
91
- return makeRefs<OwnKeys<Fields>>(
57
+ return makeRefs<CoKeys<this>>(
92
58
  (key) => this._raw.get(key as string) as unknown as ID<CoValue>,
93
- () =>
94
- Object.keys(this._encoding).filter((key) => {
95
- const schema = this._encoding[
96
- key as keyof typeof this._encoding
97
- ] as FieldDescriptor;
98
- schema !== "json" && "ref" in schema;
99
- }) as OwnKeys<Fields>[],
59
+ () => {
60
+ const keys = this._raw.keys().filter((key) => {
61
+ const schema =
62
+ this._schema[key as keyof typeof this._schema] ||
63
+ (this._schema[ItemsSym] as Schema | undefined);
64
+ return schema && schema !== "json" && isRefEncoded(schema);
65
+ }) as CoKeys<this>[];
66
+
67
+ return keys;
68
+ },
100
69
  this._loadedAs,
101
- (key) => (this._encoding[key] as RefField<CoValue>).ref()
70
+ (key) =>
71
+ (this._schema[key] ||
72
+ this._schema[ItemsSym]) as RefEncoded<CoValue>
102
73
  ) as any;
103
74
  }
104
75
 
105
- get _edits(): {
106
- [Key in OwnKeys<Fields>]: {
107
- value?: Fields[Key];
108
- ref?: Fields[Key] extends CoValue ? ValueRef<Fields[Key]> : never;
109
- by?: Account;
110
- madeAt: Date;
111
- };
112
- } {
76
+ get _edits() {
113
77
  return new Proxy(this, {
114
78
  get(target, key) {
115
79
  const rawEdit = target._raw.lastEditAt(key as string);
116
80
  if (!rawEdit) return undefined;
117
81
 
118
- const descriptor = target._encoding[
119
- key as keyof typeof target._encoding
120
- ] as FieldDescriptor;
82
+ const descriptor = target._schema[
83
+ key as keyof typeof target._schema
84
+ ] as Schema;
121
85
 
122
86
  return {
123
87
  value:
124
88
  descriptor === "json"
125
89
  ? rawEdit.value
126
90
  : "encoded" in descriptor
127
- ? Schema.decodeSync(descriptor.encoded)(
128
- rawEdit.value
129
- )
130
- : new ValueRef(
91
+ ? decodeSync(descriptor.encoded)(rawEdit.value)
92
+ : new Ref(
131
93
  rawEdit.value as ID<CoValue>,
132
94
  target._loadedAs,
133
- descriptor.ref()
95
+ descriptor
134
96
  ).accessFrom(target),
135
97
  ref:
136
- descriptor !== "json" && "ref" in descriptor
137
- ? new ValueRef(
98
+ descriptor !== "json" && isRefEncoded(descriptor)
99
+ ? new Ref(
138
100
  rawEdit.value as ID<CoValue>,
139
101
  target._loadedAs,
140
- descriptor.ref()
102
+ descriptor
141
103
  )
142
104
  : undefined,
143
105
  by:
144
106
  rawEdit.by &&
145
- new ValueRef(
107
+ new Ref(
146
108
  rawEdit.by as ID<Account>,
147
109
  target._loadedAs,
148
110
  Account
@@ -150,57 +112,59 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
150
112
  madeAt: rawEdit.at,
151
113
  };
152
114
  },
153
- }) as any;
115
+ }) as {
116
+ [Key in CoKeys<this>]: IfCo<this[Key], CoMapEdit<this[Key]>>;
117
+ };
154
118
  }
155
119
 
156
120
  get _loadedAs() {
157
121
  return Account.fromNode(this._raw.core.node);
158
122
  }
159
123
 
160
- constructor(_init: undefined, options: { fromRaw: RawCoMap });
161
- constructor(
162
- init: Simplify<CoMapInit<Fields>>,
163
- options: { owner: Account | Group }
164
- );
124
+ [InitValues]?: any;
125
+
165
126
  constructor(
166
- init: Simplify<CoMapInit<Fields>> | undefined,
167
- options: { owner: Account | Group } | { fromRaw: RawCoMap }
127
+ options: { fromRaw: RawCoMap } | { init: any; owner: Account | Group }
168
128
  ) {
169
129
  super();
170
130
 
171
- if (!this._encoding) {
172
- throw new Error(
173
- "No schema found in " +
174
- this.constructor.name +
175
- " - ensure that you have a `static { this.define({...}) }` block in the class definition."
176
- );
131
+ if ("owner" in options) {
132
+ this[InitValues] = {
133
+ init: options.init,
134
+ owner: options.owner,
135
+ } as InitValuesFor<this>;
136
+ } else if ("fromRaw" in options) {
137
+ Object.defineProperties(this, {
138
+ id: {
139
+ value: options.fromRaw.id as unknown as ID<this>,
140
+ enumerable: false,
141
+ },
142
+ _raw: { value: options.fromRaw, enumerable: false },
143
+ });
144
+ } else {
145
+ throw new Error("Invalid CoMap constructor arguments");
177
146
  }
178
147
 
179
- const raw: RawCoMap = this.rawFromInit<Fields>(options, init);
180
-
181
- Object.defineProperties(this, {
182
- id: {
183
- value: raw.id,
184
- enumerable: false,
185
- },
186
- _raw: { value: raw, enumerable: false },
187
- });
188
-
189
- this.definePropertiesFromSchema();
148
+ return new Proxy(this, CoMapProxyHandler as ProxyHandler<this>);
149
+ }
190
150
 
191
- if (this._encoding[indexSignature]) {
192
- return new Proxy(this, CoMapProxyHandler<Fields>());
193
- }
151
+ static create<M extends CoMap>(
152
+ this: SubclassedConstructor<M>,
153
+ init: Simplify<CoMapInit<M>>,
154
+ options: { owner: Account | Group }
155
+ ) {
156
+ return new this({ init, owner: options.owner });
194
157
  }
195
158
 
196
159
  toJSON() {
197
160
  const jsonedFields = this._raw.keys().map((key) => {
198
- const tKey = key as OwnKeys<Fields>;
199
- const descriptor = this._encoding[tKey] as FieldDescriptor;
161
+ const tKey = key as CoKeys<this>;
162
+ const descriptor = (this._schema[tKey] ||
163
+ this._schema[ItemsSym]) as Schema;
200
164
 
201
165
  if (descriptor == "json" || "encode" in descriptor) {
202
166
  return [key, this._raw.get(key)];
203
- } else if ("ref" in descriptor) {
167
+ } else if (isRefEncoded(descriptor)) {
204
168
  const jsonedRef = (this as any)[tKey]?.toJSON();
205
169
  return [key, jsonedRef];
206
170
  } else {
@@ -220,166 +184,98 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
220
184
  }
221
185
 
222
186
  rawFromInit<Fields extends object = Record<string, any>>(
223
- options: { owner: Account | Group } | { fromRaw: RawCoMap },
224
- init: Simplify<CoMapInit<Fields>> | undefined
187
+ init: Simplify<CoMapInit<Fields>> | undefined,
188
+ owner: Account | Group
225
189
  ) {
226
- let raw: RawCoMap;
190
+ const rawOwner = owner._raw;
227
191
 
228
- if ("fromRaw" in options) {
229
- raw = options.fromRaw;
230
- } else {
231
- const rawOwner = options.owner._raw;
232
-
233
- const rawInit = {} as {
234
- [key in keyof Fields]: JsonValue | undefined;
235
- };
236
-
237
- if (init)
238
- for (const key of Object.keys(init) as (keyof Fields)[]) {
239
- const initValue = init[key as keyof typeof init];
240
-
241
- const descriptor = (this._encoding[
242
- key as keyof typeof this._encoding
243
- ] || this._encoding[indexSignature]) as FieldDescriptor;
244
-
245
- if (descriptor === "json") {
246
- rawInit[key] = initValue as JsonValue;
247
- } else if ("ref" in descriptor) {
248
- if (initValue) {
249
- rawInit[key] = (initValue as unknown as CoValue).id;
250
- }
251
- } else if ("encoded" in descriptor) {
252
- rawInit[key] = Schema.encodeSync(descriptor.encoded)(
253
- initValue as any
254
- );
255
- }
256
- }
192
+ const rawInit = {} as {
193
+ [key in keyof Fields]: JsonValue | undefined;
194
+ };
257
195
 
258
- raw = rawOwner.createMap(rawInit);
259
- }
260
- return raw;
261
- }
196
+ if (init)
197
+ for (const key of Object.keys(init) as (keyof Fields)[]) {
198
+ const initValue = init[key as keyof typeof init];
262
199
 
263
- static encoding<V extends CoMap>(
264
- this: { new (...args: any): V } & typeof CoMap,
265
- fields: Simplify<{
266
- [Key in keyof V["_encoding"] as V["_encoding"][Key] extends never
267
- ? never
268
- : Key]: Simplify<V["_encoding"][Key]>;
269
- }>
270
- ) {
271
- this._encoding ||= {};
272
- Object.assign(this._encoding, fields);
273
- }
200
+ const descriptor = (this._schema[
201
+ key as keyof typeof this._schema
202
+ ] || this._schema[ItemsSym]) as Schema;
274
203
 
275
- private definePropertiesFromSchema() {
276
- for (const [key, fieldSchema] of Object.entries(this._encoding)) {
277
- if (key === "indexSignature") continue;
278
- const descriptor = fieldSchema as FieldDescriptor;
279
- if (descriptor === "json") {
280
- Object.defineProperty(
281
- this,
282
- key,
283
- this.primitivePropDef(key as string)
284
- );
285
- } else if ("encoded" in descriptor) {
286
- Object.defineProperty(
287
- this,
288
- key,
289
- this.encodedPropDef(key as string, descriptor.encoded)
290
- );
291
- } else if ("ref" in descriptor) {
292
- Object.defineProperty(
293
- this,
294
- key,
295
- this.refPropDef(
296
- key as string,
297
- (descriptor as RefField<CoValue>).ref
298
- )
299
- );
204
+ if (descriptor === "json") {
205
+ rawInit[key] = initValue as JsonValue;
206
+ } else if (isRefEncoded(descriptor)) {
207
+ if (initValue) {
208
+ rawInit[key] = (initValue as unknown as CoValue).id;
209
+ }
210
+ } else if ("encoded" in descriptor) {
211
+ rawInit[key] = encodeSync(descriptor.encoded)(
212
+ initValue as any
213
+ );
214
+ }
300
215
  }
301
- }
302
- }
303
216
 
304
- private primitivePropDef(key: string): PropertyDescriptor {
305
- return {
306
- get: () => {
307
- return this._raw.get(key);
308
- },
309
- set(this: CoMap, value: JsonValue) {
310
- this._raw.set(key, value);
311
- },
312
- enumerable: true,
313
- configurable: true,
314
- };
217
+ return rawOwner.createMap(rawInit);
315
218
  }
316
219
 
317
- private encodedPropDef(key: string, arg: Encoder<any>): PropertyDescriptor {
318
- return {
319
- get: () => {
320
- const raw = this._raw.get(key);
321
- return raw === undefined
322
- ? undefined
323
- : Schema.decodeSync(arg)(raw);
324
- },
325
- set(this: CoMap, value: unknown) {
326
- this._raw.set(key, Schema.encodeSync(arg)(value));
327
- },
328
- enumerable: true,
329
- configurable: true,
330
- };
331
- }
220
+ static Record<Value>(value: IfCo<Value, Value>) {
221
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
222
+ class RecordLikeCoMap extends CoMap {
223
+ [ItemsSym] = value;
224
+ }
225
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
226
+ interface RecordLikeCoMap extends Record<string, Value> {}
332
227
 
333
- private refPropDef(
334
- key: string,
335
- ref: () => CoValueClass<CoValue>
336
- ): PropertyDescriptor {
337
- return {
338
- get: () => {
339
- const rawID = this._raw.get(key);
340
- return rawID === undefined
341
- ? undefined
342
- : new ValueRef(
343
- rawID as unknown as ID<CoValue>,
344
- this._loadedAs,
345
- ref()
346
- ).accessFrom(this);
347
- },
348
- set: (value: CoValue) => {
349
- this._raw.set(key, value.id);
350
- subscriptionsScopes.get(this)?.onRefAccessedOrSet(value.id);
351
- },
352
- enumerable: true,
353
- configurable: true,
354
- };
228
+ return RecordLikeCoMap;
355
229
  }
356
230
  }
357
231
 
358
- export type OwnKeys<Fields extends object> = Exclude<
232
+ export type CoKeys<Fields extends object> = Exclude<
359
233
  keyof Fields & string,
360
- keyof CoMap<Record<string, never>> | `_${string}`
234
+ keyof CoMap
361
235
  >;
362
236
 
363
237
  export type CoMapInit<Fields extends object> = {
364
- [Key in OwnKeys<Fields> as undefined extends Fields[Key]
238
+ [Key in CoKeys<Fields> as undefined extends Fields[Key]
365
239
  ? never
366
240
  : null extends Fields[Key]
367
241
  ? never
368
- : Key]: Fields[Key];
369
- } & { [Key in OwnKeys<Fields>]?: Fields[Key] };
242
+ : IfCo<Fields[Key], Key>]: Fields[Key];
243
+ } & { [Key in CoKeys<Fields> as IfCo<Fields[Key], Key>]?: Fields[Key] };
244
+
245
+ function tryInit(map: CoMap) {
246
+ if (
247
+ map[InitValues] &&
248
+ (map._schema[ItemsSym] ||
249
+ Object.keys(map[InitValues].init).every(
250
+ (key) => (map._schema as any)[key]
251
+ ))
252
+ ) {
253
+ const raw = map.rawFromInit(
254
+ map[InitValues].init,
255
+ map[InitValues].owner
256
+ );
257
+ Object.defineProperties(map, {
258
+ id: {
259
+ value: raw.id,
260
+ enumerable: false,
261
+ },
262
+ _raw: { value: raw, enumerable: false },
263
+ });
264
+ delete map[InitValues];
265
+ }
266
+ }
370
267
 
371
268
  // TODO: cache handlers per descriptor for performance?
372
- function CoMapProxyHandler<Fields extends EnsureValid<Fields>>(): ProxyHandler<
373
- CoMap<Fields>
374
- > {
375
- return {
376
- get(target, key, receiver) {
377
- const descriptor = target._encoding[
378
- indexSignature
379
- ] as FieldDescriptor;
380
- if (key in target || typeof key === "symbol") {
381
- return Reflect.get(target, key, receiver);
382
- } else {
269
+ const CoMapProxyHandler: ProxyHandler<CoMap> = {
270
+ get(target, key, receiver) {
271
+ if (key === "_schema") {
272
+ return Reflect.get(target, key);
273
+ } else if (key in target) {
274
+ return Reflect.get(target, key, receiver);
275
+ } else {
276
+ const descriptor = (target._schema[key as keyof CoMap["_schema"]] ||
277
+ target._schema[ItemsSym]) as Schema;
278
+ if (descriptor && typeof key === "string") {
383
279
  const raw = target._raw.get(key);
384
280
 
385
281
  if (descriptor === "json") {
@@ -387,61 +283,103 @@ function CoMapProxyHandler<Fields extends EnsureValid<Fields>>(): ProxyHandler<
387
283
  } else if ("encoded" in descriptor) {
388
284
  return raw === undefined
389
285
  ? undefined
390
- : Schema.decodeSync(descriptor.encoded)(raw);
391
- } else if ("ref" in descriptor) {
286
+ : decodeSync(descriptor.encoded)(raw);
287
+ } else if (isRefEncoded(descriptor)) {
392
288
  return raw === undefined
393
289
  ? undefined
394
- : new ValueRef(
290
+ : new Ref(
395
291
  raw as unknown as ID<CoValue>,
396
292
  target._loadedAs,
397
- descriptor.ref()
398
- ).accessFrom(target);
293
+ descriptor
294
+ ).accessFrom(receiver);
399
295
  }
400
- }
401
- },
402
- set(target, key, value, receiver) {
403
- const descriptor = target._encoding[
404
- indexSignature
405
- ] as FieldDescriptor;
406
- if (key in target || typeof key === "symbol") {
407
- return Reflect.set(target, key, value, receiver);
408
296
  } else {
409
- if (descriptor === "json") {
410
- target._raw.set(key, value);
411
- } else if ("encoded" in descriptor) {
412
- target._raw.set(
413
- key,
414
- Schema.encodeSync(descriptor.encoded)(value)
415
- );
416
- } else if ("ref" in descriptor) {
417
- target._raw.set(key, value.id);
418
- subscriptionsScopes
419
- .get(target)
420
- ?.onRefAccessedOrSet(value.id);
421
- }
422
- return true;
297
+ return undefined;
423
298
  }
424
- },
425
- ownKeys(target) {
426
- const keys = Reflect.ownKeys(target);
427
- for (const key of target._raw.keys()) {
428
- if (!keys.includes(key)) {
429
- keys.push(key);
430
- }
299
+ }
300
+ },
301
+ set(target, key, value, receiver) {
302
+ if (
303
+ (typeof key === "string" || ItemsSym) &&
304
+ typeof value === "object" &&
305
+ SchemaInit in value
306
+ ) {
307
+ (target.constructor as typeof CoMap)._schema ||= {};
308
+ (target.constructor as typeof CoMap)._schema[key] =
309
+ value[SchemaInit];
310
+ tryInit(target);
311
+ return true;
312
+ }
313
+
314
+ const descriptor = (target._schema[key as keyof CoMap["_schema"]] ||
315
+ target._schema[ItemsSym]) as Schema;
316
+ if (descriptor && typeof key === "string") {
317
+ if (descriptor === "json") {
318
+ target._raw.set(key, value);
319
+ } else if ("encoded" in descriptor) {
320
+ target._raw.set(key, encodeSync(descriptor.encoded)(value));
321
+ } else if (isRefEncoded(descriptor)) {
322
+ target._raw.set(key, value.id);
323
+ subscriptionsScopes.get(target)?.onRefAccessedOrSet(value.id);
324
+ }
325
+ return true;
326
+ } else {
327
+ return Reflect.set(target, key, value, receiver);
328
+ }
329
+ },
330
+ defineProperty(target, key, attributes) {
331
+ if (
332
+ "value" in attributes &&
333
+ typeof attributes.value === "object" &&
334
+ SchemaInit in attributes.value
335
+ ) {
336
+ (target.constructor as typeof CoMap)._schema ||= {};
337
+ (target.constructor as typeof CoMap)._schema[key as string] =
338
+ attributes.value[SchemaInit];
339
+ tryInit(target);
340
+ return true;
341
+ } else {
342
+ return Reflect.defineProperty(target, key, attributes);
343
+ }
344
+ },
345
+ ownKeys(target) {
346
+ const keys = Reflect.ownKeys(target).filter((k) => k !== ItemsSym);
347
+ // for (const key of Reflect.ownKeys(target._schema)) {
348
+ // if (key !== ItemsSym && !keys.includes(key)) {
349
+ // keys.push(key);
350
+ // }
351
+ // }
352
+ for (const key of target._raw.keys()) {
353
+ if (!keys.includes(key)) {
354
+ keys.push(key);
431
355
  }
356
+ }
432
357
 
433
- return keys;
434
- },
435
- getOwnPropertyDescriptor(target, key) {
436
- if (key in target) {
437
- return Reflect.getOwnPropertyDescriptor(target, key);
438
- } else if (key in target._raw.ops) {
358
+ return keys;
359
+ },
360
+ getOwnPropertyDescriptor(target, key) {
361
+ if (key in target) {
362
+ return Reflect.getOwnPropertyDescriptor(target, key);
363
+ } else {
364
+ const descriptor = (target._schema[key as keyof CoMap["_schema"]] ||
365
+ target._schema[ItemsSym]) as Schema;
366
+ if (descriptor || key in target._raw.ops) {
439
367
  return {
440
368
  enumerable: true,
441
369
  configurable: true,
442
370
  writable: true,
443
371
  };
444
372
  }
445
- },
446
- };
447
- }
373
+ }
374
+ },
375
+ deleteProperty(target, key) {
376
+ const descriptor = (target._schema[key as keyof CoMap["_schema"]] ||
377
+ target._schema[ItemsSym]) as Schema;
378
+ if (typeof key === "string" && descriptor) {
379
+ target._raw.delete(key);
380
+ return true;
381
+ } else {
382
+ return Reflect.deleteProperty(target, key);
383
+ }
384
+ },
385
+ };