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.
- package/.turbo/turbo-build.log +78 -8
- package/CHANGELOG.md +181 -0
- package/dist/coValues/account.js +81 -41
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/coList.js +156 -104
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/coMap.js +182 -163
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coStream.js +202 -71
- 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 +46 -38
- package/dist/coValues/group.js.map +1 -1
- package/dist/coValues/interfaces.js +23 -5
- 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 +32 -36
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +170 -59
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +59 -64
- package/dist/tests/coStream.test.js.map +1 -1
- package/dist/tests/groupsAndAccounts.test.js +88 -0
- package/dist/tests/groupsAndAccounts.test.js.map +1 -0
- package/package.json +5 -4
- package/src/coValues/account.ts +129 -105
- package/src/coValues/coList.ts +200 -131
- package/src/coValues/coMap.ts +243 -305
- package/src/coValues/coStream.ts +300 -127
- package/src/coValues/extensions/imageDef.ts +14 -16
- package/src/coValues/group.ts +90 -106
- package/src/coValues/interfaces.ts +33 -12
- package/src/implementation/refs.ts +42 -25
- package/src/implementation/schema.ts +69 -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 +35 -39
- package/src/tests/coMap.test.ts +176 -81
- package/src/tests/coStream.test.ts +76 -81
- package/src/tests/groupsAndAccounts.test.ts +100 -0
package/src/coValues/coMap.ts
CHANGED
@@ -1,148 +1,110 @@
|
|
1
1
|
import type { JsonValue, RawCoMap } from "cojson";
|
2
2
|
import type { Simplify } from "effect/Types";
|
3
|
-
import {
|
3
|
+
import { encodeSync, decodeSync } from "@effect/schema/Schema";
|
4
4
|
import type {
|
5
5
|
CoValue,
|
6
|
-
|
7
|
-
FieldDescriptor,
|
8
|
-
FieldDescriptorFor,
|
6
|
+
Schema,
|
9
7
|
Group,
|
10
8
|
ID,
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
RefEncoded,
|
10
|
+
IfCo,
|
11
|
+
RefIfCoValue,
|
12
|
+
SubclassedConstructor,
|
14
13
|
} from "../internal.js";
|
15
14
|
import {
|
16
15
|
Account,
|
17
16
|
CoValueBase,
|
18
|
-
|
17
|
+
Ref,
|
18
|
+
SchemaInit,
|
19
19
|
inspect,
|
20
20
|
makeRefs,
|
21
21
|
subscriptionsScopes,
|
22
|
-
|
22
|
+
ItemsSym,
|
23
|
+
InitValues,
|
24
|
+
isRefEncoded,
|
23
25
|
} from "../internal.js";
|
24
26
|
|
25
|
-
type
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
45
|
-
|
46
|
-
|
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
|
63
|
-
|
64
|
-
|
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
|
72
|
-
|
73
|
-
static
|
74
|
-
get
|
75
|
-
|
76
|
-
|
77
|
-
[
|
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
|
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<
|
57
|
+
return makeRefs<CoKeys<this>>(
|
92
58
|
(key) => this._raw.get(key as string) as unknown as ID<CoValue>,
|
93
|
-
() =>
|
94
|
-
|
95
|
-
const schema =
|
96
|
-
key as keyof typeof this.
|
97
|
-
|
98
|
-
schema !== "json" &&
|
99
|
-
}) as
|
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) =>
|
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.
|
119
|
-
key as keyof typeof target.
|
120
|
-
] as
|
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
|
-
?
|
128
|
-
|
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
|
95
|
+
descriptor
|
134
96
|
).accessFrom(target),
|
135
97
|
ref:
|
136
|
-
descriptor !== "json" &&
|
137
|
-
? new
|
98
|
+
descriptor !== "json" && isRefEncoded(descriptor)
|
99
|
+
? new Ref(
|
138
100
|
rawEdit.value as ID<CoValue>,
|
139
101
|
target._loadedAs,
|
140
|
-
descriptor
|
102
|
+
descriptor
|
141
103
|
)
|
142
104
|
: undefined,
|
143
105
|
by:
|
144
106
|
rawEdit.by &&
|
145
|
-
new
|
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
|
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
|
-
|
161
|
-
|
162
|
-
init: Simplify<CoMapInit<Fields>>,
|
163
|
-
options: { owner: Account | Group }
|
164
|
-
);
|
124
|
+
[InitValues]?: any;
|
125
|
+
|
165
126
|
constructor(
|
166
|
-
init:
|
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 (
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
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
|
-
|
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
|
-
|
192
|
-
|
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
|
199
|
-
const descriptor = this.
|
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 (
|
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
|
-
|
224
|
-
|
187
|
+
init: Simplify<CoMapInit<Fields>> | undefined,
|
188
|
+
owner: Account | Group
|
225
189
|
) {
|
226
|
-
|
190
|
+
const rawOwner = owner._raw;
|
227
191
|
|
228
|
-
|
229
|
-
|
230
|
-
}
|
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
|
-
|
259
|
-
|
260
|
-
|
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
|
-
|
264
|
-
|
265
|
-
|
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
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
key
|
283
|
-
|
284
|
-
|
285
|
-
|
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
|
-
|
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
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
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
|
-
|
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
|
232
|
+
export type CoKeys<Fields extends object> = Exclude<
|
359
233
|
keyof Fields & string,
|
360
|
-
keyof CoMap
|
234
|
+
keyof CoMap
|
361
235
|
>;
|
362
236
|
|
363
237
|
export type CoMapInit<Fields extends object> = {
|
364
|
-
[Key in
|
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
|
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
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
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
|
-
:
|
391
|
-
} else if (
|
286
|
+
: decodeSync(descriptor.encoded)(raw);
|
287
|
+
} else if (isRefEncoded(descriptor)) {
|
392
288
|
return raw === undefined
|
393
289
|
? undefined
|
394
|
-
: new
|
290
|
+
: new Ref(
|
395
291
|
raw as unknown as ID<CoValue>,
|
396
292
|
target._loadedAs,
|
397
|
-
descriptor
|
398
|
-
).accessFrom(
|
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
|
-
|
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
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
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
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
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
|
+
};
|