jazz-tools 0.8.14 → 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 +281 -268
  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 +7 -0
  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 +7 -0
  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 -25
  74. package/src/implementation/createContext.ts +210 -204
  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,171 +1,159 @@
1
1
  import type { CoID, RawCoValue } from "cojson";
2
2
  import type {
3
- Account,
4
- AnonymousJazzAgent,
5
- CoValue,
6
- ID,
7
- RefEncoded,
8
- UnCo,
3
+ Account,
4
+ AnonymousJazzAgent,
5
+ CoValue,
6
+ ID,
7
+ RefEncoded,
8
+ UnCo,
9
9
  } from "../internal.js";
10
10
  import {
11
- instantiateRefEncoded,
12
- isRefEncoded,
13
- subscriptionsScopes,
11
+ instantiateRefEncoded,
12
+ isRefEncoded,
13
+ subscriptionsScopes,
14
14
  } from "../internal.js";
15
15
  import { coValuesCache } from "../lib/cache.js";
16
16
 
17
17
  const TRACE_ACCESSES = false;
18
18
 
19
19
  export class Ref<out V extends CoValue> {
20
- constructor(
21
- readonly id: ID<V>,
22
- readonly controlledAccount: Account | AnonymousJazzAgent,
23
- readonly schema: RefEncoded<V>,
24
- ) {
25
- if (!isRefEncoded(schema)) {
26
- throw new Error("Ref must be constructed with a ref schema");
27
- }
20
+ constructor(
21
+ readonly id: ID<V>,
22
+ readonly controlledAccount: Account | AnonymousJazzAgent,
23
+ readonly schema: RefEncoded<V>,
24
+ ) {
25
+ if (!isRefEncoded(schema)) {
26
+ throw new Error("Ref must be constructed with a ref schema");
28
27
  }
28
+ }
29
29
 
30
- get value() {
31
- const node =
32
- "node" in this.controlledAccount
33
- ? this.controlledAccount.node
34
- : this.controlledAccount._raw.core.node;
35
- const raw = node.getLoaded(this.id as unknown as CoID<RawCoValue>);
36
- if (raw) {
37
- return coValuesCache.get(raw, () =>
38
- instantiateRefEncoded(this.schema, raw),
39
- );
40
- } else {
41
- return null;
42
- }
30
+ get value() {
31
+ const node =
32
+ "node" in this.controlledAccount
33
+ ? this.controlledAccount.node
34
+ : this.controlledAccount._raw.core.node;
35
+ const raw = node.getLoaded(this.id as unknown as CoID<RawCoValue>);
36
+ if (raw) {
37
+ return coValuesCache.get(raw, () =>
38
+ instantiateRefEncoded(this.schema, raw),
39
+ );
40
+ } else {
41
+ return null;
43
42
  }
43
+ }
44
44
 
45
- private async loadHelper(): Promise<V | "unavailable"> {
46
- const node =
47
- "node" in this.controlledAccount
48
- ? this.controlledAccount.node
49
- : this.controlledAccount._raw.core.node;
50
- const raw = await node.load(
51
- this.id as unknown as CoID<RawCoValue>,
52
- );
53
- if (raw === "unavailable") {
54
- return "unavailable";
55
- } else {
56
- return new Ref(this.id, this.controlledAccount, this.schema).value!;
57
- }
45
+ private async loadHelper(): Promise<V | "unavailable"> {
46
+ const node =
47
+ "node" in this.controlledAccount
48
+ ? this.controlledAccount.node
49
+ : this.controlledAccount._raw.core.node;
50
+ const raw = await node.load(this.id as unknown as CoID<RawCoValue>);
51
+ if (raw === "unavailable") {
52
+ return "unavailable";
53
+ } else {
54
+ return new Ref(this.id, this.controlledAccount, this.schema).value!;
58
55
  }
56
+ }
59
57
 
60
- async load(): Promise<V | undefined> {
61
- const result = await this.loadHelper();
62
- if (result === "unavailable") {
63
- return undefined;
64
- } else {
65
- return result;
66
- }
58
+ async load(): Promise<V | undefined> {
59
+ const result = await this.loadHelper();
60
+ if (result === "unavailable") {
61
+ return undefined;
62
+ } else {
63
+ return result;
67
64
  }
65
+ }
68
66
 
69
- accessFrom(
70
- fromScopeValue: CoValue,
71
- key: string | number | symbol,
72
- ): V | null {
73
- const subScope = subscriptionsScopes.get(fromScopeValue);
67
+ accessFrom(fromScopeValue: CoValue, key: string | number | symbol): V | null {
68
+ const subScope = subscriptionsScopes.get(fromScopeValue);
74
69
 
75
- subScope?.onRefAccessedOrSet(fromScopeValue.id, this.id);
76
- TRACE_ACCESSES &&
77
- console.log(
78
- subScope?.scopeID,
79
- "accessing",
80
- fromScopeValue,
81
- key,
82
- this.id,
83
- );
70
+ subScope?.onRefAccessedOrSet(fromScopeValue.id, this.id);
71
+ TRACE_ACCESSES &&
72
+ console.log(subScope?.scopeID, "accessing", fromScopeValue, key, this.id);
84
73
 
85
- if (this.value && subScope) {
86
- subscriptionsScopes.set(this.value, subScope);
87
- }
74
+ if (this.value && subScope) {
75
+ subscriptionsScopes.set(this.value, subScope);
76
+ }
88
77
 
89
- if (subScope) {
90
- const cached = subScope.cachedValues[this.id];
91
- if (cached) {
92
- TRACE_ACCESSES && console.log("cached", cached);
93
- return cached as V;
94
- } else if (this.value !== null) {
95
- const freshValueInstance = instantiateRefEncoded(
96
- this.schema,
97
- this.value?._raw,
98
- );
99
- TRACE_ACCESSES &&
100
- console.log("freshValueInstance", freshValueInstance);
101
- subScope.cachedValues[this.id] = freshValueInstance;
102
- subscriptionsScopes.set(freshValueInstance, subScope);
103
- return freshValueInstance as V;
104
- } else {
105
- return null;
106
- }
107
- } else {
108
- return this.value;
109
- }
78
+ if (subScope) {
79
+ const cached = subScope.cachedValues[this.id];
80
+ if (cached) {
81
+ TRACE_ACCESSES && console.log("cached", cached);
82
+ return cached as V;
83
+ } else if (this.value !== null) {
84
+ const freshValueInstance = instantiateRefEncoded(
85
+ this.schema,
86
+ this.value?._raw,
87
+ );
88
+ TRACE_ACCESSES && console.log("freshValueInstance", freshValueInstance);
89
+ subScope.cachedValues[this.id] = freshValueInstance;
90
+ subscriptionsScopes.set(freshValueInstance, subScope);
91
+ return freshValueInstance as V;
92
+ } else {
93
+ return null;
94
+ }
95
+ } else {
96
+ return this.value;
110
97
  }
98
+ }
111
99
  }
112
100
 
113
101
  export function makeRefs<Keys extends string | number>(
114
- getIdForKey: (key: Keys) => ID<CoValue> | undefined,
115
- getKeysWithIds: () => Keys[],
116
- controlledAccount: Account | AnonymousJazzAgent,
117
- refSchemaForKey: (key: Keys) => RefEncoded<CoValue>,
102
+ getIdForKey: (key: Keys) => ID<CoValue> | undefined,
103
+ getKeysWithIds: () => Keys[],
104
+ controlledAccount: Account | AnonymousJazzAgent,
105
+ refSchemaForKey: (key: Keys) => RefEncoded<CoValue>,
118
106
  ): { [K in Keys]: Ref<CoValue> } & {
107
+ [Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
108
+ length: number;
109
+ } {
110
+ const refs = {} as { [K in Keys]: Ref<CoValue> } & {
119
111
  [Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
120
112
  length: number;
121
- } {
122
- const refs = {} as { [K in Keys]: Ref<CoValue> } & {
123
- [Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
124
- length: number;
125
- };
126
- return new Proxy(refs, {
127
- get(_target, key) {
128
- if (key === Symbol.iterator) {
129
- return function* () {
130
- for (const key of getKeysWithIds()) {
131
- yield new Ref(
132
- getIdForKey(key)!,
133
- controlledAccount,
134
- refSchemaForKey(key),
135
- );
136
- }
137
- };
138
- }
139
- if (typeof key === "symbol") return undefined;
140
- if (key === "length") {
141
- return getKeysWithIds().length;
142
- }
143
- const id = getIdForKey(key as Keys);
144
- if (!id) return undefined;
145
- return new Ref(
146
- id as ID<CoValue>,
147
- controlledAccount,
148
- refSchemaForKey(key as Keys),
113
+ };
114
+ return new Proxy(refs, {
115
+ get(_target, key) {
116
+ if (key === Symbol.iterator) {
117
+ return function* () {
118
+ for (const key of getKeysWithIds()) {
119
+ yield new Ref(
120
+ getIdForKey(key)!,
121
+ controlledAccount,
122
+ refSchemaForKey(key),
149
123
  );
150
- },
151
- ownKeys() {
152
- return getKeysWithIds().map((key) => key.toString());
153
- },
154
- getOwnPropertyDescriptor(target, key) {
155
- const id = getIdForKey(key as Keys);
156
- if (id) {
157
- return {
158
- enumerable: true,
159
- configurable: true,
160
- writable: true,
161
- };
162
- } else {
163
- return Reflect.getOwnPropertyDescriptor(target, key);
164
- }
165
- },
166
- });
124
+ }
125
+ };
126
+ }
127
+ if (typeof key === "symbol") return undefined;
128
+ if (key === "length") {
129
+ return getKeysWithIds().length;
130
+ }
131
+ const id = getIdForKey(key as Keys);
132
+ if (!id) return undefined;
133
+ return new Ref(
134
+ id as ID<CoValue>,
135
+ controlledAccount,
136
+ refSchemaForKey(key as Keys),
137
+ );
138
+ },
139
+ ownKeys() {
140
+ return getKeysWithIds().map((key) => key.toString());
141
+ },
142
+ getOwnPropertyDescriptor(target, key) {
143
+ const id = getIdForKey(key as Keys);
144
+ if (id) {
145
+ return {
146
+ enumerable: true,
147
+ configurable: true,
148
+ writable: true,
149
+ };
150
+ } else {
151
+ return Reflect.getOwnPropertyDescriptor(target, key);
152
+ }
153
+ },
154
+ });
167
155
  }
168
156
 
169
157
  export type RefIfCoValue<V> = NonNullable<V> extends CoValue
170
- ? Ref<UnCo<NonNullable<V>>>
171
- : never;
158
+ ? Ref<UnCo<NonNullable<V>>>
159
+ : never;
@@ -1,182 +1,178 @@
1
1
  import type { JsonValue, RawCoValue } from "cojson";
2
+ import { CoJsonValue } from "cojson/src/jsonValue.js";
2
3
  import {
3
- type CoValue,
4
- type CoValueClass,
5
- isCoValueClass,
6
- CoValueFromRaw,
7
- SchemaInit,
8
- ItemsSym,
9
- MembersSym,
4
+ type CoValue,
5
+ type CoValueClass,
6
+ CoValueFromRaw,
7
+ ItemsSym,
8
+ MembersSym,
9
+ SchemaInit,
10
+ isCoValueClass,
10
11
  } from "../internal.js";
11
- import { CoJsonValue } from "cojson/src/jsonValue.js";
12
12
 
13
13
  /** @category Schema definition */
14
14
  export const Encoders = {
15
- Date: {
16
- encode: (value: Date) => value.toISOString(),
17
- decode: (value: JsonValue) => new Date(value as string),
18
- },
15
+ Date: {
16
+ encode: (value: Date) => value.toISOString(),
17
+ decode: (value: JsonValue) => new Date(value as string),
18
+ },
19
19
  };
20
20
 
21
21
  export type CoMarker = { readonly __co: unique symbol };
22
22
  /** @category Schema definition */
23
23
  export type co<T> = T | (T & CoMarker);
24
24
  export type IfCo<C, R> = C extends infer _A | infer B
25
- ? B extends CoMarker
26
- ? R
27
- : never
28
- : never;
25
+ ? B extends CoMarker
26
+ ? R
27
+ : never
28
+ : never;
29
29
  export type UnCo<T> = T extends co<infer A> ? A : T;
30
30
 
31
31
  const optional = {
32
- ref: optionalRef,
33
- json<T extends CoJsonValue<T>>(): co<T | undefined> {
34
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
- return { [SchemaInit]: "json" satisfies Schema } as any;
36
- },
37
- encoded<T>(arg: OptionalEncoder<T>): co<T | undefined> {
38
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
- return { [SchemaInit]: { encoded: arg } satisfies Schema } as any;
40
- },
41
- string: {
42
- [SchemaInit]: "json" satisfies Schema,
43
- } as unknown as co<string | undefined>,
44
- number: {
45
- [SchemaInit]: "json" satisfies Schema,
46
- } as unknown as co<number | undefined>,
47
- boolean: {
48
- [SchemaInit]: "json" satisfies Schema,
49
- } as unknown as co<boolean | undefined>,
50
- null: {
51
- [SchemaInit]: "json" satisfies Schema,
52
- } as unknown as co<null | undefined>,
53
- Date: {
54
- [SchemaInit]: { encoded: Encoders.Date } satisfies Schema,
55
- } as unknown as co<Date | undefined>,
56
- literal<T extends (string | number | boolean)[]>(
57
- ..._lit: T
58
- ): co<T[number] | undefined> {
59
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
- return { [SchemaInit]: "json" satisfies Schema } as any;
61
- },
32
+ ref: optionalRef,
33
+ json<T extends CoJsonValue<T>>(): co<T | undefined> {
34
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
+ return { [SchemaInit]: "json" satisfies Schema } as any;
36
+ },
37
+ encoded<T>(arg: OptionalEncoder<T>): co<T | undefined> {
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ return { [SchemaInit]: { encoded: arg } satisfies Schema } as any;
40
+ },
41
+ string: {
42
+ [SchemaInit]: "json" satisfies Schema,
43
+ } as unknown as co<string | undefined>,
44
+ number: {
45
+ [SchemaInit]: "json" satisfies Schema,
46
+ } as unknown as co<number | undefined>,
47
+ boolean: {
48
+ [SchemaInit]: "json" satisfies Schema,
49
+ } as unknown as co<boolean | undefined>,
50
+ null: {
51
+ [SchemaInit]: "json" satisfies Schema,
52
+ } as unknown as co<null | undefined>,
53
+ Date: {
54
+ [SchemaInit]: { encoded: Encoders.Date } satisfies Schema,
55
+ } as unknown as co<Date | undefined>,
56
+ literal<T extends (string | number | boolean)[]>(
57
+ ..._lit: T
58
+ ): co<T[number] | undefined> {
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ return { [SchemaInit]: "json" satisfies Schema } as any;
61
+ },
62
62
  };
63
63
 
64
64
  /** @category Schema definition */
65
65
  export const co = {
66
- string: {
67
- [SchemaInit]: "json" satisfies Schema,
68
- } as unknown as co<string>,
69
- number: {
70
- [SchemaInit]: "json" satisfies Schema,
71
- } as unknown as co<number>,
72
- boolean: {
73
- [SchemaInit]: "json" satisfies Schema,
74
- } as unknown as co<boolean>,
75
- null: {
76
- [SchemaInit]: "json" satisfies Schema,
77
- } as unknown as co<null>,
78
- Date: {
79
- [SchemaInit]: { encoded: Encoders.Date } satisfies Schema,
80
- } as unknown as co<Date>,
81
- literal<T extends (string | number | boolean)[]>(
82
- ..._lit: T
83
- ): co<T[number]> {
84
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
- return { [SchemaInit]: "json" satisfies Schema } as any;
86
- },
87
- json<T extends CoJsonValue<T>>(): co<T> {
88
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
89
- return { [SchemaInit]: "json" satisfies Schema } as any;
90
- },
91
- encoded<T>(arg: Encoder<T>): co<T> {
92
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
- return { [SchemaInit]: { encoded: arg } satisfies Schema } as any;
94
- },
95
- ref,
96
- items: ItemsSym as ItemsSym,
97
- members: MembersSym as MembersSym,
98
- optional,
66
+ string: {
67
+ [SchemaInit]: "json" satisfies Schema,
68
+ } as unknown as co<string>,
69
+ number: {
70
+ [SchemaInit]: "json" satisfies Schema,
71
+ } as unknown as co<number>,
72
+ boolean: {
73
+ [SchemaInit]: "json" satisfies Schema,
74
+ } as unknown as co<boolean>,
75
+ null: {
76
+ [SchemaInit]: "json" satisfies Schema,
77
+ } as unknown as co<null>,
78
+ Date: {
79
+ [SchemaInit]: { encoded: Encoders.Date } satisfies Schema,
80
+ } as unknown as co<Date>,
81
+ literal<T extends (string | number | boolean)[]>(..._lit: T): co<T[number]> {
82
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
+ return { [SchemaInit]: "json" satisfies Schema } as any;
84
+ },
85
+ json<T extends CoJsonValue<T>>(): co<T> {
86
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
87
+ return { [SchemaInit]: "json" satisfies Schema } as any;
88
+ },
89
+ encoded<T>(arg: Encoder<T>): co<T> {
90
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
+ return { [SchemaInit]: { encoded: arg } satisfies Schema } as any;
92
+ },
93
+ ref,
94
+ items: ItemsSym as ItemsSym,
95
+ members: MembersSym as MembersSym,
96
+ optional,
99
97
  };
100
98
 
101
99
  function optionalRef<C extends CoValueClass>(
102
- arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
100
+ arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
103
101
  ): co<InstanceType<C> | null | undefined> {
104
- return ref(arg, { optional: true });
102
+ return ref(arg, { optional: true });
105
103
  }
106
104
 
107
105
  function ref<C extends CoValueClass>(
108
- arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
109
- options?: never,
106
+ arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
107
+ options?: never,
110
108
  ): co<InstanceType<C> | null>;
111
109
  function ref<C extends CoValueClass>(
112
- arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
113
- options: { optional: true },
110
+ arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
111
+ options: { optional: true },
114
112
  ): co<InstanceType<C> | null | undefined>;
115
113
  function ref<
116
- C extends CoValueClass,
117
- Options extends { optional?: boolean } | undefined,
114
+ C extends CoValueClass,
115
+ Options extends { optional?: boolean } | undefined,
118
116
  >(
119
- arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
120
- options?: Options,
117
+ arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
118
+ options?: Options,
121
119
  ): Options extends { optional: true }
122
- ? co<InstanceType<C> | null | undefined>
123
- : co<InstanceType<C> | null> {
124
- return {
125
- [SchemaInit]: {
126
- ref: arg,
127
- optional: options?.optional || false,
128
- } satisfies Schema,
129
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
130
- } as any;
120
+ ? co<InstanceType<C> | null | undefined>
121
+ : co<InstanceType<C> | null> {
122
+ return {
123
+ [SchemaInit]: {
124
+ ref: arg,
125
+ optional: options?.optional || false,
126
+ } satisfies Schema,
127
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
128
+ } as any;
131
129
  }
132
130
 
133
131
  export type JsonEncoded = "json";
134
132
  export type EncodedAs<V> = { encoded: Encoder<V> | OptionalEncoder<V> };
135
133
  export type RefEncoded<V extends CoValue> = {
136
- ref: CoValueClass<V> | ((raw: RawCoValue) => CoValueClass<V>);
137
- optional: boolean;
134
+ ref: CoValueClass<V> | ((raw: RawCoValue) => CoValueClass<V>);
135
+ optional: boolean;
138
136
  };
139
137
 
140
138
  export function isRefEncoded<V extends CoValue>(
141
- schema: Schema,
139
+ schema: Schema,
142
140
  ): schema is RefEncoded<V> {
143
- return (
144
- typeof schema === "object" &&
145
- "ref" in schema &&
146
- "optional" in schema &&
147
- typeof schema.ref === "function"
148
- );
141
+ return (
142
+ typeof schema === "object" &&
143
+ "ref" in schema &&
144
+ "optional" in schema &&
145
+ typeof schema.ref === "function"
146
+ );
149
147
  }
150
148
 
151
149
  export function instantiateRefEncoded<V extends CoValue>(
152
- schema: RefEncoded<V>,
153
- raw: RawCoValue,
150
+ schema: RefEncoded<V>,
151
+ raw: RawCoValue,
154
152
  ): V {
155
- return isCoValueClass<V>(schema.ref)
156
- ? schema.ref.fromRaw(raw)
157
- : (
158
- schema.ref as (
159
- raw: RawCoValue,
160
- ) => CoValueClass<V> & CoValueFromRaw<V>
161
- )(raw).fromRaw(raw);
153
+ return isCoValueClass<V>(schema.ref)
154
+ ? schema.ref.fromRaw(raw)
155
+ : (schema.ref as (raw: RawCoValue) => CoValueClass<V> & CoValueFromRaw<V>)(
156
+ raw,
157
+ ).fromRaw(raw);
162
158
  }
163
159
 
164
160
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
165
161
  export type Schema = JsonEncoded | RefEncoded<CoValue> | EncodedAs<any>;
166
162
 
167
163
  export type SchemaFor<Field> = NonNullable<Field> extends CoValue
168
- ? RefEncoded<NonNullable<Field>>
169
- : NonNullable<Field> extends JsonValue
170
- ? JsonEncoded
171
- : EncodedAs<NonNullable<Field>>;
164
+ ? RefEncoded<NonNullable<Field>>
165
+ : NonNullable<Field> extends JsonValue
166
+ ? JsonEncoded
167
+ : EncodedAs<NonNullable<Field>>;
172
168
 
173
169
  export type Encoder<V> = {
174
- encode: (value: V) => JsonValue;
175
- decode: (value: JsonValue) => V;
176
- }
170
+ encode: (value: V) => JsonValue;
171
+ decode: (value: JsonValue) => V;
172
+ };
177
173
  export type OptionalEncoder<V> =
178
- | Encoder<V>
179
- | {
180
- encode: (value: V | undefined) => JsonValue;
181
- decode: (value: JsonValue) => V | undefined;
182
- };
174
+ | Encoder<V>
175
+ | {
176
+ encode: (value: V | undefined) => JsonValue;
177
+ decode: (value: JsonValue) => V | undefined;
178
+ };