jazz-tools 0.13.31 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/.turbo/turbo-build.log +8 -8
  2. package/CHANGELOG.md +14 -3
  3. package/dist/auth/DemoAuth.d.ts.map +1 -1
  4. package/dist/auth/PassphraseAuth.d.ts +1 -3
  5. package/dist/auth/PassphraseAuth.d.ts.map +1 -1
  6. package/dist/{chunk-IJU4XPFS.js → chunk-2ASOGEYA.js} +3535 -3291
  7. package/dist/chunk-2ASOGEYA.js.map +1 -0
  8. package/dist/coValues/CoValueBase.d.ts +22 -0
  9. package/dist/coValues/CoValueBase.d.ts.map +1 -0
  10. package/dist/coValues/account.d.ts +12 -12
  11. package/dist/coValues/account.d.ts.map +1 -1
  12. package/dist/coValues/coFeed.d.ts +24 -25
  13. package/dist/coValues/coFeed.d.ts.map +1 -1
  14. package/dist/coValues/coList.d.ts +10 -13
  15. package/dist/coValues/coList.d.ts.map +1 -1
  16. package/dist/coValues/coMap.d.ts +32 -35
  17. package/dist/coValues/coMap.d.ts.map +1 -1
  18. package/dist/coValues/coPlainText.d.ts.map +1 -1
  19. package/dist/coValues/deepLoading.d.ts +17 -21
  20. package/dist/coValues/deepLoading.d.ts.map +1 -1
  21. package/dist/coValues/extensions/imageDef.d.ts +12 -11
  22. package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
  23. package/dist/coValues/group.d.ts +5 -9
  24. package/dist/coValues/group.d.ts.map +1 -1
  25. package/dist/coValues/inbox.d.ts +2 -3
  26. package/dist/coValues/inbox.d.ts.map +1 -1
  27. package/dist/coValues/interfaces.d.ts +8 -34
  28. package/dist/coValues/interfaces.d.ts.map +1 -1
  29. package/dist/coValues/profile.d.ts +4 -14
  30. package/dist/coValues/profile.d.ts.map +1 -1
  31. package/dist/coValues/registeredSchemas.d.ts +1 -3
  32. package/dist/coValues/registeredSchemas.d.ts.map +1 -1
  33. package/dist/coValues/schemaUnion.d.ts +6 -6
  34. package/dist/exports.d.ts +12 -16
  35. package/dist/exports.d.ts.map +1 -1
  36. package/dist/implementation/ContextManager.d.ts +1 -1
  37. package/dist/implementation/ContextManager.d.ts.map +1 -1
  38. package/dist/implementation/activeAccountContext.d.ts +1 -1
  39. package/dist/implementation/activeAccountContext.d.ts.map +1 -1
  40. package/dist/implementation/createContext.d.ts +10 -10
  41. package/dist/implementation/createContext.d.ts.map +1 -1
  42. package/dist/implementation/invites.d.ts +6 -6
  43. package/dist/implementation/invites.d.ts.map +1 -1
  44. package/dist/implementation/refs.d.ts +2 -2
  45. package/dist/implementation/refs.d.ts.map +1 -1
  46. package/dist/implementation/schema.d.ts +21 -28
  47. package/dist/implementation/schema.d.ts.map +1 -1
  48. package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +9 -0
  49. package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -0
  50. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +28 -0
  51. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -0
  52. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +65 -0
  53. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -0
  54. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +28 -0
  55. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -0
  56. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -0
  57. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -0
  58. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +41 -0
  59. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -0
  60. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +35 -0
  61. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -0
  62. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +9 -0
  63. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -0
  64. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +20 -0
  65. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -0
  66. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +18 -0
  67. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -0
  68. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +24 -0
  69. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -0
  70. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +21 -0
  71. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -0
  72. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +29 -0
  73. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -0
  74. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +29 -0
  75. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -0
  76. package/dist/implementation/zodSchema/unionUtils.d.ts +6 -0
  77. package/dist/implementation/zodSchema/unionUtils.d.ts.map +1 -0
  78. package/dist/implementation/zodSchema/zodCo.d.ts +35 -0
  79. package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -0
  80. package/dist/implementation/zodSchema/zodSchema.d.ts +38 -0
  81. package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -0
  82. package/dist/index.js +295 -10
  83. package/dist/index.js.map +1 -1
  84. package/dist/internal.d.ts +34 -0
  85. package/dist/internal.d.ts.map +1 -1
  86. package/dist/subscribe/SubscriptionScope.d.ts +1 -2
  87. package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
  88. package/dist/subscribe/utils.d.ts +2 -2
  89. package/dist/subscribe/utils.d.ts.map +1 -1
  90. package/dist/testing.d.ts +10 -8
  91. package/dist/testing.d.ts.map +1 -1
  92. package/dist/testing.js +1 -1
  93. package/dist/testing.js.map +1 -1
  94. package/dist/tests/utils.d.ts +6 -2
  95. package/dist/tests/utils.d.ts.map +1 -1
  96. package/dist/types.d.ts +1 -7
  97. package/dist/types.d.ts.map +1 -1
  98. package/package.json +3 -2
  99. package/src/auth/DemoAuth.ts +1 -2
  100. package/src/auth/PassphraseAuth.ts +1 -1
  101. package/src/coValues/CoValueBase.ts +83 -0
  102. package/src/coValues/account.ts +53 -43
  103. package/src/coValues/coFeed.ts +65 -83
  104. package/src/coValues/coList.ts +28 -21
  105. package/src/coValues/coMap.ts +54 -38
  106. package/src/coValues/coPlainText.ts +4 -1
  107. package/src/coValues/deepLoading.ts +35 -43
  108. package/src/coValues/extensions/imageDef.ts +21 -19
  109. package/src/coValues/group.ts +37 -38
  110. package/src/coValues/inbox.ts +24 -11
  111. package/src/coValues/interfaces.ts +29 -93
  112. package/src/coValues/profile.ts +12 -13
  113. package/src/coValues/registeredSchemas.ts +1 -3
  114. package/src/coValues/schemaUnion.ts +6 -6
  115. package/src/exports.ts +47 -25
  116. package/src/implementation/activeAccountContext.ts +1 -1
  117. package/src/implementation/createContext.ts +39 -24
  118. package/src/implementation/invites.ts +15 -12
  119. package/src/implementation/refs.ts +6 -4
  120. package/src/implementation/schema.ts +22 -34
  121. package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +101 -0
  122. package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +191 -0
  123. package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +102 -0
  124. package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +70 -0
  125. package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +59 -0
  126. package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +126 -0
  127. package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +98 -0
  128. package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +9 -0
  129. package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +27 -0
  130. package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +25 -0
  131. package/src/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +61 -0
  132. package/src/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +77 -0
  133. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +90 -0
  134. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +103 -0
  135. package/src/implementation/zodSchema/unionUtils.ts +139 -0
  136. package/src/implementation/zodSchema/zodCo.ts +409 -0
  137. package/src/implementation/zodSchema/zodSchema.ts +116 -0
  138. package/src/internal.ts +38 -0
  139. package/src/subscribe/SubscriptionScope.ts +3 -1
  140. package/src/subscribe/utils.ts +7 -2
  141. package/src/testing.ts +14 -16
  142. package/src/tests/ContextManager.test.ts +73 -47
  143. package/src/tests/DemoAuth.test.ts +1 -1
  144. package/src/tests/account.test.ts +6 -9
  145. package/src/tests/coFeed.test.ts +102 -63
  146. package/src/tests/coList.test.ts +82 -95
  147. package/src/tests/coMap.record.test.ts +53 -87
  148. package/src/tests/coMap.test.ts +297 -312
  149. package/src/tests/coPlainText.test.ts +19 -39
  150. package/src/tests/createContext.test.ts +33 -15
  151. package/src/tests/deepLoading.test.ts +196 -179
  152. package/src/tests/groupsAndAccounts.test.ts +81 -72
  153. package/src/tests/imageDef.test.ts +22 -13
  154. package/src/tests/inbox.test.ts +36 -29
  155. package/src/tests/load.test.ts +10 -10
  156. package/src/tests/patterns/requestToJoin.test.ts +31 -31
  157. package/src/tests/schema.test.ts +37 -38
  158. package/src/tests/schemaUnion.test.ts +54 -64
  159. package/src/tests/subscribe.test.ts +118 -116
  160. package/src/tests/testing.test.ts +33 -33
  161. package/src/tests/utils.ts +3 -2
  162. package/src/types.ts +1 -8
  163. package/dist/chunk-IJU4XPFS.js.map +0 -1
  164. package/dist/tests/deepLoading.test-d.d.ts +0 -2
  165. package/dist/tests/deepLoading.test-d.d.ts.map +0 -1
  166. package/src/tests/deepLoading.test-d.ts +0 -393
