@dxos/feed 0.8.4-main.ef1bc66f44 → 0.8.4-main.f466a3d56e
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/LICENSE +102 -5
- package/README.md +0 -1
- package/dist/lib/neutral/{chunk-Q5PE2Q5P.mjs → chunk-YYASRWLE.mjs} +264 -119
- package/dist/lib/neutral/chunk-YYASRWLE.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1 -5
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/lib/neutral/testing/index.mjs +8 -7
- package/dist/lib/neutral/testing/index.mjs.map +3 -3
- package/dist/types/src/errors.d.ts +17 -57
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/feed-store.d.ts +39 -4
- package/dist/types/src/feed-store.d.ts.map +1 -1
- package/dist/types/src/sync-client.d.ts +4 -3
- package/dist/types/src/sync-client.d.ts.map +1 -1
- package/dist/types/src/sync-server.d.ts +3 -2
- package/dist/types/src/sync-server.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +5 -5
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -21
- package/src/errors.ts +4 -17
- package/src/feed-store.test.ts +206 -97
- package/src/feed-store.ts +142 -123
- package/src/sync-client.ts +109 -17
- package/src/sync-server.ts +70 -12
- package/src/testing/test-builder.ts +11 -11
- package/dist/lib/neutral/chunk-Q5PE2Q5P.mjs.map +0 -7
package/src/feed-store.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { type SpaceId } from '@dxos/keys';
|
|
|
12
12
|
import { FeedProtocol } from '@dxos/protocols';
|
|
13
13
|
import { SqlTransaction } from '@dxos/sql-sqlite';
|
|
14
14
|
|
|
15
|
-
import {
|
|
15
|
+
import { PositionConflictError } from './errors';
|
|
16
16
|
|
|
17
17
|
type AppendRequest = FeedProtocol.AppendRequest;
|
|
18
18
|
type AppendResponse = FeedProtocol.AppendResponse;
|
|
@@ -124,7 +124,9 @@ export class FeedStore {
|
|
|
124
124
|
const rows = yield* sql<{ feedPrivateId: number }>`
|
|
125
125
|
SELECT feedPrivateId FROM feeds WHERE spaceId = ${spaceId} AND feedId = ${feedId}
|
|
126
126
|
`;
|
|
127
|
-
if (rows.length > 0)
|
|
127
|
+
if (rows.length > 0) {
|
|
128
|
+
return rows[0].feedPrivateId;
|
|
129
|
+
}
|
|
128
130
|
|
|
129
131
|
const newRows = yield* sql<{ feedPrivateId: number }>`
|
|
130
132
|
INSERT INTO feeds (spaceId, feedId, feedNamespace) VALUES (${spaceId}, ${feedId}, ${namespace}) RETURNING feedPrivateId
|
|
@@ -141,7 +143,9 @@ export class FeedStore {
|
|
|
141
143
|
Effect.gen(this, function* () {
|
|
142
144
|
const sql = yield* SqlClient.SqlClient;
|
|
143
145
|
const rows = yield* sql<{ token: string }>`SELECT token FROM cursor_tokens WHERE spaceId = ${spaceId}`;
|
|
144
|
-
if (rows.length > 0)
|
|
146
|
+
if (rows.length > 0) {
|
|
147
|
+
return rows[0].token;
|
|
148
|
+
}
|
|
145
149
|
|
|
146
150
|
const token = crypto.randomUUID().replace(/-/g, '').slice(0, 6);
|
|
147
151
|
yield* sql`INSERT INTO cursor_tokens (spaceId, token) VALUES (${spaceId}, ${token})`;
|
|
@@ -192,7 +196,7 @@ export class FeedStore {
|
|
|
192
196
|
// If spaceId is not provided in request (e.g. feedIds query), we can't easily validate token unless we look up spaceId for feedIds.
|
|
193
197
|
// Ideally spaceId should be required for token validation.
|
|
194
198
|
|
|
195
|
-
/*
|
|
199
|
+
/*
|
|
196
200
|
Logic:
|
|
197
201
|
1. If `cursor` is present, it's `token|insertionId`.
|
|
198
202
|
2. If `position` is present, it's `position` (legacy/manual).
|
|
@@ -355,115 +359,72 @@ export class FeedStore {
|
|
|
355
359
|
}).pipe(Effect.withSpan('FeedStore.setSyncState'));
|
|
356
360
|
|
|
357
361
|
/**
|
|
358
|
-
*
|
|
362
|
+
* Returns the number of stored blocks for a single feed in a space/namespace.
|
|
363
|
+
* Intended as a low-level primitive for callers (for example Cloudflare Worker code)
|
|
364
|
+
* that need to make retention decisions under constrained storage resources.
|
|
359
365
|
*/
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
if (!this.#options.assignPositions && block.position != null) {
|
|
378
|
-
const positionKey = `${feedPrivateId}|${block.position}`;
|
|
379
|
-
if (positionFeedIdByKey.has(positionKey)) {
|
|
380
|
-
yield* Effect.fail(
|
|
381
|
-
new DuplicateBlockPositionError({
|
|
382
|
-
feedId: block.feedId!,
|
|
383
|
-
position: block.position,
|
|
384
|
-
}),
|
|
385
|
-
);
|
|
386
|
-
}
|
|
387
|
-
positionFeedIdByKey.set(positionKey, block.feedId!);
|
|
388
|
-
}
|
|
366
|
+
countBlocks = (opts: {
|
|
367
|
+
spaceId: SpaceId;
|
|
368
|
+
feedNamespace: string;
|
|
369
|
+
feedId: string;
|
|
370
|
+
}): Effect.Effect<number, SqlError.SqlError, SqlClient.SqlClient> =>
|
|
371
|
+
Effect.gen(this, function* () {
|
|
372
|
+
const sql = yield* SqlClient.SqlClient;
|
|
373
|
+
const rows = yield* sql<{ count: number }>`
|
|
374
|
+
SELECT COUNT(*) AS count
|
|
375
|
+
FROM blocks
|
|
376
|
+
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
377
|
+
WHERE feeds.spaceId = ${opts.spaceId}
|
|
378
|
+
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
379
|
+
AND feeds.feedId = ${opts.feedId}
|
|
380
|
+
`;
|
|
381
|
+
return rows[0]?.count ?? 0;
|
|
382
|
+
}).pipe(Effect.withSpan('FeedStore.countBlocks'));
|
|
389
383
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
384
|
+
/**
|
|
385
|
+
* Deletes the oldest blocks for a single feed in a space/namespace.
|
|
386
|
+
* This API intentionally does not enforce any retention policy (such as max size);
|
|
387
|
+
* callers decide when and how much to prune, which is useful for constrained
|
|
388
|
+
* environments like Cloudflare Workers.
|
|
389
|
+
*
|
|
390
|
+
* @returns Number of deleted rows.
|
|
391
|
+
*/
|
|
392
|
+
deleteOldestBlocks = (opts: {
|
|
393
|
+
spaceId: SpaceId;
|
|
394
|
+
feedNamespace: string;
|
|
395
|
+
feedId: string;
|
|
396
|
+
count: number;
|
|
397
|
+
}): Effect.Effect<number, SqlError.SqlError, SqlClient.SqlClient> =>
|
|
398
|
+
Effect.gen(this, function* () {
|
|
399
|
+
const sql = yield* SqlClient.SqlClient;
|
|
400
|
+
if (opts.count <= 0) {
|
|
401
|
+
return 0;
|
|
402
|
+
}
|
|
402
403
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const key = `${lamportConflict.feedPrivateId}|${lamportConflict.actorId}|${lamportConflict.sequence}`;
|
|
418
|
-
yield* Effect.fail(
|
|
419
|
-
new DuplicateBlockLamportTimestampError({
|
|
420
|
-
feedId: lamportFeedIdByKey.get(key)!,
|
|
421
|
-
actorId: lamportConflict.actorId,
|
|
422
|
-
sequence: lamportConflict.sequence,
|
|
423
|
-
}),
|
|
424
|
-
);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
404
|
+
const deletedRows = yield* sql<{ insertionId: number }>`
|
|
405
|
+
DELETE FROM blocks
|
|
406
|
+
WHERE insertionId IN (
|
|
407
|
+
SELECT blocks.insertionId
|
|
408
|
+
FROM blocks
|
|
409
|
+
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
410
|
+
WHERE feeds.spaceId = ${opts.spaceId}
|
|
411
|
+
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
412
|
+
AND feeds.feedId = ${opts.feedId}
|
|
413
|
+
ORDER BY blocks.insertionId ASC
|
|
414
|
+
LIMIT ${opts.count}
|
|
415
|
+
)
|
|
416
|
+
RETURNING insertionId
|
|
417
|
+
`;
|
|
427
418
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
const positionConditions = positionedBlocks.map((block) => {
|
|
431
|
-
const feedPrivateId = feedPrivateIds.get(block.feedId!)!;
|
|
432
|
-
return sql`(feedPrivateId = ${feedPrivateId} AND position = ${block.position})`;
|
|
433
|
-
});
|
|
434
|
-
if (positionConditions.length > 0) {
|
|
435
|
-
const existingPositions = yield* sql<{ feedPrivateId: number; position: number }>`
|
|
436
|
-
SELECT feedPrivateId, position
|
|
437
|
-
FROM blocks
|
|
438
|
-
WHERE position IS NOT NULL
|
|
439
|
-
AND (${sql.or(positionConditions)})
|
|
440
|
-
LIMIT 1
|
|
441
|
-
`;
|
|
442
|
-
const positionConflict = existingPositions[0];
|
|
443
|
-
if (positionConflict) {
|
|
444
|
-
const key = `${positionConflict.feedPrivateId}|${positionConflict.position}`;
|
|
445
|
-
yield* Effect.fail(
|
|
446
|
-
new DuplicateBlockPositionError({
|
|
447
|
-
feedId: positionFeedIdByKey.get(key)!,
|
|
448
|
-
position: positionConflict.position,
|
|
449
|
-
}),
|
|
450
|
-
);
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
}),
|
|
455
|
-
);
|
|
419
|
+
return deletedRows.length;
|
|
420
|
+
}).pipe(Effect.withSpan('FeedStore.deleteOldestBlocks'));
|
|
456
421
|
|
|
457
422
|
/**
|
|
458
423
|
* Appends blocks for a space/namespace and optionally assigns global positions.
|
|
459
424
|
*/
|
|
460
425
|
append = (
|
|
461
426
|
request: AppendRequest,
|
|
462
|
-
): Effect.Effect<
|
|
463
|
-
AppendResponse,
|
|
464
|
-
SqlError.SqlError | DuplicateBlockPositionError | DuplicateBlockLamportTimestampError,
|
|
465
|
-
SqlClient.SqlClient | SqlTransaction.SqlTransaction
|
|
466
|
-
> =>
|
|
427
|
+
): Effect.Effect<AppendResponse, SqlError.SqlError, SqlClient.SqlClient | SqlTransaction.SqlTransaction> =>
|
|
467
428
|
Effect.gen(this, function* () {
|
|
468
429
|
if (!request.spaceId) {
|
|
469
430
|
return yield* Effect.die(new Error('spaceId required for append'));
|
|
@@ -506,8 +467,6 @@ export class FeedStore {
|
|
|
506
467
|
{ concurrency: 'unbounded' },
|
|
507
468
|
);
|
|
508
469
|
|
|
509
|
-
yield* this.#validateAppendBlocks(request.blocks, feedPrivateIds);
|
|
510
|
-
|
|
511
470
|
// 2. Get max position per namespace ONCE (not per block).
|
|
512
471
|
const maxPositions = new Map<string, number>();
|
|
513
472
|
if (this.#options.assignPositions) {
|
|
@@ -562,11 +521,7 @@ export class FeedStore {
|
|
|
562
521
|
appendLocal = Effect.fn('Feed.appendLocal')(
|
|
563
522
|
(
|
|
564
523
|
messages: { spaceId: string; feedId: string; feedNamespace: string; data: Uint8Array }[],
|
|
565
|
-
): Effect.Effect<
|
|
566
|
-
Block[],
|
|
567
|
-
SqlError.SqlError | DuplicateBlockPositionError | DuplicateBlockLamportTimestampError,
|
|
568
|
-
SqlClient.SqlClient | SqlTransaction.SqlTransaction
|
|
569
|
-
> =>
|
|
524
|
+
): Effect.Effect<Block[], SqlError.SqlError, SqlClient.SqlClient | SqlTransaction.SqlTransaction> =>
|
|
570
525
|
Effect.gen(this, function* () {
|
|
571
526
|
const sql = yield* SqlClient.SqlClient;
|
|
572
527
|
|
|
@@ -661,17 +616,24 @@ export class FeedStore {
|
|
|
661
616
|
blocksBySpaceNamespace.get(spaceNamespaceKey)!.blocks.push(block);
|
|
662
617
|
}
|
|
663
618
|
|
|
664
|
-
// 4. Call append once per (spaceId, namespace) batch.
|
|
665
|
-
|
|
666
|
-
|
|
619
|
+
// 4. Call append once per (spaceId, namespace) batch and assign returned positions.
|
|
620
|
+
const positionByBlock = new Map<Block, number | null>();
|
|
621
|
+
for (const { spaceId, feedNamespace, blocks: batchBlocks } of blocksBySpaceNamespace.values()) {
|
|
622
|
+
const { positions } = yield* this.append({
|
|
667
623
|
requestId: 'local-append',
|
|
668
|
-
blocks,
|
|
624
|
+
blocks: batchBlocks,
|
|
669
625
|
spaceId,
|
|
670
626
|
feedNamespace,
|
|
671
627
|
});
|
|
628
|
+
for (let i = 0; i < batchBlocks.length; i++) {
|
|
629
|
+
positionByBlock.set(batchBlocks[i], positions[i] ?? null);
|
|
630
|
+
}
|
|
672
631
|
}
|
|
673
632
|
|
|
674
|
-
return blocks
|
|
633
|
+
return blocks.map((block) => ({
|
|
634
|
+
...block,
|
|
635
|
+
position: positionByBlock.get(block) ?? block.position,
|
|
636
|
+
}));
|
|
675
637
|
}).pipe(Effect.withSpan('FeedStore.appendLocal')),
|
|
676
638
|
);
|
|
677
639
|
|
|
@@ -685,6 +647,23 @@ export class FeedStore {
|
|
|
685
647
|
Effect.gen(this, function* () {
|
|
686
648
|
const sql = yield* SqlClient.SqlClient;
|
|
687
649
|
for (const block of request.blocks) {
|
|
650
|
+
// Fold the conflict check into the UPDATE itself: only write when the row is
|
|
651
|
+
// unset or already at the requested position. RETURNING tells us whether the
|
|
652
|
+
// write took effect, so we avoid the per-block SELECT round-trip on the
|
|
653
|
+
// common path. Conflicts (rare) take the slow path below.
|
|
654
|
+
const updated = yield* sql<{ position: number | null }>`
|
|
655
|
+
UPDATE blocks SET position = ${block.position}
|
|
656
|
+
WHERE feedPrivateId = (
|
|
657
|
+
SELECT feedPrivateId FROM feeds
|
|
658
|
+
WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
|
|
659
|
+
)
|
|
660
|
+
AND actorId = ${block.actorId} AND sequence = ${block.sequence}
|
|
661
|
+
AND (position IS NULL OR position = ${block.position})
|
|
662
|
+
RETURNING position
|
|
663
|
+
`;
|
|
664
|
+
if (updated.length > 0) {
|
|
665
|
+
continue;
|
|
666
|
+
}
|
|
688
667
|
const existing = yield* sql<{ position: number | null }>`
|
|
689
668
|
SELECT position FROM blocks
|
|
690
669
|
WHERE feedPrivateId = (
|
|
@@ -705,16 +684,56 @@ export class FeedStore {
|
|
|
705
684
|
}),
|
|
706
685
|
);
|
|
707
686
|
}
|
|
708
|
-
yield* sql`
|
|
709
|
-
UPDATE blocks SET position = ${block.position}
|
|
710
|
-
WHERE feedPrivateId = (
|
|
711
|
-
SELECT feedPrivateId FROM feeds
|
|
712
|
-
WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
|
|
713
|
-
)
|
|
714
|
-
AND actorId = ${block.actorId} AND sequence = ${block.sequence}
|
|
715
|
-
`;
|
|
716
687
|
}
|
|
717
688
|
}).pipe(Effect.withSpan('FeedStore.setPosition'));
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* Gets all feeds and their blocks for a space, organized by feed ID and namespace.
|
|
692
|
+
* Used for space archive export.
|
|
693
|
+
*/
|
|
694
|
+
getAllFeedsForSpace = (opts: {
|
|
695
|
+
spaceId: SpaceId;
|
|
696
|
+
}): Effect.Effect<
|
|
697
|
+
Array<{
|
|
698
|
+
feedId: string;
|
|
699
|
+
feedNamespace: string;
|
|
700
|
+
blocks: Block[];
|
|
701
|
+
}>,
|
|
702
|
+
SqlError.SqlError,
|
|
703
|
+
SqlClient.SqlClient
|
|
704
|
+
> =>
|
|
705
|
+
Effect.gen(this, function* () {
|
|
706
|
+
const sql = yield* SqlClient.SqlClient;
|
|
707
|
+
|
|
708
|
+
const feeds = yield* sql<{ feedId: string; feedNamespace: string }>`
|
|
709
|
+
SELECT DISTINCT feedId, feedNamespace FROM feeds WHERE spaceId = ${opts.spaceId}
|
|
710
|
+
`;
|
|
711
|
+
|
|
712
|
+
const result: Array<{ feedId: string; feedNamespace: string; blocks: Block[] }> = [];
|
|
713
|
+
|
|
714
|
+
for (const feed of feeds) {
|
|
715
|
+
const blocks = yield* sql<Block>`
|
|
716
|
+
SELECT blocks.*, feeds.feedId, feeds.feedNamespace
|
|
717
|
+
FROM blocks
|
|
718
|
+
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
719
|
+
WHERE feeds.spaceId = ${opts.spaceId}
|
|
720
|
+
AND feeds.feedId = ${feed.feedId}
|
|
721
|
+
AND feeds.feedNamespace = ${feed.feedNamespace}
|
|
722
|
+
ORDER BY blocks.sequence ASC
|
|
723
|
+
`;
|
|
724
|
+
|
|
725
|
+
result.push({
|
|
726
|
+
feedId: feed.feedId,
|
|
727
|
+
feedNamespace: feed.feedNamespace,
|
|
728
|
+
blocks: blocks.map((row) => ({
|
|
729
|
+
...row,
|
|
730
|
+
data: new Uint8Array(row.data),
|
|
731
|
+
})),
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
return result;
|
|
736
|
+
}).pipe(Effect.withSpan('FeedStore.getAllFeedsForSpace'));
|
|
718
737
|
}
|
|
719
738
|
|
|
720
739
|
const encodeCursor = (token: string, insertionId: number) => FeedCursor.make(`${token}|${insertionId}`);
|
package/src/sync-client.ts
CHANGED
|
@@ -7,7 +7,9 @@ import * as Array from 'effect/Array';
|
|
|
7
7
|
import * as Deferred from 'effect/Deferred';
|
|
8
8
|
import * as Effect from 'effect/Effect';
|
|
9
9
|
|
|
10
|
+
import { Context, ContextDisposedError } from '@dxos/context';
|
|
10
11
|
import type { SpaceId } from '@dxos/keys';
|
|
12
|
+
import { log } from '@dxos/log';
|
|
11
13
|
import { type FeedProtocol } from '@dxos/protocols';
|
|
12
14
|
import type { SqlTransaction } from '@dxos/sql-sqlite';
|
|
13
15
|
|
|
@@ -30,7 +32,7 @@ export type SyncClientOptions = {
|
|
|
30
32
|
serverPeerId?: string;
|
|
31
33
|
feedStore: FeedStore;
|
|
32
34
|
/** Send a protocol message to the server. Returns Effect. */
|
|
33
|
-
sendMessage: (message: RequestMessage) => Effect.Effect<void, unknown, never>;
|
|
35
|
+
sendMessage: (ctx: Context, message: RequestMessage) => Effect.Effect<void, unknown, never>;
|
|
34
36
|
};
|
|
35
37
|
|
|
36
38
|
/**
|
|
@@ -73,21 +75,44 @@ export class SyncClient {
|
|
|
73
75
|
*/
|
|
74
76
|
handleMessage(message: ProtocolMessage): Effect.Effect<void, never, never> {
|
|
75
77
|
const requestId = 'requestId' in message && message.requestId != null ? String(message.requestId) : undefined;
|
|
76
|
-
if (requestId == null)
|
|
78
|
+
if (requestId == null) {
|
|
79
|
+
log.trace('feed sync client response ignored (no request id)', {
|
|
80
|
+
tag: message._tag,
|
|
81
|
+
senderPeerId: message.senderPeerId,
|
|
82
|
+
});
|
|
83
|
+
return Effect.void;
|
|
84
|
+
}
|
|
77
85
|
const deferred = this.#handlers.get(requestId);
|
|
78
|
-
if (deferred == null)
|
|
86
|
+
if (deferred == null) {
|
|
87
|
+
log.trace('feed sync client response ignored (no pending rpc)', {
|
|
88
|
+
requestId,
|
|
89
|
+
tag: message._tag,
|
|
90
|
+
senderPeerId: message.senderPeerId,
|
|
91
|
+
});
|
|
92
|
+
return Effect.void;
|
|
93
|
+
}
|
|
79
94
|
this.#handlers.delete(requestId);
|
|
95
|
+
log('feed sync client rpc completed', {
|
|
96
|
+
requestId,
|
|
97
|
+
tag: message._tag,
|
|
98
|
+
blockCount: 'blocks' in message ? message.blocks.length : undefined,
|
|
99
|
+
positionCount: 'positions' in message ? message.positions.length : undefined,
|
|
100
|
+
error: message._tag === 'Error' ? message.message : undefined,
|
|
101
|
+
});
|
|
80
102
|
if (message._tag === 'Error') {
|
|
81
103
|
return Effect.andThen(Deferred.fail(deferred, new Error(message.message)), () => Effect.void);
|
|
82
104
|
}
|
|
83
105
|
return Effect.andThen(Deferred.succeed(deferred, message), () => Effect.void);
|
|
84
106
|
}
|
|
85
107
|
|
|
86
|
-
pull(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
108
|
+
pull(
|
|
109
|
+
ctx: Context,
|
|
110
|
+
opts: {
|
|
111
|
+
spaceId: SpaceId;
|
|
112
|
+
feedNamespace: string;
|
|
113
|
+
limit?: number;
|
|
114
|
+
},
|
|
115
|
+
): Effect.Effect<{ done: boolean }, unknown, SqlClient.SqlClient | SqlTransaction.SqlTransaction> {
|
|
91
116
|
const self = this;
|
|
92
117
|
return Effect.gen(function* () {
|
|
93
118
|
const lastPulledPosition = yield* self.#feedStore.getSyncState({
|
|
@@ -97,6 +122,14 @@ export class SyncClient {
|
|
|
97
122
|
const requestId = crypto.randomUUID();
|
|
98
123
|
const deferred = yield* Deferred.make<ProtocolMessage, Error>();
|
|
99
124
|
self.#handlers.set(requestId, deferred);
|
|
125
|
+
const cleanupDispose = ctx.disposed
|
|
126
|
+
? () => {}
|
|
127
|
+
: ctx.onDispose(() => {
|
|
128
|
+
Effect.runFork(Deferred.fail(deferred, new ContextDisposedError()));
|
|
129
|
+
});
|
|
130
|
+
if (ctx.disposed) {
|
|
131
|
+
yield* Deferred.fail(deferred, new ContextDisposedError());
|
|
132
|
+
}
|
|
100
133
|
const request: RequestPayload = {
|
|
101
134
|
_tag: 'QueryRequest',
|
|
102
135
|
requestId,
|
|
@@ -105,10 +138,28 @@ export class SyncClient {
|
|
|
105
138
|
position: lastPulledPosition,
|
|
106
139
|
limit: opts.limit,
|
|
107
140
|
};
|
|
108
|
-
|
|
109
|
-
|
|
141
|
+
log('feed sync client pull rpc sending', {
|
|
142
|
+
requestId,
|
|
143
|
+
spaceId: opts.spaceId,
|
|
144
|
+
feedNamespace: opts.feedNamespace,
|
|
145
|
+
afterPosition: lastPulledPosition,
|
|
146
|
+
limit: opts.limit,
|
|
147
|
+
});
|
|
148
|
+
yield* self.#sendMessage(ctx, self.#withPeerIds(request));
|
|
149
|
+
const message = yield* Effect.ensuring(
|
|
150
|
+
Deferred.await(deferred),
|
|
151
|
+
Effect.sync(() => {
|
|
152
|
+
cleanupDispose();
|
|
153
|
+
self.#handlers.delete(requestId);
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
110
156
|
const response = yield* self.#expectResponse<QueryResponse>(requestId, message, 'QueryResponse');
|
|
111
157
|
if (response.blocks.length === 0) {
|
|
158
|
+
log.trace('feed sync client pull done (empty batch)', {
|
|
159
|
+
requestId,
|
|
160
|
+
spaceId: opts.spaceId,
|
|
161
|
+
feedNamespace: opts.feedNamespace,
|
|
162
|
+
});
|
|
112
163
|
return { done: true };
|
|
113
164
|
}
|
|
114
165
|
yield* self.#feedStore.append({
|
|
@@ -128,15 +179,26 @@ export class SyncClient {
|
|
|
128
179
|
lastPulledPosition: maxPulledPosition,
|
|
129
180
|
});
|
|
130
181
|
|
|
182
|
+
log('feed sync client pull applied batch', {
|
|
183
|
+
requestId,
|
|
184
|
+
spaceId: opts.spaceId,
|
|
185
|
+
feedNamespace: opts.feedNamespace,
|
|
186
|
+
batchSize: response.blocks.length,
|
|
187
|
+
hasMore: response.hasMore,
|
|
188
|
+
maxPulledPosition,
|
|
189
|
+
});
|
|
131
190
|
return { done: false };
|
|
132
191
|
});
|
|
133
192
|
}
|
|
134
193
|
|
|
135
|
-
push(
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
194
|
+
push(
|
|
195
|
+
ctx: Context,
|
|
196
|
+
opts: {
|
|
197
|
+
spaceId: SpaceId;
|
|
198
|
+
feedNamespace: string;
|
|
199
|
+
limit?: number;
|
|
200
|
+
},
|
|
201
|
+
): Effect.Effect<{ done: boolean }, unknown, SqlClient.SqlClient | SqlTransaction.SqlTransaction> {
|
|
140
202
|
const self = this;
|
|
141
203
|
return Effect.gen(function* () {
|
|
142
204
|
const unpositioned = yield* self.#feedStore.query({
|
|
@@ -146,11 +208,23 @@ export class SyncClient {
|
|
|
146
208
|
limit: opts.limit,
|
|
147
209
|
});
|
|
148
210
|
if (unpositioned.blocks.length === 0) {
|
|
211
|
+
log.trace('feed sync client push skipped (nothing to send)', {
|
|
212
|
+
spaceId: opts.spaceId,
|
|
213
|
+
feedNamespace: opts.feedNamespace,
|
|
214
|
+
});
|
|
149
215
|
return { done: true };
|
|
150
216
|
}
|
|
151
217
|
const requestId = crypto.randomUUID();
|
|
152
218
|
const deferred = yield* Deferred.make<ProtocolMessage, Error>();
|
|
153
219
|
self.#handlers.set(requestId, deferred);
|
|
220
|
+
const cleanupDispose = ctx.disposed
|
|
221
|
+
? () => {}
|
|
222
|
+
: ctx.onDispose(() => {
|
|
223
|
+
Effect.runFork(Deferred.fail(deferred, new ContextDisposedError()));
|
|
224
|
+
});
|
|
225
|
+
if (ctx.disposed) {
|
|
226
|
+
yield* Deferred.fail(deferred, new ContextDisposedError());
|
|
227
|
+
}
|
|
154
228
|
const request: RequestPayload = {
|
|
155
229
|
_tag: 'AppendRequest',
|
|
156
230
|
requestId,
|
|
@@ -158,8 +232,20 @@ export class SyncClient {
|
|
|
158
232
|
feedNamespace: opts.feedNamespace,
|
|
159
233
|
blocks: unpositioned.blocks,
|
|
160
234
|
};
|
|
161
|
-
|
|
162
|
-
|
|
235
|
+
log('feed sync client push rpc sending', {
|
|
236
|
+
requestId,
|
|
237
|
+
spaceId: opts.spaceId,
|
|
238
|
+
feedNamespace: opts.feedNamespace,
|
|
239
|
+
blockCount: unpositioned.blocks.length,
|
|
240
|
+
});
|
|
241
|
+
yield* self.#sendMessage(ctx, self.#withPeerIds(request));
|
|
242
|
+
const message = yield* Effect.ensuring(
|
|
243
|
+
Deferred.await(deferred),
|
|
244
|
+
Effect.sync(() => {
|
|
245
|
+
cleanupDispose();
|
|
246
|
+
self.#handlers.delete(requestId);
|
|
247
|
+
}),
|
|
248
|
+
);
|
|
163
249
|
const response = yield* self.#expectResponse<AppendResponse>(requestId, message, 'AppendResponse');
|
|
164
250
|
yield* self.#feedStore.setPosition({
|
|
165
251
|
spaceId: opts.spaceId,
|
|
@@ -171,6 +257,12 @@ export class SyncClient {
|
|
|
171
257
|
position,
|
|
172
258
|
})),
|
|
173
259
|
});
|
|
260
|
+
log('feed sync client push positions applied', {
|
|
261
|
+
requestId,
|
|
262
|
+
spaceId: opts.spaceId,
|
|
263
|
+
feedNamespace: opts.feedNamespace,
|
|
264
|
+
positionCount: response.positions.length,
|
|
265
|
+
});
|
|
174
266
|
return { done: false };
|
|
175
267
|
});
|
|
176
268
|
}
|