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,320 @@
|
|
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
|
+
EnsureItemNullable,
|
8
|
+
FieldDescriptor,
|
9
|
+
FieldDescriptorFor,
|
10
|
+
ID,
|
11
|
+
RefField,
|
12
|
+
SubclassedConstructor,
|
13
|
+
UnavailableError,
|
14
|
+
} from "../internal.js";
|
15
|
+
import {
|
16
|
+
Account,
|
17
|
+
CoValueBase,
|
18
|
+
Group,
|
19
|
+
ValueRef,
|
20
|
+
inspect,
|
21
|
+
makeRefs,
|
22
|
+
} from "../internal.js";
|
23
|
+
import { Schema } from "@effect/schema";
|
24
|
+
|
25
|
+
export class CoList<Item extends EnsureItemNullable<Item, 'Co.List'> = any>
|
26
|
+
extends Array<Item>
|
27
|
+
implements CoValue<"CoList", RawCoList>
|
28
|
+
{
|
29
|
+
id!: ID<this>;
|
30
|
+
_type!: "CoList";
|
31
|
+
static {
|
32
|
+
this.prototype._type = "CoList";
|
33
|
+
}
|
34
|
+
_raw!: RawCoList;
|
35
|
+
|
36
|
+
static _encoding: any;
|
37
|
+
get _encoding(): {
|
38
|
+
_item: FieldDescriptorFor<Item>;
|
39
|
+
} {
|
40
|
+
return (this.constructor as typeof CoList)._encoding;
|
41
|
+
}
|
42
|
+
|
43
|
+
get _owner(): Account | Group {
|
44
|
+
return this._raw.group instanceof RawAccount
|
45
|
+
? Account.fromRaw(this._raw.group)
|
46
|
+
: Group.fromRaw(this._raw.group);
|
47
|
+
}
|
48
|
+
|
49
|
+
get _refs(): {
|
50
|
+
[idx: number]: NonNullable<Item> extends CoValue
|
51
|
+
? ValueRef<NonNullable<Item>>
|
52
|
+
: never;
|
53
|
+
} {
|
54
|
+
return makeRefs<number>(
|
55
|
+
(idx) => this._raw.get(idx) as unknown as ID<CoValue>,
|
56
|
+
() =>
|
57
|
+
Array.from(
|
58
|
+
{ length: this._raw.entries().length },
|
59
|
+
(_, idx) => idx
|
60
|
+
),
|
61
|
+
this._loadedAs,
|
62
|
+
(_idx) => (this._encoding._item as RefField<CoValue>).ref()
|
63
|
+
) as any;
|
64
|
+
}
|
65
|
+
|
66
|
+
get _edits(): {
|
67
|
+
[idx: number]: {
|
68
|
+
value?: Item;
|
69
|
+
ref?: Item extends CoValue ? ValueRef<Item> : never;
|
70
|
+
by?: Account;
|
71
|
+
madeAt: Date;
|
72
|
+
};
|
73
|
+
} {
|
74
|
+
throw new Error("Not implemented");
|
75
|
+
}
|
76
|
+
|
77
|
+
get _loadedAs() {
|
78
|
+
return Account.fromNode(this._raw.core.node);
|
79
|
+
}
|
80
|
+
|
81
|
+
constructor(_init: undefined, options: { fromRaw: RawCoList });
|
82
|
+
constructor(init: Item[], options: { owner: Account | Group });
|
83
|
+
constructor(init: number);
|
84
|
+
constructor(
|
85
|
+
init: Item[] | undefined | number,
|
86
|
+
options?: { owner: Account | Group } | { fromRaw: RawCoList }
|
87
|
+
) {
|
88
|
+
if (typeof init === "number") {
|
89
|
+
// this might be called from an intrinsic, like map, trying to create an empty array
|
90
|
+
// passing `0` as the only parameter
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
92
|
+
return new Array(init) as any;
|
93
|
+
}
|
94
|
+
|
95
|
+
super();
|
96
|
+
|
97
|
+
if (!options) {
|
98
|
+
throw new Error("Must provide options");
|
99
|
+
}
|
100
|
+
|
101
|
+
let raw: RawCoList;
|
102
|
+
|
103
|
+
if ("fromRaw" in options) {
|
104
|
+
raw = options.fromRaw;
|
105
|
+
} else {
|
106
|
+
const rawOwner = options.owner._raw;
|
107
|
+
|
108
|
+
const rawInit = init && this.toRawItems(init);
|
109
|
+
|
110
|
+
raw = rawOwner.createList(rawInit);
|
111
|
+
}
|
112
|
+
|
113
|
+
Object.defineProperties(this, {
|
114
|
+
id: {
|
115
|
+
value: raw.id,
|
116
|
+
enumerable: false,
|
117
|
+
},
|
118
|
+
_raw: { value: raw, enumerable: false },
|
119
|
+
});
|
120
|
+
|
121
|
+
return new Proxy(this, CoListProxyHandler<Item>(this._encoding._item));
|
122
|
+
}
|
123
|
+
|
124
|
+
private toRawItems(items: Item[]) {
|
125
|
+
const itemDescriptor = this._encoding._item as FieldDescriptor;
|
126
|
+
const rawItems =
|
127
|
+
itemDescriptor === "json"
|
128
|
+
? items
|
129
|
+
: "encoded" in itemDescriptor
|
130
|
+
? items?.map((e) =>
|
131
|
+
Schema.encodeSync(itemDescriptor.encoded)(e)
|
132
|
+
)
|
133
|
+
: "ref" in itemDescriptor
|
134
|
+
? items?.map((v) => (v as unknown as CoValue).id)
|
135
|
+
: (() => {
|
136
|
+
throw new Error("Invalid element descriptor");
|
137
|
+
})();
|
138
|
+
return rawItems;
|
139
|
+
}
|
140
|
+
|
141
|
+
push(...items: Item[]): number;
|
142
|
+
/** @private For exact type compatibility with Array superclass */
|
143
|
+
push(...items: Item[]): number;
|
144
|
+
push(...items: Item[]): number {
|
145
|
+
for (const item of this.toRawItems(items as Item[])) {
|
146
|
+
this._raw.append(item);
|
147
|
+
}
|
148
|
+
|
149
|
+
return this._raw.entries().length;
|
150
|
+
}
|
151
|
+
|
152
|
+
unshift(...items: Item[]): number;
|
153
|
+
/** @private For exact type compatibility with Array superclass */
|
154
|
+
unshift(...items: Item[]): number;
|
155
|
+
unshift(...items: Item[]): number {
|
156
|
+
for (const item of this.toRawItems(items as Item[])) {
|
157
|
+
this._raw.prepend(item);
|
158
|
+
}
|
159
|
+
|
160
|
+
return this._raw.entries().length;
|
161
|
+
}
|
162
|
+
|
163
|
+
pop(): Item | undefined {
|
164
|
+
const last = this[this.length - 1];
|
165
|
+
|
166
|
+
this._raw.delete(this.length - 1);
|
167
|
+
|
168
|
+
return last;
|
169
|
+
}
|
170
|
+
|
171
|
+
shift(): Item | undefined {
|
172
|
+
const first = this[0];
|
173
|
+
|
174
|
+
this._raw.delete(0);
|
175
|
+
|
176
|
+
return first;
|
177
|
+
}
|
178
|
+
|
179
|
+
splice(start: number, deleteCount: number, ...items: Item[]): Item[];
|
180
|
+
splice(start: number, deleteCount: number, ...items: Item[]): Item[];
|
181
|
+
splice(start: number, deleteCount: number, ...items: Item[]): Item[] {
|
182
|
+
const deleted = this.slice(start, start + deleteCount);
|
183
|
+
|
184
|
+
for (
|
185
|
+
let idxToDelete = start + deleteCount;
|
186
|
+
idxToDelete > start;
|
187
|
+
idxToDelete--
|
188
|
+
) {
|
189
|
+
this._raw.delete(idxToDelete);
|
190
|
+
}
|
191
|
+
|
192
|
+
let appendAfter = start;
|
193
|
+
for (const item of this.toRawItems(items as Item[])) {
|
194
|
+
this._raw.append(item, appendAfter);
|
195
|
+
appendAfter++;
|
196
|
+
}
|
197
|
+
|
198
|
+
return deleted;
|
199
|
+
}
|
200
|
+
|
201
|
+
toJSON() {
|
202
|
+
const itemDescriptor = this._encoding._item as FieldDescriptor;
|
203
|
+
if (itemDescriptor === "json") {
|
204
|
+
return this._raw.asArray();
|
205
|
+
} else if ("encoded" in itemDescriptor) {
|
206
|
+
return this._raw
|
207
|
+
.asArray()
|
208
|
+
.map((e) => Schema.encodeSync(itemDescriptor.encoded)(e));
|
209
|
+
} else if ("ref" in itemDescriptor) {
|
210
|
+
return this.map((item) => (item as unknown as CoValue)?.toJSON());
|
211
|
+
} else {
|
212
|
+
return [];
|
213
|
+
}
|
214
|
+
}
|
215
|
+
|
216
|
+
[inspect]() {
|
217
|
+
return this.toJSON();
|
218
|
+
}
|
219
|
+
|
220
|
+
subscribe!: (listener: (update: this) => void) => () => void;
|
221
|
+
static {
|
222
|
+
this.prototype.subscribe = CoValueBase.prototype.subscribe as any;
|
223
|
+
}
|
224
|
+
|
225
|
+
subscribeEf!: () => Stream.Stream<this, "unavailable", never>;
|
226
|
+
static {
|
227
|
+
this.prototype.subscribeEf = CoValueBase.prototype.subscribeEf as any;
|
228
|
+
}
|
229
|
+
|
230
|
+
static fromRaw<V extends CoList>(
|
231
|
+
this: SubclassedConstructor<V> & typeof CoList,
|
232
|
+
raw: RawCoList
|
233
|
+
) {
|
234
|
+
return new this(undefined, { fromRaw: raw });
|
235
|
+
}
|
236
|
+
|
237
|
+
static loadEf = CoValueBase.loadEf as unknown as <V extends CoValue>(
|
238
|
+
this: SubclassedConstructor<V>,
|
239
|
+
id: ID<V>
|
240
|
+
) => Effect.Effect<V, UnavailableError, AccountCtx>;
|
241
|
+
static load = CoValueBase.load as unknown as <V extends CoValue>(
|
242
|
+
this: SubclassedConstructor<V>,
|
243
|
+
id: ID<V>,
|
244
|
+
options: { as: Account | Group }
|
245
|
+
) => Promise<V | undefined>;
|
246
|
+
static subscribeEf = CoValueBase.subscribeEf as unknown as <
|
247
|
+
V extends CoValue,
|
248
|
+
>(
|
249
|
+
this: SubclassedConstructor<V>,
|
250
|
+
id: ID<V>
|
251
|
+
) => Stream.Stream<V, UnavailableError, AccountCtx>;
|
252
|
+
static subscribe = CoValueBase.subscribe as unknown as <V extends CoValue>(
|
253
|
+
this: SubclassedConstructor<V>,
|
254
|
+
id: ID<V>,
|
255
|
+
options: { as: Account | Group },
|
256
|
+
onUpdate: (value: V) => void
|
257
|
+
) => () => void;
|
258
|
+
|
259
|
+
static encoding<V extends CoList>(
|
260
|
+
this: { new (...args: any): V } & typeof CoList,
|
261
|
+
def: { _item: V["_encoding"]["_item"] }
|
262
|
+
) {
|
263
|
+
this._encoding ||= {};
|
264
|
+
Object.assign(this._encoding, def);
|
265
|
+
}
|
266
|
+
}
|
267
|
+
|
268
|
+
function CoListProxyHandler<Item extends EnsureItemNullable<Item, "Co.List">>(
|
269
|
+
itemDescriptor: FieldDescriptor
|
270
|
+
): ProxyHandler<CoList<Item>> {
|
271
|
+
return {
|
272
|
+
get(target, key, receiver) {
|
273
|
+
if (typeof key === "string" && !isNaN(+key)) {
|
274
|
+
const rawValue = target._raw.get(Number(key));
|
275
|
+
if (itemDescriptor === "json") {
|
276
|
+
return rawValue;
|
277
|
+
} else if ("encoded" in itemDescriptor) {
|
278
|
+
return rawValue === undefined
|
279
|
+
? undefined
|
280
|
+
: Schema.decodeSync(itemDescriptor.encoded)(rawValue);
|
281
|
+
} else if ("ref" in itemDescriptor) {
|
282
|
+
return rawValue === undefined
|
283
|
+
? undefined
|
284
|
+
: new ValueRef(
|
285
|
+
rawValue as unknown as ID<CoValue>,
|
286
|
+
target._loadedAs,
|
287
|
+
itemDescriptor.ref()
|
288
|
+
).accessFrom(receiver);
|
289
|
+
}
|
290
|
+
} else if (key === "length") {
|
291
|
+
return target._raw.entries().length;
|
292
|
+
} else {
|
293
|
+
return Reflect.get(target, key, receiver);
|
294
|
+
}
|
295
|
+
},
|
296
|
+
set(target, key, value, receiver) {
|
297
|
+
if (typeof key === "string" && !isNaN(+key)) {
|
298
|
+
let rawValue;
|
299
|
+
if (itemDescriptor === "json") {
|
300
|
+
rawValue = value;
|
301
|
+
} else if ("encoded" in itemDescriptor) {
|
302
|
+
rawValue = Schema.encodeSync(itemDescriptor.encoded)(value);
|
303
|
+
} else if ("ref" in itemDescriptor) {
|
304
|
+
rawValue = value.id;
|
305
|
+
}
|
306
|
+
target._raw.replace(Number(key), rawValue);
|
307
|
+
return true;
|
308
|
+
} else {
|
309
|
+
return Reflect.set(target, key, value, receiver);
|
310
|
+
}
|
311
|
+
},
|
312
|
+
has(target, key) {
|
313
|
+
if (typeof key === "string" && !isNaN(+key)) {
|
314
|
+
return Number(key) < target._raw.entries().length;
|
315
|
+
} else {
|
316
|
+
return Reflect.has(target, key);
|
317
|
+
}
|
318
|
+
},
|
319
|
+
};
|
320
|
+
}
|