@shivaedev/contract 0.44.0 → 0.46.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 +22 -0
- package/README.md +73 -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 +3 -2
- package/dist/adapters/activityState.d.ts.map +1 -1
- package/dist/adapters/activityState.js +12 -1
- package/dist/adapters/activityState.js.map +1 -1
- package/dist/adapters/clientConnection.d.ts +1 -0
- package/dist/adapters/clientConnection.d.ts.map +1 -1
- package/dist/adapters/clientConnection.js +18 -10
- package/dist/adapters/clientConnection.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 +4 -0
- package/dist/clientActivities.d.ts.map +1 -1
- package/dist/clientActivities.js +8 -3
- package/dist/clientActivities.js.map +1 -1
- package/dist/clientActivity.d.ts +3 -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/defineModule.js +1 -1
- package/dist/defineModule.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 +4 -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 +13 -3
- package/src/adapters/clientConnection.ts +24 -16
- 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 +9 -4
- package/src/clientActivity.ts +4 -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/defineModule.ts +1 -1
- 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/clientActivities.ts
CHANGED
|
@@ -9,9 +9,9 @@ type ActivityTracker = ReturnType<typeof makeActivity>;
|
|
|
9
9
|
export const clientActivities = new WeakMap<object, ActivityTracker>();
|
|
10
10
|
|
|
11
11
|
export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
12
|
-
const { activity, change, hold, setConnection } = activityState();
|
|
12
|
+
const { activity, answered, change, hold, setConnection } = activityState();
|
|
13
13
|
const connection = clientConnection(factory, setConnection, activity);
|
|
14
|
-
function begin(key: "rpcCalls" | "commands") {
|
|
14
|
+
function begin(key: "rpcCalls" | "commands" | "unreflectedCommands") {
|
|
15
15
|
change(key, 1);
|
|
16
16
|
return () => change(key, -1);
|
|
17
17
|
}
|
|
@@ -51,9 +51,13 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
51
51
|
if (!active) {
|
|
52
52
|
return;
|
|
53
53
|
}
|
|
54
|
-
received
|
|
54
|
+
const first = !(received || AsyncResult.isInitial(result));
|
|
55
|
+
received ||= first;
|
|
55
56
|
initial.set(!received);
|
|
56
57
|
refreshing.set(result.waiting || AsyncResult.isInitial(result));
|
|
58
|
+
if (first) {
|
|
59
|
+
answered();
|
|
60
|
+
}
|
|
57
61
|
}
|
|
58
62
|
get.addFinalizer(() => {
|
|
59
63
|
active = false;
|
|
@@ -76,7 +80,7 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
76
80
|
return watched;
|
|
77
81
|
}
|
|
78
82
|
return {
|
|
79
|
-
activity: Object.assign(activity, { connectionStatus: connection.status }),
|
|
83
|
+
activity: Object.assign(activity, { connectionStatus: connection.status, firstSettled: connection.firstSettled }),
|
|
80
84
|
hint: () => change("liveHints", 1),
|
|
81
85
|
hold: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
82
86
|
track(
|
|
@@ -87,6 +91,7 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
87
91
|
),
|
|
88
92
|
true,
|
|
89
93
|
),
|
|
94
|
+
reflecting: () => begin("unreflectedCommands"),
|
|
90
95
|
statuses: connection.statuses,
|
|
91
96
|
track,
|
|
92
97
|
watch,
|
package/src/clientActivity.ts
CHANGED
|
@@ -6,15 +6,18 @@ export type ConnectionStatus = "live" | "reconnecting" | "waiting-for-connection
|
|
|
6
6
|
export interface ClientActivityState {
|
|
7
7
|
readonly commands: number;
|
|
8
8
|
readonly connectionStatus: ConnectionStatus;
|
|
9
|
+
readonly firstSettled: boolean;
|
|
9
10
|
readonly initialQueries: number;
|
|
10
11
|
readonly liveHints: number;
|
|
11
12
|
readonly refreshingAtoms: number;
|
|
12
13
|
readonly rpcCalls: number;
|
|
14
|
+
readonly unreflectedCommands: number;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
export type ClientActivity = Stream.Stream<ClientActivityState> & {
|
|
16
18
|
readonly connectionStatus: Atom.Atom<ConnectionStatus>;
|
|
17
19
|
readonly current: Effect.Effect<ClientActivityState>;
|
|
20
|
+
readonly firstSettled: Atom.Atom<boolean>;
|
|
18
21
|
};
|
|
19
22
|
|
|
20
23
|
export const Activity = Symbol.for("@shivaedev/contract/ClientActivity");
|
|
@@ -32,7 +35,7 @@ export function isClientIdle(state: ClientActivityState): boolean {
|
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
export function isClientSettled(state: ClientActivityState): boolean {
|
|
35
|
-
return state.commands === 0 && state.initialQueries === 0;
|
|
38
|
+
return state.commands === 0 && state.initialQueries === 0 && state.unreflectedCommands === 0;
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
export function activityScreenSignal(activity: ClientActivity, settled = isClientSettled) {
|
package/src/clientOperations.ts
CHANGED
|
@@ -12,9 +12,12 @@ import type { QueryOptions } from "#bind.ts";
|
|
|
12
12
|
import { clientActivities } from "#clientActivities.ts";
|
|
13
13
|
import { clientInvalidationKeys, clientReadKeys } from "#clientKeys.ts";
|
|
14
14
|
import { commandPayload } from "#commandPayload.ts";
|
|
15
|
+
import { CommandSignal, untilReflected } from "#commandSignal.ts";
|
|
15
16
|
import { waitForConnection } from "#connectionReadiness.ts";
|
|
17
|
+
import { LIVE_SYNC } from "#liveSyncRpc.ts";
|
|
16
18
|
import type { CommandShape, QueryShape } from "#operation.ts";
|
|
17
19
|
import { shareQuerySignals } from "#querySignals.ts";
|
|
20
|
+
import { type CommandObserver, reflectCommand } from "#reflectCommand.ts";
|
|
18
21
|
import { retainQuery } from "#retainQuery.ts";
|
|
19
22
|
|
|
20
23
|
type ErasedClient = AtomRpc.AtomRpcClient<unknown, string, Rpc.Rpc<string, Schema.Top, Schema.Top, Schema.Top>>;
|
|
@@ -35,6 +38,11 @@ export function clientOperations(
|
|
|
35
38
|
return owner === undefined ? effect : Effect.suspend(() => AtomRegistry.getResult(owner.registry, ordinaryClient.runtime.atom(effect)));
|
|
36
39
|
}
|
|
37
40
|
|
|
41
|
+
const observed = Effect.map(
|
|
42
|
+
CommandSignal,
|
|
43
|
+
(signal): CommandObserver => ({ pending: () => clientActivities.get(ordinaryClient)?.reflecting() ?? (() => undefined), signal }),
|
|
44
|
+
);
|
|
45
|
+
|
|
38
46
|
function boundQuery(tag: string, query: QueryShape) {
|
|
39
47
|
const cached = new WeakMap<ReturnType<typeof ordinaryClient.query>, ReturnType<typeof ordinaryClient.query>>();
|
|
40
48
|
return {
|
|
@@ -75,20 +83,29 @@ export function clientOperations(
|
|
|
75
83
|
];
|
|
76
84
|
return Reactivity.invalidate(clientInvalidationKeys(keys, owner?.context));
|
|
77
85
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
Effect.flatMap((validated) => client(tag, validated)),
|
|
85
|
-
Effect.tap((result) => invalidate(payload, result)),
|
|
86
|
-
),
|
|
87
|
-
),
|
|
86
|
+
function send(observer: CommandObserver, payload: unknown) {
|
|
87
|
+
return ordinaryClient.use((client) =>
|
|
88
|
+
commandPayload(command, payload).pipe(
|
|
89
|
+
Effect.tap(() => waitForConnection(client)),
|
|
90
|
+
Effect.flatMap((validated) => reflectCommand(client, observer, (options) => client(tag, validated, options))),
|
|
91
|
+
Effect.tap((result) => invalidate(payload, result)),
|
|
88
92
|
),
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
run: (payload: unknown) => Effect.flatMap(observed, (observer) => run(send(observer, payload))),
|
|
89
97
|
};
|
|
90
98
|
}
|
|
91
99
|
|
|
100
|
+
function synchronize(observer: CommandObserver) {
|
|
101
|
+
return ordinaryClient.use((client) => {
|
|
102
|
+
const request = reflectCommand(client, observer, (options) => Effect.asVoid(client(LIVE_SYNC, undefined, options)), true);
|
|
103
|
+
return Effect.andThen(waitForConnection(client), request);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
// A sync writes nothing and waits like a command, so it returns once every open live query shows the commits before it.
|
|
107
|
+
const sync = untilReflected(Effect.flatMap(observed, (observer) => run(synchronize(observer))));
|
|
108
|
+
|
|
92
109
|
function boundStream(tag: string) {
|
|
93
110
|
return {
|
|
94
111
|
run: (payload: unknown) => {
|
|
@@ -107,5 +124,6 @@ export function clientOperations(
|
|
|
107
124
|
command: (tag: string, command: CommandShape) => boundCommand(tag, command),
|
|
108
125
|
query: (tag: string, query: QueryShape) => boundQuery(tag, query),
|
|
109
126
|
stream: (tag: string) => boundStream(tag),
|
|
127
|
+
sync,
|
|
110
128
|
};
|
|
111
129
|
}
|
package/src/clientOptions.ts
CHANGED
|
@@ -40,4 +40,5 @@ export type MadeClient<
|
|
|
40
40
|
readonly Client: AtomRpc.AtomRpcClient<ClientService<TName, TRpcs | TExtra>, `${TName}/Client`, TRpcs | TExtra>;
|
|
41
41
|
readonly client: Effect.Effect<RpcClient.RpcClient<TRpcs | TExtra, RpcClientError>, never, Scope.Scope | Rpc.MiddlewareClient<TRpcs | TExtra>>;
|
|
42
42
|
readonly runtime: Atom.AtomRuntime<ClientService<TName, TRpcs | TExtra>>;
|
|
43
|
+
readonly sync: Effect.Effect<void, RpcClientError, ClientService<TName, TRpcs | TExtra>>;
|
|
43
44
|
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Context, Effect, Latch } from "effect";
|
|
2
|
+
|
|
3
|
+
export type CommandState = "sent" | "accepted" | "reflected";
|
|
4
|
+
|
|
5
|
+
export const CommandSignal = Context.Reference<(state: CommandState) => void>("@shivaedev/contract/CommandSignal", {
|
|
6
|
+
defaultValue: () => () => undefined,
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
export const untilReflected = Effect.fnUntraced(function* <A, E, R>(command: Effect.Effect<A, E, R>) {
|
|
10
|
+
const outer = yield* CommandSignal;
|
|
11
|
+
const reflected = Latch.makeUnsafe(true);
|
|
12
|
+
let accepted = 0;
|
|
13
|
+
const result = yield* Effect.provideService(command, CommandSignal, (state) => {
|
|
14
|
+
outer(state);
|
|
15
|
+
if (state === "accepted") {
|
|
16
|
+
accepted += 1;
|
|
17
|
+
reflected.closeUnsafe();
|
|
18
|
+
} else if (state === "reflected") {
|
|
19
|
+
accepted -= 1;
|
|
20
|
+
if (accepted === 0) {
|
|
21
|
+
reflected.openUnsafe();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
yield* reflected.await;
|
|
26
|
+
return result;
|
|
27
|
+
});
|
|
@@ -2,15 +2,21 @@ import { Context, Effect, Layer, Option } from "effect";
|
|
|
2
2
|
import * as Atom from "effect/reactivity/Atom";
|
|
3
3
|
import { RpcClient } from "effect/rpc";
|
|
4
4
|
import { ConnectionReadiness, connectionReadiness } from "#connectionReadiness.ts";
|
|
5
|
+
import { makeReflection, type Reflection } from "#reflection.ts";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
// Clients that share one transport share its readiness and its reflection, so a command waits on every stream of its connection.
|
|
8
|
+
export class ConnectionScope extends Context.Service<
|
|
9
|
+
ConnectionScope,
|
|
10
|
+
{ readonly connection: Effect.Success<typeof connectionReadiness>; readonly reflection: Reflection }
|
|
11
|
+
>()("@shivaedev/contract/ConnectionScope") {}
|
|
7
12
|
|
|
8
13
|
export const connectionProtocol = Atom.family((protocol: Layer.Layer<RpcClient.Protocol, never, ConnectionReadiness>) =>
|
|
9
14
|
Layer.unwrap(
|
|
10
15
|
Effect.gen(function* () {
|
|
11
16
|
const connection = yield* connectionReadiness;
|
|
17
|
+
const reflection = yield* makeReflection;
|
|
12
18
|
const hooks = yield* Effect.serviceOption(RpcClient.ConnectionHooks);
|
|
13
|
-
|
|
19
|
+
const managed = protocol.pipe(
|
|
14
20
|
Layer.provide(
|
|
15
21
|
Layer.merge(
|
|
16
22
|
Layer.succeed(ConnectionReadiness, connection),
|
|
@@ -20,8 +26,8 @@ export const connectionProtocol = Atom.family((protocol: Layer.Layer<RpcClient.P
|
|
|
20
26
|
}),
|
|
21
27
|
),
|
|
22
28
|
),
|
|
23
|
-
Layer.tap((context) => Effect.sync(() => protocolConnections.set(Context.get(context, RpcClient.Protocol), connection))),
|
|
24
29
|
);
|
|
30
|
+
return Layer.merge(managed, Layer.succeed(ConnectionScope, { connection, reflection }));
|
|
25
31
|
}),
|
|
26
32
|
),
|
|
27
33
|
);
|
package/src/defineModule.ts
CHANGED
|
@@ -108,5 +108,5 @@ export function defineModule(definition: {
|
|
|
108
108
|
...(definition.keyScope === undefined ? {} : { keyScope: definition.keyScope }),
|
|
109
109
|
...(definition.subject === undefined ? {} : { subject: definition.subject }),
|
|
110
110
|
});
|
|
111
|
-
return definition.middleware
|
|
111
|
+
return definition.middleware === undefined ? group : group.middleware(definition.middleware);
|
|
112
112
|
}
|
|
@@ -3,5 +3,6 @@ import { Effect, Random, Ref } from "effect";
|
|
|
3
3
|
export const initialize = Effect.gen(function* () {
|
|
4
4
|
const openStreams = yield* Ref.make(0);
|
|
5
5
|
const resyncKey = `@shivaedev/contract/resync/${yield* Random.nextInt}`;
|
|
6
|
-
|
|
6
|
+
const syncClients = new Map<string, Set<(id: number) => void>>();
|
|
7
|
+
return { openStreams, resyncKey, syncClients };
|
|
7
8
|
});
|
|
@@ -4,7 +4,7 @@ import { liveHintStream } from "#adapters/liveHintStream.ts";
|
|
|
4
4
|
import type { initialize } from "#live-server/initialize.ts";
|
|
5
5
|
|
|
6
6
|
export function register(state: Effect.Success<typeof initialize>) {
|
|
7
|
-
return Effect.fn("LiveServer.register")(function* (keys: readonly Key[]) {
|
|
8
|
-
return yield* liveHintStream(state, keys);
|
|
7
|
+
return Effect.fn("LiveServer.register")(function* (keys: readonly Key[], syncClient?: string) {
|
|
8
|
+
return yield* liveHintStream(state, keys, syncClient);
|
|
9
9
|
});
|
|
10
10
|
}
|
|
@@ -6,6 +6,7 @@ import { initialize } from "#live-server/initialize.ts";
|
|
|
6
6
|
import { register } from "#live-server/register.ts";
|
|
7
7
|
import { resync } from "#live-server/resync.ts";
|
|
8
8
|
import { stream } from "#live-server/stream.ts";
|
|
9
|
+
import { synced } from "#live-server/synced.ts";
|
|
9
10
|
|
|
10
11
|
export const LiveServer = defineService({
|
|
11
12
|
id: "@shivaedev/contract/LiveServer",
|
|
@@ -14,6 +15,7 @@ export const LiveServer = defineService({
|
|
|
14
15
|
register: register(state),
|
|
15
16
|
resync: resync(state),
|
|
16
17
|
stream: stream(state),
|
|
18
|
+
synced: synced(state),
|
|
17
19
|
}),
|
|
18
20
|
requires: [Reactivity.Reactivity, LiveDispatcher, Random.Random],
|
|
19
21
|
});
|
|
@@ -4,7 +4,7 @@ import { liveHintStream } from "#adapters/liveHintStream.ts";
|
|
|
4
4
|
import type { initialize } from "#live-server/initialize.ts";
|
|
5
5
|
|
|
6
6
|
export function stream(state: Effect.Success<typeof initialize>) {
|
|
7
|
-
return Effect.fn("LiveServer.stream")(function* (keys: readonly Key[]) {
|
|
8
|
-
return (yield* liveHintStream(state, keys)).pipe(Stream.flatMap((registration) => registration.stream));
|
|
7
|
+
return Effect.fn("LiveServer.stream")(function* (keys: readonly Key[], syncClient?: string) {
|
|
8
|
+
return (yield* liveHintStream(state, keys, syncClient)).pipe(Stream.flatMap((registration) => registration.stream));
|
|
9
9
|
});
|
|
10
10
|
}
|
|
@@ -3,8 +3,9 @@ import type { Key } from "@shivaedev/changes/keys.ts";
|
|
|
3
3
|
import { ReadRecorder, type RecordReadOptions } from "@shivaedev/changes/readRecorder.ts";
|
|
4
4
|
import { refreshSubscription } from "#adapters/refreshSubscription.ts";
|
|
5
5
|
import { compareQueries } from "#compareQueries.ts";
|
|
6
|
-
import type { LiveHint } from "#live.ts";
|
|
7
6
|
import { LiveServer } from "#live-server/service.ts";
|
|
7
|
+
import { carriesOnlyTokens, syncFrames } from "#live-server/syncFrames.ts";
|
|
8
|
+
import type { SubscriptionHint } from "#liveHintQueue.ts";
|
|
8
9
|
import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
|
|
9
10
|
import type { QueryUpdate } from "#queryUpdate.ts";
|
|
10
11
|
import { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
@@ -82,12 +83,13 @@ function subscribeQueries<E, R>(
|
|
|
82
83
|
function subscribeQueries<E, R>(subscription: QuerySubscription<E, R>, read: QueryRerun<E, R>) {
|
|
83
84
|
return Effect.fnUntraced(function* () {
|
|
84
85
|
const server = yield* LiveServer;
|
|
85
|
-
const
|
|
86
|
+
const syncing = subscription.syncClient !== undefined;
|
|
87
|
+
const hints = yield* server.register(subscription.keys, subscription.syncClient);
|
|
86
88
|
return hints.pipe(
|
|
87
89
|
Stream.flatMap((registration) => {
|
|
88
90
|
const keys = subscriptionKeys(subscription, read, registration.replaceKeys);
|
|
89
91
|
let compare = compareQueries(subscription, keys.recordingRead);
|
|
90
|
-
const next = Effect.fnUntraced(function* (hint:
|
|
92
|
+
const next = Effect.fnUntraced(function* (hint: SubscriptionHint) {
|
|
91
93
|
if (!(yield* subscription.authorized)) {
|
|
92
94
|
compare = compareQueries(subscription, keys.recordingRead, false);
|
|
93
95
|
return [new QueryReadDenied({ message: "Read no longer allowed for subscribed queries" })];
|
|
@@ -100,7 +102,11 @@ function subscribeQueries<E, R>(subscription: QuerySubscription<E, R>, read: Que
|
|
|
100
102
|
return updates;
|
|
101
103
|
});
|
|
102
104
|
return registration.stream.pipe(
|
|
103
|
-
Stream.mapEffect((hint) =>
|
|
105
|
+
Stream.mapEffect((hint) =>
|
|
106
|
+
carriesOnlyTokens(hint)
|
|
107
|
+
? Effect.succeed(syncFrames(syncing, hint, []))
|
|
108
|
+
: Effect.map(refreshSubscription(subscription, next(hint)), (updates) => syncFrames(syncing, hint, updates)),
|
|
109
|
+
),
|
|
104
110
|
Stream.flatMap(Stream.fromIterable),
|
|
105
111
|
);
|
|
106
112
|
}),
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SubscriptionHint } from "#liveHintQueue.ts";
|
|
2
|
+
import type { QueryUpdate } from "#queryUpdate.ts";
|
|
3
|
+
|
|
4
|
+
// A token alone changed nothing the stream reads, so it is confirmed without rechecking the session or the policy.
|
|
5
|
+
export function carriesOnlyTokens(hint: SubscriptionHint): boolean {
|
|
6
|
+
return hint._tag === "Changed" && hint.keys.length === 0;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// A stream of a client that named itself opens every full read with Syncing and closes every round with the tokens it passed.
|
|
10
|
+
export function syncFrames(syncing: boolean, hint: SubscriptionHint, updates: readonly QueryUpdate[]): readonly QueryUpdate[] {
|
|
11
|
+
if (!syncing) {
|
|
12
|
+
return updates;
|
|
13
|
+
}
|
|
14
|
+
return [
|
|
15
|
+
...(hint._tag === "Resync" ? [{ _tag: "Syncing" } as const] : []),
|
|
16
|
+
...updates,
|
|
17
|
+
...(hint.synced === undefined ? [] : [{ _tag: "Synced", ids: hint.synced } as const]),
|
|
18
|
+
];
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { SyncToken } from "@shivaedev/changes/syncToken.ts";
|
|
3
|
+
import type { initialize } from "#live-server/initialize.ts";
|
|
4
|
+
|
|
5
|
+
export function synced(state: Effect.Success<typeof initialize>) {
|
|
6
|
+
return Effect.fn("LiveServer.synced")(function* (token: SyncToken) {
|
|
7
|
+
yield* Effect.sync(() => {
|
|
8
|
+
for (const push of state.syncClients.get(token.client) ?? []) {
|
|
9
|
+
push(token.id);
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
});
|
|
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
|
+
}
|