jazz-tools 0.7.0-alpha.36 → 0.7.0-alpha.38
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc.cjs +3 -10
- package/.prettierrc.js +9 -0
- package/.turbo/turbo-build.log +3 -91
- package/.turbo/turbo-lint.log +4 -0
- package/.turbo/turbo-test.log +140 -0
- package/CHANGELOG.md +19 -0
- package/README.md +10 -2
- package/dist/coValues/account.js +22 -12
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/coList.js +7 -9
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/coMap.js +11 -3
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coStream.js +15 -8
- package/dist/coValues/coStream.js.map +1 -1
- package/dist/coValues/deepLoading.js +57 -0
- package/dist/coValues/deepLoading.js.map +1 -0
- package/dist/coValues/extensions/imageDef.js +1 -1
- package/dist/coValues/extensions/imageDef.js.map +1 -1
- package/dist/coValues/group.js +1 -0
- package/dist/coValues/group.js.map +1 -1
- package/dist/coValues/interfaces.js +34 -28
- package/dist/coValues/interfaces.js.map +1 -1
- package/dist/implementation/devtoolsFormatters.js +1 -0
- package/dist/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/implementation/refs.js +4 -2
- package/dist/implementation/refs.js.map +1 -1
- package/dist/implementation/schema.js +5 -1
- package/dist/implementation/schema.js.map +1 -1
- package/dist/implementation/subscriptionScope.js +9 -4
- package/dist/implementation/subscriptionScope.js.map +1 -1
- package/dist/implementation/symbols.js.map +1 -1
- package/dist/index.js +2 -4
- package/dist/index.js.map +1 -1
- package/dist/internal.js +2 -1
- package/dist/internal.js.map +1 -1
- package/dist/tests/coList.test.js +20 -17
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +27 -17
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +39 -24
- package/dist/tests/coStream.test.js.map +1 -1
- package/dist/tests/deepLoading.test.js +188 -0
- package/dist/tests/deepLoading.test.js.map +1 -0
- package/dist/tests/groupsAndAccounts.test.js +6 -11
- package/dist/tests/groupsAndAccounts.test.js.map +1 -1
- package/package.json +12 -6
- package/src/coValues/account.ts +60 -58
- package/src/coValues/coList.ts +31 -51
- package/src/coValues/coMap.ts +27 -21
- package/src/coValues/coStream.ts +44 -32
- package/src/coValues/deepLoading.ts +215 -0
- package/src/coValues/extensions/imageDef.ts +3 -3
- package/src/coValues/group.ts +11 -13
- package/src/coValues/interfaces.ts +151 -93
- package/src/implementation/devtoolsFormatters.ts +1 -0
- package/src/implementation/inspect.ts +1 -1
- package/src/implementation/refs.ts +29 -15
- package/src/implementation/schema.ts +21 -6
- package/src/implementation/subscriptionScope.ts +33 -17
- package/src/implementation/symbols.ts +0 -1
- package/src/index.ts +5 -3
- package/src/internal.ts +3 -2
- package/src/tests/coList.test.ts +33 -28
- package/src/tests/coMap.test.ts +63 -55
- package/src/tests/coStream.test.ts +82 -67
- package/src/tests/deepLoading.test.ts +301 -0
- package/src/tests/groupsAndAccounts.test.ts +9 -18
package/src/coValues/coList.ts
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
import type { RawCoList } from "cojson";
|
2
2
|
import { RawAccount } from "cojson";
|
3
|
-
import type { Effect, Stream } from "effect";
|
4
3
|
import type {
|
5
|
-
AccountCtx,
|
6
4
|
CoValue,
|
7
5
|
Schema,
|
8
6
|
SchemaFor,
|
9
7
|
ID,
|
10
8
|
RefEncoded,
|
11
9
|
ClassOf,
|
12
|
-
UnavailableError,
|
13
10
|
UnCo,
|
14
|
-
|
11
|
+
CoValueClass,
|
12
|
+
} from "../internal.js";
|
15
13
|
import {
|
16
14
|
Account,
|
17
15
|
CoValueBase,
|
@@ -28,6 +26,7 @@ import {
|
|
28
26
|
import { encodeSync, decodeSync } from "@effect/schema/Schema";
|
29
27
|
|
30
28
|
/** @category CoValues */
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
31
30
|
export class CoList<Item = any>
|
32
31
|
extends Array<Item>
|
33
32
|
implements CoValue<"CoList", RawCoList>
|
@@ -54,6 +53,7 @@ export class CoList<Item = any>
|
|
54
53
|
|
55
54
|
/** @internal This is only a marker type and doesn't exist at runtime */
|
56
55
|
[ItemsSym]!: Item;
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
57
57
|
static _schema: any;
|
58
58
|
get _schema(): {
|
59
59
|
[ItemsSym]: SchemaFor<Item>;
|
@@ -69,13 +69,15 @@ export class CoList<Item = any>
|
|
69
69
|
|
70
70
|
/** @category Content */
|
71
71
|
get _refs(): {
|
72
|
-
[idx: number]:
|
73
|
-
? Ref<
|
72
|
+
[idx: number]: Exclude<Item, null> extends CoValue
|
73
|
+
? Ref<UnCo<Exclude<Item, null>>>
|
74
74
|
: never;
|
75
75
|
} & {
|
76
76
|
length: number;
|
77
77
|
[Symbol.iterator](): IterableIterator<
|
78
|
-
|
78
|
+
Exclude<Item, null> extends CoValue
|
79
|
+
? Ref<Exclude<Item, null>>
|
80
|
+
: never
|
79
81
|
>;
|
80
82
|
} {
|
81
83
|
return makeRefs<number>(
|
@@ -83,10 +85,11 @@ export class CoList<Item = any>
|
|
83
85
|
() =>
|
84
86
|
Array.from(
|
85
87
|
{ length: this._raw.entries().length },
|
86
|
-
(_, idx) => idx
|
88
|
+
(_, idx) => idx,
|
87
89
|
),
|
88
90
|
this._loadedAs,
|
89
|
-
(_idx) => this._schema[ItemsSym] as RefEncoded<CoValue
|
91
|
+
(_idx) => this._schema[ItemsSym] as RefEncoded<CoValue>,
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
90
93
|
) as any;
|
91
94
|
}
|
92
95
|
|
@@ -105,6 +108,7 @@ export class CoList<Item = any>
|
|
105
108
|
return Account.fromNode(this._raw.core.node);
|
106
109
|
}
|
107
110
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
108
112
|
[InitValues]?: any;
|
109
113
|
|
110
114
|
static get [Symbol.species]() {
|
@@ -114,11 +118,14 @@ export class CoList<Item = any>
|
|
114
118
|
constructor(
|
115
119
|
options:
|
116
120
|
| { init: Item[]; owner: Account | Group }
|
117
|
-
| { fromRaw: RawCoList }
|
121
|
+
| { fromRaw: RawCoList },
|
118
122
|
) {
|
119
123
|
super();
|
120
124
|
|
121
|
-
Object.defineProperty(this, "_instanceID", {
|
125
|
+
Object.defineProperty(this, "_instanceID", {
|
126
|
+
value: `instance-${Math.random().toString(36).slice(2)}`,
|
127
|
+
enumerable: false,
|
128
|
+
});
|
122
129
|
|
123
130
|
if ("owner" in options) {
|
124
131
|
this[InitValues] = {
|
@@ -141,7 +148,7 @@ export class CoList<Item = any>
|
|
141
148
|
static create<L extends CoList>(
|
142
149
|
this: ClassOf<L>,
|
143
150
|
items: UnCo<L[number]>[],
|
144
|
-
options: {owner: Account | Group}
|
151
|
+
options: { owner: Account | Group },
|
145
152
|
) {
|
146
153
|
return new this({ init: items, owner: options.owner });
|
147
154
|
}
|
@@ -152,7 +159,7 @@ export class CoList<Item = any>
|
|
152
159
|
push(...items: Item[]): number {
|
153
160
|
for (const item of toRawItems(
|
154
161
|
items as Item[],
|
155
|
-
this._schema[ItemsSym]
|
162
|
+
this._schema[ItemsSym],
|
156
163
|
)) {
|
157
164
|
this._raw.append(item);
|
158
165
|
}
|
@@ -166,7 +173,7 @@ export class CoList<Item = any>
|
|
166
173
|
unshift(...items: Item[]): number {
|
167
174
|
for (const item of toRawItems(
|
168
175
|
items as Item[],
|
169
|
-
this._schema[ItemsSym]
|
176
|
+
this._schema[ItemsSym],
|
170
177
|
)) {
|
171
178
|
this._raw.prepend(item);
|
172
179
|
}
|
@@ -204,7 +211,7 @@ export class CoList<Item = any>
|
|
204
211
|
let appendAfter = Math.max(start - 1, 0);
|
205
212
|
for (const item of toRawItems(
|
206
213
|
items as Item[],
|
207
|
-
this._schema[ItemsSym]
|
214
|
+
this._schema[ItemsSym],
|
208
215
|
)) {
|
209
216
|
console.log(this._raw.asArray(), appendAfter);
|
210
217
|
this._raw.append(item, appendAfter);
|
@@ -233,49 +240,22 @@ export class CoList<Item = any>
|
|
233
240
|
return this.toJSON();
|
234
241
|
}
|
235
242
|
|
236
|
-
subscribe!: (listener: (update: this) => void, options?: RequireOptions<this>) => () => void;
|
237
|
-
static {
|
238
|
-
this.prototype.subscribe = CoValueBase.prototype.subscribe as any;
|
239
|
-
}
|
240
|
-
|
241
|
-
subscribeEf!: (options?: RequireOptions<this>) => Stream.Stream<this, "unavailable", never>;
|
242
|
-
static {
|
243
|
-
this.prototype.subscribeEf = CoValueBase.prototype.subscribeEf as any;
|
244
|
-
}
|
245
|
-
|
246
243
|
static fromRaw<V extends CoList>(
|
247
244
|
this: ClassOf<V> & typeof CoList,
|
248
|
-
raw: RawCoList
|
245
|
+
raw: RawCoList,
|
249
246
|
) {
|
250
247
|
return new this({ fromRaw: raw });
|
251
248
|
}
|
252
249
|
|
253
|
-
static
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
static load = CoValueBase.load as unknown as <V extends CoValue>(
|
258
|
-
this: ClassOf<V>,
|
259
|
-
id: ID<V>,
|
260
|
-
options: { as: Account | Group }
|
261
|
-
) => Promise<V | undefined>;
|
262
|
-
static subscribeEf = CoValueBase.subscribeEf as unknown as <
|
263
|
-
V extends CoValue,
|
264
|
-
>(
|
265
|
-
this: ClassOf<V>,
|
266
|
-
id: ID<V>,
|
267
|
-
options?: { require?: (value: V) => boolean | undefined }
|
268
|
-
) => Stream.Stream<V, UnavailableError, AccountCtx>;
|
269
|
-
static subscribe = CoValueBase.subscribe as unknown as <V extends CoValue>(
|
270
|
-
this: ClassOf<V>,
|
271
|
-
id: ID<V>,
|
272
|
-
options: { as: Account | Group, require?: (value: V) => boolean | undefined },
|
273
|
-
onUpdate: (value: V) => void
|
274
|
-
) => () => void;
|
250
|
+
static load = CoValueBase.load as CoValueClass['load'];
|
251
|
+
static loadEf = CoValueBase.loadEf as CoValueClass['loadEf'];
|
252
|
+
static subscribe = CoValueBase.subscribe as CoValueClass['subscribe'];
|
253
|
+
static subscribeEf = CoValueBase.subscribeEf as CoValueClass['subscribeEf'];
|
275
254
|
|
276
255
|
static schema<V extends CoList>(
|
256
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
277
257
|
this: { new (...args: any): V } & typeof CoList,
|
278
|
-
def: { [ItemsSym]: V["_schema"][ItemsSym] }
|
258
|
+
def: { [ItemsSym]: V["_schema"][ItemsSym] },
|
279
259
|
) {
|
280
260
|
this._schema ||= {};
|
281
261
|
Object.assign(this._schema, def);
|
@@ -300,7 +280,7 @@ function init(list: CoList) {
|
|
300
280
|
if (list[InitValues]) {
|
301
281
|
const { init, owner } = list[InitValues];
|
302
282
|
const raw = owner._raw.createList(
|
303
|
-
toRawItems(init, list._schema[ItemsSym])
|
283
|
+
toRawItems(init, list._schema[ItemsSym]),
|
304
284
|
);
|
305
285
|
|
306
286
|
Object.defineProperties(list, {
|
@@ -331,7 +311,7 @@ const CoListProxyHandler: ProxyHandler<CoList> = {
|
|
331
311
|
: new Ref(
|
332
312
|
rawValue as unknown as ID<CoValue>,
|
333
313
|
target._loadedAs,
|
334
|
-
itemDescriptor
|
314
|
+
itemDescriptor,
|
335
315
|
).accessFrom(receiver, Number(key));
|
336
316
|
}
|
337
317
|
} else if (key === "length") {
|
package/src/coValues/coMap.ts
CHANGED
@@ -80,6 +80,7 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
80
80
|
declare _raw: RawCoMap;
|
81
81
|
|
82
82
|
/** @internal */
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
83
84
|
static _schema: any;
|
84
85
|
/** @internal */
|
85
86
|
get _schema() {
|
@@ -120,7 +121,8 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
120
121
|
this._loadedAs,
|
121
122
|
(key) =>
|
122
123
|
(this._schema[key] ||
|
123
|
-
this._schema[ItemsSym]) as RefEncoded<CoValue
|
124
|
+
this._schema[ItemsSym]) as RefEncoded<CoValue>,
|
125
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
124
126
|
) as any;
|
125
127
|
}
|
126
128
|
|
@@ -144,17 +146,17 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
144
146
|
: new Ref(
|
145
147
|
rawEdit.value as ID<CoValue>,
|
146
148
|
target._loadedAs,
|
147
|
-
descriptor
|
149
|
+
descriptor,
|
148
150
|
).accessFrom(
|
149
151
|
target,
|
150
|
-
"_edits." + key.toString() + ".value"
|
152
|
+
"_edits." + key.toString() + ".value",
|
151
153
|
),
|
152
154
|
ref:
|
153
155
|
descriptor !== "json" && isRefEncoded(descriptor)
|
154
156
|
? new Ref(
|
155
157
|
rawEdit.value as ID<CoValue>,
|
156
158
|
target._loadedAs,
|
157
|
-
descriptor
|
159
|
+
descriptor,
|
158
160
|
)
|
159
161
|
: undefined,
|
160
162
|
by:
|
@@ -162,10 +164,10 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
162
164
|
new Ref(
|
163
165
|
rawEdit.by as ID<Account>,
|
164
166
|
target._loadedAs,
|
165
|
-
Account
|
167
|
+
Account,
|
166
168
|
).accessFrom(
|
167
169
|
target,
|
168
|
-
"_edits." + key.toString() + ".by"
|
170
|
+
"_edits." + key.toString() + ".by",
|
169
171
|
),
|
170
172
|
madeAt: rawEdit.at,
|
171
173
|
};
|
@@ -181,11 +183,13 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
181
183
|
}
|
182
184
|
|
183
185
|
/** @internal */
|
186
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
184
187
|
[InitValues]?: any;
|
185
188
|
|
186
189
|
/** @internal */
|
187
190
|
constructor(
|
188
|
-
|
191
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
192
|
+
options: { fromRaw: RawCoMap } | { init: any; owner: Account | Group },
|
189
193
|
) {
|
190
194
|
super();
|
191
195
|
|
@@ -213,7 +217,7 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
213
217
|
static create<M extends CoMap>(
|
214
218
|
this: ClassOf<M>,
|
215
219
|
init: Simplify<CoMapInit<M>>,
|
216
|
-
options: { owner: Account | Group }
|
220
|
+
options: { owner: Account | Group },
|
217
221
|
) {
|
218
222
|
return new this({ init, owner: options.owner });
|
219
223
|
}
|
@@ -227,6 +231,7 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
227
231
|
if (descriptor == "json" || "encode" in descriptor) {
|
228
232
|
return [key, this._raw.get(key)];
|
229
233
|
} else if (isRefEncoded(descriptor)) {
|
234
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
230
235
|
const jsonedRef = (this as any)[tKey]?.toJSON();
|
231
236
|
return [key, jsonedRef];
|
232
237
|
} else {
|
@@ -246,9 +251,10 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
246
251
|
}
|
247
252
|
|
248
253
|
/** @internal */
|
254
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
249
255
|
rawFromInit<Fields extends object = Record<string, any>>(
|
250
256
|
init: Simplify<CoMapInit<Fields>> | undefined,
|
251
|
-
owner: Account | Group
|
257
|
+
owner: Account | Group,
|
252
258
|
) {
|
253
259
|
const rawOwner = owner._raw;
|
254
260
|
|
@@ -272,7 +278,8 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
272
278
|
}
|
273
279
|
} else if ("encoded" in descriptor) {
|
274
280
|
rawInit[key] = encodeSync(descriptor.encoded)(
|
275
|
-
|
281
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
282
|
+
initValue as any,
|
276
283
|
);
|
277
284
|
}
|
278
285
|
}
|
@@ -293,30 +300,29 @@ export class CoMap extends CoValueBase implements CoValue<"CoMap", RawCoMap> {
|
|
293
300
|
}
|
294
301
|
}
|
295
302
|
|
296
|
-
export type CoKeys<
|
297
|
-
keyof
|
303
|
+
export type CoKeys<Map extends object> = Exclude<
|
304
|
+
keyof Map & string,
|
298
305
|
keyof CoMap
|
299
306
|
>;
|
300
307
|
|
301
|
-
export type CoMapInit<
|
302
|
-
[Key in CoKeys<
|
308
|
+
export type CoMapInit<Map extends object> = {
|
309
|
+
[Key in CoKeys<Map> as undefined extends Map[Key]
|
303
310
|
? never
|
304
|
-
:
|
305
|
-
|
306
|
-
: IfCo<Fields[Key], Key>]: Fields[Key];
|
307
|
-
} & { [Key in CoKeys<Fields> as IfCo<Fields[Key], Key>]?: Fields[Key] };
|
311
|
+
: IfCo<Map[Key], Key>]: Map[Key];
|
312
|
+
} & { [Key in CoKeys<Map> as IfCo<Map[Key], Key>]?: Map[Key] };
|
308
313
|
|
309
314
|
function tryInit(map: CoMap) {
|
310
315
|
if (
|
311
316
|
map[InitValues] &&
|
312
317
|
(map._schema[ItemsSym] ||
|
313
318
|
Object.keys(map[InitValues].init).every(
|
314
|
-
|
319
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
320
|
+
(key) => (map._schema as any)[key],
|
315
321
|
))
|
316
322
|
) {
|
317
323
|
const raw = map.rawFromInit(
|
318
324
|
map[InitValues].init,
|
319
|
-
map[InitValues].owner
|
325
|
+
map[InitValues].owner,
|
320
326
|
);
|
321
327
|
Object.defineProperties(map, {
|
322
328
|
id: {
|
@@ -354,7 +360,7 @@ const CoMapProxyHandler: ProxyHandler<CoMap> = {
|
|
354
360
|
: new Ref(
|
355
361
|
raw as unknown as ID<CoValue>,
|
356
362
|
target._loadedAs,
|
357
|
-
descriptor
|
363
|
+
descriptor,
|
358
364
|
).accessFrom(receiver, key);
|
359
365
|
}
|
360
366
|
} else {
|
package/src/coValues/coStream.ts
CHANGED
@@ -15,7 +15,6 @@ import type {
|
|
15
15
|
SchemaFor,
|
16
16
|
Group,
|
17
17
|
ID,
|
18
|
-
Me,
|
19
18
|
IfCo,
|
20
19
|
ClassOf,
|
21
20
|
UnCo,
|
@@ -46,6 +45,7 @@ export type SingleCoStreamEntry<Item> = {
|
|
46
45
|
};
|
47
46
|
|
48
47
|
/** @category CoValues */
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
49
49
|
export class CoStream<Item = any>
|
50
50
|
extends CoValueBase
|
51
51
|
implements CoValue<"CoStream", RawCoStream>
|
@@ -65,6 +65,7 @@ export class CoStream<Item = any>
|
|
65
65
|
|
66
66
|
/** @internal This is only a marker type and doesn't exist at runtime */
|
67
67
|
[ItemsSym]!: Item;
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
68
69
|
static _schema: any;
|
69
70
|
get _schema(): {
|
70
71
|
[ItemsSym]: SchemaFor<Item>;
|
@@ -81,15 +82,16 @@ export class CoStream<Item = any>
|
|
81
82
|
[key: SessionID]: CoStreamEntry<Item>;
|
82
83
|
};
|
83
84
|
get inCurrentSession(): CoStreamEntry<Item> | undefined {
|
84
|
-
return this.perSession[this._loadedAs.sessionID];
|
85
|
+
return this.perSession[this._loadedAs.sessionID!];
|
85
86
|
}
|
86
87
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
87
89
|
[InitValues]?: any;
|
88
90
|
|
89
91
|
constructor(
|
90
92
|
options:
|
91
93
|
| { init: Item[]; owner: Account | Group }
|
92
|
-
| { fromRaw: RawCoStream }
|
94
|
+
| { fromRaw: RawCoStream },
|
93
95
|
) {
|
94
96
|
super();
|
95
97
|
|
@@ -114,7 +116,7 @@ export class CoStream<Item = any>
|
|
114
116
|
static create<S extends CoStream>(
|
115
117
|
this: ClassOf<S>,
|
116
118
|
init: S extends CoStream<infer Item> ? UnCo<Item>[] : never,
|
117
|
-
options: { owner: Account | Group }
|
119
|
+
options: { owner: Account | Group },
|
118
120
|
) {
|
119
121
|
return new this({ init, owner: options.owner });
|
120
122
|
}
|
@@ -153,13 +155,13 @@ export class CoStream<Item = any>
|
|
153
155
|
Object.entries(this).map(([account, entry]) => [
|
154
156
|
account,
|
155
157
|
mapper(entry.value),
|
156
|
-
])
|
158
|
+
]),
|
157
159
|
),
|
158
160
|
in: Object.fromEntries(
|
159
161
|
Object.entries(this.perSession).map(([session, entry]) => [
|
160
162
|
session,
|
161
163
|
mapper(entry.value),
|
162
|
-
])
|
164
|
+
]),
|
163
165
|
),
|
164
166
|
};
|
165
167
|
}
|
@@ -169,8 +171,9 @@ export class CoStream<Item = any>
|
|
169
171
|
}
|
170
172
|
|
171
173
|
static schema<V extends CoStream>(
|
174
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
172
175
|
this: { new (...args: any): V } & typeof CoStream,
|
173
|
-
def: { [ItemsSym]: V["_schema"][ItemsSym] }
|
176
|
+
def: { [ItemsSym]: V["_schema"][ItemsSym] },
|
174
177
|
) {
|
175
178
|
this._schema ||= {};
|
176
179
|
Object.assign(this._schema, def);
|
@@ -185,9 +188,9 @@ function entryFromRawEntry<Item>(
|
|
185
188
|
at: Date;
|
186
189
|
value: JsonValue;
|
187
190
|
},
|
188
|
-
loadedAs: Account
|
191
|
+
loadedAs: Account,
|
189
192
|
accountID: ID<Account> | undefined,
|
190
|
-
itemField: Schema
|
193
|
+
itemField: Schema,
|
191
194
|
): Omit<CoStreamEntry<Item>, "all"> {
|
192
195
|
return {
|
193
196
|
get value(): NonNullable<Item> extends CoValue
|
@@ -201,7 +204,11 @@ function entryFromRawEntry<Item>(
|
|
201
204
|
return decodeSync(itemField.encoded)(rawEntry.value);
|
202
205
|
} else if (isRefEncoded(itemField)) {
|
203
206
|
return this.ref?.accessFrom(
|
204
|
-
accessFrom,
|
207
|
+
accessFrom,
|
208
|
+
rawEntry.by +
|
209
|
+
rawEntry.tx.sessionID +
|
210
|
+
rawEntry.tx.txIndex +
|
211
|
+
".value",
|
205
212
|
) as NonNullable<Item> extends CoValue
|
206
213
|
? (CoValue & Item) | null
|
207
214
|
: Item;
|
@@ -217,7 +224,7 @@ function entryFromRawEntry<Item>(
|
|
217
224
|
return new Ref(
|
218
225
|
rawId as unknown as ID<CoValue>,
|
219
226
|
loadedAs,
|
220
|
-
itemField
|
227
|
+
itemField,
|
221
228
|
) as NonNullable<Item> extends CoValue
|
222
229
|
? Ref<NonNullable<Item>>
|
223
230
|
: never;
|
@@ -231,8 +238,14 @@ function entryFromRawEntry<Item>(
|
|
231
238
|
new Ref<Account>(
|
232
239
|
accountID as unknown as ID<Account>,
|
233
240
|
loadedAs,
|
234
|
-
Account
|
235
|
-
)?.accessFrom(
|
241
|
+
Account,
|
242
|
+
)?.accessFrom(
|
243
|
+
accessFrom,
|
244
|
+
rawEntry.by +
|
245
|
+
rawEntry.tx.sessionID +
|
246
|
+
rawEntry.tx.txIndex +
|
247
|
+
".by",
|
248
|
+
)
|
236
249
|
);
|
237
250
|
},
|
238
251
|
madeAt: rawEntry.at,
|
@@ -272,7 +285,7 @@ export const CoStreamProxyHandler: ProxyHandler<CoStream> = {
|
|
272
285
|
rawEntry,
|
273
286
|
target._loadedAs,
|
274
287
|
key as unknown as ID<Account>,
|
275
|
-
target._schema[ItemsSym]
|
288
|
+
target._schema[ItemsSym],
|
276
289
|
);
|
277
290
|
|
278
291
|
Object.defineProperty(entry, "all", {
|
@@ -287,9 +300,10 @@ export const CoStreamProxyHandler: ProxyHandler<CoStream> = {
|
|
287
300
|
rawEntry.value,
|
288
301
|
target._loadedAs,
|
289
302
|
key as unknown as ID<Account>,
|
290
|
-
target._schema[ItemsSym]
|
303
|
+
target._schema[ItemsSym],
|
291
304
|
);
|
292
305
|
}
|
306
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
293
307
|
})() satisfies IterableIterator<SingleCoStreamEntry<any>>;
|
294
308
|
},
|
295
309
|
});
|
@@ -298,7 +312,7 @@ export const CoStreamProxyHandler: ProxyHandler<CoStream> = {
|
|
298
312
|
} else if (key === "perSession") {
|
299
313
|
return new Proxy(
|
300
314
|
{},
|
301
|
-
CoStreamPerSessionProxyHandler(target, receiver)
|
315
|
+
CoStreamPerSessionProxyHandler(target, receiver),
|
302
316
|
);
|
303
317
|
} else {
|
304
318
|
return Reflect.get(target, key, receiver);
|
@@ -359,7 +373,7 @@ export const CoStreamProxyHandler: ProxyHandler<CoStream> = {
|
|
359
373
|
|
360
374
|
const CoStreamPerSessionProxyHandler = (
|
361
375
|
innerTarget: CoStream,
|
362
|
-
accessFrom: CoStream
|
376
|
+
accessFrom: CoStream,
|
363
377
|
): ProxyHandler<Record<string, never>> => ({
|
364
378
|
get(_target, key, receiver) {
|
365
379
|
if (typeof key === "string" && key.includes("session")) {
|
@@ -376,7 +390,7 @@ const CoStreamPerSessionProxyHandler = (
|
|
376
390
|
cojsonInternals.isAccountID(by)
|
377
391
|
? (by as unknown as ID<Account>)
|
378
392
|
: undefined,
|
379
|
-
innerTarget._schema[ItemsSym]
|
393
|
+
innerTarget._schema[ItemsSym],
|
380
394
|
);
|
381
395
|
|
382
396
|
Object.defineProperty(entry, "all", {
|
@@ -393,9 +407,10 @@ const CoStreamPerSessionProxyHandler = (
|
|
393
407
|
cojsonInternals.isAccountID(by)
|
394
408
|
? (by as unknown as ID<Account>)
|
395
409
|
: undefined,
|
396
|
-
innerTarget._schema[ItemsSym]
|
410
|
+
innerTarget._schema[ItemsSym],
|
397
411
|
);
|
398
412
|
}
|
413
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
399
414
|
})() satisfies IterableIterator<SingleCoStreamEntry<any>>;
|
400
415
|
},
|
401
416
|
});
|
@@ -437,7 +452,7 @@ export class BinaryCoStream
|
|
437
452
|
}
|
438
453
|
| {
|
439
454
|
fromRaw: RawBinaryCoStream;
|
440
|
-
}
|
455
|
+
},
|
441
456
|
) {
|
442
457
|
super();
|
443
458
|
|
@@ -455,13 +470,14 @@ export class BinaryCoStream
|
|
455
470
|
value: raw.id,
|
456
471
|
enumerable: false,
|
457
472
|
},
|
473
|
+
_type: { value: "BinaryCoStream", enumerable: false },
|
458
474
|
_raw: { value: raw, enumerable: false },
|
459
475
|
});
|
460
476
|
}
|
461
477
|
|
462
478
|
static create<S extends BinaryCoStream>(
|
463
479
|
this: ClassOf<S>,
|
464
|
-
options: { owner: Account | Group }
|
480
|
+
options: { owner: Account | Group },
|
465
481
|
) {
|
466
482
|
return new this(options);
|
467
483
|
}
|
@@ -500,19 +516,15 @@ export class BinaryCoStream
|
|
500
516
|
|
501
517
|
static async loadAsBlob(
|
502
518
|
id: ID<BinaryCoStream>,
|
503
|
-
|
504
|
-
|
519
|
+
as: Account,
|
520
|
+
options?: {
|
505
521
|
allowUnfinished?: boolean;
|
506
|
-
|
507
|
-
}
|
522
|
+
},
|
508
523
|
): Promise<Blob | undefined> {
|
509
|
-
const stream = await this.load(id,
|
510
|
-
as: options.as,
|
511
|
-
onProgress: options.onProgress,
|
512
|
-
});
|
524
|
+
const stream = await this.load(id, as, []);
|
513
525
|
|
514
526
|
return stream?.toBlob({
|
515
|
-
allowUnfinished: options
|
527
|
+
allowUnfinished: options?.allowUnfinished,
|
516
528
|
});
|
517
529
|
}
|
518
530
|
|
@@ -521,7 +533,7 @@ export class BinaryCoStream
|
|
521
533
|
options: {
|
522
534
|
owner: Group | Account;
|
523
535
|
onProgress?: (progress: number) => void;
|
524
|
-
}
|
536
|
+
},
|
525
537
|
): Promise<BinaryCoStream> {
|
526
538
|
const stream = this.create({ owner: options.owner });
|
527
539
|
|
@@ -554,7 +566,7 @@ export class BinaryCoStream
|
|
554
566
|
"Finished creating binary stream in",
|
555
567
|
(end - start) / 1000,
|
556
568
|
"s - Throughput in MB/s",
|
557
|
-
(1000 * (blob.size / (end - start))) / (1024 * 1024)
|
569
|
+
(1000 * (blob.size / (end - start))) / (1024 * 1024),
|
558
570
|
);
|
559
571
|
options.onProgress?.(1);
|
560
572
|
|