jazz-tools 0.7.0-alpha.3 → 0.7.0-alpha.30
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +80 -8
- package/CHANGELOG.md +175 -0
- package/dist/coValues/account.js +78 -38
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/coList.js +150 -99
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/coMap.js +178 -162
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coStream.js +195 -70
- package/dist/coValues/coStream.js.map +1 -1
- package/dist/coValues/extensions/imageDef.js +13 -8
- package/dist/coValues/extensions/imageDef.js.map +1 -1
- package/dist/coValues/group.js +40 -36
- package/dist/coValues/group.js.map +1 -1
- package/dist/coValues/interfaces.js +22 -4
- package/dist/coValues/interfaces.js.map +1 -1
- package/dist/implementation/refs.js +26 -12
- package/dist/implementation/refs.js.map +1 -1
- package/dist/implementation/schema.js +38 -1
- package/dist/implementation/schema.js.map +1 -1
- package/dist/implementation/symbols.js +5 -0
- package/dist/implementation/symbols.js.map +1 -0
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/internal.js +1 -0
- package/dist/internal.js.map +1 -1
- package/dist/tests/coList.test.js +20 -24
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +154 -43
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +50 -55
- package/dist/tests/coStream.test.js.map +1 -1
- package/dist/tests/groupsAndAccounts.test.js +86 -0
- package/dist/tests/groupsAndAccounts.test.js.map +1 -0
- package/package.json +5 -4
- package/src/coValues/account.ts +116 -75
- package/src/coValues/coList.ts +189 -125
- package/src/coValues/coMap.ts +238 -295
- package/src/coValues/coStream.ts +282 -124
- package/src/coValues/extensions/imageDef.ts +13 -15
- package/src/coValues/group.ts +85 -85
- package/src/coValues/interfaces.ts +32 -11
- package/src/implementation/refs.ts +42 -25
- package/src/implementation/schema.ts +68 -46
- package/src/implementation/symbols.ts +12 -0
- package/src/index.ts +10 -8
- package/src/internal.ts +1 -0
- package/src/tests/coList.test.ts +20 -24
- package/src/tests/coMap.test.ts +153 -58
- package/src/tests/coStream.test.ts +65 -70
- package/src/tests/groupsAndAccounts.test.ts +96 -0
package/src/coValues/group.ts
CHANGED
@@ -1,72 +1,79 @@
|
|
1
|
-
import type {
|
1
|
+
import type { AccountID, Everyone, RawGroup, Role } from "cojson";
|
2
2
|
import type {
|
3
3
|
CoValue,
|
4
|
-
CoValueClass,
|
5
4
|
ID,
|
6
|
-
|
7
|
-
|
5
|
+
JsonEncoded,
|
6
|
+
RefEncoded,
|
7
|
+
Schema,
|
8
8
|
} from "../internal.js";
|
9
9
|
import {
|
10
10
|
Account,
|
11
11
|
CoMap,
|
12
12
|
CoValueBase,
|
13
|
-
|
13
|
+
Ref,
|
14
|
+
co,
|
14
15
|
isControlledAccount,
|
16
|
+
AccountAndGroupProxyHandler,
|
17
|
+
MembersSym,
|
15
18
|
} from "../internal.js";
|
16
19
|
|
17
|
-
export class Profile extends CoMap<{ name: string }> {
|
18
|
-
|
20
|
+
export class Profile extends CoMap<{ name: co<string> }> {
|
21
|
+
name = co.string;
|
19
22
|
}
|
20
|
-
|
23
|
+
|
24
|
+
type GroupSchema<Def extends Group> = {
|
25
|
+
profile: NonNullable<Def["profile"]> extends CoValue
|
26
|
+
? RefEncoded<NonNullable<Def["profile"]>>
|
27
|
+
: JsonEncoded;
|
28
|
+
root: NonNullable<Def["root"]> extends CoValue
|
29
|
+
? RefEncoded<NonNullable<Def["root"]>>
|
30
|
+
: JsonEncoded;
|
31
|
+
[MembersSym]: RefEncoded<NonNullable<Def[MembersSym]>>;
|
32
|
+
};
|
21
33
|
|
22
34
|
export class Group<
|
23
|
-
Def extends {
|
35
|
+
Def extends {
|
36
|
+
profile: Profile | null;
|
37
|
+
root: CoMap | null;
|
38
|
+
[MembersSym]: Account | null;
|
39
|
+
} = {
|
24
40
|
profile: Profile | null;
|
25
41
|
root: CoMap | null;
|
42
|
+
[MembersSym]: Account | null;
|
26
43
|
},
|
27
44
|
>
|
28
45
|
extends CoValueBase
|
29
46
|
implements CoValue<"Group", RawGroup>
|
30
47
|
{
|
31
|
-
id
|
32
|
-
_type
|
48
|
+
declare id: ID<this>;
|
49
|
+
declare _type: "Group";
|
33
50
|
static {
|
34
51
|
this.prototype._type = "Group";
|
35
52
|
}
|
36
|
-
_raw
|
53
|
+
declare _raw: RawGroup;
|
37
54
|
|
38
|
-
static
|
39
|
-
get
|
40
|
-
|
41
|
-
? RefField<Def["profile"]>
|
42
|
-
: PrimitiveField;
|
43
|
-
root: Def["root"] extends CoValue
|
44
|
-
? RefField<Def["root"]>
|
45
|
-
: PrimitiveField;
|
46
|
-
} {
|
47
|
-
return (this.constructor as typeof Group)._encoding;
|
55
|
+
static _schema: any;
|
56
|
+
get _schema(): GroupSchema<this> {
|
57
|
+
return (this.constructor as typeof Group)._schema;
|
48
58
|
}
|
49
59
|
static {
|
50
|
-
this.
|
51
|
-
profile:
|
52
|
-
root:
|
60
|
+
this._schema = {
|
61
|
+
profile: "json" satisfies Schema,
|
62
|
+
root: "json" satisfies Schema,
|
63
|
+
[MembersSym]: () => Account satisfies Schema,
|
53
64
|
} as any;
|
54
|
-
Object.defineProperty(this.prototype, "
|
55
|
-
get: () => this.
|
65
|
+
Object.defineProperty(this.prototype, "_schema", {
|
66
|
+
get: () => this._schema,
|
56
67
|
});
|
57
68
|
}
|
58
69
|
|
59
|
-
profile
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
root!: Def["root"] extends CoMap ? Def["root"] | null : undefined;
|
70
|
+
declare profile: Def["profile"] | null;
|
71
|
+
declare root: Def["root"] | null;
|
72
|
+
declare [MembersSym]: Def[MembersSym] | null;
|
64
73
|
|
65
74
|
get _refs(): {
|
66
|
-
profile: Def["profile"] extends Profile
|
67
|
-
|
68
|
-
: never;
|
69
|
-
root: Def["root"] extends CoMap ? ValueRef<Def["root"]> : never;
|
75
|
+
profile: Def["profile"] extends Profile ? Ref<Def["profile"]> : never;
|
76
|
+
root: Def["root"] extends CoMap ? Ref<Def["root"]> : never;
|
70
77
|
} {
|
71
78
|
const profileID = this._raw.get("profile") as unknown as
|
72
79
|
| ID<NonNullable<Def["profile"]>>
|
@@ -77,25 +84,19 @@ export class Group<
|
|
77
84
|
return {
|
78
85
|
profile:
|
79
86
|
profileID &&
|
80
|
-
(new
|
87
|
+
(new Ref(
|
81
88
|
profileID,
|
82
89
|
this._loadedAs,
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
>
|
87
|
-
).ref()
|
90
|
+
this._schema.profile as RefEncoded<
|
91
|
+
NonNullable<Def["profile"]>
|
92
|
+
>
|
88
93
|
) as any),
|
89
94
|
root:
|
90
95
|
rootID &&
|
91
|
-
(new
|
96
|
+
(new Ref(
|
92
97
|
rootID,
|
93
98
|
this._loadedAs,
|
94
|
-
|
95
|
-
this._encoding.root as RefField<
|
96
|
-
NonNullable<Def["root"]>
|
97
|
-
>
|
98
|
-
).ref()
|
99
|
+
this._schema.root as RefEncoded<NonNullable<Def["root"]>>
|
99
100
|
) as any),
|
100
101
|
};
|
101
102
|
}
|
@@ -134,35 +135,12 @@ export class Group<
|
|
134
135
|
enumerable: false,
|
135
136
|
},
|
136
137
|
_raw: { value: raw, enumerable: false },
|
137
|
-
profile: {
|
138
|
-
get: () => {
|
139
|
-
const ref = this._refs.profile;
|
140
|
-
return ref ? ref.accessFrom(this) : (undefined as any);
|
141
|
-
},
|
142
|
-
set: (value: Def["profile"] | null) => {
|
143
|
-
if (value) {
|
144
|
-
this._raw.set(
|
145
|
-
"profile",
|
146
|
-
value.id as unknown as CoID<RawCoMap>
|
147
|
-
);
|
148
|
-
}
|
149
|
-
},
|
150
|
-
},
|
151
|
-
root: {
|
152
|
-
get: () => {
|
153
|
-
const ref = this._refs.root;
|
154
|
-
return ref ? ref.accessFrom(this) : (undefined as any);
|
155
|
-
},
|
156
|
-
set: (value: Def["root"] | null) => {
|
157
|
-
if (value) {
|
158
|
-
this._raw.set(
|
159
|
-
"root",
|
160
|
-
value.id as unknown as CoID<RawCoMap>
|
161
|
-
);
|
162
|
-
}
|
163
|
-
},
|
164
|
-
},
|
165
138
|
});
|
139
|
+
|
140
|
+
return new Proxy(
|
141
|
+
this,
|
142
|
+
AccountAndGroupProxyHandler as ProxyHandler<this>
|
143
|
+
);
|
166
144
|
}
|
167
145
|
|
168
146
|
myRole(): Role | undefined {
|
@@ -171,16 +149,38 @@ export class Group<
|
|
171
149
|
|
172
150
|
addMember(member: Everyone | Account, role: Role) {
|
173
151
|
this._raw.addMember(member === "everyone" ? member : member._raw, role);
|
152
|
+
return this;
|
174
153
|
}
|
175
154
|
|
176
|
-
|
177
|
-
this
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
155
|
+
get members() {
|
156
|
+
return this._raw
|
157
|
+
.keys()
|
158
|
+
.filter((key) => {
|
159
|
+
return key === "everyone" || key.startsWith("co_");
|
160
|
+
})
|
161
|
+
.map((id) => {
|
162
|
+
const role = this._raw.get(id as Everyone | AccountID);
|
163
|
+
const accountID =
|
164
|
+
id === "everyone"
|
165
|
+
? undefined
|
166
|
+
: (id as unknown as ID<Account>);
|
167
|
+
const ref =
|
168
|
+
accountID &&
|
169
|
+
new Ref<NonNullable<this[MembersSym]>>(
|
170
|
+
accountID,
|
171
|
+
this._loadedAs,
|
172
|
+
this._schema[MembersSym]
|
173
|
+
);
|
174
|
+
const accessRef = () => ref?.accessFrom(this);
|
175
|
+
|
176
|
+
return {
|
177
|
+
id: id as unknown as Everyone | ID<this[MembersSym]>,
|
178
|
+
role,
|
179
|
+
ref,
|
180
|
+
get account() {
|
181
|
+
return accessRef();
|
182
|
+
},
|
183
|
+
};
|
184
|
+
});
|
185
185
|
}
|
186
186
|
}
|
@@ -7,18 +7,16 @@ import {
|
|
7
7
|
AccountCtx,
|
8
8
|
Group,
|
9
9
|
SubscriptionScope,
|
10
|
-
|
10
|
+
Ref,
|
11
11
|
inspect,
|
12
|
+
subscriptionsScopes,
|
12
13
|
} from "../internal.js";
|
13
14
|
|
14
15
|
export type SubclassedConstructor<T> = {
|
15
16
|
new (...args: any[]): T;
|
16
17
|
};
|
17
18
|
|
18
|
-
export interface CoValueClass<
|
19
|
-
Value extends CoValue = CoValue,
|
20
|
-
Init = any,
|
21
|
-
> {
|
19
|
+
export interface CoValueClass<Value extends CoValue = CoValue, Init = any> {
|
22
20
|
/** @category Construction and loading */
|
23
21
|
new (init: Init, options: { owner: Account | Group }): Value;
|
24
22
|
|
@@ -81,11 +79,14 @@ export interface CoValue<Type extends string = string, Raw = any> {
|
|
81
79
|
export function isCoValue(value: any): value is CoValue {
|
82
80
|
return value && value._type !== undefined;
|
83
81
|
}
|
82
|
+
export function isCoValueClass(value: any): value is CoValueClass {
|
83
|
+
return typeof value === "function" && value.fromRaw !== undefined;
|
84
|
+
}
|
84
85
|
|
85
86
|
/** @category Schemas & CoValues - Abstract interfaces */
|
86
|
-
export type ID<T> = CojsonInternalTypes.RawCoID &
|
87
|
-
|
88
|
-
};
|
87
|
+
export type ID<T> = CojsonInternalTypes.RawCoID & IDMarker<T>;
|
88
|
+
|
89
|
+
type IDMarker<out T> = { __type(_: never): T };
|
89
90
|
|
90
91
|
export class CoValueBase implements CoValue {
|
91
92
|
id!: ID<this>;
|
@@ -93,11 +94,20 @@ export class CoValueBase implements CoValue {
|
|
93
94
|
_raw!: RawCoValue;
|
94
95
|
|
95
96
|
get _owner(): Account | Group {
|
96
|
-
|
97
|
+
const owner = this._raw.group instanceof RawAccount
|
97
98
|
? Account.fromRaw(this._raw.group)
|
98
99
|
: Group.fromRaw(this._raw.group);
|
100
|
+
|
101
|
+
const subScope = subscriptionsScopes.get(this);
|
102
|
+
if (subScope) {
|
103
|
+
subScope.onRefAccessedOrSet(owner.id);
|
104
|
+
subscriptionsScopes.set(owner, subScope);
|
105
|
+
}
|
106
|
+
|
107
|
+
return owner;
|
99
108
|
}
|
100
109
|
|
110
|
+
/** @private */
|
101
111
|
get _loadedAs() {
|
102
112
|
return Account.fromNode(this._raw.core.node);
|
103
113
|
}
|
@@ -117,7 +127,9 @@ export class CoValueBase implements CoValue {
|
|
117
127
|
): Effect.Effect<V, UnavailableError, AccountCtx> {
|
118
128
|
return Effect.gen(this, function* (_) {
|
119
129
|
const account = yield* _(AccountCtx);
|
120
|
-
return yield* _(
|
130
|
+
return yield* _(
|
131
|
+
new Ref(id as ID<V>, account, this as CoValueClass<V>).loadEf()
|
132
|
+
);
|
121
133
|
});
|
122
134
|
}
|
123
135
|
|
@@ -129,7 +141,7 @@ export class CoValueBase implements CoValue {
|
|
129
141
|
onProgress?: (progress: number) => void;
|
130
142
|
}
|
131
143
|
): Promise<V | undefined> {
|
132
|
-
return new
|
144
|
+
return new Ref(id as ID<V>, options.as, this as CoValueClass<V>).load(
|
133
145
|
options?.onProgress && { onProgress: options.onProgress }
|
134
146
|
);
|
135
147
|
}
|
@@ -213,4 +225,13 @@ export class CoValueBase implements CoValue {
|
|
213
225
|
[inspect]() {
|
214
226
|
return this.toJSON();
|
215
227
|
}
|
228
|
+
|
229
|
+
as<C extends CoValueClass>(otherSchema: C): InstanceType<C> {
|
230
|
+
const cast = otherSchema.fromRaw(this._raw) as InstanceType<C>;
|
231
|
+
const subScope = subscriptionsScopes.get(this);
|
232
|
+
if (subScope) {
|
233
|
+
subscriptionsScopes.set(cast, subScope);
|
234
|
+
}
|
235
|
+
return cast;
|
236
|
+
}
|
216
237
|
}
|
@@ -5,20 +5,27 @@ import type {
|
|
5
5
|
CoValue,
|
6
6
|
ID,
|
7
7
|
Me,
|
8
|
-
|
8
|
+
RefEncoded,
|
9
9
|
UnavailableError,
|
10
|
-
indexSignature,
|
11
10
|
} from "../internal.js";
|
12
|
-
import {
|
11
|
+
import {
|
12
|
+
instantiateRefEncoded,
|
13
|
+
isRefEncoded,
|
14
|
+
subscriptionsScopes,
|
15
|
+
} from "../internal.js";
|
13
16
|
|
14
|
-
export class
|
17
|
+
export class Ref<out V extends CoValue> {
|
15
18
|
private cachedValue: V | undefined;
|
16
19
|
|
17
20
|
constructor(
|
18
21
|
readonly id: ID<V>,
|
19
22
|
readonly controlledAccount: Account & Me,
|
20
|
-
readonly
|
21
|
-
) {
|
23
|
+
readonly schema: RefEncoded<V>
|
24
|
+
) {
|
25
|
+
if (!isRefEncoded(schema)) {
|
26
|
+
throw new Error("Ref must be constructed with a ref schema");
|
27
|
+
}
|
28
|
+
}
|
22
29
|
|
23
30
|
get value() {
|
24
31
|
if (this.cachedValue) return this.cachedValue;
|
@@ -27,9 +34,7 @@ export class ValueRef<V extends CoValue> {
|
|
27
34
|
this.id as unknown as CoID<RawCoValue>
|
28
35
|
);
|
29
36
|
if (raw) {
|
30
|
-
const value =
|
31
|
-
fromRaw: raw,
|
32
|
-
}) as V;
|
37
|
+
const value = instantiateRefEncoded(this.schema, raw);
|
33
38
|
this.cachedValue = value;
|
34
39
|
return value;
|
35
40
|
} else {
|
@@ -63,11 +68,7 @@ export class ValueRef<V extends CoValue> {
|
|
63
68
|
if (raw === "unavailable") {
|
64
69
|
return "unavailable";
|
65
70
|
} else {
|
66
|
-
return new
|
67
|
-
this.id,
|
68
|
-
this.controlledAccount,
|
69
|
-
this.valueConstructor
|
70
|
-
).value!;
|
71
|
+
return new Ref(this.id, this.controlledAccount, this.schema).value!;
|
71
72
|
}
|
72
73
|
}
|
73
74
|
|
@@ -95,28 +96,28 @@ export class ValueRef<V extends CoValue> {
|
|
95
96
|
}
|
96
97
|
}
|
97
98
|
|
98
|
-
export function makeRefs<Keys extends string | number
|
99
|
+
export function makeRefs<Keys extends string | number>(
|
99
100
|
getIdForKey: (key: Keys) => ID<CoValue> | undefined,
|
100
101
|
getKeysWithIds: () => Keys[],
|
101
102
|
controlledAccount: Account & Me,
|
102
|
-
|
103
|
-
): { [K in Keys]:
|
104
|
-
[Symbol.iterator]: () => IterableIterator<
|
103
|
+
refSchemaForKey: (key: Keys) => RefEncoded<CoValue>
|
104
|
+
): { [K in Keys]: Ref<CoValue> } & {
|
105
|
+
[Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
|
105
106
|
length: number;
|
106
107
|
} {
|
107
|
-
const refs = {} as { [K in Keys]:
|
108
|
-
[Symbol.iterator]: () => IterableIterator<
|
108
|
+
const refs = {} as { [K in Keys]: Ref<CoValue> } & {
|
109
|
+
[Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
|
109
110
|
length: number;
|
110
111
|
};
|
111
112
|
return new Proxy(refs, {
|
112
|
-
get(
|
113
|
+
get(_target, key) {
|
113
114
|
if (key === Symbol.iterator) {
|
114
115
|
return function* () {
|
115
116
|
for (const key of getKeysWithIds()) {
|
116
|
-
yield new
|
117
|
+
yield new Ref(
|
117
118
|
getIdForKey(key)!,
|
118
119
|
controlledAccount,
|
119
|
-
|
120
|
+
refSchemaForKey(key)
|
120
121
|
);
|
121
122
|
}
|
122
123
|
};
|
@@ -127,14 +128,30 @@ export function makeRefs<Keys extends string | number | indexSignature>(
|
|
127
128
|
}
|
128
129
|
const id = getIdForKey(key as Keys);
|
129
130
|
if (!id) return undefined;
|
130
|
-
return new
|
131
|
+
return new Ref(
|
131
132
|
id as ID<CoValue>,
|
132
133
|
controlledAccount,
|
133
|
-
|
134
|
+
refSchemaForKey(key as Keys)
|
134
135
|
);
|
135
136
|
},
|
136
137
|
ownKeys() {
|
137
138
|
return getKeysWithIds().map((key) => key.toString());
|
138
139
|
},
|
140
|
+
getOwnPropertyDescriptor(target, key) {
|
141
|
+
const id = getIdForKey(key as Keys);
|
142
|
+
if (id) {
|
143
|
+
return {
|
144
|
+
enumerable: true,
|
145
|
+
configurable: true,
|
146
|
+
writable: true,
|
147
|
+
};
|
148
|
+
} else {
|
149
|
+
return Reflect.getOwnPropertyDescriptor(target, key);
|
150
|
+
}
|
151
|
+
},
|
139
152
|
});
|
140
153
|
}
|
154
|
+
|
155
|
+
export type RefIfCoValue<V> = NonNullable<V> extends CoValue
|
156
|
+
? Ref<NonNullable<V>>
|
157
|
+
: never;
|
@@ -1,25 +1,76 @@
|
|
1
|
-
import type { JsonValue } from "cojson";
|
2
|
-
import type
|
3
|
-
import type { Schema,
|
1
|
+
import type { JsonValue, RawCoValue } from "cojson";
|
2
|
+
import { type CoValue, type CoValueClass, isCoValueClass } from "../internal.js";
|
3
|
+
import type { Schema as EffectSchema, TypeId } from "@effect/schema/Schema";
|
4
4
|
|
5
|
-
export type
|
6
|
-
export type
|
7
|
-
export type
|
8
|
-
|
5
|
+
export type CoMarker = { readonly __co: unique symbol };
|
6
|
+
export type co<T> = T | (T & CoMarker);
|
7
|
+
export type IfCo<C, R> = C extends infer _A | infer B
|
8
|
+
? B extends CoMarker
|
9
|
+
? R
|
10
|
+
: never
|
11
|
+
: never;
|
12
|
+
|
13
|
+
export const co = {
|
14
|
+
string: {
|
15
|
+
[SchemaInit]: "json" satisfies Schema,
|
16
|
+
} as unknown as co<string>,
|
17
|
+
number: {
|
18
|
+
[SchemaInit]: "json" satisfies Schema,
|
19
|
+
} as unknown as co<number>,
|
20
|
+
boolean: {
|
21
|
+
[SchemaInit]: "json" satisfies Schema,
|
22
|
+
} as unknown as co<boolean>,
|
23
|
+
null: {
|
24
|
+
[SchemaInit]: "json" satisfies Schema,
|
25
|
+
} as unknown as co<null>,
|
26
|
+
literal: <T extends string | number | boolean>(_lit: T): co<T> => {
|
27
|
+
return { [SchemaInit]: "json" satisfies Schema } as any;
|
28
|
+
},
|
29
|
+
json: <T extends JsonValue>(): co<T> => {
|
30
|
+
return { [SchemaInit]: "json" satisfies Schema } as any;
|
31
|
+
},
|
32
|
+
encoded: <T>(arg: Encoder<T>): co<T> => {
|
33
|
+
return { [SchemaInit]: { encoded: arg } satisfies Schema } as any;
|
34
|
+
},
|
35
|
+
ref: <C extends CoValueClass>(
|
36
|
+
arg: C | ((_raw: InstanceType<C>["_raw"]) => C)
|
37
|
+
): co<InstanceType<C> | null> => {
|
38
|
+
return { [SchemaInit]: arg satisfies Schema } as any;
|
39
|
+
},
|
40
|
+
items: ItemsSym as ItemsSym,
|
41
|
+
members: MembersSym as MembersSym,
|
9
42
|
};
|
10
43
|
|
11
|
-
export type
|
12
|
-
|
13
|
-
|
14
|
-
|
|
44
|
+
export type JsonEncoded = "json";
|
45
|
+
export type EncodedAs<V> = { encoded: Encoder<V> };
|
46
|
+
export type RefEncoded<V extends CoValue> =
|
47
|
+
| CoValueClass<V>
|
48
|
+
| ((raw: RawCoValue) => CoValueClass<V>);
|
49
|
+
|
50
|
+
export function isRefEncoded<V extends CoValue>(
|
51
|
+
schema: Schema
|
52
|
+
): schema is RefEncoded<V> {
|
53
|
+
return typeof schema === "function";
|
54
|
+
}
|
55
|
+
|
56
|
+
export function instantiateRefEncoded<V extends CoValue>(
|
57
|
+
schema: RefEncoded<V>,
|
58
|
+
raw: RawCoValue
|
59
|
+
): V {
|
60
|
+
return isCoValueClass(schema)
|
61
|
+
? schema.fromRaw(raw)
|
62
|
+
: (schema as (raw: RawCoValue) => CoValueClass<V>)(raw).fromRaw(raw);
|
63
|
+
}
|
64
|
+
|
65
|
+
export type Schema = JsonEncoded | RefEncoded<CoValue> | EncodedAs<any>;
|
15
66
|
|
16
|
-
export type
|
17
|
-
?
|
67
|
+
export type SchemaFor<Field> = NonNullable<Field> extends CoValue
|
68
|
+
? RefEncoded<NonNullable<Field>>
|
18
69
|
: NonNullable<Field> extends JsonValue
|
19
|
-
?
|
20
|
-
:
|
70
|
+
? JsonEncoded
|
71
|
+
: EncodedAs<NonNullable<Field>>;
|
21
72
|
|
22
|
-
export type EffectSchemaWithInputAndOutput<A, I = A> =
|
73
|
+
export type EffectSchemaWithInputAndOutput<A, I = A> = EffectSchema<
|
23
74
|
any,
|
24
75
|
any,
|
25
76
|
never
|
@@ -33,37 +84,8 @@ export type EffectSchemaWithInputAndOutput<A, I = A> = Schema<
|
|
33
84
|
export type Encoder<V> = EffectSchemaWithInputAndOutput<V, JsonValue>;
|
34
85
|
|
35
86
|
import { Date } from "@effect/schema/Schema";
|
87
|
+
import { SchemaInit, ItemsSym, MembersSym } from "./symbols.js";
|
36
88
|
|
37
89
|
export const Encoders = {
|
38
90
|
Date,
|
39
91
|
};
|
40
|
-
|
41
|
-
export const indexSignature = Symbol.for("indexSignature");
|
42
|
-
export type indexSignature = typeof indexSignature;
|
43
|
-
|
44
|
-
export type EnsureCoValueNullable<
|
45
|
-
V,
|
46
|
-
Key extends string | indexSignature,
|
47
|
-
> = NonNullable<V> extends CoValue
|
48
|
-
? null extends V
|
49
|
-
? V
|
50
|
-
: Key extends string
|
51
|
-
? [
|
52
|
-
`👋 CoMap fields that are CoValue references should be nullable, declare ${Key} as:`,
|
53
|
-
V | null,
|
54
|
-
]
|
55
|
-
: [
|
56
|
-
`👋 CoMap fields that are CoValue references should be nullable, declare [indexSignature] as:`,
|
57
|
-
V | null,
|
58
|
-
]
|
59
|
-
: V;
|
60
|
-
|
61
|
-
export type EnsureItemNullable<Item, ContainerType extends string> =
|
62
|
-
NonNullable<Item> extends CoValue
|
63
|
-
? null extends Item
|
64
|
-
? any
|
65
|
-
: [
|
66
|
-
`👋 CoList items that are CoValue references should be nullable, make sure the Item generic parameter of ${ContainerType} is:`,
|
67
|
-
Item | null
|
68
|
-
]
|
69
|
-
: any;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
export const SchemaInit = Symbol.for("SchemaInit");
|
3
|
+
export type SchemaInit = typeof SchemaInit;
|
4
|
+
|
5
|
+
export const InitValues = Symbol.for("InitValues");
|
6
|
+
export type InitValues = typeof InitValues;
|
7
|
+
|
8
|
+
export const ItemsSym = Symbol.for("items");
|
9
|
+
export type ItemsSym = typeof ItemsSym;
|
10
|
+
|
11
|
+
export const MembersSym = Symbol.for("members");
|
12
|
+
export type MembersSym = typeof MembersSym;
|
package/src/index.ts
CHANGED
@@ -1,23 +1,25 @@
|
|
1
|
-
/** @category Internal types */
|
2
1
|
export {
|
2
|
+
/** @category Internal types */
|
3
3
|
cojsonReady as jazzReady,
|
4
|
+
cojsonInternals,
|
5
|
+
MAX_RECOMMENDED_TX_SIZE,
|
6
|
+
} from "cojson";
|
7
|
+
export type {
|
4
8
|
InviteSecret,
|
5
9
|
Peer,
|
6
10
|
SessionID,
|
7
11
|
AgentID,
|
8
12
|
SyncMessage,
|
9
|
-
cojsonInternals,
|
10
|
-
MAX_RECOMMENDED_TX_SIZE,
|
11
13
|
} from "cojson";
|
12
14
|
|
13
|
-
export { ID, CoValue } from "./internal.js";
|
15
|
+
export type { ID, CoValue } from "./internal.js";
|
14
16
|
|
15
|
-
export { Encoders } from "./internal.js";
|
17
|
+
export { Encoders, co } from "./internal.js";
|
16
18
|
|
17
|
-
export { CoMap
|
19
|
+
export { CoMap } from "./internal.js";
|
18
20
|
export { CoList } from "./internal.js";
|
19
21
|
export { CoStream, BinaryCoStream } from "./internal.js";
|
20
22
|
export { Group, Profile } from "./internal.js";
|
21
|
-
export { Account, Me } from "./internal.js";
|
23
|
+
export { Account, type Me } from "./internal.js";
|
22
24
|
export { ImageDefinition } from "./internal.js";
|
23
|
-
export { CoValueBase, CoValueClass } from "./internal.js";
|
25
|
+
export { CoValueBase, type CoValueClass } from "./internal.js";
|
package/src/internal.ts
CHANGED