jazz-tools 0.7.0-alpha.0 → 0.7.0-alpha.11
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc.cjs +8 -1
- package/.turbo/turbo-build.log +79 -9
- package/CHANGELOG.md +76 -0
- package/dist/coValues/account.js +171 -0
- package/dist/coValues/account.js.map +1 -0
- package/dist/coValues/coList.js +246 -0
- package/dist/coValues/coList.js.map +1 -0
- package/dist/coValues/coMap.js +235 -0
- package/dist/coValues/coMap.js.map +1 -0
- package/dist/coValues/coStream.js +263 -0
- package/dist/coValues/coStream.js.map +1 -0
- package/dist/coValues/extensions/imageDef.js +14 -11
- package/dist/coValues/extensions/imageDef.js.map +1 -1
- package/dist/coValues/group.js +71 -0
- package/dist/coValues/group.js.map +1 -0
- package/dist/coValues/interfaces.js +61 -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} +16 -15
- package/dist/implementation/refs.js.map +1 -0
- package/dist/implementation/schema.js +41 -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 +10 -29
- 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 +22 -22
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +140 -46
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +55 -49
- package/dist/tests/coStream.test.js.map +1 -1
- package/package.json +11 -4
- package/src/coValues/account.ts +327 -0
- package/src/coValues/coList.ts +391 -0
- package/src/coValues/coMap.ts +413 -0
- package/src/coValues/coStream.ts +404 -0
- package/src/coValues/extensions/imageDef.ts +20 -16
- package/src/coValues/group.ts +139 -0
- package/src/coValues/interfaces.ts +218 -0
- package/src/implementation/inspect.ts +2 -0
- package/src/{refs.ts → implementation/refs.ts} +53 -39
- package/src/implementation/schema.ts +124 -0
- package/src/{subscriptionScope.ts → implementation/subscriptionScope.ts} +11 -13
- package/src/index.ts +14 -57
- package/src/internal.ts +15 -0
- package/src/tests/coList.test.ts +23 -25
- package/src/tests/coMap.test.ts +140 -53
- package/src/tests/coStream.test.ts +76 -68
- 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,391 @@
|
|
1
|
+
import type { RawCoList } from "cojson";
|
2
|
+
import { RawAccount } from "cojson";
|
3
|
+
import type { Effect, Stream } from "effect";
|
4
|
+
import type {
|
5
|
+
AccountCtx,
|
6
|
+
CoValue,
|
7
|
+
ValidItem,
|
8
|
+
Schema,
|
9
|
+
SchemaFor,
|
10
|
+
ID,
|
11
|
+
RefEncoded,
|
12
|
+
SubclassedConstructor,
|
13
|
+
UnavailableError,
|
14
|
+
IfCo,
|
15
|
+
} from "../internal.js";
|
16
|
+
import {
|
17
|
+
Account,
|
18
|
+
CoValueBase,
|
19
|
+
Group,
|
20
|
+
InitValues,
|
21
|
+
ItemsSym,
|
22
|
+
Ref,
|
23
|
+
SchemaInit,
|
24
|
+
co,
|
25
|
+
inspect,
|
26
|
+
isRefEncoded,
|
27
|
+
makeRefs,
|
28
|
+
} from "../internal.js";
|
29
|
+
import { Schema as EffectSchema } from "@effect/schema";
|
30
|
+
|
31
|
+
export class CoList<Item extends ValidItem<Item, "CoList"> = any>
|
32
|
+
extends Array<Item>
|
33
|
+
implements CoValue<"CoList", RawCoList>
|
34
|
+
{
|
35
|
+
static Of<Item extends ValidItem<Item, "CoList"> = any>(
|
36
|
+
item: IfCo<Item, Item>
|
37
|
+
): typeof CoList<Item> {
|
38
|
+
return class CoListOf extends CoList<Item> {
|
39
|
+
[co.items] = item;
|
40
|
+
};
|
41
|
+
}
|
42
|
+
|
43
|
+
/** @deprecated Use UPPERCASE `CoList.Of` instead! */
|
44
|
+
static of(..._args: never): never {
|
45
|
+
throw new Error("Can't use Array.of with CoLists");
|
46
|
+
}
|
47
|
+
|
48
|
+
id!: ID<this>;
|
49
|
+
_type!: "CoList";
|
50
|
+
static {
|
51
|
+
this.prototype._type = "CoList";
|
52
|
+
}
|
53
|
+
_raw!: RawCoList;
|
54
|
+
|
55
|
+
/** @internal This is only a marker type and doesn't exist at runtime */
|
56
|
+
[ItemsSym]!: Item;
|
57
|
+
static _schema: any;
|
58
|
+
get _schema(): {
|
59
|
+
[ItemsSym]: SchemaFor<Item>;
|
60
|
+
} {
|
61
|
+
return (this.constructor as typeof CoList)._schema;
|
62
|
+
}
|
63
|
+
|
64
|
+
get _owner(): Account | Group {
|
65
|
+
return this._raw.group instanceof RawAccount
|
66
|
+
? Account.fromRaw(this._raw.group)
|
67
|
+
: Group.fromRaw(this._raw.group);
|
68
|
+
}
|
69
|
+
|
70
|
+
get _refs(): {
|
71
|
+
[idx: number]: NonNullable<Item> extends CoValue
|
72
|
+
? Ref<NonNullable<Item>>
|
73
|
+
: never;
|
74
|
+
} & {
|
75
|
+
length: number;
|
76
|
+
[Symbol.iterator](): IterableIterator<
|
77
|
+
NonNullable<Item> extends CoValue ? Ref<NonNullable<Item>> : never
|
78
|
+
>;
|
79
|
+
} {
|
80
|
+
return makeRefs<number>(
|
81
|
+
(idx) => this._raw.get(idx) as unknown as ID<CoValue>,
|
82
|
+
() =>
|
83
|
+
Array.from(
|
84
|
+
{ length: this._raw.entries().length },
|
85
|
+
(_, idx) => idx
|
86
|
+
),
|
87
|
+
this._loadedAs,
|
88
|
+
(_idx) => this._schema[ItemsSym] as RefEncoded<CoValue>
|
89
|
+
) as any;
|
90
|
+
}
|
91
|
+
|
92
|
+
get _edits(): {
|
93
|
+
[idx: number]: {
|
94
|
+
value?: Item;
|
95
|
+
ref?: Item extends CoValue ? Ref<Item> : never;
|
96
|
+
by?: Account;
|
97
|
+
madeAt: Date;
|
98
|
+
};
|
99
|
+
} {
|
100
|
+
throw new Error("Not implemented");
|
101
|
+
}
|
102
|
+
|
103
|
+
get _loadedAs() {
|
104
|
+
return Account.fromNode(this._raw.core.node);
|
105
|
+
}
|
106
|
+
|
107
|
+
[InitValues]?: {
|
108
|
+
init: Item[];
|
109
|
+
owner: Account | Group;
|
110
|
+
};
|
111
|
+
|
112
|
+
static get [Symbol.species]() {
|
113
|
+
return Array;
|
114
|
+
}
|
115
|
+
|
116
|
+
constructor(_init: undefined, options: { fromRaw: RawCoList });
|
117
|
+
constructor(init: Item[], options: { owner: Account | Group });
|
118
|
+
constructor(
|
119
|
+
init: Item[] | undefined,
|
120
|
+
options?: { owner: Account | Group } | { fromRaw: RawCoList }
|
121
|
+
) {
|
122
|
+
super();
|
123
|
+
|
124
|
+
if (!options) {
|
125
|
+
throw new Error("Must provide options");
|
126
|
+
}
|
127
|
+
|
128
|
+
if (init && "owner" in options) {
|
129
|
+
this[InitValues] = {
|
130
|
+
init,
|
131
|
+
owner: options.owner,
|
132
|
+
};
|
133
|
+
} else if ("fromRaw" in options) {
|
134
|
+
Object.defineProperties(this, {
|
135
|
+
id: {
|
136
|
+
value: options.fromRaw.id,
|
137
|
+
enumerable: false,
|
138
|
+
},
|
139
|
+
_raw: { value: options.fromRaw, enumerable: false },
|
140
|
+
});
|
141
|
+
}
|
142
|
+
|
143
|
+
return new Proxy(this, CoListProxyHandler as ProxyHandler<this>);
|
144
|
+
}
|
145
|
+
|
146
|
+
push(...items: Item[]): number;
|
147
|
+
/** @private For exact type compatibility with Array superclass */
|
148
|
+
push(...items: Item[]): number;
|
149
|
+
push(...items: Item[]): number {
|
150
|
+
for (const item of toRawItems(
|
151
|
+
items as Item[],
|
152
|
+
this._schema[ItemsSym]
|
153
|
+
)) {
|
154
|
+
this._raw.append(item);
|
155
|
+
}
|
156
|
+
|
157
|
+
return this._raw.entries().length;
|
158
|
+
}
|
159
|
+
|
160
|
+
unshift(...items: Item[]): number;
|
161
|
+
/** @private For exact type compatibility with Array superclass */
|
162
|
+
unshift(...items: Item[]): number;
|
163
|
+
unshift(...items: Item[]): number {
|
164
|
+
for (const item of toRawItems(
|
165
|
+
items as Item[],
|
166
|
+
this._schema[ItemsSym]
|
167
|
+
)) {
|
168
|
+
this._raw.prepend(item);
|
169
|
+
}
|
170
|
+
|
171
|
+
return this._raw.entries().length;
|
172
|
+
}
|
173
|
+
|
174
|
+
pop(): Item | undefined {
|
175
|
+
const last = this[this.length - 1];
|
176
|
+
|
177
|
+
this._raw.delete(this.length - 1);
|
178
|
+
|
179
|
+
return last;
|
180
|
+
}
|
181
|
+
|
182
|
+
shift(): Item | undefined {
|
183
|
+
const first = this[0];
|
184
|
+
|
185
|
+
this._raw.delete(0);
|
186
|
+
|
187
|
+
return first;
|
188
|
+
}
|
189
|
+
|
190
|
+
splice(start: number, deleteCount: number, ...items: Item[]): Item[];
|
191
|
+
splice(start: number, deleteCount: number, ...items: Item[]): Item[];
|
192
|
+
splice(start: number, deleteCount: number, ...items: Item[]): Item[] {
|
193
|
+
const deleted = this.slice(start, start + deleteCount);
|
194
|
+
|
195
|
+
for (
|
196
|
+
let idxToDelete = start + deleteCount;
|
197
|
+
idxToDelete > start;
|
198
|
+
idxToDelete--
|
199
|
+
) {
|
200
|
+
this._raw.delete(idxToDelete);
|
201
|
+
}
|
202
|
+
|
203
|
+
let appendAfter = start;
|
204
|
+
for (const item of toRawItems(
|
205
|
+
items as Item[],
|
206
|
+
this._schema[ItemsSym]
|
207
|
+
)) {
|
208
|
+
this._raw.append(item, appendAfter);
|
209
|
+
appendAfter++;
|
210
|
+
}
|
211
|
+
|
212
|
+
return deleted;
|
213
|
+
}
|
214
|
+
|
215
|
+
toJSON() {
|
216
|
+
const itemDescriptor = this._schema[ItemsSym] as Schema;
|
217
|
+
if (itemDescriptor === "json") {
|
218
|
+
return this._raw.asArray();
|
219
|
+
} else if ("encoded" in itemDescriptor) {
|
220
|
+
return this._raw
|
221
|
+
.asArray()
|
222
|
+
.map((e) => EffectSchema.encodeSync(itemDescriptor.encoded)(e));
|
223
|
+
} else if (isRefEncoded(itemDescriptor)) {
|
224
|
+
return this.map((item) => (item as unknown as CoValue)?.toJSON());
|
225
|
+
} else {
|
226
|
+
return [];
|
227
|
+
}
|
228
|
+
}
|
229
|
+
|
230
|
+
[inspect]() {
|
231
|
+
return this.toJSON();
|
232
|
+
}
|
233
|
+
|
234
|
+
subscribe!: (listener: (update: this) => void) => () => void;
|
235
|
+
static {
|
236
|
+
this.prototype.subscribe = CoValueBase.prototype.subscribe as any;
|
237
|
+
}
|
238
|
+
|
239
|
+
subscribeEf!: () => Stream.Stream<this, "unavailable", never>;
|
240
|
+
static {
|
241
|
+
this.prototype.subscribeEf = CoValueBase.prototype.subscribeEf as any;
|
242
|
+
}
|
243
|
+
|
244
|
+
static fromRaw<V extends CoList>(
|
245
|
+
this: SubclassedConstructor<V> & typeof CoList,
|
246
|
+
raw: RawCoList
|
247
|
+
) {
|
248
|
+
return new this(undefined, { fromRaw: raw });
|
249
|
+
}
|
250
|
+
|
251
|
+
static loadEf = CoValueBase.loadEf as unknown as <V extends CoValue>(
|
252
|
+
this: SubclassedConstructor<V>,
|
253
|
+
id: ID<V>
|
254
|
+
) => Effect.Effect<V, UnavailableError, AccountCtx>;
|
255
|
+
static load = CoValueBase.load as unknown as <V extends CoValue>(
|
256
|
+
this: SubclassedConstructor<V>,
|
257
|
+
id: ID<V>,
|
258
|
+
options: { as: Account | Group }
|
259
|
+
) => Promise<V | undefined>;
|
260
|
+
static subscribeEf = CoValueBase.subscribeEf as unknown as <
|
261
|
+
V extends CoValue,
|
262
|
+
>(
|
263
|
+
this: SubclassedConstructor<V>,
|
264
|
+
id: ID<V>
|
265
|
+
) => Stream.Stream<V, UnavailableError, AccountCtx>;
|
266
|
+
static subscribe = CoValueBase.subscribe as unknown as <V extends CoValue>(
|
267
|
+
this: SubclassedConstructor<V>,
|
268
|
+
id: ID<V>,
|
269
|
+
options: { as: Account | Group },
|
270
|
+
onUpdate: (value: V) => void
|
271
|
+
) => () => void;
|
272
|
+
|
273
|
+
static schema<V extends CoList>(
|
274
|
+
this: { new (...args: any): V } & typeof CoList,
|
275
|
+
def: { [ItemsSym]: V["_schema"][ItemsSym] }
|
276
|
+
) {
|
277
|
+
this._schema ||= {};
|
278
|
+
Object.assign(this._schema, def);
|
279
|
+
}
|
280
|
+
}
|
281
|
+
|
282
|
+
function toRawItems<Item>(items: Item[], itemDescriptor: Schema) {
|
283
|
+
const rawItems =
|
284
|
+
itemDescriptor === "json"
|
285
|
+
? items
|
286
|
+
: "encoded" in itemDescriptor
|
287
|
+
? items?.map((e) => EffectSchema.encodeSync(itemDescriptor.encoded)(e))
|
288
|
+
: isRefEncoded(itemDescriptor)
|
289
|
+
? items?.map((v) => (v as unknown as CoValue).id)
|
290
|
+
: (() => {
|
291
|
+
throw new Error("Invalid element descriptor");
|
292
|
+
})();
|
293
|
+
return rawItems;
|
294
|
+
}
|
295
|
+
|
296
|
+
function init(list: CoList) {
|
297
|
+
if (list[InitValues]) {
|
298
|
+
const { init, owner } = list[InitValues];
|
299
|
+
const raw = owner._raw.createList(
|
300
|
+
toRawItems(init, list._schema[ItemsSym])
|
301
|
+
);
|
302
|
+
|
303
|
+
Object.defineProperties(list, {
|
304
|
+
id: {
|
305
|
+
value: raw.id,
|
306
|
+
enumerable: false,
|
307
|
+
},
|
308
|
+
_raw: { value: raw, enumerable: false },
|
309
|
+
});
|
310
|
+
delete list[InitValues];
|
311
|
+
}
|
312
|
+
}
|
313
|
+
|
314
|
+
const CoListProxyHandler: ProxyHandler<CoList> = {
|
315
|
+
get(target, key, receiver) {
|
316
|
+
if (typeof key === "string" && !isNaN(+key)) {
|
317
|
+
const itemDescriptor = target._schema[ItemsSym] as Schema;
|
318
|
+
const rawValue = target._raw.get(Number(key));
|
319
|
+
if (itemDescriptor === "json") {
|
320
|
+
return rawValue;
|
321
|
+
} else if ("encoded" in itemDescriptor) {
|
322
|
+
return rawValue === undefined
|
323
|
+
? undefined
|
324
|
+
: EffectSchema.decodeSync(itemDescriptor.encoded)(rawValue);
|
325
|
+
} else if (isRefEncoded(itemDescriptor)) {
|
326
|
+
return rawValue === undefined
|
327
|
+
? undefined
|
328
|
+
: new Ref(
|
329
|
+
rawValue as unknown as ID<CoValue>,
|
330
|
+
target._loadedAs,
|
331
|
+
itemDescriptor
|
332
|
+
).accessFrom(receiver);
|
333
|
+
}
|
334
|
+
} else if (key === "length") {
|
335
|
+
return target._raw.entries().length;
|
336
|
+
} else {
|
337
|
+
return Reflect.get(target, key, receiver);
|
338
|
+
}
|
339
|
+
},
|
340
|
+
set(target, key, value, receiver) {
|
341
|
+
if (
|
342
|
+
key === ItemsSym &&
|
343
|
+
typeof value === "object" &&
|
344
|
+
SchemaInit in value
|
345
|
+
) {
|
346
|
+
(target.constructor as typeof CoList)._schema ||= {};
|
347
|
+
(target.constructor as typeof CoList)._schema[ItemsSym] =
|
348
|
+
value[SchemaInit];
|
349
|
+
init(target);
|
350
|
+
return true;
|
351
|
+
}
|
352
|
+
if (typeof key === "string" && !isNaN(+key)) {
|
353
|
+
const itemDescriptor = target._schema[ItemsSym] as Schema;
|
354
|
+
let rawValue;
|
355
|
+
if (itemDescriptor === "json") {
|
356
|
+
rawValue = value;
|
357
|
+
} else if ("encoded" in itemDescriptor) {
|
358
|
+
rawValue = EffectSchema.encodeSync(itemDescriptor.encoded)(value);
|
359
|
+
} else if (isRefEncoded(itemDescriptor)) {
|
360
|
+
rawValue = value.id;
|
361
|
+
}
|
362
|
+
target._raw.replace(Number(key), rawValue);
|
363
|
+
return true;
|
364
|
+
} else {
|
365
|
+
return Reflect.set(target, key, value, receiver);
|
366
|
+
}
|
367
|
+
},
|
368
|
+
defineProperty(target, key, descriptor) {
|
369
|
+
if (
|
370
|
+
descriptor.value &&
|
371
|
+
key === ItemsSym &&
|
372
|
+
typeof descriptor.value === "object" &&
|
373
|
+
SchemaInit in descriptor.value
|
374
|
+
) {
|
375
|
+
(target.constructor as typeof CoList)._schema ||= {};
|
376
|
+
(target.constructor as typeof CoList)._schema[ItemsSym] =
|
377
|
+
descriptor.value[SchemaInit];
|
378
|
+
init(target);
|
379
|
+
return true;
|
380
|
+
} else {
|
381
|
+
return Reflect.defineProperty(target, key, descriptor);
|
382
|
+
}
|
383
|
+
},
|
384
|
+
has(target, key) {
|
385
|
+
if (typeof key === "string" && !isNaN(+key)) {
|
386
|
+
return Number(key) < target._raw.entries().length;
|
387
|
+
} else {
|
388
|
+
return Reflect.has(target, key);
|
389
|
+
}
|
390
|
+
},
|
391
|
+
};
|