jazz-tools 0.13.30 → 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.
- package/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +23 -3
- package/dist/auth/DemoAuth.d.ts.map +1 -1
- package/dist/auth/PassphraseAuth.d.ts +1 -3
- package/dist/auth/PassphraseAuth.d.ts.map +1 -1
- package/dist/{chunk-LMV6J7GN.js → chunk-2ASOGEYA.js} +3531 -3269
- package/dist/chunk-2ASOGEYA.js.map +1 -0
- package/dist/coValues/CoValueBase.d.ts +22 -0
- package/dist/coValues/CoValueBase.d.ts.map +1 -0
- package/dist/coValues/account.d.ts +12 -12
- package/dist/coValues/account.d.ts.map +1 -1
- package/dist/coValues/coFeed.d.ts +24 -25
- package/dist/coValues/coFeed.d.ts.map +1 -1
- package/dist/coValues/coList.d.ts +10 -13
- package/dist/coValues/coList.d.ts.map +1 -1
- package/dist/coValues/coMap.d.ts +32 -35
- package/dist/coValues/coMap.d.ts.map +1 -1
- package/dist/coValues/coPlainText.d.ts.map +1 -1
- package/dist/coValues/deepLoading.d.ts +28 -22
- package/dist/coValues/deepLoading.d.ts.map +1 -1
- package/dist/coValues/extensions/imageDef.d.ts +12 -11
- package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
- package/dist/coValues/group.d.ts +5 -9
- package/dist/coValues/group.d.ts.map +1 -1
- package/dist/coValues/inbox.d.ts +2 -3
- package/dist/coValues/inbox.d.ts.map +1 -1
- package/dist/coValues/interfaces.d.ts +8 -34
- package/dist/coValues/interfaces.d.ts.map +1 -1
- package/dist/coValues/profile.d.ts +4 -14
- package/dist/coValues/profile.d.ts.map +1 -1
- package/dist/coValues/registeredSchemas.d.ts +1 -3
- package/dist/coValues/registeredSchemas.d.ts.map +1 -1
- package/dist/coValues/schemaUnion.d.ts +6 -6
- package/dist/exports.d.ts +12 -16
- package/dist/exports.d.ts.map +1 -1
- package/dist/implementation/ContextManager.d.ts +1 -1
- package/dist/implementation/ContextManager.d.ts.map +1 -1
- package/dist/implementation/activeAccountContext.d.ts +1 -1
- package/dist/implementation/activeAccountContext.d.ts.map +1 -1
- package/dist/implementation/createContext.d.ts +10 -10
- package/dist/implementation/createContext.d.ts.map +1 -1
- package/dist/implementation/invites.d.ts +6 -6
- package/dist/implementation/invites.d.ts.map +1 -1
- package/dist/implementation/refs.d.ts +2 -2
- package/dist/implementation/refs.d.ts.map +1 -1
- package/dist/implementation/schema.d.ts +21 -28
- package/dist/implementation/schema.d.ts.map +1 -1
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +9 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +28 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +65 -0
- package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +28 -0
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -0
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +41 -0
- package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +35 -0
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +9 -0
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +20 -0
- package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +18 -0
- package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +24 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +21 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +29 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +29 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -0
- package/dist/implementation/zodSchema/unionUtils.d.ts +6 -0
- package/dist/implementation/zodSchema/unionUtils.d.ts.map +1 -0
- package/dist/implementation/zodSchema/zodCo.d.ts +35 -0
- package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -0
- package/dist/implementation/zodSchema/zodSchema.d.ts +38 -0
- package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -0
- package/dist/index.js +295 -10
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +34 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/subscribe/SubscriptionScope.d.ts +2 -2
- package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/subscribe/utils.d.ts +2 -2
- package/dist/subscribe/utils.d.ts.map +1 -1
- package/dist/testing.d.ts +10 -8
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/dist/tests/utils.d.ts +6 -2
- package/dist/tests/utils.d.ts.map +1 -1
- package/dist/types.d.ts +1 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/auth/DemoAuth.ts +1 -2
- package/src/auth/PassphraseAuth.ts +1 -1
- package/src/coValues/CoValueBase.ts +83 -0
- package/src/coValues/account.ts +53 -43
- package/src/coValues/coFeed.ts +65 -83
- package/src/coValues/coList.ts +28 -21
- package/src/coValues/coMap.ts +54 -38
- package/src/coValues/coPlainText.ts +4 -1
- package/src/coValues/deepLoading.ts +46 -36
- package/src/coValues/extensions/imageDef.ts +21 -19
- package/src/coValues/group.ts +37 -38
- package/src/coValues/inbox.ts +24 -11
- package/src/coValues/interfaces.ts +29 -93
- package/src/coValues/profile.ts +12 -13
- package/src/coValues/registeredSchemas.ts +1 -3
- package/src/coValues/schemaUnion.ts +6 -6
- package/src/exports.ts +47 -25
- package/src/implementation/activeAccountContext.ts +1 -1
- package/src/implementation/createContext.ts +39 -24
- package/src/implementation/invites.ts +15 -12
- package/src/implementation/refs.ts +6 -4
- package/src/implementation/schema.ts +22 -34
- package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +101 -0
- package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +191 -0
- package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +102 -0
- package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +70 -0
- package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +59 -0
- package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +126 -0
- package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +98 -0
- package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +9 -0
- package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +27 -0
- package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +25 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +61 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +77 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +90 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +103 -0
- package/src/implementation/zodSchema/unionUtils.ts +139 -0
- package/src/implementation/zodSchema/zodCo.ts +409 -0
- package/src/implementation/zodSchema/zodSchema.ts +116 -0
- package/src/internal.ts +38 -0
- package/src/subscribe/SubscriptionScope.ts +30 -3
- package/src/subscribe/utils.ts +7 -2
- package/src/testing.ts +14 -16
- package/src/tests/ContextManager.test.ts +73 -47
- package/src/tests/DemoAuth.test.ts +1 -1
- package/src/tests/account.test.ts +6 -9
- package/src/tests/coFeed.test.ts +102 -63
- package/src/tests/coList.test.ts +82 -95
- package/src/tests/coMap.record.test.ts +53 -87
- package/src/tests/coMap.test.ts +297 -312
- package/src/tests/coPlainText.test.ts +19 -39
- package/src/tests/createContext.test.ts +33 -15
- package/src/tests/deepLoading.test.ts +397 -131
- package/src/tests/groupsAndAccounts.test.ts +81 -72
- package/src/tests/imageDef.test.ts +22 -13
- package/src/tests/inbox.test.ts +36 -29
- package/src/tests/load.test.ts +10 -10
- package/src/tests/patterns/requestToJoin.test.ts +31 -31
- package/src/tests/schema.test.ts +37 -38
- package/src/tests/schemaUnion.test.ts +54 -64
- package/src/tests/subscribe.test.ts +118 -116
- package/src/tests/testing.test.ts +33 -33
- package/src/tests/utils.ts +3 -2
- package/src/types.ts +1 -8
- package/dist/chunk-LMV6J7GN.js.map +0 -1
@@ -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(
|
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
|
2
|
+
import { ItemsSym } from "../internal.js";
|
3
3
|
import { type Account } from "./account.js";
|
4
4
|
import { CoFeedEntry } from "./coFeed.js";
|
5
|
-
import { type CoKeys } from "./coMap.js";
|
5
|
+
import { type CoKeys, type CoMap } from "./coMap.js";
|
6
6
|
import { type CoValue, type ID } from "./interfaces.js";
|
7
7
|
|
8
|
-
type
|
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,31 +21,33 @@ export type RefsToResolve<
|
|
22
21
|
?
|
23
22
|
| {
|
24
23
|
$each: RefsToResolve<
|
25
|
-
|
24
|
+
NotNull<Item>,
|
26
25
|
DepthLimit,
|
27
26
|
[0, ...CurrentDepth]
|
28
27
|
>;
|
28
|
+
$onError?: null;
|
29
29
|
}
|
30
30
|
| boolean
|
31
31
|
: // Basically V extends CoMap | Group | Account - but if we used that we'd introduce circularity into the definition of CoMap itself
|
32
32
|
V extends { _type: "CoMap" | "Group" | "Account" }
|
33
33
|
?
|
34
|
-
| {
|
35
|
-
[Key in CoKeys<V> as
|
34
|
+
| ({
|
35
|
+
[Key in CoKeys<V> as NonNullable<V[Key]> extends CoValue
|
36
36
|
? Key
|
37
37
|
: never]?: RefsToResolve<
|
38
|
-
|
38
|
+
NonNullable<V[Key]>,
|
39
39
|
DepthLimit,
|
40
40
|
[0, ...CurrentDepth]
|
41
41
|
>;
|
42
|
-
}
|
42
|
+
} & { $onError?: null })
|
43
43
|
| (ItemsSym extends keyof V
|
44
44
|
? {
|
45
45
|
$each: RefsToResolve<
|
46
|
-
|
46
|
+
NonNullable<V[ItemsSym]>,
|
47
47
|
DepthLimit,
|
48
48
|
[0, ...CurrentDepth]
|
49
49
|
>;
|
50
|
+
$onError?: null;
|
50
51
|
}
|
51
52
|
: never)
|
52
53
|
| boolean
|
@@ -57,10 +58,11 @@ export type RefsToResolve<
|
|
57
58
|
?
|
58
59
|
| {
|
59
60
|
$each: RefsToResolve<
|
60
|
-
|
61
|
+
NotNull<Item>,
|
61
62
|
DepthLimit,
|
62
63
|
[0, ...CurrentDepth]
|
63
64
|
>;
|
65
|
+
$onError?: null;
|
64
66
|
}
|
65
67
|
| boolean
|
66
68
|
: boolean);
|
@@ -69,12 +71,14 @@ export type RefsToResolveStrict<T, V> = V extends RefsToResolve<T>
|
|
69
71
|
? RefsToResolve<T>
|
70
72
|
: V;
|
71
73
|
|
72
|
-
export type Resolved<
|
74
|
+
export type Resolved<
|
73
75
|
T,
|
74
|
-
R,
|
75
|
-
|
76
|
-
|
77
|
-
|
76
|
+
R extends RefsToResolve<T> | undefined = true,
|
77
|
+
> = DeeplyLoaded<T, R, 10, []>;
|
78
|
+
|
79
|
+
type onErrorNullEnabled<Depth> = Depth extends { $onError: null }
|
80
|
+
? null
|
81
|
+
: never;
|
78
82
|
|
79
83
|
export type DeeplyLoaded<
|
80
84
|
V,
|
@@ -87,16 +91,19 @@ export type DeeplyLoaded<
|
|
87
91
|
? V
|
88
92
|
: // Basically V extends CoList - but if we used that we'd introduce circularity into the definition of CoList itself
|
89
93
|
[V] extends [Array<infer Item>]
|
90
|
-
?
|
94
|
+
? NotNull<Item> extends CoValue
|
91
95
|
? Depth extends { $each: infer ItemDepth }
|
92
96
|
? // Deeply loaded CoList
|
93
|
-
(
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
97
|
+
(
|
98
|
+
| (NotNull<Item> &
|
99
|
+
DeeplyLoaded<
|
100
|
+
NotNull<Item>,
|
101
|
+
ItemDepth,
|
102
|
+
DepthLimit,
|
103
|
+
[0, ...CurrentDepth]
|
104
|
+
>)
|
105
|
+
| onErrorNullEnabled<Depth["$each"]>
|
106
|
+
)[] &
|
100
107
|
V // the CoList base type needs to be intersected after so that built-in methods return the correct narrowed array type
|
101
108
|
: never
|
102
109
|
: V
|
@@ -106,12 +113,14 @@ export type DeeplyLoaded<
|
|
106
113
|
? Depth extends { $each: infer ItemDepth }
|
107
114
|
? // Deeply loaded Record-like CoMap
|
108
115
|
{
|
109
|
-
[key: string]:
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
116
|
+
[key: string]:
|
117
|
+
| DeeplyLoaded<
|
118
|
+
NonNullable<V[ItemsSym]>,
|
119
|
+
ItemDepth,
|
120
|
+
DepthLimit,
|
121
|
+
[0, ...CurrentDepth]
|
122
|
+
>
|
123
|
+
| onErrorNullEnabled<Depth["$each"]>;
|
115
124
|
} & V // same reason as in CoList
|
116
125
|
: never
|
117
126
|
: keyof Depth extends never // Depth = {}
|
@@ -119,15 +128,16 @@ export type DeeplyLoaded<
|
|
119
128
|
: // Deeply loaded CoMap
|
120
129
|
{
|
121
130
|
-readonly [Key in keyof Depth]-?: Key extends CoKeys<V>
|
122
|
-
?
|
131
|
+
? NonNullable<V[Key]> extends CoValue
|
123
132
|
?
|
124
133
|
| DeeplyLoaded<
|
125
|
-
|
134
|
+
NonNullable<V[Key]>,
|
126
135
|
Depth[Key],
|
127
136
|
DepthLimit,
|
128
137
|
[0, ...CurrentDepth]
|
129
138
|
>
|
130
139
|
| (undefined extends V[Key] ? undefined : never)
|
140
|
+
| onErrorNullEnabled<Depth[Key]>
|
131
141
|
: never
|
132
142
|
: never;
|
133
143
|
} & V // same reason as in CoList
|
@@ -139,12 +149,12 @@ export type DeeplyLoaded<
|
|
139
149
|
]
|
140
150
|
? // Deeply loaded CoStream
|
141
151
|
{
|
142
|
-
byMe?: { value:
|
143
|
-
inCurrentSession?: { value:
|
152
|
+
byMe?: { value: NotNull<Item> };
|
153
|
+
inCurrentSession?: { value: NotNull<Item> };
|
144
154
|
perSession: {
|
145
|
-
[key: SessionID]: { value:
|
155
|
+
[key: SessionID]: { value: NotNull<Item> };
|
146
156
|
};
|
147
|
-
} & { [key: ID<Account>]: { value:
|
157
|
+
} & { [key: ID<Account>]: { value: NotNull<Item> } } & V // same reason as in CoList
|
148
158
|
: [V] extends [
|
149
159
|
{
|
150
160
|
_type: "BinaryCoStream";
|
@@ -1,20 +1,22 @@
|
|
1
|
-
import
|
2
|
-
import {
|
3
|
-
|
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
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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 (
|
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:
|
55
|
+
stream: imageDef[highestAvailableResolution]!,
|
54
56
|
}
|
55
57
|
);
|
56
|
-
}
|
57
|
-
}
|
58
|
+
},
|
59
|
+
}));
|
package/src/coValues/group.ts
CHANGED
@@ -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
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
root:
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
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:
|
159
|
+
id: string;
|
158
160
|
role: AccountRole;
|
159
|
-
ref: Ref<
|
160
|
-
account:
|
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: () =>
|
167
|
+
ref: () => Account,
|
169
168
|
optional: false,
|
170
|
-
} satisfies RefEncoded<
|
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<
|
184
|
-
accountID
|
182
|
+
const ref = new Ref<Account>(
|
183
|
+
accountID,
|
185
184
|
this._loadedAs,
|
186
185
|
refEncodedAccountSchema,
|
187
186
|
this,
|
package/src/coValues/inbox.ts
CHANGED
@@ -1,8 +1,17 @@
|
|
1
1
|
import { CoID, InviteSecret, RawAccount, RawCoMap, SessionID } from "cojson";
|
2
2
|
import { CoStreamItem, RawCoStream } from "cojson";
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
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<
|
105
|
-
Schema:
|
113
|
+
subscribe<M extends CoValueOrZodSchema, O extends CoValue | undefined>(
|
114
|
+
Schema: M,
|
106
115
|
callback: (
|
107
|
-
message:
|
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<
|
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(
|
167
|
-
|
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
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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> =
|
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
|
-
|
363
|
-
const R extends
|
298
|
+
S extends CoValueOrZodSchema,
|
299
|
+
const R extends ResolveQuery<S>,
|
364
300
|
>(initialValue: undefined | null = undefined) {
|
365
|
-
let currentValue:
|
301
|
+
let currentValue: Loaded<S, R> | undefined | null = initialValue;
|
366
302
|
let subscriberCount = 0;
|
367
303
|
|
368
304
|
function subscribe(
|
369
|
-
cls:
|
370
|
-
id:
|
305
|
+
cls: S,
|
306
|
+
id: string,
|
371
307
|
options: {
|
372
308
|
loadAs: Account | AnonymousJazzAgent;
|
373
|
-
resolve?:
|
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
|
);
|
package/src/coValues/profile.ts
CHANGED
@@ -1,19 +1,18 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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 =
|
11
|
-
inbox =
|
12
|
-
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 "
|
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 =
|
18
|
+
* type = coField.string;
|
19
19
|
* }
|
20
20
|
*
|
21
21
|
* class ButtonWidget extends BaseWidget {
|
22
|
-
* type =
|
23
|
-
* label =
|
22
|
+
* type = coField.literal("button");
|
23
|
+
* label = coField.string;
|
24
24
|
* }
|
25
25
|
*
|
26
26
|
* class SliderWidget extends BaseWidget {
|
27
|
-
* type =
|
28
|
-
* min =
|
29
|
-
* max =
|
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) => {
|