@shivaedev/contract 0.45.0 → 0.47.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/CHANGELOG.md +26 -0
- package/README.md +71 -37
- package/dist/AutomaticQueryOptions.d.ts +2 -0
- package/dist/AutomaticQueryOptions.d.ts.map +1 -1
- package/dist/CommandGuard.d.ts +2 -1
- package/dist/CommandGuard.d.ts.map +1 -1
- package/dist/CommandGuard.js +3 -2
- package/dist/CommandGuard.js.map +1 -1
- package/dist/HostVersion.d.ts +6 -1
- package/dist/HostVersion.d.ts.map +1 -1
- package/dist/LiveSync.d.ts +18 -0
- package/dist/LiveSync.d.ts.map +1 -0
- package/dist/LiveSync.js +9 -0
- package/dist/LiveSync.js.map +1 -0
- package/dist/adapters/activityState.d.ts.map +1 -1
- package/dist/adapters/activityState.js +1 -0
- package/dist/adapters/activityState.js.map +1 -1
- package/dist/adapters/liveHintStream.d.ts +3 -19
- package/dist/adapters/liveHintStream.d.ts.map +1 -1
- package/dist/adapters/liveHintStream.js +2 -2
- package/dist/adapters/liveHintStream.js.map +1 -1
- package/dist/adapters/openLiveHints.d.ts +3 -19
- package/dist/adapters/openLiveHints.d.ts.map +1 -1
- package/dist/adapters/openLiveHints.js +21 -3
- package/dist/adapters/openLiveHints.js.map +1 -1
- package/dist/adapters/standaloneLiveServer.d.ts +5 -18
- package/dist/adapters/standaloneLiveServer.d.ts.map +1 -1
- package/dist/adapters/standaloneLiveServer.js +6 -1
- package/dist/adapters/standaloneLiveServer.js.map +1 -1
- package/dist/automaticLive.d.ts.map +1 -1
- package/dist/automaticLive.js +21 -1
- package/dist/automaticLive.js.map +1 -1
- package/dist/automaticQuery.d.ts +1 -1
- package/dist/automaticQuery.d.ts.map +1 -1
- package/dist/automaticQuery.js +7 -1
- package/dist/automaticQuery.js.map +1 -1
- package/dist/bind.js +3 -2
- package/dist/bind.js.map +1 -1
- package/dist/catchUp.d.ts +6 -0
- package/dist/catchUp.d.ts.map +1 -0
- package/dist/catchUp.js +9 -0
- package/dist/catchUp.js.map +1 -0
- package/dist/changesSuccess.d.ts +2 -1
- package/dist/changesSuccess.d.ts.map +1 -1
- package/dist/changesSuccess.js +3 -0
- package/dist/changesSuccess.js.map +1 -1
- package/dist/clientActivities.d.ts +2 -0
- package/dist/clientActivities.d.ts.map +1 -1
- package/dist/clientActivities.js +1 -0
- package/dist/clientActivities.js.map +1 -1
- package/dist/clientActivity.d.ts +1 -0
- package/dist/clientActivity.d.ts.map +1 -1
- package/dist/clientActivity.js +1 -1
- package/dist/clientActivity.js.map +1 -1
- package/dist/clientOperations.d.ts +1 -0
- package/dist/clientOperations.d.ts.map +1 -1
- package/dist/clientOperations.js +17 -1
- package/dist/clientOperations.js.map +1 -1
- package/dist/clientOptions.d.ts +1 -0
- package/dist/clientOptions.d.ts.map +1 -1
- package/dist/commandSignal.d.ts +5 -0
- package/dist/commandSignal.d.ts.map +1 -0
- package/dist/commandSignal.js +25 -0
- package/dist/commandSignal.js.map +1 -0
- package/dist/connectionProtocol.d.ts +10 -14
- package/dist/connectionProtocol.d.ts.map +1 -1
- package/dist/connectionProtocol.js +8 -3
- package/dist/connectionProtocol.js.map +1 -1
- package/dist/live-server/initialize.d.ts +1 -0
- package/dist/live-server/initialize.d.ts.map +1 -1
- package/dist/live-server/initialize.js +2 -1
- package/dist/live-server/initialize.js.map +1 -1
- package/dist/live-server/register.d.ts +2 -19
- package/dist/live-server/register.d.ts.map +1 -1
- package/dist/live-server/register.js +2 -2
- package/dist/live-server/register.js.map +1 -1
- package/dist/live-server/service.d.ts +7 -37
- package/dist/live-server/service.d.ts.map +1 -1
- package/dist/live-server/service.js +2 -0
- package/dist/live-server/service.js.map +1 -1
- package/dist/live-server/stream.d.ts +1 -18
- package/dist/live-server/stream.d.ts.map +1 -1
- package/dist/live-server/stream.js +2 -2
- package/dist/live-server/stream.js.map +1 -1
- package/dist/live-server/subscribe.d.ts.map +1 -1
- package/dist/live-server/subscribe.js +6 -2
- package/dist/live-server/subscribe.js.map +1 -1
- package/dist/live-server/syncFrames.d.ts +5 -0
- package/dist/live-server/syncFrames.d.ts.map +1 -0
- package/dist/live-server/syncFrames.js +16 -0
- package/dist/live-server/syncFrames.js.map +1 -0
- package/dist/live-server/synced.d.ts +7 -0
- package/dist/live-server/synced.d.ts.map +1 -0
- package/dist/live-server/synced.js +11 -0
- package/dist/live-server/synced.js.map +1 -0
- package/dist/liveHintQueue.d.ts +5 -19
- package/dist/liveHintQueue.d.ts.map +1 -1
- package/dist/liveHintQueue.js +4 -2
- package/dist/liveHintQueue.js.map +1 -1
- package/dist/liveRetry.d.ts +1 -0
- package/dist/liveRetry.d.ts.map +1 -1
- package/dist/liveRetry.js +5 -2
- package/dist/liveRetry.js.map +1 -1
- package/dist/liveSyncRequest.d.ts +6 -0
- package/dist/liveSyncRequest.d.ts.map +1 -0
- package/dist/liveSyncRequest.js +7 -0
- package/dist/liveSyncRequest.js.map +1 -0
- package/dist/liveSyncRpc.d.ts +7 -0
- package/dist/liveSyncRpc.d.ts.map +1 -0
- package/dist/liveSyncRpc.js +10 -0
- package/dist/liveSyncRpc.js.map +1 -0
- package/dist/makeLiveServer.d.ts +5 -18
- package/dist/makeLiveServer.d.ts.map +1 -1
- package/dist/publicConfig.d.ts +6 -1
- package/dist/publicConfig.d.ts.map +1 -1
- package/dist/queryDelivery.d.ts +2 -1
- package/dist/queryDelivery.d.ts.map +1 -1
- package/dist/queryDelivery.js +20 -4
- package/dist/queryDelivery.js.map +1 -1
- package/dist/querySubscription.d.ts +1 -0
- package/dist/querySubscription.d.ts.map +1 -1
- package/dist/querySubscription.js.map +1 -1
- package/dist/queryUpdate.d.ts +15 -1
- package/dist/queryUpdate.d.ts.map +1 -1
- package/dist/queryUpdate.js +4 -0
- package/dist/queryUpdate.js.map +1 -1
- package/dist/reflectCommand.d.ts +10 -0
- package/dist/reflectCommand.d.ts.map +1 -0
- package/dist/reflectCommand.js +35 -0
- package/dist/reflectCommand.js.map +1 -0
- package/dist/reflectedStream.d.ts +18 -0
- package/dist/reflectedStream.d.ts.map +1 -0
- package/dist/reflectedStream.js +35 -0
- package/dist/reflectedStream.js.map +1 -0
- package/dist/reflection.d.ts +26 -0
- package/dist/reflection.d.ts.map +1 -0
- package/dist/reflection.js +94 -0
- package/dist/reflection.js.map +1 -0
- package/dist/rpcClient.d.ts +3 -2
- package/dist/rpcClient.d.ts.map +1 -1
- package/dist/rpcClient.js +12 -6
- package/dist/rpcClient.js.map +1 -1
- package/dist/screenSync.d.ts +8 -0
- package/dist/screenSync.d.ts.map +1 -0
- package/dist/screenSync.js +6 -0
- package/dist/screenSync.js.map +1 -0
- package/dist/socketProtocol.d.ts +1 -1
- package/dist/socketProtocol.d.ts.map +1 -1
- package/dist/syncHeader.d.ts +7 -0
- package/dist/syncHeader.d.ts.map +1 -0
- package/dist/syncHeader.js +22 -0
- package/dist/syncHeader.js.map +1 -0
- package/package.json +9 -9
- package/src/AutomaticQueryOptions.ts +2 -0
- package/src/CommandGuard.ts +12 -4
- package/src/LiveSync.ts +9 -0
- package/src/adapters/activityState.ts +1 -0
- package/src/adapters/liveHintStream.ts +6 -2
- package/src/adapters/openLiveHints.ts +25 -3
- package/src/adapters/standaloneLiveServer.ts +6 -1
- package/src/automaticLive.ts +24 -2
- package/src/automaticQuery.ts +7 -0
- package/src/bind.ts +3 -2
- package/src/catchUp.ts +21 -0
- package/src/changesSuccess.ts +4 -1
- package/src/clientActivities.ts +2 -1
- package/src/clientActivity.ts +2 -1
- package/src/clientOperations.ts +28 -10
- package/src/clientOptions.ts +1 -0
- package/src/commandSignal.ts +27 -0
- package/src/connectionProtocol.ts +9 -3
- package/src/live-server/initialize.ts +2 -1
- package/src/live-server/register.ts +2 -2
- package/src/live-server/service.ts +2 -0
- package/src/live-server/stream.ts +2 -2
- package/src/live-server/subscribe.ts +10 -4
- package/src/live-server/syncFrames.ts +19 -0
- package/src/live-server/synced.ts +13 -0
- package/src/liveHintQueue.ts +9 -4
- package/src/liveRetry.ts +6 -2
- package/src/liveSyncRequest.ts +17 -0
- package/src/liveSyncRpc.ts +11 -0
- package/src/queryDelivery.ts +29 -5
- package/src/querySubscription.ts +1 -0
- package/src/queryUpdate.ts +7 -1
- package/src/reflectCommand.ts +48 -0
- package/src/reflectedStream.ts +54 -0
- package/src/reflection.ts +110 -0
- package/src/rpcClient.ts +12 -6
- package/src/screenSync.ts +10 -0
- package/src/syncHeader.ts +27 -0
- package/dist/liveAnswers.d.ts +0 -9
- package/dist/liveAnswers.d.ts.map +0 -1
- package/dist/liveAnswers.js +0 -12
- package/dist/liveAnswers.js.map +0 -1
- package/src/liveAnswers.ts +0 -13
package/src/liveHintQueue.ts
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
import { Effect, Queue } from "effect";
|
|
2
2
|
import type { LiveHint } from "#live.ts";
|
|
3
3
|
|
|
4
|
+
// A subscription's hint also carries the sync tokens that arrived behind its keys; they never reach a hint stream's wire.
|
|
5
|
+
export type SubscriptionHint = LiveHint & { readonly synced?: readonly number[] };
|
|
6
|
+
|
|
4
7
|
export const liveHintQueue = Effect.gen(function* () {
|
|
5
|
-
const queue = yield* Queue.bounded<
|
|
8
|
+
const queue = yield* Queue.bounded<SubscriptionHint>(1);
|
|
6
9
|
let count = 0;
|
|
7
|
-
function offer(hint:
|
|
10
|
+
function offer(hint: SubscriptionHint) {
|
|
8
11
|
const queued = Queue.takeUnsafe(queue);
|
|
9
12
|
const pending = queued?._tag === "Success" ? queued.value : undefined;
|
|
10
13
|
count = pending === undefined ? 1 : count + 1;
|
|
14
|
+
const ids = [...new Set([...(pending?.synced ?? []), ...(hint.synced ?? [])])];
|
|
15
|
+
const synced = ids.length === 0 ? {} : { synced: ids };
|
|
11
16
|
if (pending?._tag === "Resync" || hint._tag === "Resync" || count > 256) {
|
|
12
|
-
Queue.offerUnsafe(queue, { _tag: "Resync" });
|
|
17
|
+
Queue.offerUnsafe(queue, { _tag: "Resync", ...synced });
|
|
13
18
|
return;
|
|
14
19
|
}
|
|
15
20
|
const keys = new Map([...(pending?.keys ?? []), ...hint.keys].map((key) => [JSON.stringify(key), key]));
|
|
16
|
-
Queue.offerUnsafe(queue, { _tag: "Changed", keys: [...keys.values()] });
|
|
21
|
+
Queue.offerUnsafe(queue, { _tag: "Changed", keys: [...keys.values()], ...synced });
|
|
17
22
|
}
|
|
18
23
|
return { offer, queue };
|
|
19
24
|
});
|
package/src/liveRetry.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { Cause, type Duration, Effect, Option, Predicate, Queue, Schedule } from "effect";
|
|
2
2
|
|
|
3
|
+
export function retriesLive<E>(failure: Cause.Cause<E>): boolean {
|
|
4
|
+
const error = Cause.findErrorOption(failure);
|
|
5
|
+
return Option.isNone(error) || Predicate.isTagged(error.value, "RpcClientError");
|
|
6
|
+
}
|
|
7
|
+
|
|
3
8
|
export function liveRetry<E, R>(
|
|
4
9
|
attempt: Effect.Effect<void, E, R>,
|
|
5
10
|
ended: (failure: Option.Option<Cause.Cause<E>>, retry: boolean) => void,
|
|
@@ -8,8 +13,7 @@ export function liveRetry<E, R>(
|
|
|
8
13
|
) {
|
|
9
14
|
const next = Effect.matchCause(attempt, {
|
|
10
15
|
onFailure: (failure) => {
|
|
11
|
-
const
|
|
12
|
-
const retry = Option.isNone(error) || Predicate.isTagged(error.value, "RpcClientError");
|
|
16
|
+
const retry = retriesLive(failure);
|
|
13
17
|
ended(Option.some(failure), retry);
|
|
14
18
|
return retry;
|
|
15
19
|
},
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Effect, Schema } from "effect";
|
|
2
|
+
import type { Rpc, RpcClient } from "effect/rpc";
|
|
3
|
+
import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
4
|
+
import type { SyncToken } from "@shivaedev/changes/syncToken.ts";
|
|
5
|
+
import { LIVE_SYNC } from "#liveSyncRpc.ts";
|
|
6
|
+
import { commandSyncHeaders } from "#syncHeader.ts";
|
|
7
|
+
|
|
8
|
+
type Erased = Rpc.Rpc<string, Schema.Top, Schema.Top, Schema.Top>;
|
|
9
|
+
|
|
10
|
+
export function liveSyncRequest(client: RpcClient.RpcClient.Flat<Rpc.Any, RpcClientError>, token: SyncToken): Effect.Effect<void, unknown>;
|
|
11
|
+
export function liveSyncRequest(
|
|
12
|
+
client: RpcClient.RpcClient.Flat<Rpc.Any, RpcClientError>,
|
|
13
|
+
token: SyncToken,
|
|
14
|
+
): Effect.Effect<unknown, unknown, unknown> {
|
|
15
|
+
const erased: RpcClient.RpcClient.Flat<Erased, RpcClientError> = client;
|
|
16
|
+
return erased(LIVE_SYNC, undefined, { headers: commandSyncHeaders(token) });
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { Rpc, RpcGroup } from "effect/rpc";
|
|
3
|
+
import { CommandInvalidation } from "#serverMetadata.ts";
|
|
4
|
+
|
|
5
|
+
export const LIVE_SYNC_CONTRACT = "platform.liveSync";
|
|
6
|
+
export const LIVE_SYNC_COMMAND = "request";
|
|
7
|
+
const LIVE_SYNC_TAG = `${LIVE_SYNC_CONTRACT}.${LIVE_SYNC_COMMAND}` as const;
|
|
8
|
+
export const LIVE_SYNC: string = LIVE_SYNC_TAG;
|
|
9
|
+
|
|
10
|
+
// The browser's copy of the sync command: the host's declaration in LiveSync.ts is an operation file, which a browser bundle refuses.
|
|
11
|
+
export const liveSyncRpc = RpcGroup.make(Rpc.make(LIVE_SYNC_TAG, { success: Schema.Void }).annotate(CommandInvalidation, { invalidates: () => [] }));
|
package/src/queryDelivery.ts
CHANGED
|
@@ -3,16 +3,36 @@ import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
|
3
3
|
import * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
5
5
|
import type { QuerySignals } from "#querySignals.ts";
|
|
6
|
-
import type { QueryUpdate, SubscriptionEvent } from "#queryUpdate.ts";
|
|
6
|
+
import type { QuerySynced, QuerySyncing, QueryUpdate, SubscriptionEvent } from "#queryUpdate.ts";
|
|
7
|
+
import type { ReflectionFollower } from "#reflectedStream.ts";
|
|
8
|
+
|
|
9
|
+
function confirm(update: QuerySyncing | QuerySynced, follower: ReflectionFollower | undefined) {
|
|
10
|
+
if (update._tag === "Syncing") {
|
|
11
|
+
follower?.armed();
|
|
12
|
+
} else {
|
|
13
|
+
follower?.confirmed(update.ids);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
7
16
|
|
|
8
17
|
export function queryDelivery<A, E>(decode: (value: unknown) => A, paused: Atom.Atom<boolean>, signals: Atom.Writable<QuerySignals>) {
|
|
9
18
|
const updates = Atom.make(0);
|
|
10
19
|
const pushed = Atom.make<AsyncResult.AsyncResult<A, E>>(AsyncResult.initial(true));
|
|
11
|
-
const deliver = Effect.fnUntraced(function* (
|
|
20
|
+
const deliver = Effect.fnUntraced(function* (
|
|
21
|
+
update: QueryUpdate,
|
|
22
|
+
follower: ReflectionFollower | undefined,
|
|
23
|
+
): Effect.fn.Return<SubscriptionEvent | undefined, never, AtomRegistry.AtomRegistry> {
|
|
12
24
|
const registry = yield* AtomRegistry.AtomRegistry;
|
|
25
|
+
const showing = !registry.get(paused);
|
|
26
|
+
// A paused screen shows none of what its stream passed, so its confirmations wait for the stream it reopens on resume.
|
|
27
|
+
const shown = showing ? follower : undefined;
|
|
28
|
+
if (update._tag === "Syncing" || update._tag === "Synced") {
|
|
29
|
+
confirm(update, shown);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
shown?.data();
|
|
13
33
|
registry.update(updates, (current) => current + 1);
|
|
14
34
|
if (update._tag === "Unchanged") {
|
|
15
|
-
if (
|
|
35
|
+
if (showing) {
|
|
16
36
|
registry.update(signals, (current) => ({ ...current, completed: current.completed + 1, pending: false }));
|
|
17
37
|
}
|
|
18
38
|
return { _tag: "Delivered" };
|
|
@@ -20,14 +40,18 @@ export function queryDelivery<A, E>(decode: (value: unknown) => A, paused: Atom.
|
|
|
20
40
|
if (update._tag !== "Result") {
|
|
21
41
|
return update;
|
|
22
42
|
}
|
|
23
|
-
if (
|
|
43
|
+
if (showing) {
|
|
24
44
|
registry.set(pushed, AsyncResult.success(decode(update.value)));
|
|
25
45
|
}
|
|
26
46
|
return { _tag: "Delivered" };
|
|
27
47
|
});
|
|
28
48
|
return {
|
|
29
49
|
atom: pushed,
|
|
30
|
-
stream: <TError, R>(stream: Stream.Stream<QueryUpdate, TError, R
|
|
50
|
+
stream: <TError, R>(stream: Stream.Stream<QueryUpdate, TError, R>, follower?: ReflectionFollower) =>
|
|
51
|
+
stream.pipe(
|
|
52
|
+
Stream.mapEffect((update) => deliver(update, follower)),
|
|
53
|
+
Stream.filter((event) => event !== undefined),
|
|
54
|
+
),
|
|
31
55
|
updates,
|
|
32
56
|
};
|
|
33
57
|
}
|
package/src/querySubscription.ts
CHANGED
|
@@ -17,6 +17,7 @@ export interface QuerySubscription<E = never, R = never> {
|
|
|
17
17
|
readonly readScopes?: Effect.Effect<readonly (readonly Key[])[], E, R>;
|
|
18
18
|
readonly refresh?: Effect.Effect<Context.Context<never>, E, R>;
|
|
19
19
|
readonly scopes: readonly (readonly Key[])[];
|
|
20
|
+
readonly syncClient?: string | undefined;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
export function querySubscription<TRpc extends Rpc.Any>(
|
package/src/queryUpdate.ts
CHANGED
|
@@ -11,11 +11,17 @@ export interface QueryUnchanged {
|
|
|
11
11
|
readonly _tag: "Unchanged";
|
|
12
12
|
readonly queryIndex: number;
|
|
13
13
|
}
|
|
14
|
+
export const QuerySyncing = Schema.Struct({ _tag: Schema.Literal("Syncing") });
|
|
15
|
+
export type QuerySyncing = typeof QuerySyncing.Type;
|
|
16
|
+
export const QuerySynced = Schema.Struct({ _tag: Schema.Literal("Synced"), ids: Schema.Array(Schema.Number) });
|
|
17
|
+
export type QuerySynced = typeof QuerySynced.Type;
|
|
14
18
|
export type SubscriptionEvent = { readonly _tag: "Delivered" } | QueryReadDenied;
|
|
15
|
-
export type QueryUpdate = QueryReadDenied | QueryResult | QueryUnchanged;
|
|
19
|
+
export type QueryUpdate = QueryReadDenied | QueryResult | QueryUnchanged | QuerySyncing | QuerySynced;
|
|
16
20
|
|
|
17
21
|
export const QueryUpdate = Schema.Union([
|
|
18
22
|
Schema.Struct({ _tag: Schema.Literal("Result"), query: Schema.String, queryIndex: Schema.Number, value: Schema.Unknown }),
|
|
19
23
|
Schema.Struct({ _tag: Schema.Literal("Unchanged"), queryIndex: Schema.Number }),
|
|
24
|
+
QuerySyncing,
|
|
25
|
+
QuerySynced,
|
|
20
26
|
QueryReadDenied,
|
|
21
27
|
]);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Effect, Exit } from "effect";
|
|
2
|
+
import type { CommandState } from "#commandSignal.ts";
|
|
3
|
+
import { reflections } from "#reflection.ts";
|
|
4
|
+
import { commandSyncHeaders } from "#syncHeader.ts";
|
|
5
|
+
|
|
6
|
+
export interface CommandObserver {
|
|
7
|
+
readonly pending: () => () => void;
|
|
8
|
+
readonly signal: (state: CommandState) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function reflectCommand<A, E, R>(
|
|
12
|
+
client: object,
|
|
13
|
+
observer: CommandObserver,
|
|
14
|
+
send: (options?: { readonly headers: Record<string, string> }) => Effect.Effect<A, E, R>,
|
|
15
|
+
standalone = false,
|
|
16
|
+
): Effect.Effect<A, E, R> {
|
|
17
|
+
return Effect.suspend(() => {
|
|
18
|
+
const scope = reflections.get(client);
|
|
19
|
+
const reflection = scope?.reflection;
|
|
20
|
+
let own: number | undefined;
|
|
21
|
+
if (scope !== undefined) {
|
|
22
|
+
own = standalone ? scope.reflection.flight() : scope.reflection.send(scope.piggyback);
|
|
23
|
+
}
|
|
24
|
+
const finish = observer.pending();
|
|
25
|
+
observer.signal("sent");
|
|
26
|
+
const sent =
|
|
27
|
+
reflection === undefined || own === undefined ? send() : send({ headers: commandSyncHeaders({ client: reflection.client, id: own }) });
|
|
28
|
+
return Effect.onExit(sent, (exit) =>
|
|
29
|
+
Effect.sync(() => {
|
|
30
|
+
if (Exit.isFailure(exit)) {
|
|
31
|
+
reflection?.failed(own);
|
|
32
|
+
finish();
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
observer.signal("accepted");
|
|
36
|
+
function reflected() {
|
|
37
|
+
finish();
|
|
38
|
+
observer.signal("reflected");
|
|
39
|
+
}
|
|
40
|
+
if (reflection === undefined) {
|
|
41
|
+
reflected();
|
|
42
|
+
} else {
|
|
43
|
+
reflection.accepted(own, reflected);
|
|
44
|
+
}
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Effect, Exit, Stream } from "effect";
|
|
2
|
+
import { Headers } from "effect/http";
|
|
3
|
+
import { retriesLive } from "#liveRetry.ts";
|
|
4
|
+
import { type FollowedStream, type Reflection, reflections } from "#reflection.ts";
|
|
5
|
+
import { streamSyncHeaders } from "#syncHeader.ts";
|
|
6
|
+
|
|
7
|
+
export interface StreamAnchor extends FollowedStream {
|
|
8
|
+
mounted: boolean;
|
|
9
|
+
reflection: Reflection | undefined;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ReflectionFollower {
|
|
13
|
+
readonly armed: () => void;
|
|
14
|
+
readonly confirmed: (ids: readonly number[]) => void;
|
|
15
|
+
readonly data: () => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// A mounted query keeps one place in its connection's waits across attempts; it leaves only when it unmounts or stops retrying.
|
|
19
|
+
export function followReflection(client: object, anchor: StreamAnchor) {
|
|
20
|
+
const reflection = reflections.get(client)?.reflection;
|
|
21
|
+
if (reflection === undefined) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
const follower: ReflectionFollower = {
|
|
25
|
+
armed: () => reflection.armed(anchor),
|
|
26
|
+
confirmed: (ids) => reflection.confirmed(anchor, ids),
|
|
27
|
+
data: () => reflection.data(anchor),
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
follower,
|
|
31
|
+
headers: (headers: Headers.Input | undefined) =>
|
|
32
|
+
Headers.merge(Headers.fromInput(headers), Headers.fromInput(streamSyncHeaders(reflection.client))),
|
|
33
|
+
lifetime: <A, E, R>(stream: Stream.Stream<A, E, R>) =>
|
|
34
|
+
stream.pipe(
|
|
35
|
+
Stream.onStart(
|
|
36
|
+
Effect.sync(() => {
|
|
37
|
+
anchor.reflection = reflection;
|
|
38
|
+
if (anchor.mounted) {
|
|
39
|
+
reflection.opening(anchor);
|
|
40
|
+
}
|
|
41
|
+
}),
|
|
42
|
+
),
|
|
43
|
+
Stream.onExit((exit) =>
|
|
44
|
+
Effect.sync(() => {
|
|
45
|
+
if (anchor.mounted && (Exit.isSuccess(exit) || retriesLive(exit.cause))) {
|
|
46
|
+
reflection.opening(anchor);
|
|
47
|
+
} else {
|
|
48
|
+
reflection.detach(anchor);
|
|
49
|
+
}
|
|
50
|
+
}),
|
|
51
|
+
),
|
|
52
|
+
),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Effect, Queue } from "effect";
|
|
2
|
+
import { clientId } from "#adapters/clientId.ts";
|
|
3
|
+
|
|
4
|
+
export interface FollowedStream {
|
|
5
|
+
armedAt: number | undefined;
|
|
6
|
+
supported: boolean | undefined;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface Wait {
|
|
10
|
+
readonly after: number;
|
|
11
|
+
readonly own: number | undefined;
|
|
12
|
+
readonly reflected: () => void;
|
|
13
|
+
readonly streams: Set<FollowedStream>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// One per connection: every command and live stream that shares it folds into the same tokens.
|
|
17
|
+
export const makeReflection = Effect.gen(function* () {
|
|
18
|
+
const catchUps = yield* Queue.sliding<void>(1);
|
|
19
|
+
const client = clientId();
|
|
20
|
+
let issued = 0;
|
|
21
|
+
const streams = new Set<FollowedStream>();
|
|
22
|
+
const flights = new Map<number, Set<FollowedStream>>();
|
|
23
|
+
const waits = new Set<Wait>();
|
|
24
|
+
|
|
25
|
+
// A stream armed after the command's token was issued may have registered behind it, so only a later token proves it caught up.
|
|
26
|
+
function uncertain(wait: Wait, stream: FollowedStream) {
|
|
27
|
+
return stream.armedAt !== undefined && (wait.own === undefined || stream.armedAt >= wait.own);
|
|
28
|
+
}
|
|
29
|
+
function release(covers: (wait: Wait, stream: FollowedStream) => boolean, stream?: FollowedStream) {
|
|
30
|
+
for (const wait of [...waits]) {
|
|
31
|
+
const covered = [...wait.streams].filter((waited) => (stream === undefined || waited === stream) && covers(wait, waited));
|
|
32
|
+
for (const waited of covered) {
|
|
33
|
+
wait.streams.delete(waited);
|
|
34
|
+
}
|
|
35
|
+
if (wait.streams.size === 0 && waits.delete(wait)) {
|
|
36
|
+
wait.reflected();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function token() {
|
|
41
|
+
issued += 1;
|
|
42
|
+
return issued;
|
|
43
|
+
}
|
|
44
|
+
function flight() {
|
|
45
|
+
const own = token();
|
|
46
|
+
flights.set(own, new Set());
|
|
47
|
+
return own;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
abandon: () => release(uncertain),
|
|
51
|
+
accepted: (own: number | undefined, reflected: () => void) => {
|
|
52
|
+
const confirmed = own === undefined ? undefined : flights.get(own);
|
|
53
|
+
if (own !== undefined) {
|
|
54
|
+
flights.delete(own);
|
|
55
|
+
}
|
|
56
|
+
const waited = [...streams].filter((stream) => stream.supported !== false && confirmed?.has(stream) !== true);
|
|
57
|
+
if (waited.length === 0) {
|
|
58
|
+
reflected();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const wait: Wait = { after: issued, own, reflected, streams: new Set(waited) };
|
|
62
|
+
waits.add(wait);
|
|
63
|
+
if (waited.some((stream) => uncertain(wait, stream))) {
|
|
64
|
+
Queue.offerUnsafe(catchUps, undefined);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
armed: (stream: FollowedStream) => {
|
|
68
|
+
stream.supported = true;
|
|
69
|
+
stream.armedAt = issued;
|
|
70
|
+
if ([...waits].some((wait) => wait.streams.has(stream))) {
|
|
71
|
+
Queue.offerUnsafe(catchUps, undefined);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
catchUps,
|
|
75
|
+
client,
|
|
76
|
+
confirmed: (stream: FollowedStream, ids: readonly number[]) => {
|
|
77
|
+
for (const id of ids) {
|
|
78
|
+
flights.get(id)?.add(stream);
|
|
79
|
+
}
|
|
80
|
+
release((wait) => ids.some((id) => id === wait.own || id > wait.after), stream);
|
|
81
|
+
},
|
|
82
|
+
data: (stream: FollowedStream) => {
|
|
83
|
+
if (stream.supported === undefined) {
|
|
84
|
+
stream.supported = false;
|
|
85
|
+
release(() => true, stream);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
detach: (stream: FollowedStream) => {
|
|
89
|
+
streams.delete(stream);
|
|
90
|
+
release(() => true, stream);
|
|
91
|
+
},
|
|
92
|
+
failed: (own: number | undefined) => {
|
|
93
|
+
if (own !== undefined) {
|
|
94
|
+
flights.delete(own);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
flight,
|
|
98
|
+
opening: (stream: FollowedStream) => {
|
|
99
|
+
stream.armedAt = undefined;
|
|
100
|
+
stream.supported = undefined;
|
|
101
|
+
streams.add(stream);
|
|
102
|
+
},
|
|
103
|
+
send: (piggyback: boolean) => (piggyback && [...streams].some((stream) => stream.supported !== false) ? flight() : undefined),
|
|
104
|
+
token,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
export type Reflection = Effect.Success<typeof makeReflection>;
|
|
109
|
+
|
|
110
|
+
export const reflections = new WeakMap<object, { readonly piggyback: boolean; readonly reflection: Reflection }>();
|
package/src/rpcClient.ts
CHANGED
|
@@ -4,11 +4,14 @@ import * as AtomRpc from "effect/reactivity/AtomRpc";
|
|
|
4
4
|
import { type Rpc, RpcClient, RpcGroup } from "effect/rpc";
|
|
5
5
|
import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
6
6
|
import { withClientProtocol } from "#adapters/withClientProtocol.ts";
|
|
7
|
+
import { catchUp } from "#catchUp.ts";
|
|
7
8
|
import { clientActivities, makeActivity } from "#clientActivities.ts";
|
|
8
9
|
import { Activity } from "#clientActivity.ts";
|
|
9
10
|
import type { ClientOptions } from "#clientOptions.ts";
|
|
10
|
-
import {
|
|
11
|
+
import { ConnectionScope, connectionProtocol } from "#connectionProtocol.ts";
|
|
11
12
|
import { commandConnections } from "#connectionReadiness.ts";
|
|
13
|
+
import { liveSyncRpc } from "#liveSyncRpc.ts";
|
|
14
|
+
import { reflections } from "#reflection.ts";
|
|
12
15
|
import type { ResumeWindow } from "#resume.ts";
|
|
13
16
|
import { CommandInvalidation } from "#serverMetadata.ts";
|
|
14
17
|
import { socketProtocol } from "#socketProtocol.ts";
|
|
@@ -22,7 +25,7 @@ export interface RpcSource {
|
|
|
22
25
|
|
|
23
26
|
export function rpcClient(name: string, source: RpcSource, options: ClientOptions) {
|
|
24
27
|
const activity = makeActivity(options.runtime ?? Atom.runtime);
|
|
25
|
-
const group = RpcGroup.make(...source.requests.values()).annotateMerge(source.annotations);
|
|
28
|
+
const group = RpcGroup.make(...source.requests.values(), ...liveSyncRpc.requests.values()).annotateMerge(source.annotations);
|
|
26
29
|
const commands = new Set(
|
|
27
30
|
[...group.requests].filter(([, rpc]) => Option.isSome(Context.getOption(rpc.annotations, CommandInvalidation))).map(([tag]) => tag),
|
|
28
31
|
);
|
|
@@ -67,11 +70,14 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
67
70
|
return observed;
|
|
68
71
|
}
|
|
69
72
|
const make = Effect.gen(function* () {
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
const wait = connection === undefined ? Effect.void : Effect.suspend(() => (connection.isReady() ? Effect.void : activity.hold(connection.wait)));
|
|
73
|
+
const { connection, reflection } = yield* ConnectionScope;
|
|
74
|
+
const wait = Effect.suspend(() => (connection.isReady() ? Effect.void : activity.hold(connection.wait)));
|
|
73
75
|
const native = yield* RpcClient.make<Rpc.Any, true>(group, { flatten: true });
|
|
74
|
-
|
|
76
|
+
const observed = observeClient(native, wait);
|
|
77
|
+
// Only the shared socket proves a command reaches the host that holds its streams; any other protocol syncs after the reply.
|
|
78
|
+
reflections.set(observed, { piggyback: options.protocol === undefined, reflection });
|
|
79
|
+
yield* catchUp(reflection, native, wait);
|
|
80
|
+
return observed;
|
|
75
81
|
});
|
|
76
82
|
const Client = AtomRpc.Service<unknown>()(`${name}/Client`, {
|
|
77
83
|
group,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
+
|
|
5
|
+
export function screenSync<E, R, TRuntimeError>(
|
|
6
|
+
client: { readonly runtime: Atom.AtomRuntime<R, TRuntimeError>; readonly sync: Effect.Effect<void, E, R> },
|
|
7
|
+
registry: AtomRegistry.AtomRegistry,
|
|
8
|
+
) {
|
|
9
|
+
return AtomRegistry.getResult(registry, client.runtime.atom(client.sync).pipe(Atom.setIdleTTL(0)));
|
|
10
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import type { Headers } from "effect/http";
|
|
3
|
+
import { SyncToken } from "@shivaedev/changes/syncToken.ts";
|
|
4
|
+
|
|
5
|
+
const HEADER = "x-shivaedev-sync";
|
|
6
|
+
const decodeToken = Schema.decodeUnknownOption(SyncToken);
|
|
7
|
+
|
|
8
|
+
export function streamSyncHeaders(client: string): Record<string, string> {
|
|
9
|
+
return { [HEADER]: client };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function commandSyncHeaders(token: SyncToken): Record<string, string> {
|
|
13
|
+
return { [HEADER]: `${token.client}:${token.id}` };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// A live stream names only its client; the id 1 stands in so one schema validates both header forms.
|
|
17
|
+
export function syncClient(headers: Headers.Headers): string | undefined {
|
|
18
|
+
const value = headers[HEADER];
|
|
19
|
+
return value === undefined ? undefined : Option.getOrUndefined(Option.map(decodeToken({ client: value, id: 1 }), (token) => token.client));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function syncToken(headers: Headers.Headers): SyncToken | undefined {
|
|
23
|
+
const [client, id, ...rest] = headers[HEADER]?.split(":") ?? [];
|
|
24
|
+
return rest.length > 0 || id === undefined || !/^[1-9]\d{0,15}$/u.test(id)
|
|
25
|
+
? undefined
|
|
26
|
+
: Option.getOrUndefined(decodeToken({ client, id: Number(id) }));
|
|
27
|
+
}
|
package/dist/liveAnswers.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Effect, Stream } from "effect";
|
|
2
|
-
import type * as Atom from "effect/reactivity/Atom";
|
|
3
|
-
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
-
export declare function liveAnswers(registry: AtomRegistry.AtomRegistry, name: string, query: Atom.Atom<unknown>): {
|
|
5
|
-
answered: Stream.Stream<number, never, never>;
|
|
6
|
-
current: Effect.Effect<number, never, never>;
|
|
7
|
-
name: string;
|
|
8
|
-
};
|
|
9
|
-
//# sourceMappingURL=liveAnswers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"liveAnswers.d.ts","sourceRoot":"","sources":["../src/liveAnswers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,YAAY,MAAM,gCAAgC,CAAC;AAG/D,wBAAgB,WAAW,CAAC,QAAQ,EAAE,YAAY,CAAC,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;;;;EAOvG"}
|
package/dist/liveAnswers.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Effect, Stream } from "effect";
|
|
2
|
-
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
3
|
-
import { querySignals } from "#querySignals.ts";
|
|
4
|
-
export function liveAnswers(registry, name, query) {
|
|
5
|
-
const signals = querySignals(query);
|
|
6
|
-
return {
|
|
7
|
-
answered: Stream.map(AtomRegistry.toStream(registry, signals), (state) => state.completed),
|
|
8
|
-
current: Effect.sync(() => registry.get(signals).completed),
|
|
9
|
-
name,
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=liveAnswers.js.map
|
package/dist/liveAnswers.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"liveAnswers.js","sourceRoot":"","sources":["../src/liveAnswers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAExC,OAAO,KAAK,YAAY,MAAM,gCAAgC,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD,MAAM,UAAU,WAAW,CAAC,QAAmC,EAAE,IAAY,EAAE,KAAyB;IACvG,MAAM,OAAO,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;IACpC,OAAO;QACN,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC;QAC1F,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,SAAS,CAAC;QAC3D,IAAI;KACJ,CAAC;AACH,CAAC"}
|
package/src/liveAnswers.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Effect, Stream } from "effect";
|
|
2
|
-
import type * as Atom from "effect/reactivity/Atom";
|
|
3
|
-
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
-
import { querySignals } from "#querySignals.ts";
|
|
5
|
-
|
|
6
|
-
export function liveAnswers(registry: AtomRegistry.AtomRegistry, name: string, query: Atom.Atom<unknown>) {
|
|
7
|
-
const signals = querySignals(query);
|
|
8
|
-
return {
|
|
9
|
-
answered: Stream.map(AtomRegistry.toStream(registry, signals), (state) => state.completed),
|
|
10
|
-
current: Effect.sync(() => registry.get(signals).completed),
|
|
11
|
-
name,
|
|
12
|
-
};
|
|
13
|
-
}
|