jazz-tools 0.7.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +24 -0
- package/.turbo/turbo-build.log +24 -0
- package/CHANGELOG.md +42 -0
- package/LICENSE.txt +19 -0
- package/README.md +3 -0
- package/dist/coValueInterfaces.js +8 -0
- package/dist/coValueInterfaces.js.map +1 -0
- package/dist/coValues/account/account.js +11 -0
- package/dist/coValues/account/account.js.map +1 -0
- package/dist/coValues/account/accountOf.js +150 -0
- package/dist/coValues/account/accountOf.js.map +1 -0
- package/dist/coValues/account/migration.js +4 -0
- package/dist/coValues/account/migration.js.map +1 -0
- package/dist/coValues/coList/coList.js +2 -0
- package/dist/coValues/coList/coList.js.map +1 -0
- package/dist/coValues/coList/coListOf.js +235 -0
- package/dist/coValues/coList/coListOf.js.map +1 -0
- package/dist/coValues/coList/internalDocs.js +2 -0
- package/dist/coValues/coList/internalDocs.js.map +1 -0
- package/dist/coValues/coMap/coMap.js +2 -0
- package/dist/coValues/coMap/coMap.js.map +1 -0
- package/dist/coValues/coMap/coMapOf.js +262 -0
- package/dist/coValues/coMap/coMapOf.js.map +1 -0
- package/dist/coValues/coMap/internalDocs.js +2 -0
- package/dist/coValues/coMap/internalDocs.js.map +1 -0
- package/dist/coValues/coStream/coStream.js +2 -0
- package/dist/coValues/coStream/coStream.js.map +1 -0
- package/dist/coValues/coStream/coStreamOf.js +244 -0
- package/dist/coValues/coStream/coStreamOf.js.map +1 -0
- package/dist/coValues/construction.js +34 -0
- package/dist/coValues/construction.js.map +1 -0
- package/dist/coValues/extensions/imageDef.js +36 -0
- package/dist/coValues/extensions/imageDef.js.map +1 -0
- package/dist/coValues/group/group.js +2 -0
- package/dist/coValues/group/group.js.map +1 -0
- package/dist/coValues/group/groupOf.js +109 -0
- package/dist/coValues/group/groupOf.js.map +1 -0
- package/dist/coValues/resolution.js +66 -0
- package/dist/coValues/resolution.js.map +1 -0
- package/dist/errors.js +2 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/refs.js +95 -0
- package/dist/refs.js.map +1 -0
- package/dist/schemaHelpers.js +14 -0
- package/dist/schemaHelpers.js.map +1 -0
- package/dist/subscriptionScope.js +81 -0
- package/dist/subscriptionScope.js.map +1 -0
- package/dist/tests/coList.test.js +207 -0
- package/dist/tests/coList.test.js.map +1 -0
- package/dist/tests/coMap.test.js +238 -0
- package/dist/tests/coMap.test.js.map +1 -0
- package/dist/tests/coStream.test.js +263 -0
- package/dist/tests/coStream.test.js.map +1 -0
- package/dist/tests/types.test.js +33 -0
- package/dist/tests/types.test.js.map +1 -0
- package/package.json +23 -0
- package/src/coValueInterfaces.ts +105 -0
- package/src/coValues/account/account.ts +106 -0
- package/src/coValues/account/accountOf.ts +284 -0
- package/src/coValues/account/migration.ts +12 -0
- package/src/coValues/coList/coList.ts +57 -0
- package/src/coValues/coList/coListOf.ts +377 -0
- package/src/coValues/coList/internalDocs.ts +1 -0
- package/src/coValues/coMap/coMap.ts +110 -0
- package/src/coValues/coMap/coMapOf.ts +451 -0
- package/src/coValues/coMap/internalDocs.ts +1 -0
- package/src/coValues/coStream/coStream.ts +63 -0
- package/src/coValues/coStream/coStreamOf.ts +404 -0
- package/src/coValues/construction.ts +110 -0
- package/src/coValues/extensions/imageDef.ts +51 -0
- package/src/coValues/group/group.ts +27 -0
- package/src/coValues/group/groupOf.ts +183 -0
- package/src/coValues/resolution.ts +111 -0
- package/src/errors.ts +1 -0
- package/src/index.ts +68 -0
- package/src/refs.ts +128 -0
- package/src/schemaHelpers.ts +72 -0
- package/src/subscriptionScope.ts +118 -0
- package/src/tests/coList.test.ts +283 -0
- package/src/tests/coMap.test.ts +357 -0
- package/src/tests/coStream.test.ts +415 -0
- package/src/tests/types.test.ts +37 -0
- package/tsconfig.json +15 -0
@@ -0,0 +1,404 @@
|
|
1
|
+
import {
|
2
|
+
AccountID,
|
3
|
+
AgentID,
|
4
|
+
BinaryStreamInfo,
|
5
|
+
CojsonInternalTypes,
|
6
|
+
JsonValue,
|
7
|
+
RawAccount,
|
8
|
+
RawBinaryCoStream,
|
9
|
+
RawCoStream,
|
10
|
+
SessionID,
|
11
|
+
cojsonInternals,
|
12
|
+
} from "cojson";
|
13
|
+
import {
|
14
|
+
CoValue,
|
15
|
+
CoValueSchema,
|
16
|
+
ID,
|
17
|
+
inspect,
|
18
|
+
} from "../../coValueInterfaces.js";
|
19
|
+
import { AnyAccount, ControlledAccount } from "../account/account.js";
|
20
|
+
import { AnyGroup } from "../group/group.js";
|
21
|
+
import {
|
22
|
+
BinaryCoStream,
|
23
|
+
BinaryCoStreamSchema,
|
24
|
+
CoStream,
|
25
|
+
CoStreamSchema,
|
26
|
+
} from "./coStream.js";
|
27
|
+
import { SharedCoValueConstructor } from "../construction.js";
|
28
|
+
import { AST, Schema } from "@effect/schema";
|
29
|
+
import {
|
30
|
+
constructorOfSchemaSym,
|
31
|
+
propertyIsCoValueSchema,
|
32
|
+
} from "../resolution.js";
|
33
|
+
import { pipeArguments } from "effect/Pipeable";
|
34
|
+
import { ValueRef } from "../../refs.js";
|
35
|
+
import { SchemaWithOutput } from "../../schemaHelpers.js";
|
36
|
+
import { Account, controlledAccountFromNode } from "../account/accountOf.js";
|
37
|
+
import { Group } from "../group/groupOf.js";
|
38
|
+
|
39
|
+
export function CoStreamOf<
|
40
|
+
Item extends SchemaWithOutput<CoValue> | SchemaWithOutput<JsonValue>,
|
41
|
+
>(itemSchema: Item) {
|
42
|
+
const decodeItem = Schema.decodeSync(itemSchema);
|
43
|
+
const encodeItem = Schema.encodeSync(itemSchema);
|
44
|
+
const itemIsCoValue = propertyIsCoValueSchema(itemSchema);
|
45
|
+
|
46
|
+
class CoStreamOfItem extends SharedCoValueConstructor {
|
47
|
+
static get ast() {
|
48
|
+
return AST.setAnnotation(
|
49
|
+
Schema.instanceOf(this).ast,
|
50
|
+
constructorOfSchemaSym,
|
51
|
+
this
|
52
|
+
);
|
53
|
+
}
|
54
|
+
static [Schema.TypeId]: Schema.Schema.Variance<
|
55
|
+
CoStreamOfItem,
|
56
|
+
CoStreamOfItem,
|
57
|
+
never
|
58
|
+
>[Schema.TypeId];
|
59
|
+
static pipe() {
|
60
|
+
// eslint-disable-next-line prefer-rest-params
|
61
|
+
return pipeArguments(this, arguments);
|
62
|
+
}
|
63
|
+
static type = "CoStream" as const;
|
64
|
+
|
65
|
+
id!: ID<this>;
|
66
|
+
_type!: "CoStream";
|
67
|
+
_owner!: AnyAccount | AnyGroup;
|
68
|
+
_raw!: RawCoStream;
|
69
|
+
_loadedAs!: ControlledAccount;
|
70
|
+
_schema!: typeof CoStreamOfItem;
|
71
|
+
|
72
|
+
by: CoStream<Item>["by"];
|
73
|
+
in: CoStream<Item>["in"];
|
74
|
+
|
75
|
+
constructor(
|
76
|
+
init: Schema.Schema.To<Item>[] | undefined,
|
77
|
+
options: { owner: AnyAccount | AnyGroup } | { fromRaw: RawCoStream }
|
78
|
+
) {
|
79
|
+
super();
|
80
|
+
|
81
|
+
let raw: RawCoStream;
|
82
|
+
|
83
|
+
if ("fromRaw" in options) {
|
84
|
+
raw = options.fromRaw;
|
85
|
+
} else {
|
86
|
+
const rawOwner = options.owner._raw;
|
87
|
+
|
88
|
+
raw = rawOwner.createStream();
|
89
|
+
}
|
90
|
+
|
91
|
+
this.by = {} as CoStream<Item>["by"];
|
92
|
+
this.in = {} as CoStream<Item>["in"];
|
93
|
+
|
94
|
+
Object.defineProperties(this, {
|
95
|
+
id: { value: raw.id, enumerable: false },
|
96
|
+
_type: { value: "CoStream", enumerable: false },
|
97
|
+
_owner: {
|
98
|
+
get: () =>
|
99
|
+
raw.group instanceof RawAccount
|
100
|
+
? Account.fromRaw(raw.group)
|
101
|
+
: Group.fromRaw(raw.group),
|
102
|
+
enumerable: false,
|
103
|
+
},
|
104
|
+
_raw: { value: raw, enumerable: false },
|
105
|
+
_loadedAs: {
|
106
|
+
get: () => controlledAccountFromNode(raw.core.node),
|
107
|
+
enumerable: false,
|
108
|
+
},
|
109
|
+
_schema: { value: this.constructor, enumerable: false },
|
110
|
+
});
|
111
|
+
|
112
|
+
if (init !== undefined) {
|
113
|
+
for (const item of init) {
|
114
|
+
this.pushItem(item);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
this.updateEntries();
|
119
|
+
}
|
120
|
+
|
121
|
+
private updateEntries() {
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
123
|
+
const self = this;
|
124
|
+
const raw = this._raw;
|
125
|
+
const loadedAs = this._loadedAs;
|
126
|
+
|
127
|
+
for (const accountID of this._raw.accounts() as unknown as Set<
|
128
|
+
ID<AnyAccount>
|
129
|
+
>) {
|
130
|
+
Object.defineProperty(this.by, accountID, {
|
131
|
+
get() {
|
132
|
+
const rawEntry = raw.lastItemBy(
|
133
|
+
accountID as unknown as AccountID
|
134
|
+
);
|
135
|
+
|
136
|
+
if (!rawEntry) return;
|
137
|
+
return entryFromRawEntry(
|
138
|
+
self,
|
139
|
+
rawEntry,
|
140
|
+
loadedAs,
|
141
|
+
accountID
|
142
|
+
);
|
143
|
+
},
|
144
|
+
configurable: true,
|
145
|
+
});
|
146
|
+
}
|
147
|
+
Object.defineProperty(this.by, "me", {
|
148
|
+
get() {
|
149
|
+
return self.by[loadedAs.id];
|
150
|
+
},
|
151
|
+
enumerable: false,
|
152
|
+
configurable: true,
|
153
|
+
});
|
154
|
+
|
155
|
+
for (const sessionID of raw.sessions() as unknown as Set<SessionID>) {
|
156
|
+
Object.defineProperty(this.in, sessionID, {
|
157
|
+
get() {
|
158
|
+
const rawEntry = raw.lastItemIn(
|
159
|
+
sessionID as unknown as SessionID
|
160
|
+
);
|
161
|
+
|
162
|
+
if (!rawEntry) return;
|
163
|
+
const by =
|
164
|
+
cojsonInternals.accountOrAgentIDfromSessionID(
|
165
|
+
sessionID
|
166
|
+
);
|
167
|
+
return entryFromRawEntry(
|
168
|
+
self,
|
169
|
+
rawEntry,
|
170
|
+
loadedAs,
|
171
|
+
cojsonInternals.isAccountID(by)
|
172
|
+
? (by as unknown as ID<AnyAccount>)
|
173
|
+
: undefined
|
174
|
+
);
|
175
|
+
},
|
176
|
+
configurable: true,
|
177
|
+
});
|
178
|
+
}
|
179
|
+
Object.defineProperty(this.in, "currentSession", {
|
180
|
+
get() {
|
181
|
+
return self.in[loadedAs.sessionID];
|
182
|
+
},
|
183
|
+
enumerable: false,
|
184
|
+
configurable: true,
|
185
|
+
});
|
186
|
+
}
|
187
|
+
|
188
|
+
static fromRaw(raw: RawCoStream) {
|
189
|
+
return new CoStreamOfItem(undefined, { fromRaw: raw });
|
190
|
+
}
|
191
|
+
|
192
|
+
push(...items: Schema.Schema.To<Item>[]) {
|
193
|
+
for (const item of items) {
|
194
|
+
this.pushItem(item);
|
195
|
+
}
|
196
|
+
this.updateEntries();
|
197
|
+
}
|
198
|
+
|
199
|
+
private pushItem(item: Schema.Schema.To<Item>) {
|
200
|
+
if (itemIsCoValue) {
|
201
|
+
this._raw.push(item.id);
|
202
|
+
} else {
|
203
|
+
this._raw.push(encodeItem(item));
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
toJSON() {
|
208
|
+
return {
|
209
|
+
by: Object.fromEntries(
|
210
|
+
Object.entries(this.by).map(([key, value]) => [
|
211
|
+
key,
|
212
|
+
value &&
|
213
|
+
typeof value === "object" &&
|
214
|
+
"toJSON" in value &&
|
215
|
+
typeof value.toJSON === "function"
|
216
|
+
? value.toJSON()
|
217
|
+
: value,
|
218
|
+
])
|
219
|
+
),
|
220
|
+
in: Object.fromEntries(
|
221
|
+
Object.entries(this.in).map(([key, value]) => [
|
222
|
+
key,
|
223
|
+
value &&
|
224
|
+
typeof value === "object" &&
|
225
|
+
"toJSON" in value &&
|
226
|
+
typeof value.toJSON === "function"
|
227
|
+
? value.toJSON()
|
228
|
+
: value,
|
229
|
+
])
|
230
|
+
),
|
231
|
+
co: {
|
232
|
+
id: this.id,
|
233
|
+
type: "CoStream",
|
234
|
+
},
|
235
|
+
};
|
236
|
+
}
|
237
|
+
|
238
|
+
[inspect]() {
|
239
|
+
return this.toJSON();
|
240
|
+
}
|
241
|
+
|
242
|
+
static as<SubClass>() {
|
243
|
+
return CoStreamOfItem as unknown as CoStreamSchema<SubClass, Item>;
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
247
|
+
return CoStreamOfItem as CoStreamSchema<CoStreamOfItem, Item> & {
|
248
|
+
as<SubClass>(): CoStreamSchema<SubClass, Item>;
|
249
|
+
};
|
250
|
+
|
251
|
+
function entryFromRawEntry(
|
252
|
+
accessFrom: CoValue,
|
253
|
+
rawEntry: {
|
254
|
+
by: AccountID | AgentID;
|
255
|
+
tx: CojsonInternalTypes.TransactionID;
|
256
|
+
at: Date;
|
257
|
+
value: JsonValue;
|
258
|
+
},
|
259
|
+
loadedAs: ControlledAccount,
|
260
|
+
accountID: ID<AnyAccount> | undefined
|
261
|
+
) {
|
262
|
+
return {
|
263
|
+
get value(): Schema.Schema.To<Item> | undefined {
|
264
|
+
if (itemIsCoValue) {
|
265
|
+
return this.ref?.accessFrom(accessFrom);
|
266
|
+
} else {
|
267
|
+
return decodeItem(rawEntry.value);
|
268
|
+
}
|
269
|
+
},
|
270
|
+
get ref() {
|
271
|
+
if (itemIsCoValue) {
|
272
|
+
const rawId = rawEntry.value;
|
273
|
+
return new ValueRef(
|
274
|
+
rawId as unknown as ID<Schema.Schema.To<Item>>,
|
275
|
+
loadedAs,
|
276
|
+
itemSchema
|
277
|
+
);
|
278
|
+
}
|
279
|
+
},
|
280
|
+
get by() {
|
281
|
+
return (
|
282
|
+
accountID &&
|
283
|
+
new ValueRef(
|
284
|
+
accountID as unknown as ID<AnyAccount>,
|
285
|
+
loadedAs,
|
286
|
+
Account
|
287
|
+
)?.accessFrom(accessFrom)
|
288
|
+
);
|
289
|
+
},
|
290
|
+
madeAt: rawEntry.at,
|
291
|
+
tx: rawEntry.tx,
|
292
|
+
};
|
293
|
+
}
|
294
|
+
}
|
295
|
+
|
296
|
+
class BinaryCoStreamImplClass
|
297
|
+
extends SharedCoValueConstructor
|
298
|
+
implements BinaryCoStream
|
299
|
+
{
|
300
|
+
static ast = AST.setAnnotation(
|
301
|
+
Schema.instanceOf(this).ast,
|
302
|
+
constructorOfSchemaSym,
|
303
|
+
this
|
304
|
+
);
|
305
|
+
static [Schema.TypeId]: Schema.Schema.Variance<
|
306
|
+
BinaryCoStream,
|
307
|
+
BinaryCoStream,
|
308
|
+
never
|
309
|
+
>[Schema.TypeId];
|
310
|
+
static pipe() {
|
311
|
+
// eslint-disable-next-line prefer-rest-params
|
312
|
+
return pipeArguments(this, arguments);
|
313
|
+
}
|
314
|
+
static type = "BinaryCoStream" as const;
|
315
|
+
|
316
|
+
id!: ID<this>;
|
317
|
+
_type!: "BinaryCoStream";
|
318
|
+
_owner!: AnyAccount | AnyGroup;
|
319
|
+
_raw!: RawBinaryCoStream;
|
320
|
+
_loadedAs!: ControlledAccount;
|
321
|
+
_schema!: typeof BinaryCoStreamImplClass;
|
322
|
+
|
323
|
+
constructor(
|
324
|
+
init: [] | undefined,
|
325
|
+
options:
|
326
|
+
| {
|
327
|
+
owner: AnyAccount | AnyGroup;
|
328
|
+
}
|
329
|
+
| {
|
330
|
+
fromRaw: RawBinaryCoStream;
|
331
|
+
}
|
332
|
+
) {
|
333
|
+
super();
|
334
|
+
|
335
|
+
let raw: RawBinaryCoStream;
|
336
|
+
|
337
|
+
if ("fromRaw" in options) {
|
338
|
+
raw = options.fromRaw;
|
339
|
+
} else {
|
340
|
+
const rawOwner = options.owner._raw;
|
341
|
+
|
342
|
+
raw = rawOwner.createBinaryStream();
|
343
|
+
}
|
344
|
+
|
345
|
+
Object.defineProperties(this, {
|
346
|
+
id: { value: raw.id, enumerable: false },
|
347
|
+
_type: { value: "BinaryCoStream", enumerable: false },
|
348
|
+
_owner: {
|
349
|
+
get: () =>
|
350
|
+
raw.group instanceof RawAccount
|
351
|
+
? Account.fromRaw(raw.group)
|
352
|
+
: Group.fromRaw(raw.group),
|
353
|
+
enumerable: false,
|
354
|
+
},
|
355
|
+
_raw: { value: raw, enumerable: false },
|
356
|
+
_loadedAs: {
|
357
|
+
get: () => controlledAccountFromNode(raw.core.node),
|
358
|
+
enumerable: false,
|
359
|
+
},
|
360
|
+
_schema: { value: this.constructor, enumerable: false },
|
361
|
+
});
|
362
|
+
}
|
363
|
+
|
364
|
+
static fromRaw(raw: RawBinaryCoStream) {
|
365
|
+
return new BinaryCoStreamImplClass(undefined, { fromRaw: raw });
|
366
|
+
}
|
367
|
+
|
368
|
+
getChunks(options?: {
|
369
|
+
allowUnfinished?: boolean;
|
370
|
+
}):
|
371
|
+
| (BinaryStreamInfo & { chunks: Uint8Array[]; finished: boolean })
|
372
|
+
| undefined {
|
373
|
+
return this._raw.getBinaryChunks(options?.allowUnfinished);
|
374
|
+
}
|
375
|
+
|
376
|
+
start(options: BinaryStreamInfo): void {
|
377
|
+
this._raw.startBinaryStream(options);
|
378
|
+
}
|
379
|
+
|
380
|
+
push(data: Uint8Array): void {
|
381
|
+
this._raw.pushBinaryStreamChunk(data);
|
382
|
+
}
|
383
|
+
|
384
|
+
end(): void {
|
385
|
+
this._raw.endBinaryStream();
|
386
|
+
}
|
387
|
+
|
388
|
+
toJSON(): object | any[] {
|
389
|
+
return {
|
390
|
+
...this.getChunks(),
|
391
|
+
co: {
|
392
|
+
id: this.id,
|
393
|
+
type: "BinaryCoStream",
|
394
|
+
},
|
395
|
+
};
|
396
|
+
}
|
397
|
+
|
398
|
+
[inspect]() {
|
399
|
+
return this.toJSON();
|
400
|
+
}
|
401
|
+
}
|
402
|
+
|
403
|
+
export const BinaryCoStreamImpl =
|
404
|
+
BinaryCoStreamImplClass as BinaryCoStreamSchema;
|
@@ -0,0 +1,110 @@
|
|
1
|
+
import { Effect, Sink, Stream } from "effect";
|
2
|
+
import {
|
3
|
+
CoValueSchema,
|
4
|
+
CoValue,
|
5
|
+
ID,
|
6
|
+
SubclassedConstructor,
|
7
|
+
} from "../coValueInterfaces.js";
|
8
|
+
import { UnavailableError } from "../errors.js";
|
9
|
+
import { ControlledAccount, ControlledAccountCtx } from "./account/account.js";
|
10
|
+
import { ValueRef } from "../refs.js";
|
11
|
+
import { SubscriptionScope } from "../subscriptionScope.js";
|
12
|
+
|
13
|
+
export abstract class SharedCoValueConstructor {
|
14
|
+
id!: ID<this>;
|
15
|
+
_schema!: CoValueSchema;
|
16
|
+
_loadedAs!: ControlledAccount;
|
17
|
+
|
18
|
+
static loadEf<V extends CoValue>(
|
19
|
+
this: CoValueSchema & SubclassedConstructor<V>,
|
20
|
+
id: ID<V>
|
21
|
+
): Effect.Effect<V, UnavailableError, ControlledAccountCtx> {
|
22
|
+
return Effect.gen(this, function* (_) {
|
23
|
+
const controlledAccount = yield* _(ControlledAccountCtx);
|
24
|
+
return yield* _(
|
25
|
+
new ValueRef(id as ID<V>, controlledAccount, this).loadEf()
|
26
|
+
);
|
27
|
+
});
|
28
|
+
}
|
29
|
+
|
30
|
+
static load<V extends CoValue>(
|
31
|
+
this: CoValueSchema & SubclassedConstructor<V>,
|
32
|
+
id: ID<V>,
|
33
|
+
options: {
|
34
|
+
as: ControlledAccount;
|
35
|
+
onProgress?: (progress: number) => void;
|
36
|
+
}
|
37
|
+
): Promise<V | undefined> {
|
38
|
+
return new ValueRef(id as ID<V>, options.as, this).load(
|
39
|
+
options?.onProgress && { onProgress: options.onProgress }
|
40
|
+
);
|
41
|
+
}
|
42
|
+
|
43
|
+
static subscribe<V extends CoValue>(
|
44
|
+
this: CoValueSchema & SubclassedConstructor<V>,
|
45
|
+
id: ID<V>,
|
46
|
+
options: { as: ControlledAccount },
|
47
|
+
onUpdate: (value: V) => void
|
48
|
+
): () => void {
|
49
|
+
void Effect.runPromise(
|
50
|
+
Effect.provideService(
|
51
|
+
this.subscribeEf(id).pipe(
|
52
|
+
Stream.run(
|
53
|
+
Sink.forEach((update) =>
|
54
|
+
Effect.sync(() => onUpdate(update))
|
55
|
+
)
|
56
|
+
)
|
57
|
+
),
|
58
|
+
ControlledAccountCtx,
|
59
|
+
options.as
|
60
|
+
)
|
61
|
+
);
|
62
|
+
|
63
|
+
return function unsubscribe() {};
|
64
|
+
}
|
65
|
+
|
66
|
+
static subscribeEf<V extends CoValue>(
|
67
|
+
this: CoValueSchema & SubclassedConstructor<V>,
|
68
|
+
id: ID<V>
|
69
|
+
): Stream.Stream<V, UnavailableError, ControlledAccountCtx> {
|
70
|
+
return Stream.fromEffect(this.loadEf(id)).pipe(
|
71
|
+
Stream.flatMap((value) =>
|
72
|
+
Stream.asyncScoped<V, UnavailableError>((emit) =>
|
73
|
+
Effect.gen(this, function* (_) {
|
74
|
+
const subscription = new SubscriptionScope(
|
75
|
+
value,
|
76
|
+
this,
|
77
|
+
(update) => {
|
78
|
+
void emit.single(update as V);
|
79
|
+
}
|
80
|
+
);
|
81
|
+
|
82
|
+
yield* _(
|
83
|
+
Effect.addFinalizer(() =>
|
84
|
+
Effect.sync(() => subscription.unsubscribeAll())
|
85
|
+
)
|
86
|
+
);
|
87
|
+
})
|
88
|
+
)
|
89
|
+
)
|
90
|
+
);
|
91
|
+
}
|
92
|
+
|
93
|
+
subscribe(listener: (update: this) => void): () => void {
|
94
|
+
return (this._schema as unknown as CoValueSchema).subscribe(
|
95
|
+
this.id as unknown as ID<CoValue>,
|
96
|
+
{ as: this._loadedAs },
|
97
|
+
listener as unknown as (update: CoValue) => void
|
98
|
+
);
|
99
|
+
}
|
100
|
+
|
101
|
+
subscribeEf(): Stream.Stream<this, UnavailableError, never> {
|
102
|
+
return Stream.provideService(
|
103
|
+
(this._schema as unknown as CoValueSchema).subscribeEf(
|
104
|
+
this.id as unknown as ID<CoValue>
|
105
|
+
),
|
106
|
+
ControlledAccountCtx,
|
107
|
+
this._loadedAs
|
108
|
+
) as unknown as Stream.Stream<this, UnavailableError, never>;
|
109
|
+
}
|
110
|
+
}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
import * as S from "@effect/schema/Schema";
|
2
|
+
import { CoMapOf } from "../coMap/coMapOf.js";
|
3
|
+
import { BinaryCoStreamImpl } from "../coStream/coStreamOf.js";
|
4
|
+
import { BinaryCoStream } from "../coStream/coStream.js";
|
5
|
+
import { subscriptionsScopes } from "../../subscriptionScope.js";
|
6
|
+
|
7
|
+
export class ImageDefinition extends CoMapOf(
|
8
|
+
{
|
9
|
+
originalSize: S.tuple(S.number, S.number),
|
10
|
+
placeholderDataURL: S.optional(S.string),
|
11
|
+
},
|
12
|
+
{
|
13
|
+
key: S.templateLiteral(S.string, S.literal("x"), S.string),
|
14
|
+
value: BinaryCoStreamImpl,
|
15
|
+
}
|
16
|
+
).as<ImageDefinition>() {
|
17
|
+
get highestResAvailable():
|
18
|
+
| { res: `${number}x${number}`; stream: BinaryCoStream }
|
19
|
+
| undefined {
|
20
|
+
if (!subscriptionsScopes.get(this)) {
|
21
|
+
console.warn(
|
22
|
+
"highestResAvailable() only makes sense when used within a subscription."
|
23
|
+
);
|
24
|
+
}
|
25
|
+
|
26
|
+
const resolutions = Object.keys(this).filter((key) =>
|
27
|
+
key.match(/^\d+x\d+$/)
|
28
|
+
) as `${number}x${number}`[];
|
29
|
+
|
30
|
+
resolutions.sort((a, b) => {
|
31
|
+
const aWidth = Number(a.split("x")[0]);
|
32
|
+
const bWidth = Number(b.split("x")[0]);
|
33
|
+
return aWidth - bWidth;
|
34
|
+
});
|
35
|
+
|
36
|
+
let highestAvailableResolution: `${number}x${number}` | undefined;
|
37
|
+
|
38
|
+
for (const resolution of resolutions) {
|
39
|
+
if (this[resolution] && this[resolution]?.getChunks()) {
|
40
|
+
highestAvailableResolution = resolution;
|
41
|
+
} else {
|
42
|
+
return (
|
43
|
+
highestAvailableResolution && {
|
44
|
+
res: highestAvailableResolution,
|
45
|
+
stream: this[highestAvailableResolution]!,
|
46
|
+
}
|
47
|
+
);
|
48
|
+
}
|
49
|
+
}
|
50
|
+
}
|
51
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import * as S from "@effect/schema/Schema";
|
2
|
+
import { Everyone, RawGroup, Role } from "cojson";
|
3
|
+
import { CoValue, CoValueSchema } from "../../coValueInterfaces.js";
|
4
|
+
import { ValueRef } from "../../refs.js";
|
5
|
+
import { AnyAccount } from "../account/account.js";
|
6
|
+
|
7
|
+
export interface AnyGroup<
|
8
|
+
P extends CoValueSchema | S.Schema<null> = CoValueSchema | S.Schema<null>,
|
9
|
+
R extends CoValueSchema | S.Schema<null> = CoValueSchema | S.Schema<null>,
|
10
|
+
> extends CoValue<"Group", RawGroup> {
|
11
|
+
profile?: S.Schema.To<P>;
|
12
|
+
root?: S.Schema.To<R>;
|
13
|
+
_refs: {
|
14
|
+
profile: ValueRef<S.Schema.To<P>>;
|
15
|
+
root: ValueRef<S.Schema.To<R>>;
|
16
|
+
};
|
17
|
+
addMember(member: Everyone | AnyAccount, role: Role): this;
|
18
|
+
myRole(): Role | undefined;
|
19
|
+
}
|
20
|
+
|
21
|
+
export interface GroupSchema<
|
22
|
+
Self,
|
23
|
+
P extends CoValueSchema | S.Schema<null>,
|
24
|
+
R extends CoValueSchema | S.Schema<null>,
|
25
|
+
> extends CoValueSchema<Self, AnyGroup<P, R>, "Group", undefined> {
|
26
|
+
new (options: { owner: AnyAccount | AnyGroup }): AnyGroup<P, R>;
|
27
|
+
}
|