@@ -14,6 +14,7 @@ import {
14
14
  Resolved,
15
15
  SubscribeListenerOptions,
16
16
  SubscribeRestArgs,
17
+ anySchemaToCoSchema,
17
18
  parseCoValueCreateOptions,
18
19
  } from "../internal.js";
19
20
  import {
@@ -46,7 +47,9 @@ export class CoPlainText extends String implements CoValue {
46
47
 
47
48
  if (agent instanceof ControlledAccount) {
48
49
  return coValuesCache.get(agent.account, () =>
49
- RegisteredSchemas["Account"].fromRaw(agent.account),
50
+ anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(
51
+ agent.account,
52
+ ),
50
53
  );
51
54
  }
52
55
 
@@ -1,12 +1,11 @@
1
1
  import { SessionID } from "cojson";
2
- import { ItemsSym, UnCo } from "../internal.js";
2
+ import { ItemsSym } from "../internal.js";
3
3
  import { type Account } from "./account.js";
4
4
  import { CoFeedEntry } from "./coFeed.js";
5
5
  import { type CoKeys, type CoMap } from "./coMap.js";
6
6
  import { type CoValue, type ID } from "./interfaces.js";
7
7
 
8
- type UnCoNotNull<T> = UnCo<Exclude<T, null>>;
9
- export type Clean<T> = UnCo<NonNullable<T>>;
8
+ type NotNull<T> = Exclude<T, null>;
10
9
 
11
10
  export type RefsToResolve<
12
11
  V,
@@ -22,7 +21,7 @@ export type RefsToResolve<
22
21
  ?
23
22
  | {
24
23
  $each: RefsToResolve<
25
- UnCoNotNull<Item>,
24
+ NotNull<Item>,
26
25
  DepthLimit,
27
26
  [0, ...CurrentDepth]
28
27
  >;
@@ -33,10 +32,10 @@ export type RefsToResolve<
33
32
  V extends { _type: "CoMap" | "Group" | "Account" }
34
33
  ?
35
34
  | ({
36
- [Key in CoKeys<V> as Clean<V[Key]> extends CoValue
35
+ [Key in CoKeys<V> as NonNullable<V[Key]> extends CoValue
37
36
  ? Key
38
37
  : never]?: RefsToResolve<
39
- Clean<V[Key]>,
38
+ NonNullable<V[Key]>,
40
39
  DepthLimit,
41
40
  [0, ...CurrentDepth]
42
41
  >;
@@ -44,7 +43,7 @@ export type RefsToResolve<
44
43
  | (ItemsSym extends keyof V
45
44
  ? {
46
45
  $each: RefsToResolve<
47
- Clean<V[ItemsSym]>,
46
+ NonNullable<V[ItemsSym]>,
48
47
  DepthLimit,
49
48
  [0, ...CurrentDepth]
50
49
  >;
@@ -59,7 +58,7 @@ export type RefsToResolve<
59
58
  ?
60
59
  | {
61
60
  $each: RefsToResolve<
62
- UnCoNotNull<Item>,
61
+ NotNull<Item>,
63
62
  DepthLimit,
64
63
  [0, ...CurrentDepth]
65
64
  >;
@@ -72,12 +71,10 @@ export type RefsToResolveStrict<T, V> = V extends RefsToResolve<T>
72
71
  ? RefsToResolve<T>
73
72
  : V;
74
73
 
75
- export type Resolved<T, R extends RefsToResolve<T> | undefined> = DeeplyLoaded<
74
+ export type Resolved<
76
75
  T,
77
- R,
78
- 10,
79
- []
80
- >;
76
+ R extends RefsToResolve<T> | undefined = true,
77
+ > = DeeplyLoaded<T, R, 10, []>;
81
78
 
82
79
  type onErrorNullEnabled<Depth> = Depth extends { $onError: null }
83
80
  ? null
@@ -94,13 +91,13 @@ export type DeeplyLoaded<
94
91
  ? V
95
92
  : // Basically V extends CoList - but if we used that we'd introduce circularity into the definition of CoList itself
96
93
  [V] extends [Array<infer Item>]
97
- ? UnCoNotNull<Item> extends CoValue
94
+ ? NotNull<Item> extends CoValue
98
95
  ? Depth extends { $each: infer ItemDepth }
99
96
  ? // Deeply loaded CoList
100
97
  (
101
- | (Clean<Item> &
98
+ | (NotNull<Item> &
102
99
  DeeplyLoaded<
103
- Clean<Item>,
100
+ NotNull<Item>,
104
101
  ItemDepth,
105
102
  DepthLimit,
106
103
  [0, ...CurrentDepth]
@@ -118,37 +115,32 @@ export type DeeplyLoaded<
118
115
  {
119
116
  [key: string]:
120
117
  | DeeplyLoaded<
121
- Clean<V[ItemsSym]>,
118
+ NonNullable<V[ItemsSym]>,
122
119
  ItemDepth,
123
120
  DepthLimit,
124
121
  [0, ...CurrentDepth]
125
122
  >
126
- | onErrorNullEnabled<ItemDepth>;
123
+ | onErrorNullEnabled<Depth["$each"]>;
127
124
  } & V // same reason as in CoList
128
125
  : never
129
126
  : keyof Depth extends never // Depth = {}
130
127
  ? V
131
- : Depth extends { $onError: null }
132
- ? V
133
- : // Deeply loaded CoMap
134
- {
135
- -readonly [Key in Exclude<
136
- keyof Depth,
137
- "$onError"
138
- >]-?: Key extends CoKeys<V>
139
- ? Clean<V[Key]> extends CoValue
140
- ?
141
- | DeeplyLoaded<
142
- Clean<V[Key]>,
143
- Depth[Key],
144
- DepthLimit,
145
- [0, ...CurrentDepth]
146
- >
147
- | (undefined extends V[Key] ? undefined : never)
148
- | onErrorNullEnabled<Depth[Key]>
149
- : never
150
- : never;
151
- } & V // same reason as in CoList
128
+ : // Deeply loaded CoMap
129
+ {
130
+ -readonly [Key in keyof Depth]-?: Key extends CoKeys<V>
131
+ ? NonNullable<V[Key]> extends CoValue
132
+ ?
133
+ | DeeplyLoaded<
134
+ NonNullable<V[Key]>,
135
+ Depth[Key],
136
+ DepthLimit,
137
+ [0, ...CurrentDepth]
138
+ >
139
+ | (undefined extends V[Key] ? undefined : never)
140
+ | onErrorNullEnabled<Depth[Key]>
141
+ : never
142
+ : never;
143
+ } & V // same reason as in CoList
152
144
  : [V] extends [
153
145
  {
154
146
  _type: "CoStream";
@@ -157,12 +149,12 @@ export type DeeplyLoaded<
157
149
  ]
158
150
  ? // Deeply loaded CoStream
159
151
  {
160
- byMe?: { value: UnCoNotNull<Item> };
161
- inCurrentSession?: { value: UnCoNotNull<Item> };
152
+ byMe?: { value: NotNull<Item> };
153
+ inCurrentSession?: { value: NotNull<Item> };
162
154
  perSession: {
163
- [key: SessionID]: { value: UnCoNotNull<Item> };
155
+ [key: SessionID]: { value: NotNull<Item> };
164
156
  };
165
- } & { [key: ID<Account>]: { value: UnCoNotNull<Item> } } & V // same reason as in CoList
157
+ } & { [key: ID<Account>]: { value: NotNull<Item> } } & V // same reason as in CoList
166
158
  : [V] extends [
167
159
  {
168
160
  _type: "BinaryCoStream";
@@ -1,20 +1,22 @@
1
- import { co } from "../../internal.js";
2
- import { FileStream } from "../coFeed.js";
3
- import { CoMap } from "../coMap.js";
1
+ import z from "zod/v4";
2
+ import { Loaded, coFileStreamDefiner, coMapDefiner } from "../../internal.js";
3
+
4
+ // avoiding circularity by using the standalone definers instead of `co`
5
+ const ImageDefinitionBase = coMapDefiner({
6
+ originalSize: z.tuple([z.number(), z.number()]),
7
+ placeholderDataURL: z.string().optional(),
8
+ }).catchall(coFileStreamDefiner());
4
9
 
5
10
  /** @category Media */
6
- export class ImageDefinition extends CoMap {
7
- originalSize = co.json<[number, number]>();
8
- placeholderDataURL? = co.string;
9
-
10
- [co.items] = co.ref(FileStream);
11
- [res: `${number}x${number}`]: co<FileStream | null>;
12
-
13
- highestResAvailable(options?: {
14
- maxWidth?: number;
15
- targetWidth?: number;
16
- }): { res: `${number}x${number}`; stream: FileStream } | undefined {
17
- const resolutions = Object.keys(this).filter((key) =>
11
+ export const ImageDefinition = ImageDefinitionBase.withHelpers((Self) => ({
12
+ highestResAvailable(
13
+ imageDef: Loaded<typeof Self>,
14
+ options?: {
15
+ maxWidth?: number;
16
+ targetWidth?: number;
17
+ },
18
+ ) {
19
+ const resolutions = Object.keys(imageDef).filter((key) =>
18
20
  key.match(/^\d+x\d+$/),
19
21
  ) as `${number}x${number}`[];
20
22
 
@@ -41,7 +43,7 @@ export class ImageDefinition extends CoMap {
41
43
  let highestAvailableResolution: `${number}x${number}` | undefined;
42
44
 
43
45
  for (const resolution of validResolutions) {
44
- if (this[resolution] && this[resolution]?.getChunks()) {
46
+ if (imageDef[resolution] && imageDef[resolution]?.getChunks()) {
45
47
  highestAvailableResolution = resolution;
46
48
  }
47
49
  }
@@ -50,8 +52,8 @@ export class ImageDefinition extends CoMap {
50
52
  return (
51
53
  highestAvailableResolution && {
52
54
  res: highestAvailableResolution,
53
- stream: this[highestAvailableResolution]!,
55
+ stream: imageDef[highestAvailableResolution]!,
54
56
  }
55
57
  );
56
- }
57
- }
58
+ },
59
+ }));
@@ -6,11 +6,13 @@ import type {
6
6
  RawGroup,
7
7
  Role,
8
8
  } from "cojson";
9
- import { activeAccountContext } from "../implementation/activeAccountContext.js";
10
9
  import type {
10
+ AnyAccountSchema,
11
+ CoMap,
11
12
  CoValue,
12
13
  CoValueClass,
13
14
  ID,
15
+ InstanceOfSchema,
14
16
  RefEncoded,
15
17
  RefsToResolve,
16
18
  RefsToResolveStrict,
@@ -20,23 +22,23 @@ import type {
20
22
  SubscribeRestArgs,
21
23
  } from "../internal.js";
22
24
  import {
25
+ Account,
26
+ AccountAndGroupProxyHandler,
23
27
  CoValueBase,
28
+ Profile,
24
29
  Ref,
30
+ RegisteredSchemas,
25
31
  accessChildById,
26
- co,
32
+ activeAccountContext,
33
+ anySchemaToCoSchema,
27
34
  ensureCoValueLoaded,
35
+ isControlledAccount,
28
36
  loadCoValueWithoutMe,
29
37
  parseGroupCreateOptions,
30
38
  parseSubscribeRestArgs,
31
39
  subscribeToCoValueWithoutMe,
32
40
  subscribeToExistingCoValue,
33
41
  } from "../internal.js";
34
- import { RegisteredAccount } from "../types.js";
35
- import { AccountAndGroupProxyHandler, isControlledAccount } from "./account.js";
36
- import { type Account } from "./account.js";
37
- import { type CoMap } from "./coMap.js";
38
- import { type Profile } from "./profile.js";
39
- import { RegisteredSchemas } from "./registeredSchemas.js";
40
42
 
41
43
  /** @category Identity & Permissions */
42
44
  export class Group extends CoValueBase implements CoValue {
@@ -80,26 +82,26 @@ export class Group extends CoValueBase implements CoValue {
80
82
  | ID<NonNullable<this["root"]>>
81
83
  | undefined;
82
84
  return {
83
- profile:
84
- profileID &&
85
- (new Ref(
86
- profileID,
87
- this._loadedAs,
88
- this._schema.profile as RefEncoded<NonNullable<this["profile"]>>,
89
- this,
90
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
- ) as any as this["profile"] extends Profile
92
- ? Ref<this["profile"]>
93
- : never),
94
- root:
95
- rootID &&
96
- (new Ref(
97
- rootID,
98
- this._loadedAs,
99
- this._schema.root as RefEncoded<NonNullable<this["root"]>>,
100
- this,
101
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
- ) as any as this["root"] extends CoMap ? Ref<this["root"]> : never),
85
+ profile: profileID
86
+ ? (new Ref(
87
+ profileID,
88
+ this._loadedAs,
89
+ this._schema.profile as RefEncoded<NonNullable<this["profile"]>>,
90
+ this,
91
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
+ ) as any as this["profile"] extends Profile
93
+ ? Ref<this["profile"]>
94
+ : never)
95
+ : undefined,
96
+ root: rootID
97
+ ? (new Ref(
98
+ rootID,
99
+ this._loadedAs,
100
+ this._schema.root as RefEncoded<NonNullable<this["root"]>>,
101
+ this,
102
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
+ ) as any as this["root"] extends CoMap ? Ref<this["root"]> : never)
104
+ : undefined,
103
105
  };
104
106
  }
105
107
 
@@ -154,20 +156,17 @@ export class Group extends CoValueBase implements CoValue {
154
156
  }
155
157
 
156
158
  get members(): Array<{
157
- id: ID<RegisteredAccount>;
159
+ id: string;
158
160
  role: AccountRole;
159
- ref: Ref<RegisteredAccount>;
160
- account: RegisteredAccount;
161
+ ref: Ref<Account>;
162
+ account: Account;
161
163
  }> {
162
164
  const members = [];
163
165
 
164
- const BaseAccountSchema =
165
- (activeAccountContext.maybeGet()?.constructor as typeof Account) ||
166
- RegisteredSchemas["Account"];
167
166
  const refEncodedAccountSchema = {
168
- ref: () => BaseAccountSchema,
167
+ ref: () => Account,
169
168
  optional: false,
170
- } satisfies RefEncoded<RegisteredAccount>;
169
+ } satisfies RefEncoded<Account>;
171
170
 
172
171
  for (const accountID of this._raw.getAllMemberKeysSet()) {
173
172
  if (!isAccountID(accountID)) continue;
@@ -180,8 +179,8 @@ export class Group extends CoValueBase implements CoValue {
180
179
  role === "reader" ||
181
180
  role === "writeOnly"
182
181
  ) {
183
- const ref = new Ref<RegisteredAccount>(
184
- accountID as unknown as ID<RegisteredAccount>,
182
+ const ref = new Ref<Account>(
183
+ accountID,
185
184
  this._loadedAs,
186
185
  refEncodedAccountSchema,
187
186
  this,
@@ -1,8 +1,17 @@
1
1
  import { CoID, InviteSecret, RawAccount, RawCoMap, SessionID } from "cojson";
2
2
  import { CoStreamItem, RawCoStream } from "cojson";
3
- import { activeAccountContext } from "../implementation/activeAccountContext.js";
4
- import { type Account } from "./account.js";
5
- import { CoValue, CoValueClass, ID, loadCoValue } from "./interfaces.js";
3
+ import {
4
+ type Account,
5
+ CoValue,
6
+ CoValueClass,
7
+ CoValueOrZodSchema,
8
+ ID,
9
+ InstanceOfSchema,
10
+ activeAccountContext,
11
+ anySchemaToCoSchema,
12
+ loadCoValue,
13
+ zodSchemaToCoSchema,
14
+ } from "../internal.js";
6
15
 
7
16
  export type InboxInvite = `${CoID<MessagesStream>}/${InviteSecret}`;
8
17
  type TxKey = `${SessionID}/${number}`;
@@ -101,10 +110,10 @@ export class Inbox {
101
110
  this.failed = failed;
102
111
  }
103
112
 
104
- subscribe<I extends CoValue, O extends CoValue | undefined>(
105
- Schema: CoValueClass<I>,
113
+ subscribe<M extends CoValueOrZodSchema, O extends CoValue | undefined>(
114
+ Schema: M,
106
115
  callback: (
107
- message: I,
116
+ message: InstanceOfSchema<M>,
108
117
  senderAccountID: ID<Account>,
109
118
  ) => Promise<O | undefined | void>,
110
119
  options: { retries?: number } = {},
@@ -137,7 +146,7 @@ export class Inbox {
137
146
 
138
147
  for (const [sessionID, items] of Object.entries(stream.items) as [
139
148
  SessionID,
140
- CoStreamItem<CoID<InboxMessage<I, O>>>[],
149
+ CoStreamItem<CoID<InboxMessage<InstanceOfSchema<M>, O>>>[],
141
150
  ][]) {
142
151
  const accountID = getAccountIDfromSessionID(sessionID);
143
152
 
@@ -163,9 +172,13 @@ export class Inbox {
163
172
  );
164
173
  }
165
174
 
166
- return loadCoValue(Schema, message.get("payload") as ID<I>, {
167
- loadAs: account,
168
- });
175
+ return loadCoValue(
176
+ anySchemaToCoSchema(Schema),
177
+ message.get("payload")!,
178
+ {
179
+ loadAs: account,
180
+ },
181
+ );
169
182
  })
170
183
  .then((value) => {
171
184
  if (!value) {
@@ -174,7 +187,7 @@ export class Inbox {
174
187
  );
175
188
  }
176
189
 
177
- return callback(value, accountID);
190
+ return callback(value as InstanceOfSchema<M>, accountID);
178
191
  })
179
192
  .then((result) => {
180
193
  const inboxMessage = node
@@ -1,19 +1,22 @@
1
- import type {
2
- CoValueUniqueness,
3
- CojsonInternalTypes,
4
- RawCoValue,
5
- } from "cojson";
6
- import { ControlledAccount, RawAccount } from "cojson";
7
- import { activeAccountContext } from "../implementation/activeAccountContext.js";
8
- import { AnonymousJazzAgent } from "../implementation/anonymousJazzAgent.js";
9
- import { inspect } from "../internal.js";
10
- import { coValuesCache } from "../lib/cache.js";
11
- import { SubscriptionScope } from "../subscribe/SubscriptionScope.js";
12
- import { SubscriptionValue } from "../subscribe/types.js";
13
- import { type Account } from "./account.js";
14
- import { RefsToResolve, RefsToResolveStrict, Resolved } from "./deepLoading.js";
15
- import { type Group } from "./group.js";
16
- import { RegisteredSchemas } from "./registeredSchemas.js";
1
+ import type { CoValueUniqueness, RawCoValue } from "cojson";
2
+ import {
3
+ type Account,
4
+ AnonymousJazzAgent,
5
+ CoValueOrZodSchema,
6
+ type Group,
7
+ Loaded,
8
+ RefsToResolve,
9
+ RefsToResolveStrict,
10
+ RegisteredSchemas,
11
+ ResolveQuery,
12
+ ResolveQueryStrict,
13
+ Resolved,
14
+ SubscriptionScope,
15
+ type SubscriptionValue,
16
+ activeAccountContext,
17
+ anySchemaToCoSchema,
18
+ inspect,
19
+ } from "../internal.js";
17
20
 
18
21
  /** @category Abstract interfaces */
19
22
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -76,74 +79,7 @@ export function isCoValueClass<V extends CoValue>(
76
79
  *
77
80
  * @category CoValues
78
81
  */
79
- export type ID<T> = CojsonInternalTypes.RawCoID & IDMarker<T>;
80
-
81
- type IDMarker<out T> = { __type(_: never): T };
82
-
83
- /** @internal */
84
- export class CoValueBase implements CoValue {
85
- declare id: ID<this>;
86
- declare _type: string;
87
- declare _raw: RawCoValue;
88
- /** @category Internals */
89
- declare _instanceID: string;
90
-
91
- get _owner(): Account | Group {
92
- const owner = coValuesCache.get(this._raw.group, () =>
93
- this._raw.group instanceof RawAccount
94
- ? RegisteredSchemas["Account"].fromRaw(this._raw.group)
95
- : RegisteredSchemas["Group"].fromRaw(this._raw.group),
96
- );
97
-
98
- return owner;
99
- }
100
-
101
- /** @private */
102
- get _loadedAs() {
103
- const agent = this._raw.core.node.getCurrentAgent();
104
-
105
- if (agent instanceof ControlledAccount) {
106
- return coValuesCache.get(agent.account, () =>
107
- RegisteredSchemas["Account"].fromRaw(agent.account),
108
- );
109
- }
110
-
111
- return new AnonymousJazzAgent(this._raw.core.node);
112
- }
113
-
114
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
115
- constructor(..._args: any) {
116
- Object.defineProperty(this, "_instanceID", {
117
- value: `instance-${Math.random().toString(36).slice(2)}`,
118
- enumerable: false,
119
- });
120
- }
121
-
122
- /** @category Internals */
123
- static fromRaw<V extends CoValue>(this: CoValueClass<V>, raw: RawCoValue): V {
124
- return new this({ fromRaw: raw });
125
- }
126
-
127
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
128
- toJSON(): object | any[] | string {
129
- return {
130
- id: this.id,
131
- type: this._type,
132
- error: "unknown CoValue class",
133
- };
134
- }
135
-
136
- [inspect]() {
137
- return this.toJSON();
138
- }
139
-
140
- /** @category Type Helpers */
141
- castAs<Cl extends CoValueClass & CoValueFromRaw<CoValue>>(
142
- cl: Cl,
143
- ): InstanceType<Cl> {
144
- return cl.fromRaw(this._raw) as InstanceType<Cl>;
145
- }
146
- }
82
+ export type ID<T> = string;
147
83
 
148
84
  export function loadCoValueWithoutMe<
149
85
  V extends CoValue,
@@ -359,18 +295,18 @@ export function subscribeToCoValue<
359
295
  }
360
296
 
361
297
  export function createCoValueObservable<
362
- V extends CoValue,
363
- const R extends RefsToResolve<V>,
298
+ S extends CoValueOrZodSchema,
299
+ const R extends ResolveQuery<S>,
364
300
  >(initialValue: undefined | null = undefined) {
365
- let currentValue: Resolved<V, R> | undefined | null = initialValue;
301
+ let currentValue: Loaded<S, R> | undefined | null = initialValue;
366
302
  let subscriberCount = 0;
367
303
 
368
304
  function subscribe(
369
- cls: CoValueClass<V>,
370
- id: ID<CoValue>,
305
+ cls: S,
306
+ id: string,
371
307
  options: {
372
308
  loadAs: Account | AnonymousJazzAgent;
373
- resolve?: RefsToResolveStrict<V, R>;
309
+ resolve?: ResolveQueryStrict<S, R>;
374
310
  onUnavailable?: () => void;
375
311
  onUnauthorized?: () => void;
376
312
  syncResolution?: boolean;
@@ -380,11 +316,11 @@ export function createCoValueObservable<
380
316
  subscriberCount++;
381
317
 
382
318
  const unsubscribe = subscribeToCoValue(
383
- cls,
319
+ anySchemaToCoSchema(cls),
384
320
  id,
385
321
  {
386
322
  loadAs: options.loadAs,
387
- resolve: options.resolve,
323
+ resolve: options.resolve as any,
388
324
  onUnavailable: () => {
389
325
  currentValue = null;
390
326
  options.onUnavailable?.();
@@ -396,7 +332,7 @@ export function createCoValueObservable<
396
332
  syncResolution: options.syncResolution,
397
333
  },
398
334
  (value) => {
399
- currentValue = value;
335
+ currentValue = value as Loaded<S, R>;
400
336
  listener();
401
337
  },
402
338
  );
@@ -1,19 +1,18 @@
1
- import { CoID } from "cojson";
2
- import { CoValueClass, co } from "../internal.js";
3
- import { Account } from "./account.js";
4
- import { CoMap, CoMapInit, Simplify } from "./coMap.js";
5
- import { Group } from "./group.js";
6
- import { InboxInvite, InboxRoot } from "./inbox.js";
1
+ import {
2
+ Account,
3
+ CoMap,
4
+ CoMapInit,
5
+ CoValueClass,
6
+ Group,
7
+ Simplify,
8
+ coField,
9
+ } from "../internal.js";
7
10
 
8
11
  /** @category Identity & Permissions */
9
12
  export class Profile extends CoMap {
10
- name = co.string;
11
- inbox = co.optional.json<CoID<InboxRoot>>();
12
- inboxInvite = co.optional.json<InboxInvite>();
13
-
14
- override get _owner(): Group {
15
- return super._owner as Group;
16
- }
13
+ name = coField.string;
14
+ inbox? = coField.optional.string;
15
+ inboxInvite? = coField.optional.string;
17
16
 
18
17
  /**
19
18
  * Creates a new profile with the given initial values and owner.
@@ -1,6 +1,4 @@
1
- import type { Account } from "./account.js";
2
- import type { CoMap } from "./coMap.js";
3
- import type { Group } from "./group.js";
1
+ import type { Account, CoMap, Group } from "../internal.js";
4
2
 
5
3
  /**
6
4
  * Regisering schemas into this Record to avoid circular dependencies.
@@ -15,18 +15,18 @@ import {
15
15
  * import { SchemaUnion, CoMap } from "jazz-tools";
16
16
  *
17
17
  * class BaseWidget extends CoMap {
18
- * type = co.string;
18
+ * type = coField.string;
19
19
  * }
20
20
  *
21
21
  * class ButtonWidget extends BaseWidget {
22
- * type = co.literal("button");
23
- * label = co.string;
22
+ * type = coField.literal("button");
23
+ * label = coField.string;
24
24
  * }
25
25
  *
26
26
  * class SliderWidget extends BaseWidget {
27
- * type = co.literal("slider");
28
- * min = co.number;
29
- * max = co.number;
27
+ * type = coField.literal("slider");
28
+ * min = coField.number;
29
+ * max = coField.number;
30
30
  * }
31
31
  *
32
32
  * const WidgetUnion = SchemaUnion.Of<BaseWidget>((raw) => {