jazz-tools 0.7.0-alpha.1 → 0.7.0-alpha.3
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +12 -0
- package/dist/coValues/account.js +7 -7
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/coList.js +8 -8
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/coMap.js +15 -15
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coStream.js +8 -8
- package/dist/coValues/coStream.js.map +1 -1
- package/dist/coValues/extensions/imageDef.js +6 -11
- package/dist/coValues/extensions/imageDef.js.map +1 -1
- package/dist/coValues/group.js +13 -16
- package/dist/coValues/group.js.map +1 -1
- package/dist/implementation/refs.js +5 -4
- package/dist/implementation/refs.js.map +1 -1
- package/dist/index.js +0 -16
- package/dist/index.js.map +1 -1
- package/dist/tests/coList.test.js +5 -5
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +31 -48
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +10 -10
- package/dist/tests/coStream.test.js.map +1 -1
- package/package.json +1 -1
- package/src/coValues/account.ts +11 -11
- package/src/coValues/coList.ts +10 -10
- package/src/coValues/coMap.ts +22 -22
- package/src/coValues/coStream.ts +10 -10
- package/src/coValues/extensions/imageDef.ts +14 -13
- package/src/coValues/group.ts +15 -16
- package/src/implementation/refs.ts +33 -10
- package/src/index.ts +0 -17
- package/src/tests/coList.test.ts +5 -5
- package/src/tests/coMap.test.ts +32 -46
- package/src/tests/coStream.test.ts +10 -10
package/src/coValues/coMap.ts
CHANGED
@@ -70,15 +70,15 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
|
|
70
70
|
}
|
71
71
|
_raw!: RawCoMap;
|
72
72
|
|
73
|
-
static
|
74
|
-
get
|
73
|
+
static _encoding: any;
|
74
|
+
get _encoding(): {
|
75
75
|
[Key in OwnKeys<Fields>]: FieldDescriptorFor<Fields[Key]>;
|
76
76
|
} & {
|
77
77
|
[indexSignature]: indexSignature extends keyof Fields
|
78
78
|
? FieldDescriptorFor<Fields[indexSignature]>
|
79
79
|
: never;
|
80
80
|
} {
|
81
|
-
return (this.constructor as typeof CoMap).
|
81
|
+
return (this.constructor as typeof CoMap)._encoding;
|
82
82
|
}
|
83
83
|
|
84
84
|
get _refs(): {
|
@@ -91,14 +91,14 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
|
|
91
91
|
return makeRefs<OwnKeys<Fields>>(
|
92
92
|
(key) => this._raw.get(key as string) as unknown as ID<CoValue>,
|
93
93
|
() =>
|
94
|
-
Object.keys(this.
|
95
|
-
const schema = this.
|
96
|
-
key as keyof typeof this.
|
94
|
+
Object.keys(this._encoding).filter((key) => {
|
95
|
+
const schema = this._encoding[
|
96
|
+
key as keyof typeof this._encoding
|
97
97
|
] as FieldDescriptor;
|
98
98
|
schema !== "json" && "ref" in schema;
|
99
99
|
}) as OwnKeys<Fields>[],
|
100
100
|
this._loadedAs,
|
101
|
-
(key) => (this.
|
101
|
+
(key) => (this._encoding[key] as RefField<CoValue>).ref()
|
102
102
|
) as any;
|
103
103
|
}
|
104
104
|
|
@@ -115,8 +115,8 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
|
|
115
115
|
const rawEdit = target._raw.lastEditAt(key as string);
|
116
116
|
if (!rawEdit) return undefined;
|
117
117
|
|
118
|
-
const descriptor = target.
|
119
|
-
key as keyof typeof target.
|
118
|
+
const descriptor = target._encoding[
|
119
|
+
key as keyof typeof target._encoding
|
120
120
|
] as FieldDescriptor;
|
121
121
|
|
122
122
|
return {
|
@@ -168,7 +168,7 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
|
|
168
168
|
) {
|
169
169
|
super();
|
170
170
|
|
171
|
-
if (!this.
|
171
|
+
if (!this._encoding) {
|
172
172
|
throw new Error(
|
173
173
|
"No schema found in " +
|
174
174
|
this.constructor.name +
|
@@ -188,7 +188,7 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
|
|
188
188
|
|
189
189
|
this.definePropertiesFromSchema();
|
190
190
|
|
191
|
-
if (this.
|
191
|
+
if (this._encoding[indexSignature]) {
|
192
192
|
return new Proxy(this, CoMapProxyHandler<Fields>());
|
193
193
|
}
|
194
194
|
}
|
@@ -196,7 +196,7 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
|
|
196
196
|
toJSON() {
|
197
197
|
const jsonedFields = this._raw.keys().map((key) => {
|
198
198
|
const tKey = key as OwnKeys<Fields>;
|
199
|
-
const descriptor = this.
|
199
|
+
const descriptor = this._encoding[tKey] as FieldDescriptor;
|
200
200
|
|
201
201
|
if (descriptor == "json" || "encode" in descriptor) {
|
202
202
|
return [key, this._raw.get(key)];
|
@@ -238,9 +238,9 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
|
|
238
238
|
for (const key of Object.keys(init) as (keyof Fields)[]) {
|
239
239
|
const initValue = init[key as keyof typeof init];
|
240
240
|
|
241
|
-
const descriptor = (this.
|
242
|
-
key as keyof typeof this.
|
243
|
-
] || this.
|
241
|
+
const descriptor = (this._encoding[
|
242
|
+
key as keyof typeof this._encoding
|
243
|
+
] || this._encoding[indexSignature]) as FieldDescriptor;
|
244
244
|
|
245
245
|
if (descriptor === "json") {
|
246
246
|
rawInit[key] = initValue as JsonValue;
|
@@ -263,17 +263,17 @@ export class CoMap<Fields extends EnsureValid<Fields> = DefaultFields>
|
|
263
263
|
static encoding<V extends CoMap>(
|
264
264
|
this: { new (...args: any): V } & typeof CoMap,
|
265
265
|
fields: Simplify<{
|
266
|
-
[Key in keyof V["
|
266
|
+
[Key in keyof V["_encoding"] as V["_encoding"][Key] extends never
|
267
267
|
? never
|
268
|
-
: Key]: Simplify<V["
|
268
|
+
: Key]: Simplify<V["_encoding"][Key]>;
|
269
269
|
}>
|
270
270
|
) {
|
271
|
-
this.
|
272
|
-
Object.assign(this.
|
271
|
+
this._encoding ||= {};
|
272
|
+
Object.assign(this._encoding, fields);
|
273
273
|
}
|
274
274
|
|
275
275
|
private definePropertiesFromSchema() {
|
276
|
-
for (const [key, fieldSchema] of Object.entries(this.
|
276
|
+
for (const [key, fieldSchema] of Object.entries(this._encoding)) {
|
277
277
|
if (key === "indexSignature") continue;
|
278
278
|
const descriptor = fieldSchema as FieldDescriptor;
|
279
279
|
if (descriptor === "json") {
|
@@ -374,7 +374,7 @@ function CoMapProxyHandler<Fields extends EnsureValid<Fields>>(): ProxyHandler<
|
|
374
374
|
> {
|
375
375
|
return {
|
376
376
|
get(target, key, receiver) {
|
377
|
-
const descriptor = target.
|
377
|
+
const descriptor = target._encoding[
|
378
378
|
indexSignature
|
379
379
|
] as FieldDescriptor;
|
380
380
|
if (key in target || typeof key === "symbol") {
|
@@ -400,7 +400,7 @@ function CoMapProxyHandler<Fields extends EnsureValid<Fields>>(): ProxyHandler<
|
|
400
400
|
}
|
401
401
|
},
|
402
402
|
set(target, key, value, receiver) {
|
403
|
-
const descriptor = target.
|
403
|
+
const descriptor = target._encoding[
|
404
404
|
indexSignature
|
405
405
|
] as FieldDescriptor;
|
406
406
|
if (key in target || typeof key === "symbol") {
|
package/src/coValues/coStream.ts
CHANGED
@@ -44,11 +44,11 @@ export class CoStream<Item extends EnsureItemNullable<Item, "Co.Stream"> = any>
|
|
44
44
|
|
45
45
|
/** @internal This is only a marker type and doesn't exist at runtime */
|
46
46
|
_item!: Item;
|
47
|
-
static
|
48
|
-
get
|
47
|
+
static _encoding: any;
|
48
|
+
get _encoding(): {
|
49
49
|
_item: FieldDescriptorFor<Item>;
|
50
50
|
} {
|
51
|
-
return (this.constructor as typeof CoStream).
|
51
|
+
return (this.constructor as typeof CoStream)._encoding;
|
52
52
|
}
|
53
53
|
|
54
54
|
by: {
|
@@ -111,7 +111,7 @@ export class CoStream<Item extends EnsureItemNullable<Item, "Co.Stream"> = any>
|
|
111
111
|
rawEntry,
|
112
112
|
this._loadedAs,
|
113
113
|
accountID as unknown as ID<Account>,
|
114
|
-
this.
|
114
|
+
this._encoding._item
|
115
115
|
);
|
116
116
|
},
|
117
117
|
configurable: true,
|
@@ -138,7 +138,7 @@ export class CoStream<Item extends EnsureItemNullable<Item, "Co.Stream"> = any>
|
|
138
138
|
cojsonInternals.isAccountID(by)
|
139
139
|
? (by as unknown as ID<Account>)
|
140
140
|
: undefined,
|
141
|
-
this.
|
141
|
+
this._encoding._item
|
142
142
|
);
|
143
143
|
},
|
144
144
|
configurable: true,
|
@@ -155,7 +155,7 @@ export class CoStream<Item extends EnsureItemNullable<Item, "Co.Stream"> = any>
|
|
155
155
|
}
|
156
156
|
|
157
157
|
private pushItem(item: Item) {
|
158
|
-
const itemDescriptor = this.
|
158
|
+
const itemDescriptor = this._encoding._item as FieldDescriptor;
|
159
159
|
|
160
160
|
if (itemDescriptor === "json") {
|
161
161
|
this._raw.push(item as JsonValue);
|
@@ -167,7 +167,7 @@ export class CoStream<Item extends EnsureItemNullable<Item, "Co.Stream"> = any>
|
|
167
167
|
}
|
168
168
|
|
169
169
|
toJSON() {
|
170
|
-
const itemDescriptor = this.
|
170
|
+
const itemDescriptor = this._encoding._item as FieldDescriptor;
|
171
171
|
const mapper =
|
172
172
|
itemDescriptor === "json"
|
173
173
|
? (v: unknown) => v
|
@@ -199,10 +199,10 @@ export class CoStream<Item extends EnsureItemNullable<Item, "Co.Stream"> = any>
|
|
199
199
|
|
200
200
|
static encoding<V extends CoStream>(
|
201
201
|
this: { new (...args: any): V } & typeof CoStream,
|
202
|
-
def: { _item: V["
|
202
|
+
def: { _item: V["_encoding"]["_item"] }
|
203
203
|
) {
|
204
|
-
this.
|
205
|
-
Object.assign(this.
|
204
|
+
this._encoding ||= {};
|
205
|
+
Object.assign(this._encoding, def);
|
206
206
|
}
|
207
207
|
}
|
208
208
|
|
@@ -1,20 +1,16 @@
|
|
1
|
-
import {
|
1
|
+
import {
|
2
|
+
BinaryCoStream,
|
3
|
+
CoMap,
|
4
|
+
indexSignature,
|
5
|
+
subscriptionsScopes,
|
6
|
+
} from "../../internal.js";
|
2
7
|
|
3
8
|
export class ImageDefinition extends CoMap<ImageDefinition> {
|
4
|
-
declare originalSize: [number, number]
|
5
|
-
declare placeholderDataURL?: string
|
9
|
+
declare originalSize: [number, number];
|
10
|
+
declare placeholderDataURL?: string;
|
6
11
|
|
7
12
|
[res: `${number}x${number}`]: BinaryCoStream | null;
|
8
|
-
declare [indexSignature]: BinaryCoStream | null
|
9
|
-
|
10
|
-
static {
|
11
|
-
this.prototype._schema
|
12
|
-
this.encoding({
|
13
|
-
originalSize: "json",
|
14
|
-
placeholderDataURL: "json",
|
15
|
-
[indexSignature]: {ref: () => BinaryCoStream},
|
16
|
-
})
|
17
|
-
}
|
13
|
+
declare [indexSignature]: BinaryCoStream | null;
|
18
14
|
|
19
15
|
get _highestResAvailable():
|
20
16
|
| { res: `${number}x${number}`; stream: BinaryCoStream }
|
@@ -57,3 +53,8 @@ export class ImageDefinition extends CoMap<ImageDefinition> {
|
|
57
53
|
);
|
58
54
|
}
|
59
55
|
}
|
56
|
+
ImageDefinition.encoding({
|
57
|
+
originalSize: "json",
|
58
|
+
placeholderDataURL: "json",
|
59
|
+
[indexSignature]: { ref: () => BinaryCoStream },
|
60
|
+
});
|
package/src/coValues/group.ts
CHANGED
@@ -16,11 +16,8 @@ import {
|
|
16
16
|
|
17
17
|
export class Profile extends CoMap<{ name: string }> {
|
18
18
|
declare name: string;
|
19
|
-
|
20
|
-
static {
|
21
|
-
this.encoding({ name: "json" } as any);
|
22
|
-
}
|
23
19
|
}
|
20
|
+
Profile.encoding({ name: "json" });
|
24
21
|
|
25
22
|
export class Group<
|
26
23
|
Def extends { profile: Profile | null; root: CoMap | null } = {
|
@@ -38,8 +35,8 @@ export class Group<
|
|
38
35
|
}
|
39
36
|
_raw!: RawGroup;
|
40
37
|
|
41
|
-
static
|
42
|
-
get
|
38
|
+
static _encoding: any;
|
39
|
+
get _encoding(): {
|
43
40
|
profile: Def["profile"] extends CoValue
|
44
41
|
? RefField<Def["profile"]>
|
45
42
|
: PrimitiveField;
|
@@ -47,15 +44,15 @@ export class Group<
|
|
47
44
|
? RefField<Def["root"]>
|
48
45
|
: PrimitiveField;
|
49
46
|
} {
|
50
|
-
return (this.constructor as typeof Group).
|
47
|
+
return (this.constructor as typeof Group)._encoding;
|
51
48
|
}
|
52
49
|
static {
|
53
|
-
this.
|
50
|
+
this._encoding = {
|
54
51
|
profile: { json: true },
|
55
52
|
root: { json: true },
|
56
53
|
} as any;
|
57
|
-
Object.defineProperty(this.prototype, "
|
58
|
-
get: () => this.
|
54
|
+
Object.defineProperty(this.prototype, "_encoding", {
|
55
|
+
get: () => this._encoding,
|
59
56
|
});
|
60
57
|
}
|
61
58
|
|
@@ -84,7 +81,7 @@ export class Group<
|
|
84
81
|
profileID,
|
85
82
|
this._loadedAs,
|
86
83
|
(
|
87
|
-
this.
|
84
|
+
this._encoding.profile as RefField<
|
88
85
|
NonNullable<Def["profile"]>
|
89
86
|
>
|
90
87
|
).ref()
|
@@ -95,7 +92,9 @@ export class Group<
|
|
95
92
|
rootID,
|
96
93
|
this._loadedAs,
|
97
94
|
(
|
98
|
-
this.
|
95
|
+
this._encoding.root as RefField<
|
96
|
+
NonNullable<Def["root"]>
|
97
|
+
>
|
99
98
|
).ref()
|
100
99
|
) as any),
|
101
100
|
};
|
@@ -177,11 +176,11 @@ export class Group<
|
|
177
176
|
static define<V extends Account>(
|
178
177
|
this: CoValueClass<V> & typeof Account,
|
179
178
|
fields: {
|
180
|
-
profile: V["
|
181
|
-
root: V["
|
179
|
+
profile: V["_encoding"]["profile"];
|
180
|
+
root: V["_encoding"]["root"];
|
182
181
|
}
|
183
182
|
) {
|
184
|
-
this.
|
185
|
-
Object.assign(this.
|
183
|
+
this._encoding ||= {};
|
184
|
+
Object.assign(this._encoding, fields);
|
186
185
|
}
|
187
186
|
}
|
@@ -1,7 +1,15 @@
|
|
1
1
|
import { Effect } from "effect";
|
2
2
|
import type { CoID, RawCoValue } from "cojson";
|
3
|
-
import type {
|
4
|
-
|
3
|
+
import type {
|
4
|
+
Account,
|
5
|
+
CoValue,
|
6
|
+
ID,
|
7
|
+
Me,
|
8
|
+
SubclassedConstructor,
|
9
|
+
UnavailableError,
|
10
|
+
indexSignature,
|
11
|
+
} from "../internal.js";
|
12
|
+
import { subscriptionsScopes } from "../internal.js";
|
5
13
|
|
6
14
|
export class ValueRef<V extends CoValue> {
|
7
15
|
private cachedValue: V | undefined;
|
@@ -19,7 +27,9 @@ export class ValueRef<V extends CoValue> {
|
|
19
27
|
this.id as unknown as CoID<RawCoValue>
|
20
28
|
);
|
21
29
|
if (raw) {
|
22
|
-
const value = new this.valueConstructor(undefined, {
|
30
|
+
const value = new this.valueConstructor(undefined, {
|
31
|
+
fromRaw: raw,
|
32
|
+
}) as V;
|
23
33
|
this.cachedValue = value;
|
24
34
|
return value;
|
25
35
|
} else {
|
@@ -43,7 +53,9 @@ export class ValueRef<V extends CoValue> {
|
|
43
53
|
});
|
44
54
|
}
|
45
55
|
|
46
|
-
private async loadHelper(options?: {
|
56
|
+
private async loadHelper(options?: {
|
57
|
+
onProgress: (p: number) => void;
|
58
|
+
}): Promise<V | "unavailable"> {
|
47
59
|
const raw = await this.controlledAccount._raw.core.node.load(
|
48
60
|
this.id as unknown as CoID<RawCoValue>,
|
49
61
|
options?.onProgress
|
@@ -51,12 +63,17 @@ export class ValueRef<V extends CoValue> {
|
|
51
63
|
if (raw === "unavailable") {
|
52
64
|
return "unavailable";
|
53
65
|
} else {
|
54
|
-
return new ValueRef(
|
55
|
-
.
|
66
|
+
return new ValueRef(
|
67
|
+
this.id,
|
68
|
+
this.controlledAccount,
|
69
|
+
this.valueConstructor
|
70
|
+
).value!;
|
56
71
|
}
|
57
72
|
}
|
58
73
|
|
59
|
-
async load(options?: {
|
74
|
+
async load(options?: {
|
75
|
+
onProgress: (p: number) => void;
|
76
|
+
}): Promise<V | undefined> {
|
60
77
|
const result = await this.loadHelper(options);
|
61
78
|
if (result === "unavailable") {
|
62
79
|
return undefined;
|
@@ -82,9 +99,15 @@ export function makeRefs<Keys extends string | number | indexSignature>(
|
|
82
99
|
getIdForKey: (key: Keys) => ID<CoValue> | undefined,
|
83
100
|
getKeysWithIds: () => Keys[],
|
84
101
|
controlledAccount: Account & Me,
|
85
|
-
valueConstructorForKey: (key: Keys) => SubclassedConstructor<CoValue
|
86
|
-
): { [K in Keys]: ValueRef<CoValue> } {
|
87
|
-
|
102
|
+
valueConstructorForKey: (key: Keys) => SubclassedConstructor<CoValue>
|
103
|
+
): { [K in Keys]: ValueRef<CoValue> } & {
|
104
|
+
[Symbol.iterator]: () => IterableIterator<ValueRef<CoValue>>;
|
105
|
+
length: number;
|
106
|
+
} {
|
107
|
+
const refs = {} as { [K in Keys]: ValueRef<CoValue> } & {
|
108
|
+
[Symbol.iterator]: () => IterableIterator<ValueRef<CoValue>>;
|
109
|
+
length: number;
|
110
|
+
};
|
88
111
|
return new Proxy(refs, {
|
89
112
|
get(target, key) {
|
90
113
|
if (key === Symbol.iterator) {
|
package/src/index.ts
CHANGED
@@ -1,20 +1,3 @@
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-namespace */
|
2
|
-
import { BinaryCoStream, CoMap, ImageDefinition, Account as Account_, Group as Group_, CoList, CoStream } from "./internal.js";
|
3
|
-
|
4
|
-
/** @category Schemas & CoValues - Schema definers */
|
5
|
-
export namespace Co {
|
6
|
-
export const Map = CoMap;
|
7
|
-
export const List = CoList;
|
8
|
-
export const Stream = CoStream;
|
9
|
-
export const BinaryStream = BinaryCoStream;
|
10
|
-
export const Account = Account_;
|
11
|
-
export const Group = Group_;
|
12
|
-
export namespace media {
|
13
|
-
export const ImageDef = ImageDefinition;
|
14
|
-
export type ImageDef = ImageDefinition;
|
15
|
-
}
|
16
|
-
}
|
17
|
-
|
18
1
|
/** @category Internal types */
|
19
2
|
export {
|
20
3
|
cojsonReady as jazzReady,
|
package/src/tests/coList.test.ts
CHANGED
@@ -4,7 +4,7 @@ import { webcrypto } from "node:crypto";
|
|
4
4
|
import { connectedPeers } from "cojson/src/streamUtils.js";
|
5
5
|
import { newRandomSessionID } from "cojson/src/coValueCore.js";
|
6
6
|
import { Effect, Queue } from "effect";
|
7
|
-
import {
|
7
|
+
import { Account, CoList, jazzReady } from "..";
|
8
8
|
|
9
9
|
if (!("crypto" in globalThis)) {
|
10
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
@@ -20,7 +20,7 @@ describe("Simple CoList operations", async () => {
|
|
20
20
|
name: "Hermes Puggington",
|
21
21
|
});
|
22
22
|
|
23
|
-
class TestList extends
|
23
|
+
class TestList extends CoList<string> {}
|
24
24
|
TestList.encoding({ _item: "json" });
|
25
25
|
|
26
26
|
const list = new TestList(["bread", "butter", "onion"], { owner: me });
|
@@ -115,17 +115,17 @@ describe("Simple CoList operations", async () => {
|
|
115
115
|
});
|
116
116
|
|
117
117
|
describe("CoList resolution", async () => {
|
118
|
-
class TwiceNestedList extends
|
118
|
+
class TwiceNestedList extends CoList<string> {
|
119
119
|
joined() {
|
120
120
|
return this.join(",");
|
121
121
|
}
|
122
122
|
}
|
123
123
|
TwiceNestedList.encoding({ _item: "json" });
|
124
124
|
|
125
|
-
class NestedList extends
|
125
|
+
class NestedList extends CoList<TwiceNestedList | null> {}
|
126
126
|
NestedList.encoding({ _item: { ref: () => TwiceNestedList } });
|
127
127
|
|
128
|
-
class TestList extends
|
128
|
+
class TestList extends CoList<NestedList | null> {}
|
129
129
|
TestList.encoding({ _item: { ref: () => NestedList } });
|
130
130
|
|
131
131
|
const initNodeAndList = async () => {
|
package/src/tests/coMap.test.ts
CHANGED
@@ -4,7 +4,7 @@ import { webcrypto } from "node:crypto";
|
|
4
4
|
import { connectedPeers } from "cojson/src/streamUtils.js";
|
5
5
|
import { newRandomSessionID } from "cojson/src/coValueCore.js";
|
6
6
|
import { Effect, Queue } from "effect";
|
7
|
-
import {
|
7
|
+
import { Account, jazzReady, Encoders, indexSignature, CoMap } from "..";
|
8
8
|
|
9
9
|
if (!("crypto" in globalThis)) {
|
10
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
@@ -20,7 +20,7 @@ describe("Simple CoMap operations", async () => {
|
|
20
20
|
name: "Hermes Puggington",
|
21
21
|
});
|
22
22
|
|
23
|
-
class TestMap extends
|
23
|
+
class TestMap extends CoMap<TestMap> {
|
24
24
|
declare color: string;
|
25
25
|
declare height: number;
|
26
26
|
declare birthday: Date;
|
@@ -29,18 +29,15 @@ describe("Simple CoMap operations", async () => {
|
|
29
29
|
get _roughColor() {
|
30
30
|
return this.color + "ish";
|
31
31
|
}
|
32
|
-
|
33
|
-
static {
|
34
|
-
this.encoding({
|
35
|
-
color: "json",
|
36
|
-
height: "json",
|
37
|
-
birthday: { encoded: Encoders.Date },
|
38
|
-
name: "json",
|
39
|
-
});
|
40
|
-
}
|
41
32
|
}
|
33
|
+
TestMap.encoding({
|
34
|
+
color: "json",
|
35
|
+
height: "json",
|
36
|
+
birthday: { encoded: Encoders.Date },
|
37
|
+
name: "json",
|
38
|
+
});
|
42
39
|
|
43
|
-
console.log("TestMap schema", TestMap.prototype.
|
40
|
+
console.log("TestMap schema", TestMap.prototype._encoding);
|
44
41
|
|
45
42
|
const birthday = new Date();
|
46
43
|
|
@@ -84,17 +81,15 @@ describe("Simple CoMap operations", async () => {
|
|
84
81
|
});
|
85
82
|
});
|
86
83
|
|
87
|
-
class RecursiveMap extends
|
84
|
+
class RecursiveMap extends CoMap<RecursiveMap> {
|
88
85
|
declare name: string;
|
89
86
|
declare next: RecursiveMap | null;
|
90
|
-
|
91
|
-
static {
|
92
|
-
this.encoding({
|
93
|
-
name: "json",
|
94
|
-
next: { ref: () => RecursiveMap },
|
95
|
-
});
|
96
|
-
}
|
97
87
|
}
|
88
|
+
RecursiveMap.encoding({
|
89
|
+
name: "json",
|
90
|
+
next: { ref: () => RecursiveMap },
|
91
|
+
});
|
92
|
+
|
98
93
|
const recursiveMap = new RecursiveMap(
|
99
94
|
{
|
100
95
|
name: "first",
|
@@ -124,14 +119,14 @@ describe("Simple CoMap operations", async () => {
|
|
124
119
|
});
|
125
120
|
|
126
121
|
describe("CoMap resolution", async () => {
|
127
|
-
class TwiceNestedMap extends
|
122
|
+
class TwiceNestedMap extends CoMap<TwiceNestedMap> {
|
128
123
|
taste!: string;
|
129
124
|
}
|
130
125
|
TwiceNestedMap.encoding({
|
131
126
|
taste: "json",
|
132
127
|
});
|
133
128
|
|
134
|
-
class NestedMap extends
|
129
|
+
class NestedMap extends CoMap<NestedMap> {
|
135
130
|
name!: string;
|
136
131
|
twiceNested!: TwiceNestedMap | null;
|
137
132
|
|
@@ -144,23 +139,20 @@ describe("CoMap resolution", async () => {
|
|
144
139
|
twiceNested: { ref: () => TwiceNestedMap },
|
145
140
|
});
|
146
141
|
|
147
|
-
class TestMap extends
|
142
|
+
class TestMap extends CoMap<TestMap> {
|
148
143
|
declare color: string;
|
149
144
|
declare height: number;
|
150
145
|
declare nested: NestedMap | null;
|
151
146
|
|
152
|
-
static {
|
153
|
-
this.encoding({
|
154
|
-
color: "json",
|
155
|
-
height: "json",
|
156
|
-
nested: { ref: () => NestedMap },
|
157
|
-
});
|
158
|
-
}
|
159
|
-
|
160
147
|
get _roughColor() {
|
161
148
|
return this.color + "ish";
|
162
149
|
}
|
163
150
|
}
|
151
|
+
TestMap.encoding({
|
152
|
+
color: "json",
|
153
|
+
height: "json",
|
154
|
+
nested: { ref: () => NestedMap },
|
155
|
+
});
|
164
156
|
|
165
157
|
const initNodeAndMap = async () => {
|
166
158
|
const me = await Account.create({
|
@@ -349,17 +341,14 @@ describe("CoMap resolution", async () => {
|
|
349
341
|
);
|
350
342
|
});
|
351
343
|
|
352
|
-
class TestMapWithOptionalRef extends
|
344
|
+
class TestMapWithOptionalRef extends CoMap<TestMapWithOptionalRef> {
|
353
345
|
declare color: string;
|
354
346
|
declare nested?: NestedMap | null;
|
355
|
-
|
356
|
-
static {
|
357
|
-
this.encoding({
|
358
|
-
color: "json",
|
359
|
-
nested: { ref: () => NestedMap },
|
360
|
-
});
|
361
|
-
}
|
362
347
|
}
|
348
|
+
TestMapWithOptionalRef.encoding({
|
349
|
+
color: "json",
|
350
|
+
nested: { ref: () => NestedMap },
|
351
|
+
});
|
363
352
|
|
364
353
|
test("Construction with optional", async () => {
|
365
354
|
const me = await Account.create({
|
@@ -399,16 +388,13 @@ describe("CoMap resolution", async () => {
|
|
399
388
|
expect(mapWith.nested?._raw).toBeDefined();
|
400
389
|
});
|
401
390
|
|
402
|
-
class TestRecord extends
|
391
|
+
class TestRecord extends CoMap<TestRecord> {
|
403
392
|
declare [indexSignature]: number;
|
404
|
-
|
405
|
-
static {
|
406
|
-
this.encoding({
|
407
|
-
[indexSignature]: "json"
|
408
|
-
});
|
409
|
-
}
|
410
393
|
}
|
411
394
|
interface TestRecord extends Record<string, number> {}
|
395
|
+
TestRecord.encoding({
|
396
|
+
[indexSignature]: "json",
|
397
|
+
});
|
412
398
|
|
413
399
|
test("Construction with index signature", async () => {
|
414
400
|
const me = await Account.create({
|