jazz-tools 0.7.0-alpha.0 → 0.7.0-alpha.2
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc.cjs +8 -1
- package/CHANGELOG.md +14 -0
- package/dist/coValues/account.js +138 -0
- package/dist/coValues/account.js.map +1 -0
- package/dist/coValues/coList.js +196 -0
- package/dist/coValues/coList.js.map +1 -0
- package/dist/coValues/coMap.js +249 -0
- package/dist/coValues/coMap.js.map +1 -0
- package/dist/coValues/coStream.js +189 -0
- package/dist/coValues/coStream.js.map +1 -0
- package/dist/coValues/extensions/imageDef.js +12 -12
- package/dist/coValues/extensions/imageDef.js.map +1 -1
- package/dist/coValues/group.js +93 -0
- package/dist/coValues/group.js.map +1 -0
- package/dist/coValues/interfaces.js +58 -0
- package/dist/coValues/interfaces.js.map +1 -0
- package/dist/implementation/errors.js.map +1 -0
- package/dist/implementation/inspect.js +2 -0
- package/dist/implementation/inspect.js.map +1 -0
- package/dist/{refs.js → implementation/refs.js} +11 -13
- package/dist/implementation/refs.js.map +1 -0
- package/dist/implementation/schema.js +6 -0
- package/dist/implementation/schema.js.map +1 -0
- package/dist/{subscriptionScope.js → implementation/subscriptionScope.js} +1 -1
- package/dist/implementation/subscriptionScope.js.map +1 -0
- package/dist/index.js +8 -28
- package/dist/index.js.map +1 -1
- package/dist/internal.js +13 -0
- package/dist/internal.js.map +1 -0
- package/dist/tests/coList.test.js +26 -22
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +74 -43
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +35 -24
- package/dist/tests/coStream.test.js.map +1 -1
- package/package.json +11 -4
- package/src/coValues/account.ts +297 -0
- package/src/coValues/coList.ts +320 -0
- package/src/coValues/coMap.ts +447 -0
- package/src/coValues/coStream.ts +325 -0
- package/src/coValues/extensions/imageDef.ts +25 -16
- package/src/coValues/group.ts +186 -0
- package/src/coValues/interfaces.ts +216 -0
- package/src/implementation/inspect.ts +2 -0
- package/src/{refs.ts → implementation/refs.ts} +21 -32
- package/src/implementation/schema.ts +69 -0
- package/src/{subscriptionScope.ts → implementation/subscriptionScope.ts} +11 -13
- package/src/index.ts +9 -54
- package/src/internal.ts +15 -0
- package/src/tests/coList.test.ts +27 -25
- package/src/tests/coMap.test.ts +111 -50
- package/src/tests/coStream.test.ts +51 -38
- package/dist/coValueInterfaces.js +0 -8
- package/dist/coValueInterfaces.js.map +0 -1
- package/dist/coValues/account/account.js +0 -11
- package/dist/coValues/account/account.js.map +0 -1
- package/dist/coValues/account/accountOf.js +0 -150
- package/dist/coValues/account/accountOf.js.map +0 -1
- package/dist/coValues/account/migration.js +0 -4
- package/dist/coValues/account/migration.js.map +0 -1
- package/dist/coValues/coList/coList.js +0 -2
- package/dist/coValues/coList/coList.js.map +0 -1
- package/dist/coValues/coList/coListOf.js +0 -235
- package/dist/coValues/coList/coListOf.js.map +0 -1
- package/dist/coValues/coList/internalDocs.js +0 -2
- package/dist/coValues/coList/internalDocs.js.map +0 -1
- package/dist/coValues/coMap/coMap.js +0 -2
- package/dist/coValues/coMap/coMap.js.map +0 -1
- package/dist/coValues/coMap/coMapOf.js +0 -262
- package/dist/coValues/coMap/coMapOf.js.map +0 -1
- package/dist/coValues/coMap/internalDocs.js +0 -2
- package/dist/coValues/coMap/internalDocs.js.map +0 -1
- package/dist/coValues/coStream/coStream.js +0 -2
- package/dist/coValues/coStream/coStream.js.map +0 -1
- package/dist/coValues/coStream/coStreamOf.js +0 -244
- package/dist/coValues/coStream/coStreamOf.js.map +0 -1
- package/dist/coValues/construction.js +0 -34
- package/dist/coValues/construction.js.map +0 -1
- package/dist/coValues/group/group.js +0 -2
- package/dist/coValues/group/group.js.map +0 -1
- package/dist/coValues/group/groupOf.js +0 -109
- package/dist/coValues/group/groupOf.js.map +0 -1
- package/dist/coValues/resolution.js +0 -66
- package/dist/coValues/resolution.js.map +0 -1
- package/dist/errors.js.map +0 -1
- package/dist/refs.js.map +0 -1
- package/dist/schemaHelpers.js +0 -14
- package/dist/schemaHelpers.js.map +0 -1
- package/dist/subscriptionScope.js.map +0 -1
- package/dist/tests/types.test.js +0 -33
- package/dist/tests/types.test.js.map +0 -1
- package/src/coValueInterfaces.ts +0 -105
- package/src/coValues/account/account.ts +0 -106
- package/src/coValues/account/accountOf.ts +0 -284
- package/src/coValues/account/migration.ts +0 -12
- package/src/coValues/coList/coList.ts +0 -57
- package/src/coValues/coList/coListOf.ts +0 -377
- package/src/coValues/coList/internalDocs.ts +0 -1
- package/src/coValues/coMap/coMap.ts +0 -110
- package/src/coValues/coMap/coMapOf.ts +0 -451
- package/src/coValues/coMap/internalDocs.ts +0 -1
- package/src/coValues/coStream/coStream.ts +0 -63
- package/src/coValues/coStream/coStreamOf.ts +0 -404
- package/src/coValues/construction.ts +0 -110
- package/src/coValues/group/group.ts +0 -27
- package/src/coValues/group/groupOf.ts +0 -183
- package/src/coValues/resolution.ts +0 -111
- package/src/schemaHelpers.ts +0 -72
- package/src/tests/types.test.ts +0 -37
- /package/dist/{errors.js → implementation/errors.js} +0 -0
- /package/src/{errors.ts → implementation/errors.ts} +0 -0
@@ -0,0 +1,216 @@
|
|
1
|
+
import { Effect, Sink, Stream } from "effect";
|
2
|
+
import type { CojsonInternalTypes, RawCoValue } from "cojson";
|
3
|
+
import { RawAccount } from "cojson";
|
4
|
+
import type { Me, UnavailableError } from "../internal.js";
|
5
|
+
import {
|
6
|
+
Account,
|
7
|
+
AccountCtx,
|
8
|
+
Group,
|
9
|
+
SubscriptionScope,
|
10
|
+
ValueRef,
|
11
|
+
inspect,
|
12
|
+
} from "../internal.js";
|
13
|
+
|
14
|
+
export type SubclassedConstructor<T> = {
|
15
|
+
new (...args: any[]): T;
|
16
|
+
};
|
17
|
+
|
18
|
+
export interface CoValueClass<
|
19
|
+
Value extends CoValue = CoValue,
|
20
|
+
Init = any,
|
21
|
+
> {
|
22
|
+
/** @category Construction and loading */
|
23
|
+
new (init: Init, options: { owner: Account | Group }): Value;
|
24
|
+
|
25
|
+
/** @ignore */
|
26
|
+
fromRaw(raw: Value["_raw"]): Value;
|
27
|
+
|
28
|
+
/** @category Construction and loading */
|
29
|
+
load<V extends Value>(
|
30
|
+
this: SubclassedConstructor<V>,
|
31
|
+
id: ID<V>,
|
32
|
+
options: {
|
33
|
+
as: Account & Me;
|
34
|
+
onProgress?: (progress: number) => void;
|
35
|
+
}
|
36
|
+
): Promise<V | undefined>;
|
37
|
+
|
38
|
+
/** @category Construction and loading */
|
39
|
+
loadEf<V extends Value>(
|
40
|
+
this: SubclassedConstructor<V>,
|
41
|
+
id: ID<V>
|
42
|
+
): Effect.Effect<V, UnavailableError, AccountCtx>;
|
43
|
+
|
44
|
+
/** @category Subscription */
|
45
|
+
subscribe<V extends Value, Acc extends Account>(
|
46
|
+
this: SubclassedConstructor<V>,
|
47
|
+
id: ID<V>,
|
48
|
+
options: { as: Acc & Me },
|
49
|
+
onUpdate: (value: V) => void
|
50
|
+
): () => void;
|
51
|
+
|
52
|
+
/** @category Subscription */
|
53
|
+
subscribeEf<V extends Value>(
|
54
|
+
this: SubclassedConstructor<V>,
|
55
|
+
id: ID<V>
|
56
|
+
): Stream.Stream<V, UnavailableError, AccountCtx>;
|
57
|
+
}
|
58
|
+
|
59
|
+
/** @category Schemas & CoValues - Abstract interfaces */
|
60
|
+
export interface CoValue<Type extends string = string, Raw = any> {
|
61
|
+
/** @category Value identity */
|
62
|
+
readonly id: ID<this>;
|
63
|
+
/** @category Value identity */
|
64
|
+
_type: Type;
|
65
|
+
/** @category Collaboration */
|
66
|
+
_owner: Account | Group;
|
67
|
+
/** @category Subscription */
|
68
|
+
subscribe(listener: (update: this) => void): () => void;
|
69
|
+
/** @category Subscription */
|
70
|
+
subscribeEf(): Stream.Stream<this, UnavailableError, never>;
|
71
|
+
/** @category Internals */
|
72
|
+
_raw: Raw;
|
73
|
+
/** @category Internals */
|
74
|
+
readonly _loadedAs: Account & Me;
|
75
|
+
/** @category Stringifying & inspection */
|
76
|
+
toJSON(): any[] | object;
|
77
|
+
/** @category Stringifying & inspection */
|
78
|
+
[inspect](): any;
|
79
|
+
}
|
80
|
+
|
81
|
+
export function isCoValue(value: any): value is CoValue {
|
82
|
+
return value && value._type !== undefined;
|
83
|
+
}
|
84
|
+
|
85
|
+
/** @category Schemas & CoValues - Abstract interfaces */
|
86
|
+
export type ID<T> = CojsonInternalTypes.RawCoID & {
|
87
|
+
readonly __type: T;
|
88
|
+
};
|
89
|
+
|
90
|
+
export class CoValueBase implements CoValue {
|
91
|
+
id!: ID<this>;
|
92
|
+
_type!: string;
|
93
|
+
_raw!: RawCoValue;
|
94
|
+
|
95
|
+
get _owner(): Account | Group {
|
96
|
+
return this._raw.group instanceof RawAccount
|
97
|
+
? Account.fromRaw(this._raw.group)
|
98
|
+
: Group.fromRaw(this._raw.group);
|
99
|
+
}
|
100
|
+
|
101
|
+
get _loadedAs() {
|
102
|
+
return Account.fromNode(this._raw.core.node);
|
103
|
+
}
|
104
|
+
|
105
|
+
constructor(..._args: any) {}
|
106
|
+
|
107
|
+
static fromRaw<V extends CoValue>(
|
108
|
+
this: SubclassedConstructor<V>,
|
109
|
+
raw: RawCoValue
|
110
|
+
): V {
|
111
|
+
return new this(undefined, { fromRaw: raw });
|
112
|
+
}
|
113
|
+
|
114
|
+
static loadEf<V extends CoValue>(
|
115
|
+
this: SubclassedConstructor<V> & typeof CoValueBase,
|
116
|
+
id: ID<V>
|
117
|
+
): Effect.Effect<V, UnavailableError, AccountCtx> {
|
118
|
+
return Effect.gen(this, function* (_) {
|
119
|
+
const account = yield* _(AccountCtx);
|
120
|
+
return yield* _(new ValueRef(id as ID<V>, account, this).loadEf());
|
121
|
+
});
|
122
|
+
}
|
123
|
+
|
124
|
+
static load<V extends CoValue>(
|
125
|
+
this: SubclassedConstructor<V> & typeof CoValueBase,
|
126
|
+
id: ID<V>,
|
127
|
+
options: {
|
128
|
+
as: Account & Me;
|
129
|
+
onProgress?: (progress: number) => void;
|
130
|
+
}
|
131
|
+
): Promise<V | undefined> {
|
132
|
+
return new ValueRef(id as ID<V>, options.as, this).load(
|
133
|
+
options?.onProgress && { onProgress: options.onProgress }
|
134
|
+
);
|
135
|
+
}
|
136
|
+
|
137
|
+
static subscribe<V extends CoValue, Acc extends Account>(
|
138
|
+
this: SubclassedConstructor<V> & typeof CoValueBase,
|
139
|
+
id: ID<V>,
|
140
|
+
options: { as: Acc & Me },
|
141
|
+
onUpdate: (value: V) => void
|
142
|
+
): () => void {
|
143
|
+
void Effect.runPromise(
|
144
|
+
Effect.provideService(
|
145
|
+
this.subscribeEf(id).pipe(
|
146
|
+
Stream.run(
|
147
|
+
Sink.forEach((update) =>
|
148
|
+
Effect.sync(() => onUpdate(update))
|
149
|
+
)
|
150
|
+
)
|
151
|
+
),
|
152
|
+
AccountCtx,
|
153
|
+
options.as as Account & Me
|
154
|
+
)
|
155
|
+
);
|
156
|
+
|
157
|
+
return function unsubscribe() {};
|
158
|
+
}
|
159
|
+
|
160
|
+
static subscribeEf<V extends CoValue>(
|
161
|
+
this: SubclassedConstructor<V> & typeof CoValueBase,
|
162
|
+
id: ID<V>
|
163
|
+
): Stream.Stream<V, UnavailableError, AccountCtx> {
|
164
|
+
return Stream.fromEffect(this.loadEf(id)).pipe(
|
165
|
+
Stream.flatMap((value) =>
|
166
|
+
Stream.asyncScoped<V, UnavailableError>((emit) =>
|
167
|
+
Effect.gen(this, function* (_) {
|
168
|
+
const subscription = new SubscriptionScope(
|
169
|
+
value,
|
170
|
+
this,
|
171
|
+
(update) => {
|
172
|
+
void emit.single(update as V);
|
173
|
+
}
|
174
|
+
);
|
175
|
+
|
176
|
+
yield* _(
|
177
|
+
Effect.addFinalizer(() =>
|
178
|
+
Effect.sync(() => subscription.unsubscribeAll())
|
179
|
+
)
|
180
|
+
);
|
181
|
+
})
|
182
|
+
)
|
183
|
+
)
|
184
|
+
);
|
185
|
+
}
|
186
|
+
|
187
|
+
subscribe(listener: (update: this) => void): () => void {
|
188
|
+
return (this.constructor as unknown as typeof CoValueBase).subscribe(
|
189
|
+
this.id as unknown as ID<CoValue>,
|
190
|
+
{ as: this._loadedAs },
|
191
|
+
listener as unknown as (update: CoValue) => void
|
192
|
+
);
|
193
|
+
}
|
194
|
+
|
195
|
+
subscribeEf(): Stream.Stream<this, UnavailableError, never> {
|
196
|
+
return Stream.provideService(
|
197
|
+
(this.constructor as unknown as typeof CoValueBase).subscribeEf(
|
198
|
+
this.id as unknown as ID<CoValue>
|
199
|
+
),
|
200
|
+
AccountCtx,
|
201
|
+
this._loadedAs
|
202
|
+
) as unknown as Stream.Stream<this, UnavailableError, never>;
|
203
|
+
}
|
204
|
+
|
205
|
+
toJSON(): object | any[] {
|
206
|
+
return {
|
207
|
+
id: this.id,
|
208
|
+
type: this._type,
|
209
|
+
error: "unknown CoValue class",
|
210
|
+
};
|
211
|
+
}
|
212
|
+
|
213
|
+
[inspect]() {
|
214
|
+
return this.toJSON();
|
215
|
+
}
|
216
|
+
}
|
@@ -1,19 +1,15 @@
|
|
1
1
|
import { Effect } from "effect";
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
import {
|
5
|
-
import { UnavailableError } from "./errors.js";
|
6
|
-
import { getCoValueConstructorInProperty } from "./coValues/resolution.js";
|
7
|
-
import { PropDef } from "./schemaHelpers.js";
|
8
|
-
import { subscriptionsScopes } from "./subscriptionScope.js";
|
2
|
+
import type { CoID, RawCoValue } from "cojson";
|
3
|
+
import type { Account, CoValue, ID, Me, SubclassedConstructor, UnavailableError, indexSignature} from '../internal.js';
|
4
|
+
import { subscriptionsScopes } from '../internal.js';
|
9
5
|
|
10
6
|
export class ValueRef<V extends CoValue> {
|
11
7
|
private cachedValue: V | undefined;
|
12
8
|
|
13
9
|
constructor(
|
14
10
|
readonly id: ID<V>,
|
15
|
-
readonly controlledAccount:
|
16
|
-
readonly
|
11
|
+
readonly controlledAccount: Account & Me,
|
12
|
+
readonly valueConstructor: SubclassedConstructor<V>
|
17
13
|
) {}
|
18
14
|
|
19
15
|
get value() {
|
@@ -23,18 +19,11 @@ export class ValueRef<V extends CoValue> {
|
|
23
19
|
this.id as unknown as CoID<RawCoValue>
|
24
20
|
);
|
25
21
|
if (raw) {
|
26
|
-
const
|
27
|
-
this.propDef,
|
28
|
-
raw
|
29
|
-
);
|
30
|
-
if (!Constructor) {
|
31
|
-
throw new Error(
|
32
|
-
"Couldn't extract CoValue constructor from property definition"
|
33
|
-
);
|
34
|
-
}
|
35
|
-
const value = new Constructor(undefined, { fromRaw: raw }) as V;
|
22
|
+
const value = new this.valueConstructor(undefined, { fromRaw: raw }) as V;
|
36
23
|
this.cachedValue = value;
|
37
24
|
return value;
|
25
|
+
} else {
|
26
|
+
return null;
|
38
27
|
}
|
39
28
|
}
|
40
29
|
|
@@ -62,7 +51,7 @@ export class ValueRef<V extends CoValue> {
|
|
62
51
|
if (raw === "unavailable") {
|
63
52
|
return "unavailable";
|
64
53
|
} else {
|
65
|
-
return new ValueRef(this.id, this.controlledAccount, this.
|
54
|
+
return new ValueRef(this.id, this.controlledAccount, this.valueConstructor)
|
66
55
|
.value!;
|
67
56
|
}
|
68
57
|
}
|
@@ -76,7 +65,7 @@ export class ValueRef<V extends CoValue> {
|
|
76
65
|
}
|
77
66
|
}
|
78
67
|
|
79
|
-
accessFrom(fromScopeValue: CoValue): V |
|
68
|
+
accessFrom(fromScopeValue: CoValue): V | null {
|
80
69
|
const subScope = subscriptionsScopes.get(fromScopeValue);
|
81
70
|
|
82
71
|
subScope?.onRefAccessedOrSet(this.id);
|
@@ -89,13 +78,13 @@ export class ValueRef<V extends CoValue> {
|
|
89
78
|
}
|
90
79
|
}
|
91
80
|
|
92
|
-
export function makeRefs<
|
93
|
-
getIdForKey:
|
94
|
-
getKeysWithIds: () =>
|
95
|
-
controlledAccount:
|
96
|
-
|
97
|
-
): { [K in
|
98
|
-
const refs = {} as { [K in
|
81
|
+
export function makeRefs<Keys extends string | number | indexSignature>(
|
82
|
+
getIdForKey: (key: Keys) => ID<CoValue> | undefined,
|
83
|
+
getKeysWithIds: () => Keys[],
|
84
|
+
controlledAccount: Account & Me,
|
85
|
+
valueConstructorForKey: (key: Keys) => SubclassedConstructor<CoValue>,
|
86
|
+
): { [K in Keys]: ValueRef<CoValue> } {
|
87
|
+
const refs = {} as { [K in Keys]: ValueRef<CoValue> };
|
99
88
|
return new Proxy(refs, {
|
100
89
|
get(target, key) {
|
101
90
|
if (key === Symbol.iterator) {
|
@@ -104,7 +93,7 @@ export function makeRefs<F extends { [key: string | number]: CoValue }>(
|
|
104
93
|
yield new ValueRef(
|
105
94
|
getIdForKey(key)!,
|
106
95
|
controlledAccount,
|
107
|
-
|
96
|
+
valueConstructorForKey(key)
|
108
97
|
);
|
109
98
|
}
|
110
99
|
};
|
@@ -113,12 +102,12 @@ export function makeRefs<F extends { [key: string | number]: CoValue }>(
|
|
113
102
|
if (key === "length") {
|
114
103
|
return getKeysWithIds().length;
|
115
104
|
}
|
116
|
-
const id = getIdForKey(key as
|
105
|
+
const id = getIdForKey(key as Keys);
|
117
106
|
if (!id) return undefined;
|
118
107
|
return new ValueRef(
|
119
|
-
id as ID<
|
108
|
+
id as ID<CoValue>,
|
120
109
|
controlledAccount,
|
121
|
-
|
110
|
+
valueConstructorForKey(key as Keys)
|
122
111
|
);
|
123
112
|
},
|
124
113
|
ownKeys() {
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import type { JsonValue } from "cojson";
|
2
|
+
import type { CoValue, CoValueClass } from "../internal.js";
|
3
|
+
import type { Schema, TypeId } from "@effect/schema/Schema";
|
4
|
+
|
5
|
+
export type PrimitiveField = "json";
|
6
|
+
export type EncodedField<V> = { encoded: Encoder<V> };
|
7
|
+
export type RefField<V extends CoValue> = {
|
8
|
+
ref: () => CoValueClass<V>;
|
9
|
+
};
|
10
|
+
|
11
|
+
export type FieldDescriptor =
|
12
|
+
| PrimitiveField
|
13
|
+
| RefField<CoValue>
|
14
|
+
| EncodedField<any>;
|
15
|
+
|
16
|
+
export type FieldDescriptorFor<Field> = NonNullable<Field> extends CoValue
|
17
|
+
? RefField<NonNullable<Field>>
|
18
|
+
: NonNullable<Field> extends JsonValue
|
19
|
+
? PrimitiveField
|
20
|
+
: EncodedField<NonNullable<Field>>;
|
21
|
+
|
22
|
+
export type EffectSchemaWithInputAndOutput<A, I = A> = Schema<
|
23
|
+
any,
|
24
|
+
any,
|
25
|
+
never
|
26
|
+
> & {
|
27
|
+
[TypeId]: {
|
28
|
+
_A: (_: any) => A;
|
29
|
+
_I: (_: any) => I;
|
30
|
+
};
|
31
|
+
};
|
32
|
+
|
33
|
+
export type Encoder<V> = EffectSchemaWithInputAndOutput<V, JsonValue>;
|
34
|
+
|
35
|
+
import { Date } from "@effect/schema/Schema";
|
36
|
+
|
37
|
+
export const Encoders = {
|
38
|
+
Date,
|
39
|
+
};
|
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;
|
@@ -1,7 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
import * as S from "@effect/schema/Schema";
|
4
|
-
import { RawCoValue } from "cojson";
|
1
|
+
import type { RawCoValue } from "cojson";
|
2
|
+
import type { Account, CoValue, CoValueBase, ID, Me, SubclassedConstructor } from "../internal.js";
|
5
3
|
|
6
4
|
export const subscriptionsScopes = new WeakMap<
|
7
5
|
CoValue,
|
@@ -9,10 +7,10 @@ export const subscriptionsScopes = new WeakMap<
|
|
9
7
|
>();
|
10
8
|
|
11
9
|
export class SubscriptionScope<
|
12
|
-
|
10
|
+
Root extends CoValue
|
13
11
|
> {
|
14
12
|
scopeID: string = `scope-${Math.random().toString(36).slice(2)}`;
|
15
|
-
subscriber:
|
13
|
+
subscriber: Account & Me;
|
16
14
|
entries = new Map<
|
17
15
|
ID<CoValue>,
|
18
16
|
| { state: "loading"; immediatelyUnsub?: boolean }
|
@@ -20,16 +18,16 @@ export class SubscriptionScope<
|
|
20
18
|
>();
|
21
19
|
rootEntry: {
|
22
20
|
state: "loaded";
|
23
|
-
value:
|
21
|
+
value: Root;
|
24
22
|
rawUnsub: () => void;
|
25
23
|
};
|
26
|
-
onUpdate: (newRoot:
|
24
|
+
onUpdate: (newRoot: Root) => void;
|
27
25
|
scheduledUpdate: boolean = false;
|
28
26
|
|
29
27
|
constructor(
|
30
|
-
root:
|
31
|
-
rootSchema:
|
32
|
-
onUpdate: (newRoot:
|
28
|
+
root: Root,
|
29
|
+
rootSchema: SubclassedConstructor<Root> & typeof CoValueBase,
|
30
|
+
onUpdate: (newRoot: Root) => void
|
33
31
|
) {
|
34
32
|
this.rootEntry = {
|
35
33
|
state: "loaded" as const,
|
@@ -47,7 +45,7 @@ export class SubscriptionScope<
|
|
47
45
|
if (!rawUpdate) return;
|
48
46
|
this.rootEntry.value = rootSchema.fromRaw(
|
49
47
|
rawUpdate
|
50
|
-
) as
|
48
|
+
) as Root;
|
51
49
|
// console.log("root update", this.rootEntry.value.toJSON());
|
52
50
|
subscriptionsScopes.set(this.rootEntry.value, this);
|
53
51
|
this.scheduleUpdate();
|
@@ -77,7 +75,7 @@ export class SubscriptionScope<
|
|
77
75
|
immediatelyUnsub: false,
|
78
76
|
} as const;
|
79
77
|
this.entries.set(accessedOrSetId, loadingEntry);
|
80
|
-
this.subscriber._raw.core.node
|
78
|
+
void this.subscriber._raw.core.node
|
81
79
|
.loadCoValueCore(accessedOrSetId)
|
82
80
|
.then((core) => {
|
83
81
|
if (
|
package/src/index.ts
CHANGED
@@ -1,29 +1,3 @@
|
|
1
|
-
import { AccountOf } from "./coValues/account/accountOf.js";
|
2
|
-
import { CoListOf } from "./coValues/coList/coListOf.js";
|
3
|
-
import { CoMapOf } from "./coValues/coMap/coMapOf.js";
|
4
|
-
import {
|
5
|
-
BinaryCoStreamImpl,
|
6
|
-
CoStreamOf,
|
7
|
-
} from "./coValues/coStream/coStreamOf.js";
|
8
|
-
import { ImageDefinition } from "./coValues/extensions/imageDef.js";
|
9
|
-
import { GroupOf } from "./coValues/group/groupOf.js";
|
10
|
-
|
11
|
-
/** @hidden */
|
12
|
-
export * as S from "@effect/schema/Schema";
|
13
|
-
|
14
|
-
/** @category Schemas & CoValues - Schema definers */
|
15
|
-
export const Co = {
|
16
|
-
map: CoMapOf,
|
17
|
-
list: CoListOf,
|
18
|
-
stream: CoStreamOf,
|
19
|
-
binaryStream: BinaryCoStreamImpl,
|
20
|
-
account: AccountOf,
|
21
|
-
group: GroupOf,
|
22
|
-
media: {
|
23
|
-
imageDef: ImageDefinition
|
24
|
-
}
|
25
|
-
};
|
26
|
-
|
27
1
|
/** @category Internal types */
|
28
2
|
export {
|
29
3
|
cojsonReady as jazzReady,
|
@@ -36,33 +10,14 @@ export {
|
|
36
10
|
MAX_RECOMMENDED_TX_SIZE,
|
37
11
|
} from "cojson";
|
38
12
|
|
39
|
-
export { ID, CoValue
|
40
|
-
|
41
|
-
|
42
|
-
export { CoMap, CoMapSchema } from "./coValues/coMap/coMap.js";
|
43
|
-
/** @category Schemas & CoValues - CoMap */
|
44
|
-
export * as CoMapInternals from "./coValues/coMap/internalDocs.js";
|
45
|
-
|
46
|
-
export { CoList, CoListSchema } from "./coValues/coList/coList.js";
|
47
|
-
/** @category Schemas & CoValues - CoList */
|
48
|
-
export * as CoListInternals from "./coValues/coList/internalDocs.js";
|
49
|
-
|
50
|
-
export {
|
51
|
-
CoStream,
|
52
|
-
CoStreamSchema,
|
53
|
-
BinaryCoStream,
|
54
|
-
} from "./coValues/coStream/coStream.js";
|
55
|
-
|
56
|
-
export {
|
57
|
-
AnyAccount,
|
58
|
-
ControlledAccount,
|
59
|
-
AccountSchema,
|
60
|
-
controlledAccountSym,
|
61
|
-
} from "./coValues/account/account.js";
|
13
|
+
export { ID, CoValue } from "./internal.js";
|
62
14
|
|
63
|
-
export {
|
64
|
-
export { Account, BaseProfile } from "./coValues/account/accountOf.js";
|
65
|
-
export { AnyGroup } from "./coValues/group/group.js";
|
66
|
-
export { Group } from "./coValues/group/groupOf.js";
|
15
|
+
export { Encoders } from "./internal.js";
|
67
16
|
|
68
|
-
export {
|
17
|
+
export { CoMap, indexSignature } from "./internal.js";
|
18
|
+
export { CoList } from "./internal.js";
|
19
|
+
export { CoStream, BinaryCoStream } from "./internal.js";
|
20
|
+
export { Group, Profile } from "./internal.js";
|
21
|
+
export { Account, Me } from "./internal.js";
|
22
|
+
export { ImageDefinition } from "./internal.js";
|
23
|
+
export { CoValueBase, CoValueClass } from "./internal.js";
|
package/src/internal.ts
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
export * from './implementation/inspect.js';
|
2
|
+
export * from "./coValues/interfaces.js";
|
3
|
+
|
4
|
+
export * from "./coValues/coMap.js";
|
5
|
+
export * from "./coValues/account.js";
|
6
|
+
export * from "./coValues/coList.js";
|
7
|
+
export * from "./coValues/coStream.js";
|
8
|
+
export * from "./coValues/group.js";
|
9
|
+
|
10
|
+
export * from "./implementation/errors.js";
|
11
|
+
export * from "./implementation/refs.js";
|
12
|
+
export * from "./implementation/schema.js";
|
13
|
+
export * from "./implementation/subscriptionScope.js";
|
14
|
+
|
15
|
+
export * from "./coValues/extensions/imageDef.js";
|
package/src/tests/coList.test.ts
CHANGED
@@ -4,7 +4,7 @@ import { webcrypto } from "node:crypto";
|
|
4
4
|
import { connectedPeers } from "cojson/src/streamUtils.js";
|
5
5
|
import { newRandomSessionID } from "cojson/src/coValueCore.js";
|
6
6
|
import { Effect, Queue } from "effect";
|
7
|
-
import {
|
7
|
+
import { Account, CoList, jazzReady } from "..";
|
8
8
|
|
9
9
|
if (!("crypto" in globalThis)) {
|
10
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
@@ -20,7 +20,8 @@ describe("Simple CoList operations", async () => {
|
|
20
20
|
name: "Hermes Puggington",
|
21
21
|
});
|
22
22
|
|
23
|
-
class TestList extends
|
23
|
+
class TestList extends CoList<string> {}
|
24
|
+
TestList.encoding({ _item: "json" });
|
24
25
|
|
25
26
|
const list = new TestList(["bread", "butter", "onion"], { owner: me });
|
26
27
|
|
@@ -97,32 +98,35 @@ describe("Simple CoList operations", async () => {
|
|
97
98
|
expect(list._raw.asArray()).toEqual(["butter", "onion"]);
|
98
99
|
});
|
99
100
|
|
100
|
-
test("splice", () => {
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
});
|
101
|
+
// test("splice", () => {
|
102
|
+
// const list = new TestList(["bread", "butter", "onion"], {
|
103
|
+
// owner: me,
|
104
|
+
// });
|
105
|
+
// list.splice(1, 1, "salt", "pepper");
|
106
|
+
// expect(list.length).toBe(4);
|
107
|
+
// expect(list._raw.asArray()).toEqual([
|
108
|
+
// "bread",
|
109
|
+
// "salt",
|
110
|
+
// "pepper",
|
111
|
+
// "onion",
|
112
|
+
// ]);
|
113
|
+
// });
|
113
114
|
});
|
114
115
|
});
|
115
116
|
|
116
117
|
describe("CoList resolution", async () => {
|
117
|
-
class TwiceNestedList extends
|
118
|
+
class TwiceNestedList extends CoList<string> {
|
118
119
|
joined() {
|
119
120
|
return this.join(",");
|
120
121
|
}
|
121
122
|
}
|
123
|
+
TwiceNestedList.encoding({ _item: "json" });
|
122
124
|
|
123
|
-
class NestedList extends
|
125
|
+
class NestedList extends CoList<TwiceNestedList | null> {}
|
126
|
+
NestedList.encoding({ _item: { ref: () => TwiceNestedList } });
|
124
127
|
|
125
|
-
class TestList extends
|
128
|
+
class TestList extends CoList<NestedList | null> {}
|
129
|
+
TestList.encoding({ _item: { ref: () => NestedList } });
|
126
130
|
|
127
131
|
const initNodeAndList = async () => {
|
128
132
|
const me = await Account.create({
|
@@ -173,7 +177,7 @@ describe("CoList resolution", async () => {
|
|
173
177
|
|
174
178
|
const loadedList = await TestList.load(list.id, { as: meOnSecondPeer });
|
175
179
|
|
176
|
-
expect(loadedList?.[0]).toBe(
|
180
|
+
expect(loadedList?.[0]).toBe(null);
|
177
181
|
expect(loadedList?._refs[0]?.id).toEqual(list[0]!.id);
|
178
182
|
|
179
183
|
const loadedNestedList = await NestedList.load(list[0]!.id, {
|
@@ -181,7 +185,7 @@ describe("CoList resolution", async () => {
|
|
181
185
|
});
|
182
186
|
|
183
187
|
expect(loadedList?.[0]).toBeDefined();
|
184
|
-
expect(loadedList?.[0]?.[0]).
|
188
|
+
expect(loadedList?.[0]?.[0]).toBe(null);
|
185
189
|
expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]![0]!.id);
|
186
190
|
expect(loadedList?._refs[0]?.value).toEqual(loadedNestedList);
|
187
191
|
|
@@ -194,9 +198,7 @@ describe("CoList resolution", async () => {
|
|
194
198
|
expect(loadedList?.[0]?.[0]?.[0]).toBe("a");
|
195
199
|
expect(loadedList?.[0]?.[0]?.joined()).toBe("a,b");
|
196
200
|
expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]?.[0]?.id);
|
197
|
-
expect(loadedList?.[0]?._refs[0]?.value).toEqual(
|
198
|
-
loadedTwiceNestedList
|
199
|
-
);
|
201
|
+
expect(loadedList?.[0]?._refs[0]?.value).toEqual(loadedTwiceNestedList);
|
200
202
|
|
201
203
|
const otherNestedList = new NestedList(
|
202
204
|
[new TwiceNestedList(["e", "f"], { owner: meOnSecondPeer })],
|
@@ -241,11 +243,11 @@ describe("CoList resolution", async () => {
|
|
241
243
|
);
|
242
244
|
|
243
245
|
const update1 = yield* $(Queue.take(queue));
|
244
|
-
expect(update1?.[0]).
|
246
|
+
expect(update1?.[0]).toBe(null);
|
245
247
|
|
246
248
|
const update2 = yield* $(Queue.take(queue));
|
247
249
|
expect(update2?.[0]).toBeDefined();
|
248
|
-
expect(update2?.[0]?.[0]).
|
250
|
+
expect(update2?.[0]?.[0]).toBe(null);
|
249
251
|
|
250
252
|
const update3 = yield* $(Queue.take(queue));
|
251
253
|
expect(update3?.[0]?.[0]).toBeDefined();
|