@shivaedev/contract 0.40.0 → 0.42.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 +12 -6
- package/dist/AutomaticQueryOptions.d.ts +22 -0
- package/dist/AutomaticQueryOptions.d.ts.map +1 -0
- package/dist/AutomaticQueryOptions.js +2 -0
- package/dist/AutomaticQueryOptions.js.map +1 -0
- package/dist/adapters/activityState.d.ts.map +1 -1
- package/dist/adapters/activityState.js +9 -1
- package/dist/adapters/activityState.js.map +1 -1
- package/dist/adapters/retainedQueries.d.ts +7 -0
- package/dist/adapters/retainedQueries.d.ts.map +1 -0
- package/dist/adapters/retainedQueries.js +45 -0
- package/dist/adapters/retainedQueries.js.map +1 -0
- package/dist/automaticLive.d.ts +1 -1
- package/dist/automaticLive.d.ts.map +1 -1
- package/dist/automaticLive.js +9 -7
- package/dist/automaticLive.js.map +1 -1
- package/dist/automaticQuery.d.ts +3 -17
- package/dist/automaticQuery.d.ts.map +1 -1
- package/dist/automaticQuery.js +12 -7
- package/dist/automaticQuery.js.map +1 -1
- package/dist/bind.d.ts +1 -2
- package/dist/bind.d.ts.map +1 -1
- package/dist/bind.js +8 -12
- package/dist/bind.js.map +1 -1
- package/dist/bindOperations.d.ts +6 -0
- package/dist/bindOperations.d.ts.map +1 -0
- package/dist/bindOperations.js +10 -0
- package/dist/bindOperations.js.map +1 -0
- package/dist/changesSuccess.d.ts +2 -3
- package/dist/changesSuccess.d.ts.map +1 -1
- package/dist/changesSuccess.js +2 -4
- package/dist/changesSuccess.js.map +1 -1
- package/dist/clientActivities.d.ts +2 -2
- package/dist/clientActivities.d.ts.map +1 -1
- package/dist/clientActivities.js +28 -12
- package/dist/clientActivities.js.map +1 -1
- package/dist/clientActivity.d.ts +4 -1
- package/dist/clientActivity.d.ts.map +1 -1
- package/dist/clientActivity.js +5 -2
- package/dist/clientActivity.js.map +1 -1
- package/dist/clientOperations.d.ts +1 -1
- package/dist/clientOperations.d.ts.map +1 -1
- package/dist/clientOperations.js +54 -51
- package/dist/clientOperations.js.map +1 -1
- package/dist/compareQueries.d.ts +1 -1
- package/dist/compareQueries.d.ts.map +1 -1
- package/dist/compareQueries.js +10 -4
- package/dist/compareQueries.js.map +1 -1
- package/dist/live-server/subscribe.js +1 -1
- package/dist/live-server/subscribe.js.map +1 -1
- package/dist/liveAttempt.d.ts.map +1 -1
- package/dist/liveAttempt.js +8 -2
- package/dist/liveAttempt.js.map +1 -1
- package/dist/publicConfig.d.ts +6 -1
- package/dist/publicConfig.d.ts.map +1 -1
- package/dist/queryDelivery.d.ts +3 -1
- package/dist/queryDelivery.d.ts.map +1 -1
- package/dist/queryDelivery.js +18 -5
- package/dist/queryDelivery.js.map +1 -1
- package/dist/queryHints.js +1 -1
- package/dist/queryHints.js.map +1 -1
- package/dist/queryRetention.d.ts +8 -0
- package/dist/queryRetention.d.ts.map +1 -0
- package/dist/queryRetention.js +3 -0
- package/dist/queryRetention.js.map +1 -0
- package/dist/querySignals.d.ts +1 -0
- package/dist/querySignals.d.ts.map +1 -1
- package/dist/querySignals.js.map +1 -1
- package/dist/retainQuery.d.ts +4 -0
- package/dist/retainQuery.d.ts.map +1 -0
- package/dist/retainQuery.js +38 -0
- package/dist/retainQuery.js.map +1 -0
- package/dist/rpcClient.d.ts +1 -1
- package/dist/rpcClient.d.ts.map +1 -1
- package/dist/rpcClient.js +4 -2
- package/dist/rpcClient.js.map +1 -1
- package/package.json +8 -8
- package/src/AutomaticQueryOptions.ts +22 -0
- package/src/adapters/activityState.ts +9 -1
- package/src/adapters/retainedQueries.ts +47 -0
- package/src/automaticLive.ts +11 -7
- package/src/automaticQuery.ts +16 -21
- package/src/bind.ts +16 -22
- package/src/bindOperations.ts +20 -0
- package/src/changesSuccess.ts +11 -14
- package/src/clientActivities.ts +29 -12
- package/src/clientActivity.ts +8 -2
- package/src/clientOperations.ts +79 -82
- package/src/compareQueries.ts +10 -4
- package/src/live-server/subscribe.ts +1 -1
- package/src/liveAttempt.ts +10 -2
- package/src/queryDelivery.ts +22 -5
- package/src/queryHints.ts +1 -1
- package/src/queryRetention.ts +9 -0
- package/src/querySignals.ts +1 -0
- package/src/retainQuery.ts +39 -0
- package/src/rpcClient.ts +6 -2
package/src/clientActivities.ts
CHANGED
|
@@ -12,40 +12,56 @@ export const clientActivities = new WeakMap<object, ActivityTracker>();
|
|
|
12
12
|
export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
13
13
|
const { activity, change, hold, setConnection } = activityState();
|
|
14
14
|
const connection = clientConnection(factory, setConnection, activity);
|
|
15
|
-
function begin(key: "rpcCalls" | "liveInvalidations") {
|
|
15
|
+
function begin(key: "rpcCalls" | "liveInvalidations" | "commands") {
|
|
16
16
|
change(key, 1);
|
|
17
17
|
return () => change(key, -1);
|
|
18
18
|
}
|
|
19
|
-
function track<A, E, R>(effect: Effect.Effect<A, E, R
|
|
19
|
+
function track<A, E, R>(effect: Effect.Effect<A, E, R>, command = false) {
|
|
20
20
|
return Effect.acquireUseRelease(
|
|
21
|
-
Effect.sync(() =>
|
|
21
|
+
Effect.sync(() => {
|
|
22
|
+
const endCall = begin("rpcCalls");
|
|
23
|
+
const endCommand = command ? begin("commands") : undefined;
|
|
24
|
+
return () => {
|
|
25
|
+
endCall();
|
|
26
|
+
endCommand?.();
|
|
27
|
+
};
|
|
28
|
+
}),
|
|
22
29
|
() => effect,
|
|
23
30
|
(end) => Effect.sync(end),
|
|
24
31
|
);
|
|
25
32
|
}
|
|
33
|
+
function counter(key: "initialQueries" | "refreshingAtoms") {
|
|
34
|
+
let pending = false;
|
|
35
|
+
function set(next: boolean) {
|
|
36
|
+
if (pending !== next) {
|
|
37
|
+
pending = next;
|
|
38
|
+
change(key, next ? 1 : -1);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return { release: () => set(false), set };
|
|
42
|
+
}
|
|
26
43
|
function watch<A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) {
|
|
27
44
|
const watched = wrapAtom(atom, (get) => {
|
|
28
45
|
get.mount(connection.status);
|
|
29
46
|
get.once(queryCoalesceWindow(watched));
|
|
30
47
|
get.subscribe(queryCoalesceWindow(watched), (duration) => get.set(queryCoalesceWindow(atom), duration), { immediate: true });
|
|
31
|
-
|
|
48
|
+
const refreshing = counter("refreshingAtoms");
|
|
49
|
+
const initial = counter("initialQueries");
|
|
50
|
+
let received = false;
|
|
32
51
|
let active = true;
|
|
33
52
|
let published = false;
|
|
34
53
|
function observe(result: AsyncResult.AsyncResult<A, E>) {
|
|
35
54
|
if (!active) {
|
|
36
55
|
return;
|
|
37
56
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
change("refreshingAtoms", next ? 1 : -1);
|
|
42
|
-
}
|
|
57
|
+
received ||= !AsyncResult.isInitial(result);
|
|
58
|
+
initial.set(!received);
|
|
59
|
+
refreshing.set(result.waiting || AsyncResult.isInitial(result));
|
|
43
60
|
}
|
|
44
61
|
get.addFinalizer(() => {
|
|
45
62
|
active = false;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
63
|
+
refreshing.release();
|
|
64
|
+
initial.release();
|
|
49
65
|
});
|
|
50
66
|
function publish(result: AsyncResult.AsyncResult<A, E>) {
|
|
51
67
|
published = true;
|
|
@@ -72,6 +88,7 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
72
88
|
() => effect,
|
|
73
89
|
(end) => Effect.sync(end),
|
|
74
90
|
),
|
|
91
|
+
true,
|
|
75
92
|
),
|
|
76
93
|
queued: () => begin("liveInvalidations"),
|
|
77
94
|
statuses: connection.statuses,
|
package/src/clientActivity.ts
CHANGED
|
@@ -4,7 +4,9 @@ import type * as Atom from "effect/reactivity/Atom";
|
|
|
4
4
|
export type ConnectionStatus = "live" | "reconnecting" | "waiting-for-connection";
|
|
5
5
|
|
|
6
6
|
export interface ClientActivityState {
|
|
7
|
+
readonly commands: number;
|
|
7
8
|
readonly connectionStatus: ConnectionStatus;
|
|
9
|
+
readonly initialQueries: number;
|
|
8
10
|
readonly liveHints: number;
|
|
9
11
|
readonly liveInvalidations: number;
|
|
10
12
|
readonly refreshingAtoms: number;
|
|
@@ -30,9 +32,13 @@ export function isClientIdle(state: ClientActivityState): boolean {
|
|
|
30
32
|
return state.rpcCalls === 0 && state.liveInvalidations === 0 && state.refreshingAtoms === 0;
|
|
31
33
|
}
|
|
32
34
|
|
|
33
|
-
export function
|
|
35
|
+
export function isClientSettled(state: ClientActivityState): boolean {
|
|
36
|
+
return state.commands === 0 && state.initialQueries === 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function activityScreenSignal(activity: ClientActivity, settled = isClientSettled) {
|
|
34
40
|
function screenState(state: ClientActivityState) {
|
|
35
|
-
return { connectionStatus: state.connectionStatus, hints: state.liveHints, pending: !
|
|
41
|
+
return { connectionStatus: state.connectionStatus, hints: state.liveHints, pending: !settled(state) };
|
|
36
42
|
}
|
|
37
43
|
return Object.assign(Stream.map(activity, screenState), { current: Effect.map(activity.current, screenState) });
|
|
38
44
|
}
|
package/src/clientOperations.ts
CHANGED
|
@@ -16,102 +16,99 @@ import { waitForConnection } from "#connectionReadiness.ts";
|
|
|
16
16
|
import type { CommandShape, QueryShape } from "#operation.ts";
|
|
17
17
|
import { queryReactivity } from "#queryReactivity.ts";
|
|
18
18
|
import { shareQuerySignals } from "#querySignals.ts";
|
|
19
|
+
import { retainQuery } from "#retainQuery.ts";
|
|
19
20
|
|
|
20
21
|
type ErasedClient = AtomRpc.AtomRpcClient<unknown, string, Rpc.Rpc<string, Schema.Top, Schema.Top, Schema.Top>>;
|
|
21
|
-
|
|
22
22
|
type ErasedStreamingClient = AtomRpc.AtomRpcClient<
|
|
23
23
|
unknown,
|
|
24
24
|
string,
|
|
25
25
|
Rpc.Rpc<string, Schema.Top, RpcSchema.Stream<Schema.Top, Schema.Top>, Schema.Never>
|
|
26
26
|
>;
|
|
27
27
|
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const existing = cached.get(atom);
|
|
39
|
-
if (existing !== undefined) {
|
|
40
|
-
return existing;
|
|
41
|
-
}
|
|
42
|
-
const local = query.reads(payload, Option.none());
|
|
43
|
-
const keys = [...local, { _tag: "List" as const, collection: `@query/${tag}/${clientId()}` }];
|
|
44
|
-
const dependencies =
|
|
45
|
-
local.length === 0 ? [...keys, { _tag: "List" as const, collection: `@query/${tag.slice(0, tag.lastIndexOf("."))}` }] : keys;
|
|
46
|
-
const reference = { payload, query: query.name };
|
|
47
|
-
const result = automatic.query(
|
|
48
|
-
reactive(atom, clientReadKeys(dependencies, owner?.context), owner?.registry),
|
|
49
|
-
reference,
|
|
50
|
-
keys,
|
|
51
|
-
Schema.toEquivalence(query.success),
|
|
52
|
-
query.live === "rerun-and-compare" ? Schema.decodeUnknownSync(Schema.toType(query.success)) : undefined,
|
|
53
|
-
options.headers,
|
|
54
|
-
);
|
|
55
|
-
const tracked = clientActivities.get(service)?.watch(result) ?? result;
|
|
56
|
-
shareQuerySignals(tracked, result);
|
|
57
|
-
cached.set(atom, tracked);
|
|
58
|
-
return tracked;
|
|
59
|
-
},
|
|
60
|
-
run: (payload: unknown) =>
|
|
61
|
-
run(
|
|
62
|
-
service,
|
|
63
|
-
service.use((client) => client(tag, payload)),
|
|
64
|
-
owner,
|
|
65
|
-
),
|
|
66
|
-
};
|
|
67
|
-
}
|
|
28
|
+
export function clientOperations(
|
|
29
|
+
service: AtomRpc.AtomRpcClient<unknown, string, Rpc.Any>,
|
|
30
|
+
automatic: ReturnType<typeof automaticLive>,
|
|
31
|
+
owner?: BindingOwner,
|
|
32
|
+
) {
|
|
33
|
+
const ordinaryClient: ErasedClient = service;
|
|
34
|
+
const streaming: ErasedStreamingClient = service;
|
|
35
|
+
function run<A, E>(effect: Effect.Effect<A, E, unknown>) {
|
|
36
|
+
return owner === undefined ? effect : Effect.suspend(() => AtomRegistry.getResult(owner.registry, ordinaryClient.runtime.atom(effect)));
|
|
37
|
+
}
|
|
68
38
|
|
|
69
|
-
function
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
39
|
+
function boundQuery(tag: string, query: QueryShape) {
|
|
40
|
+
const reactive = queryReactivity(ordinaryClient.runtime, clientActivities.get(ordinaryClient)?.queued);
|
|
41
|
+
const cached = new WeakMap<ReturnType<typeof ordinaryClient.query>, ReturnType<typeof ordinaryClient.query>>();
|
|
42
|
+
return {
|
|
43
|
+
query: (payload: unknown, options: QueryOptions = {}) => {
|
|
44
|
+
const atom = ordinaryClient.query(tag, payload, { ...options, timeToLive: 0 });
|
|
45
|
+
const existing = cached.get(atom);
|
|
46
|
+
if (existing !== undefined) {
|
|
47
|
+
return existing;
|
|
48
|
+
}
|
|
49
|
+
const local = query.reads(payload, Option.none());
|
|
50
|
+
const keys = [...local, { _tag: "List" as const, collection: `@query/${tag}/${clientId()}` }];
|
|
51
|
+
const dependencies =
|
|
52
|
+
local.length === 0 ? [...keys, { _tag: "List" as const, collection: `@query/${tag.slice(0, tag.lastIndexOf("."))}` }] : keys;
|
|
53
|
+
const reference = { payload, query: query.name };
|
|
54
|
+
const result = automatic.query(
|
|
55
|
+
atom,
|
|
56
|
+
reference,
|
|
57
|
+
keys,
|
|
58
|
+
Schema.toEquivalence(query.success),
|
|
59
|
+
Schema.decodeUnknownSync(Schema.toType(query.success)),
|
|
60
|
+
query.live === "rerun-and-compare",
|
|
61
|
+
(source) => reactive(source, clientReadKeys(dependencies, owner?.context), owner?.registry),
|
|
62
|
+
options.headers,
|
|
63
|
+
);
|
|
64
|
+
const tracked = retainQuery(ordinaryClient.runtime, clientActivities.get(ordinaryClient)?.watch(result) ?? result);
|
|
65
|
+
shareQuerySignals(tracked, result);
|
|
66
|
+
cached.set(atom, tracked);
|
|
67
|
+
return tracked;
|
|
68
|
+
},
|
|
69
|
+
run: (payload: unknown) => run(ordinaryClient.use((client) => client(tag, payload))),
|
|
70
|
+
};
|
|
76
71
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
72
|
+
|
|
73
|
+
function boundCommand(tag: string, command: CommandShape) {
|
|
74
|
+
function invalidate(payload: unknown, result: unknown) {
|
|
75
|
+
const keys: readonly Key[] = [
|
|
76
|
+
...command.invalidates(payload, result),
|
|
77
|
+
{ _tag: "List", collection: `@query/${tag.slice(0, tag.lastIndexOf("."))}` },
|
|
78
|
+
];
|
|
79
|
+
return Reactivity.invalidate(clientInvalidationKeys(keys, owner?.context));
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
run: (payload: unknown) =>
|
|
83
|
+
run(
|
|
84
|
+
ordinaryClient.use((client) =>
|
|
85
|
+
commandPayload(command, payload).pipe(
|
|
86
|
+
Effect.tap(() => waitForConnection(client)),
|
|
87
|
+
Effect.flatMap((validated) => client(tag, validated)),
|
|
88
|
+
Effect.tap((result) => invalidate(payload, result)),
|
|
89
|
+
),
|
|
86
90
|
),
|
|
87
91
|
),
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
92
94
|
|
|
93
|
-
function boundStream(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
95
|
+
function boundStream(tag: string) {
|
|
96
|
+
return {
|
|
97
|
+
run: (payload: unknown) => {
|
|
98
|
+
const stream = Stream.unwrap(streaming.use((client) => Effect.succeed(client(tag, payload))));
|
|
99
|
+
return owner === undefined
|
|
100
|
+
? stream
|
|
101
|
+
: Stream.callback<unknown, unknown>(
|
|
102
|
+
(queue) => AtomRegistry.getResult(owner.registry, streaming.runtime.atom(Stream.runIntoQueue(stream, queue)).pipe(Atom.setIdleTTL(0))),
|
|
103
|
+
{ bufferSize: 1 },
|
|
104
|
+
);
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
106
108
|
|
|
107
|
-
export function clientOperations(
|
|
108
|
-
service: AtomRpc.AtomRpcClient<unknown, string, Rpc.Any>,
|
|
109
|
-
automatic: ReturnType<typeof automaticLive>,
|
|
110
|
-
owner?: BindingOwner,
|
|
111
|
-
) {
|
|
112
109
|
return {
|
|
113
|
-
command: (tag: string, command: CommandShape) => boundCommand(
|
|
114
|
-
query: (tag: string, query: QueryShape) => boundQuery(
|
|
115
|
-
stream: (tag: string) => boundStream(
|
|
110
|
+
command: (tag: string, command: CommandShape) => boundCommand(tag, command),
|
|
111
|
+
query: (tag: string, query: QueryShape) => boundQuery(tag, query),
|
|
112
|
+
stream: (tag: string) => boundStream(tag),
|
|
116
113
|
};
|
|
117
114
|
}
|
package/src/compareQueries.ts
CHANGED
|
@@ -27,14 +27,18 @@ function hintsFor(hint: LiveHint, subscription: QuerySubscription<unknown, unkno
|
|
|
27
27
|
return visible.length === 0 ? [] : [changedHint(visible, scopes)];
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
export function compareQueries<E, R>(subscription: QuerySubscription<unknown, unknown>, read?: QueryRerun<E, R
|
|
30
|
+
export function compareQueries<E, R>(subscription: QuerySubscription<unknown, unknown>, read?: QueryRerun<E, R>, initialRead = true) {
|
|
31
31
|
const queries = subscription.queries ?? [];
|
|
32
32
|
const hashes = new Map<number, string>();
|
|
33
|
+
let initial = initialRead && read !== undefined && queries.length > 0;
|
|
33
34
|
const resultFor = Effect.fnUntraced(function* (request: QueryRequest, index: number) {
|
|
34
35
|
if (read === undefined) {
|
|
35
|
-
return yield* Effect.die(new Error("
|
|
36
|
+
return yield* Effect.die(new Error("Result subscriptions require an authorized query runner"));
|
|
36
37
|
}
|
|
37
38
|
const value = yield* read(request);
|
|
39
|
+
if (request.query.live !== "rerun-and-compare") {
|
|
40
|
+
return { _tag: "Result", query: request.query.name, queryIndex: index, value } satisfies QueryResult;
|
|
41
|
+
}
|
|
38
42
|
const encoded = yield* Schema.encodeUnknownEffect(Schema.toCodecJson(request.query.success))(value).pipe(Effect.orDie);
|
|
39
43
|
const digest = yield* Digest.Digest;
|
|
40
44
|
const hash = yield* digest.digestHex(Digest.sha256, new TextEncoder().encode(JSON.stringify(encoded)));
|
|
@@ -51,12 +55,14 @@ export function compareQueries<E, R>(subscription: QuerySubscription<unknown, un
|
|
|
51
55
|
.map((request, index) => ({ index, request }))
|
|
52
56
|
.filter(
|
|
53
57
|
({ index, request }) =>
|
|
54
|
-
request.query.live === "rerun-and-compare" && (affected?._tag !== "Changed" || affected.queryIndexes?.includes(index)),
|
|
58
|
+
initial || (request.query.live === "rerun-and-compare" && (affected?._tag !== "Changed" || affected.queryIndexes?.includes(index))),
|
|
55
59
|
);
|
|
56
60
|
for (const { index, request } of matching) {
|
|
57
61
|
const result = yield* resultFor(request, index);
|
|
58
62
|
updates.push(result);
|
|
59
63
|
}
|
|
60
|
-
|
|
64
|
+
const hints = initial ? [] : hintsFor(hint, { ...subscription, scopes });
|
|
65
|
+
initial = false;
|
|
66
|
+
return [...updates, ...hints];
|
|
61
67
|
});
|
|
62
68
|
}
|
|
@@ -25,7 +25,7 @@ function subscribeQueries<E, R>(subscription: QuerySubscription<E, R>, read?: Qu
|
|
|
25
25
|
let compare = compareQueries(subscription, read);
|
|
26
26
|
const next = Effect.fnUntraced(function* (hint: LiveHint) {
|
|
27
27
|
if (!(yield* subscription.authorized)) {
|
|
28
|
-
compare = compareQueries(subscription, read);
|
|
28
|
+
compare = compareQueries(subscription, read, false);
|
|
29
29
|
return [new QueryReadDenied({ message: "Read no longer allowed for subscribed queries" })];
|
|
30
30
|
}
|
|
31
31
|
const updates = yield* compare(hint, scopes);
|
package/src/liveAttempt.ts
CHANGED
|
@@ -3,6 +3,14 @@ import type { Key } from "@shivaedev/changes/keys.ts";
|
|
|
3
3
|
import type { SubscriptionHint } from "#queryUpdate.ts";
|
|
4
4
|
import type { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
5
5
|
|
|
6
|
+
function deliveredResult(hint: SubscriptionHint): boolean {
|
|
7
|
+
return hint._tag === "Changed" && hint.keys.length === 0 && hint.queryIndexes?.length === 0;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function needsRead(hint: SubscriptionHint, connected: boolean): boolean {
|
|
11
|
+
return connected && !deliveredResult(hint);
|
|
12
|
+
}
|
|
13
|
+
|
|
6
14
|
export function liveAttempt<E, R>(
|
|
7
15
|
stream: Stream.Stream<SubscriptionHint, E, R>,
|
|
8
16
|
observe: {
|
|
@@ -29,8 +37,8 @@ export function liveAttempt<E, R>(
|
|
|
29
37
|
observe.live();
|
|
30
38
|
if (observe.paused()) {
|
|
31
39
|
observe.queued();
|
|
32
|
-
} else if (first || hint._tag === "Resync") {
|
|
33
|
-
observe.reconcile(connected);
|
|
40
|
+
} else if (first || hint._tag === "Resync" || deliveredResult(hint)) {
|
|
41
|
+
observe.reconcile(needsRead(hint, connected));
|
|
34
42
|
} else {
|
|
35
43
|
observe.changed(hint.keys);
|
|
36
44
|
}
|
package/src/queryDelivery.ts
CHANGED
|
@@ -7,14 +7,18 @@ import type { QueryUpdate, SubscriptionHint } from "#queryUpdate.ts";
|
|
|
7
7
|
|
|
8
8
|
export function queryDelivery<A, E>(
|
|
9
9
|
atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
|
|
10
|
-
decode: (
|
|
10
|
+
decode: (value: unknown) => A,
|
|
11
|
+
compared: boolean,
|
|
11
12
|
paused: Atom.Atom<boolean>,
|
|
12
13
|
signals: Atom.Writable<QuerySignals>,
|
|
13
14
|
) {
|
|
15
|
+
const version = Atom.make(0);
|
|
16
|
+
const updates = Atom.make(0);
|
|
14
17
|
const pushed = Atom.make<AsyncResult.AsyncResult<A, E>>(AsyncResult.initial());
|
|
15
18
|
const deliver = Effect.fnUntraced(function* (update: QueryUpdate): Effect.fn.Return<SubscriptionHint, never, AtomRegistry.AtomRegistry> {
|
|
19
|
+
const registry = yield* AtomRegistry.AtomRegistry;
|
|
20
|
+
registry.update(updates, (current) => current + 1);
|
|
16
21
|
if (update._tag === "Unchanged") {
|
|
17
|
-
const registry = yield* AtomRegistry.AtomRegistry;
|
|
18
22
|
if (!registry.get(paused)) {
|
|
19
23
|
registry.update(signals, (current) => ({ ...current, completed: current.completed + 1, pending: false }));
|
|
20
24
|
}
|
|
@@ -23,14 +27,27 @@ export function queryDelivery<A, E>(
|
|
|
23
27
|
if (update._tag !== "Result") {
|
|
24
28
|
return update;
|
|
25
29
|
}
|
|
26
|
-
|
|
27
|
-
if (decode !== undefined && !registry.get(paused)) {
|
|
30
|
+
if (!registry.get(paused)) {
|
|
28
31
|
registry.set(pushed, AsyncResult.success(decode(update.value)));
|
|
32
|
+
registry.update(version, (current) => current + 1);
|
|
29
33
|
}
|
|
30
34
|
return { _tag: "Changed", keys: [], queryIndexes: [] };
|
|
31
35
|
});
|
|
32
36
|
return {
|
|
33
|
-
atom:
|
|
37
|
+
atom: compared
|
|
38
|
+
? pushed
|
|
39
|
+
: Atom.readable(
|
|
40
|
+
(get) => {
|
|
41
|
+
const initial = get(pushed);
|
|
42
|
+
return AsyncResult.isInitial(initial) ? get(atom) : initial;
|
|
43
|
+
},
|
|
44
|
+
(refresh) => {
|
|
45
|
+
refresh(atom);
|
|
46
|
+
refresh(pushed);
|
|
47
|
+
},
|
|
48
|
+
),
|
|
34
49
|
stream: <TError, R>(stream: Stream.Stream<QueryUpdate, TError, R>) => Stream.mapEffect(stream, deliver),
|
|
50
|
+
updates,
|
|
51
|
+
version,
|
|
35
52
|
};
|
|
36
53
|
}
|
package/src/queryHints.ts
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Duration } from "effect";
|
|
2
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
|
|
4
|
+
export interface QueryRetentionOptions {
|
|
5
|
+
readonly capacity?: number;
|
|
6
|
+
readonly timeToLive?: Duration.Input;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const queryRetention = Atom.keepAlive(Atom.make<QueryRetentionOptions>({}));
|
package/src/querySignals.ts
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Option } from "effect";
|
|
2
|
+
import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
import { retainedQueries } from "#adapters/retainedQueries.ts";
|
|
5
|
+
import { queryCoalesceWindow } from "#queryCoalesceWindow.ts";
|
|
6
|
+
import { querySignals, shareQuerySignals } from "#querySignals.ts";
|
|
7
|
+
import { wrapAtom } from "#wrapAtom.ts";
|
|
8
|
+
|
|
9
|
+
export function retainQuery<A, E>(runtime: Atom.AtomRuntime<unknown, unknown>, atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) {
|
|
10
|
+
const snapshot = Atom.make<Option.Option<AsyncResult.Success<A, E>>>(Option.none()).pipe(Atom.setIdleTTL(0));
|
|
11
|
+
const signals = querySignals(atom);
|
|
12
|
+
const retained = wrapAtom(atom, (get) => {
|
|
13
|
+
get.mount(snapshot);
|
|
14
|
+
const owner = get.once(retainedQueries);
|
|
15
|
+
owner.take(retained);
|
|
16
|
+
get.once(queryCoalesceWindow(retained));
|
|
17
|
+
get.subscribe(queryCoalesceWindow(retained), (value) => get.set(queryCoalesceWindow(atom), value), { immediate: true });
|
|
18
|
+
get.addFinalizer(() => {
|
|
19
|
+
if (get.registry.getNodes().has(snapshot) && Option.isSome(get.registry.get(snapshot))) {
|
|
20
|
+
owner.keep(retained, snapshot, runtime);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
function display(result: AsyncResult.AsyncResult<A, E>) {
|
|
24
|
+
if (AsyncResult.isSuccess(result) && !result.waiting) {
|
|
25
|
+
get.set(snapshot, Option.some(result));
|
|
26
|
+
}
|
|
27
|
+
const kept = AsyncResult.isInitial(result) ? get.once(snapshot) : Option.none<AsyncResult.Success<A, E>>();
|
|
28
|
+
get.set(signals, { ...get.once(signals), retained: Option.isSome(kept) });
|
|
29
|
+
return Option.match(kept, { onNone: () => result, onSome: AsyncResult.waiting });
|
|
30
|
+
}
|
|
31
|
+
const context = new Proxy(get, {
|
|
32
|
+
get: (target, key, receiver) =>
|
|
33
|
+
key === "setSelf" ? (value: AsyncResult.AsyncResult<A, E>) => get.setSelf(display(value)) : Reflect.get(target, key, receiver),
|
|
34
|
+
});
|
|
35
|
+
return display(atom.read(context));
|
|
36
|
+
}).pipe(Atom.setIdleTTL(0));
|
|
37
|
+
shareQuerySignals(retained, atom);
|
|
38
|
+
return retained;
|
|
39
|
+
}
|
package/src/rpcClient.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Context, Effect, Layer, Option, Stream } from "effect";
|
|
2
2
|
import * as Atom from "effect/reactivity/Atom";
|
|
3
3
|
import * as AtomRpc from "effect/reactivity/AtomRpc";
|
|
4
4
|
import { type Rpc, RpcClient, RpcGroup } from "effect/rpc";
|
|
@@ -7,6 +7,7 @@ import { withClientProtocol } from "#adapters/withClientProtocol.ts";
|
|
|
7
7
|
import { Activity } from "#clientActivity.ts";
|
|
8
8
|
import type { ClientOptions } from "#clientOptions.ts";
|
|
9
9
|
import type { ResumeWindow } from "#resume.ts";
|
|
10
|
+
import { CommandInvalidation } from "#serverMetadata.ts";
|
|
10
11
|
import { clientActivities, makeActivity } from "./clientActivities.ts";
|
|
11
12
|
import { connectionProtocol, protocolConnections } from "./connectionProtocol.ts";
|
|
12
13
|
import { commandConnections } from "./connectionReadiness.ts";
|
|
@@ -22,6 +23,9 @@ export interface RpcSource {
|
|
|
22
23
|
export function rpcClient(name: string, source: RpcSource, options: ClientOptions) {
|
|
23
24
|
const activity = makeActivity(options.runtime ?? Atom.runtime);
|
|
24
25
|
const group = RpcGroup.make(...source.requests.values()).annotateMerge(source.annotations);
|
|
26
|
+
const commands = new Set(
|
|
27
|
+
[...group.requests].filter(([, rpc]) => Option.isSome(Context.getOption(rpc.annotations, CommandInvalidation))).map(([tag]) => tag),
|
|
28
|
+
);
|
|
25
29
|
function sharedProtocol(identity?: string) {
|
|
26
30
|
return socketProtocol({
|
|
27
31
|
bearerToken: options.bearerToken,
|
|
@@ -56,7 +60,7 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
56
60
|
Stream.flatMap((hint) => Stream.concat(Stream.succeed(hint), Stream.fromEffect(Effect.sync(activity.hint)).pipe(Stream.drain))),
|
|
57
61
|
);
|
|
58
62
|
}
|
|
59
|
-
return Effect.isEffect(result) && !args[2]?.asQueue ? activity.track(result) : result;
|
|
63
|
+
return Effect.isEffect(result) && !args[2]?.asQueue ? activity.track(result, commands.has(args[0])) : result;
|
|
60
64
|
},
|
|
61
65
|
});
|
|
62
66
|
commandConnections.set(observed, wait);
|