jazz-tools 0.7.0-alpha.3 → 0.7.0-alpha.31
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +78 -8
- package/CHANGELOG.md +181 -0
- package/dist/coValues/account.js +81 -41
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/coList.js +156 -104
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/coMap.js +182 -163
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coStream.js +202 -71
- 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 +46 -38
- package/dist/coValues/group.js.map +1 -1
- package/dist/coValues/interfaces.js +23 -5
- 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 +32 -36
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +170 -59
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +59 -64
- package/dist/tests/coStream.test.js.map +1 -1
- package/dist/tests/groupsAndAccounts.test.js +88 -0
- package/dist/tests/groupsAndAccounts.test.js.map +1 -0
- package/package.json +5 -4
- package/src/coValues/account.ts +129 -105
- package/src/coValues/coList.ts +200 -131
- package/src/coValues/coMap.ts +243 -305
- package/src/coValues/coStream.ts +300 -127
- package/src/coValues/extensions/imageDef.ts +14 -16
- package/src/coValues/group.ts +90 -106
- package/src/coValues/interfaces.ts +33 -12
- package/src/implementation/refs.ts +42 -25
- package/src/implementation/schema.ts +69 -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 +35 -39
- package/src/tests/coMap.test.ts +176 -81
- package/src/tests/coStream.test.ts +76 -81
- package/src/tests/groupsAndAccounts.test.ts +100 -0
package/src/coValues/group.ts
CHANGED
@@ -1,119 +1,97 @@
|
|
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
|
+
RefEncoded,
|
6
|
+
Schema,
|
7
|
+
SubclassedConstructor,
|
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
|
18
|
-
|
20
|
+
export class Profile extends CoMap {
|
21
|
+
name = co.string;
|
19
22
|
}
|
20
|
-
Profile.encoding({ name: "json" });
|
21
23
|
|
22
|
-
export class Group<
|
23
|
-
|
24
|
-
|
25
|
-
root: CoMap | null;
|
26
|
-
},
|
27
|
-
>
|
28
|
-
extends CoValueBase
|
29
|
-
implements CoValue<"Group", RawGroup>
|
30
|
-
{
|
31
|
-
id!: ID<this>;
|
32
|
-
_type!: "Group";
|
24
|
+
export class Group extends CoValueBase implements CoValue<"Group", RawGroup> {
|
25
|
+
declare id: ID<this>;
|
26
|
+
declare _type: "Group";
|
33
27
|
static {
|
34
28
|
this.prototype._type = "Group";
|
35
29
|
}
|
36
|
-
_raw
|
30
|
+
declare _raw: RawGroup;
|
37
31
|
|
38
|
-
static
|
39
|
-
get
|
40
|
-
profile:
|
41
|
-
|
42
|
-
|
43
|
-
root: Def["root"] extends CoValue
|
44
|
-
? RefField<Def["root"]>
|
45
|
-
: PrimitiveField;
|
32
|
+
static _schema: any;
|
33
|
+
get _schema(): {
|
34
|
+
profile: Schema;
|
35
|
+
root: Schema;
|
36
|
+
[MembersSym]: RefEncoded<Account>;
|
46
37
|
} {
|
47
|
-
return (this.constructor as typeof Group).
|
38
|
+
return (this.constructor as typeof Group)._schema;
|
48
39
|
}
|
49
40
|
static {
|
50
|
-
this.
|
51
|
-
profile:
|
52
|
-
root:
|
41
|
+
this._schema = {
|
42
|
+
profile: "json" satisfies Schema,
|
43
|
+
root: "json" satisfies Schema,
|
44
|
+
[MembersSym]: () => Account satisfies Schema,
|
53
45
|
} as any;
|
54
|
-
Object.defineProperty(this.prototype, "
|
55
|
-
get: () => this.
|
46
|
+
Object.defineProperty(this.prototype, "_schema", {
|
47
|
+
get: () => this._schema,
|
56
48
|
});
|
57
49
|
}
|
58
50
|
|
59
|
-
|
60
|
-
|
61
|
-
|
51
|
+
declare profile: Profile | null;
|
52
|
+
declare root: CoMap | null;
|
53
|
+
declare [MembersSym]: Account | null;
|
62
54
|
|
63
|
-
|
64
|
-
|
65
|
-
get _refs(): {
|
66
|
-
profile: Def["profile"] extends Profile
|
67
|
-
? ValueRef<Def["profile"]>
|
68
|
-
: never;
|
69
|
-
root: Def["root"] extends CoMap ? ValueRef<Def["root"]> : never;
|
70
|
-
} {
|
55
|
+
get _refs() {
|
71
56
|
const profileID = this._raw.get("profile") as unknown as
|
72
|
-
| ID<NonNullable<
|
57
|
+
| ID<NonNullable<this["profile"]>>
|
73
58
|
| undefined;
|
74
59
|
const rootID = this._raw.get("root") as unknown as
|
75
|
-
| ID<NonNullable<
|
60
|
+
| ID<NonNullable<this["root"]>>
|
76
61
|
| undefined;
|
77
62
|
return {
|
78
63
|
profile:
|
79
64
|
profileID &&
|
80
|
-
(new
|
65
|
+
(new Ref(
|
81
66
|
profileID,
|
82
67
|
this._loadedAs,
|
83
|
-
|
84
|
-
this
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
68
|
+
this._schema.profile as RefEncoded<
|
69
|
+
NonNullable<this["profile"]>
|
70
|
+
>
|
71
|
+
) as any as this["profile"] extends Profile
|
72
|
+
? Ref<this["profile"]>
|
73
|
+
: never),
|
89
74
|
root:
|
90
75
|
rootID &&
|
91
|
-
(new
|
76
|
+
(new Ref(
|
92
77
|
rootID,
|
93
78
|
this._loadedAs,
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
).ref()
|
99
|
-
) as any),
|
79
|
+
this._schema.root as RefEncoded<NonNullable<this["root"]>>
|
80
|
+
) as any as this["root"] extends CoMap
|
81
|
+
? Ref<this["root"]>
|
82
|
+
: never),
|
100
83
|
};
|
101
84
|
}
|
102
85
|
|
103
|
-
|
104
|
-
constructor(
|
105
|
-
constructor(init: undefined, options: { owner: Account | Group });
|
106
|
-
constructor(
|
107
|
-
init: undefined | { owner: Account | Group },
|
108
|
-
options?: { fromRaw: RawGroup } | { owner: Account | Group }
|
109
|
-
) {
|
86
|
+
/** @deprecated Don't use constructor directly, use .create */
|
87
|
+
constructor(options: { fromRaw: RawGroup } | { owner: Account | Group }) {
|
110
88
|
super();
|
111
89
|
let raw: RawGroup;
|
112
90
|
|
113
91
|
if (options && "fromRaw" in options) {
|
114
92
|
raw = options.fromRaw;
|
115
93
|
} else {
|
116
|
-
const initOwner = options
|
94
|
+
const initOwner = options.owner;
|
117
95
|
if (!initOwner) throw new Error("No owner provided");
|
118
96
|
if (
|
119
97
|
initOwner instanceof Account &&
|
@@ -134,35 +112,19 @@ export class Group<
|
|
134
112
|
enumerable: false,
|
135
113
|
},
|
136
114
|
_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
115
|
});
|
116
|
+
|
117
|
+
return new Proxy(
|
118
|
+
this,
|
119
|
+
AccountAndGroupProxyHandler as ProxyHandler<this>
|
120
|
+
);
|
121
|
+
}
|
122
|
+
|
123
|
+
static create<G extends Group>(
|
124
|
+
this: SubclassedConstructor<G>,
|
125
|
+
options: { owner: Account }
|
126
|
+
) {
|
127
|
+
return new this(options);
|
166
128
|
}
|
167
129
|
|
168
130
|
myRole(): Role | undefined {
|
@@ -171,16 +133,38 @@ export class Group<
|
|
171
133
|
|
172
134
|
addMember(member: Everyone | Account, role: Role) {
|
173
135
|
this._raw.addMember(member === "everyone" ? member : member._raw, role);
|
136
|
+
return this;
|
174
137
|
}
|
175
138
|
|
176
|
-
|
177
|
-
this
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
139
|
+
get members() {
|
140
|
+
return this._raw
|
141
|
+
.keys()
|
142
|
+
.filter((key) => {
|
143
|
+
return key === "everyone" || key.startsWith("co_");
|
144
|
+
})
|
145
|
+
.map((id) => {
|
146
|
+
const role = this._raw.get(id as Everyone | AccountID);
|
147
|
+
const accountID =
|
148
|
+
id === "everyone"
|
149
|
+
? undefined
|
150
|
+
: (id as unknown as ID<Account>);
|
151
|
+
const ref =
|
152
|
+
accountID &&
|
153
|
+
new Ref<NonNullable<this[MembersSym]>>(
|
154
|
+
accountID,
|
155
|
+
this._loadedAs,
|
156
|
+
this._schema[MembersSym]
|
157
|
+
);
|
158
|
+
const accessRef = () => ref?.accessFrom(this);
|
159
|
+
|
160
|
+
return {
|
161
|
+
id: id as unknown as Everyone | ID<this[MembersSym]>,
|
162
|
+
role,
|
163
|
+
ref,
|
164
|
+
get account() {
|
165
|
+
return accessRef();
|
166
|
+
},
|
167
|
+
};
|
168
|
+
});
|
185
169
|
}
|
186
170
|
}
|
@@ -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
|
}
|
@@ -108,7 +118,7 @@ export class CoValueBase implements CoValue {
|
|
108
118
|
this: SubclassedConstructor<V>,
|
109
119
|
raw: RawCoValue
|
110
120
|
): V {
|
111
|
-
return new this(
|
121
|
+
return new this({ fromRaw: raw });
|
112
122
|
}
|
113
123
|
|
114
124
|
static loadEf<V extends CoValue>(
|
@@ -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,77 @@
|
|
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
|
+
export type UnCo<T> = T extends co<infer A> ? A : T;
|
13
|
+
|
14
|
+
export const co = {
|
15
|
+
string: {
|
16
|
+
[SchemaInit]: "json" satisfies Schema,
|
17
|
+
} as unknown as co<string>,
|
18
|
+
number: {
|
19
|
+
[SchemaInit]: "json" satisfies Schema,
|
20
|
+
} as unknown as co<number>,
|
21
|
+
boolean: {
|
22
|
+
[SchemaInit]: "json" satisfies Schema,
|
23
|
+
} as unknown as co<boolean>,
|
24
|
+
null: {
|
25
|
+
[SchemaInit]: "json" satisfies Schema,
|
26
|
+
} as unknown as co<null>,
|
27
|
+
literal: <T extends string | number | boolean>(_lit: T): co<T> => {
|
28
|
+
return { [SchemaInit]: "json" satisfies Schema } as any;
|
29
|
+
},
|
30
|
+
json: <T extends JsonValue>(): co<T> => {
|
31
|
+
return { [SchemaInit]: "json" satisfies Schema } as any;
|
32
|
+
},
|
33
|
+
encoded: <T>(arg: Encoder<T>): co<T> => {
|
34
|
+
return { [SchemaInit]: { encoded: arg } satisfies Schema } as any;
|
35
|
+
},
|
36
|
+
ref: <C extends CoValueClass>(
|
37
|
+
arg: C | ((_raw: InstanceType<C>["_raw"]) => C)
|
38
|
+
): co<InstanceType<C> | null> => {
|
39
|
+
return { [SchemaInit]: arg satisfies Schema } as any;
|
40
|
+
},
|
41
|
+
items: ItemsSym as ItemsSym,
|
42
|
+
members: MembersSym as MembersSym,
|
9
43
|
};
|
10
44
|
|
11
|
-
export type
|
12
|
-
|
13
|
-
|
14
|
-
|
|
45
|
+
export type JsonEncoded = "json";
|
46
|
+
export type EncodedAs<V> = { encoded: Encoder<V> };
|
47
|
+
export type RefEncoded<V extends CoValue> =
|
48
|
+
| CoValueClass<V>
|
49
|
+
| ((raw: RawCoValue) => CoValueClass<V>);
|
50
|
+
|
51
|
+
export function isRefEncoded<V extends CoValue>(
|
52
|
+
schema: Schema
|
53
|
+
): schema is RefEncoded<V> {
|
54
|
+
return typeof schema === "function";
|
55
|
+
}
|
56
|
+
|
57
|
+
export function instantiateRefEncoded<V extends CoValue>(
|
58
|
+
schema: RefEncoded<V>,
|
59
|
+
raw: RawCoValue
|
60
|
+
): V {
|
61
|
+
return isCoValueClass(schema)
|
62
|
+
? schema.fromRaw(raw)
|
63
|
+
: (schema as (raw: RawCoValue) => CoValueClass<V>)(raw).fromRaw(raw);
|
64
|
+
}
|
65
|
+
|
66
|
+
export type Schema = JsonEncoded | RefEncoded<CoValue> | EncodedAs<any>;
|
15
67
|
|
16
|
-
export type
|
17
|
-
?
|
68
|
+
export type SchemaFor<Field> = NonNullable<Field> extends CoValue
|
69
|
+
? RefEncoded<NonNullable<Field>>
|
18
70
|
: NonNullable<Field> extends JsonValue
|
19
|
-
?
|
20
|
-
:
|
71
|
+
? JsonEncoded
|
72
|
+
: EncodedAs<NonNullable<Field>>;
|
21
73
|
|
22
|
-
export type EffectSchemaWithInputAndOutput<A, I = A> =
|
74
|
+
export type EffectSchemaWithInputAndOutput<A, I = A> = EffectSchema<
|
23
75
|
any,
|
24
76
|
any,
|
25
77
|
never
|
@@ -33,37 +85,8 @@ export type EffectSchemaWithInputAndOutput<A, I = A> = Schema<
|
|
33
85
|
export type Encoder<V> = EffectSchemaWithInputAndOutput<V, JsonValue>;
|
34
86
|
|
35
87
|
import { Date } from "@effect/schema/Schema";
|
88
|
+
import { SchemaInit, ItemsSym, MembersSym } from "./symbols.js";
|
36
89
|
|
37
90
|
export const Encoders = {
|
38
91
|
Date,
|
39
92
|
};
|
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