@shivaedev/contract 0.42.0 → 0.43.1
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 +17 -0
- package/README.md +27 -177
- package/dist/AutomaticQueryOptions.d.ts +0 -5
- package/dist/AutomaticQueryOptions.d.ts.map +1 -1
- package/dist/HostVersion.d.ts +1 -19
- package/dist/HostVersion.d.ts.map +1 -1
- package/dist/adapters/activityState.d.ts.map +1 -1
- package/dist/adapters/activityState.js +0 -1
- package/dist/adapters/activityState.js.map +1 -1
- package/dist/adapters/bindingOwner.d.ts.map +1 -1
- package/dist/adapters/bindingOwner.js +1 -1
- package/dist/adapters/bindingOwner.js.map +1 -1
- package/dist/adapters/liveHintStream.d.ts +1 -2
- 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 +1 -2
- package/dist/adapters/openLiveHints.d.ts.map +1 -1
- package/dist/adapters/openLiveHints.js +1 -1
- package/dist/adapters/openLiveHints.js.map +1 -1
- package/dist/adapters/standaloneLiveServer.d.ts +1 -7
- package/dist/adapters/standaloneLiveServer.d.ts.map +1 -1
- package/dist/adapters/standaloneLiveServer.js.map +1 -1
- package/dist/automaticLive.d.ts +2 -4
- package/dist/automaticLive.d.ts.map +1 -1
- package/dist/automaticLive.js +5 -14
- 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 +20 -26
- package/dist/automaticQuery.js.map +1 -1
- package/dist/changesSuccess.d.ts +2 -3
- package/dist/changesSuccess.d.ts.map +1 -1
- package/dist/changesSuccess.js +1 -3
- package/dist/changesSuccess.js.map +1 -1
- package/dist/clientActivities.d.ts +0 -2
- package/dist/clientActivities.d.ts.map +1 -1
- package/dist/clientActivities.js +1 -5
- package/dist/clientActivities.js.map +1 -1
- package/dist/clientActivity.d.ts +0 -1
- 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.map +1 -1
- package/dist/clientOperations.js +1 -3
- package/dist/clientOperations.js.map +1 -1
- package/dist/compareQueries.d.ts +2 -19
- package/dist/compareQueries.d.ts.map +1 -1
- package/dist/compareQueries.js +6 -33
- package/dist/compareQueries.js.map +1 -1
- package/dist/connectionProtocol.d.ts +1 -1
- package/dist/connectionProtocol.d.ts.map +1 -1
- package/dist/connectionProtocol.js +1 -1
- package/dist/connectionProtocol.js.map +1 -1
- package/dist/live-server/layer.js +1 -1
- package/dist/live-server/layer.js.map +1 -1
- package/dist/live-server/register.d.ts +1 -2
- package/dist/live-server/register.d.ts.map +1 -1
- package/dist/live-server/resync.d.ts +1 -1
- package/dist/live-server/resync.d.ts.map +1 -1
- package/dist/live-server/service.d.ts +0 -2
- package/dist/live-server/service.d.ts.map +1 -1
- package/dist/live-server/service.js +4 -4
- package/dist/live-server/service.js.map +1 -1
- package/dist/live-server/stream.d.ts +1 -2
- package/dist/live-server/stream.d.ts.map +1 -1
- package/dist/live-server/subscribe.d.ts +2 -2
- package/dist/live-server/subscribe.d.ts.map +1 -1
- package/dist/live-server/subscribe.js +1 -1
- package/dist/live-server/subscribe.js.map +1 -1
- package/dist/live.d.ts +5 -12
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +15 -85
- package/dist/live.js.map +1 -1
- package/dist/liveAttempt.d.ts +3 -6
- package/dist/liveAttempt.d.ts.map +1 -1
- package/dist/liveAttempt.js +11 -30
- package/dist/liveAttempt.js.map +1 -1
- package/dist/liveHintQueue.d.ts +0 -1
- package/dist/liveHintQueue.d.ts.map +1 -1
- package/dist/liveHintQueue.js +9 -12
- package/dist/liveHintQueue.js.map +1 -1
- package/dist/makeLiveServer.d.ts +1 -5
- package/dist/makeLiveServer.d.ts.map +1 -1
- package/dist/operation.d.ts +2 -4
- package/dist/operation.d.ts.map +1 -1
- package/dist/operation.js +0 -1
- package/dist/operation.js.map +1 -1
- package/dist/operationDeclaration.d.ts +1 -2
- package/dist/operationDeclaration.d.ts.map +1 -1
- package/dist/operationDeclaration.js.map +1 -1
- package/dist/publicConfig.d.ts +6 -21
- package/dist/publicConfig.d.ts.map +1 -1
- package/dist/queryDelivery.d.ts +4 -5
- package/dist/queryDelivery.d.ts.map +1 -1
- package/dist/queryDelivery.js +4 -15
- package/dist/queryDelivery.js.map +1 -1
- package/dist/queryProgress.d.ts.map +1 -1
- package/dist/queryProgress.js +1 -4
- package/dist/queryProgress.js.map +1 -1
- package/dist/querySubscription.d.ts +1 -1
- package/dist/querySubscription.d.ts.map +1 -1
- package/dist/queryUpdate.d.ts +15 -4
- package/dist/queryUpdate.d.ts.map +1 -1
- package/dist/queryUpdate.js +7 -1
- package/dist/queryUpdate.js.map +1 -1
- package/dist/retainQuery.d.ts.map +1 -1
- package/dist/retainQuery.js +0 -3
- package/dist/retainQuery.js.map +1 -1
- package/dist/rpcClient.d.ts.map +1 -1
- package/dist/rpcClient.js +4 -4
- package/dist/rpcClient.js.map +1 -1
- package/dist/socketProtocol.js +1 -1
- package/dist/socketProtocol.js.map +1 -1
- package/package.json +15 -15
- package/src/AutomaticQueryOptions.ts +0 -5
- package/src/adapters/activityState.ts +0 -1
- package/src/adapters/bindingOwner.ts +1 -1
- package/src/adapters/liveHintStream.ts +2 -2
- package/src/adapters/openLiveHints.ts +1 -1
- package/src/adapters/standaloneLiveServer.ts +1 -5
- package/src/automaticLive.ts +4 -16
- package/src/automaticQuery.ts +28 -42
- package/src/changesSuccess.ts +3 -5
- package/src/clientActivities.ts +2 -6
- package/src/clientActivity.ts +1 -2
- package/src/clientOperations.ts +1 -4
- package/src/compareQueries.ts +12 -38
- package/src/connectionProtocol.ts +1 -1
- package/src/live-server/layer.ts +1 -1
- package/src/live-server/register.ts +1 -1
- package/src/live-server/resync.ts +1 -1
- package/src/live-server/service.ts +4 -4
- package/src/live-server/stream.ts +1 -1
- package/src/live-server/subscribe.ts +6 -10
- package/src/live.ts +19 -97
- package/src/liveAttempt.ts +17 -41
- package/src/liveHintQueue.ts +9 -12
- package/src/operation.ts +3 -17
- package/src/operationDeclaration.ts +0 -2
- package/src/queryDelivery.ts +6 -26
- package/src/queryProgress.ts +1 -4
- package/src/querySubscription.ts +1 -1
- package/src/queryUpdate.ts +10 -4
- package/src/retainQuery.ts +0 -3
- package/src/rpcClient.ts +4 -4
- package/src/socketProtocol.ts +1 -1
- package/dist/liveInvalidation.d.ts +0 -12
- package/dist/liveInvalidation.d.ts.map +0 -1
- package/dist/liveInvalidation.js +0 -14
- package/dist/liveInvalidation.js.map +0 -1
- package/dist/livePending.d.ts +0 -7
- package/dist/livePending.d.ts.map +0 -1
- package/dist/livePending.js +0 -27
- package/dist/livePending.js.map +0 -1
- package/dist/queryCoalesceWindow.d.ts +0 -4
- package/dist/queryCoalesceWindow.d.ts.map +0 -1
- package/dist/queryCoalesceWindow.js +0 -11
- package/dist/queryCoalesceWindow.js.map +0 -1
- package/dist/queryFailure.d.ts +0 -4
- package/dist/queryFailure.d.ts.map +0 -1
- package/dist/queryFailure.js +0 -12
- package/dist/queryFailure.js.map +0 -1
- package/dist/queryHints.d.ts +0 -9
- package/dist/queryHints.d.ts.map +0 -1
- package/dist/queryHints.js +0 -34
- package/dist/queryHints.js.map +0 -1
- package/dist/queryReactivity.d.ts +0 -5
- package/dist/queryReactivity.d.ts.map +0 -1
- package/dist/queryReactivity.js +0 -109
- package/dist/queryReactivity.js.map +0 -1
- package/src/liveInvalidation.ts +0 -21
- package/src/livePending.ts +0 -28
- package/src/queryCoalesceWindow.ts +0 -13
- package/src/queryFailure.ts +0 -13
- package/src/queryHints.ts +0 -39
- package/src/queryReactivity.ts +0 -127
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Duration } from "effect";
|
|
2
1
|
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
2
|
import type * as Atom from "effect/reactivity/Atom";
|
|
4
3
|
import type { LiveBound, LiveConnection } from "#automaticLive.ts";
|
|
@@ -6,14 +5,10 @@ import type { LiveStatus } from "#live.ts";
|
|
|
6
5
|
import type { QuerySignals } from "#querySignals.ts";
|
|
7
6
|
|
|
8
7
|
export interface AutomaticQueryOptions<A, E> {
|
|
9
|
-
readonly atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>;
|
|
10
8
|
readonly clientStatuses: Atom.Writable<ReadonlyMap<object, LiveStatus<unknown>>> | undefined;
|
|
11
|
-
readonly coalesceWindow: Atom.Writable<Duration.Input>;
|
|
12
9
|
readonly equivalent: (left: A, right: A) => boolean;
|
|
13
10
|
readonly paused: Atom.Writable<boolean>;
|
|
14
|
-
readonly pushed: boolean;
|
|
15
11
|
readonly ready: Atom.Writable<boolean>;
|
|
16
|
-
readonly resultVersion: Atom.Atom<number>;
|
|
17
12
|
readonly signals: Atom.Writable<QuerySignals>;
|
|
18
13
|
readonly source: Atom.Atom<AsyncResult.AsyncResult<A, E>>;
|
|
19
14
|
readonly statuses: Atom.Writable<ReadonlyMap<object, LiveStatus<unknown>>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
2
|
+
import { clientId } from "#adapters/clientId.ts";
|
|
2
3
|
import type { KeyContext } from "#clientKeys.ts";
|
|
3
|
-
import { clientId } from "./clientId.ts";
|
|
4
4
|
|
|
5
5
|
export interface BindingOwner {
|
|
6
6
|
readonly context: KeyContext;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Effect, Stream } from "effect";
|
|
2
2
|
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
3
3
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
|
+
import { LiveDispatcher } from "#adapters/LiveDispatcher.ts";
|
|
5
|
+
import { openLiveHints } from "#adapters/openLiveHints.ts";
|
|
4
6
|
import type { initialize } from "#live-server/initialize.ts";
|
|
5
|
-
import { LiveDispatcher } from "./LiveDispatcher.ts";
|
|
6
|
-
import { openLiveHints } from "./openLiveHints.ts";
|
|
7
7
|
|
|
8
8
|
export const liveHintStream = Effect.fn("LiveServer.liveHintStream")(function* (state: Effect.Success<typeof initialize>, keys: readonly Key[]) {
|
|
9
9
|
const reactivity = yield* Reactivity.Reactivity;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Effect, Queue, Ref, Stream } from "effect";
|
|
2
2
|
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
3
3
|
import { type Key, readKeys } from "@shivaedev/changes/keys.ts";
|
|
4
|
+
import { LiveDispatcher } from "#adapters/LiveDispatcher.ts";
|
|
4
5
|
import type { initialize } from "#live-server/initialize.ts";
|
|
5
6
|
import { liveHintQueue } from "#liveHintQueue.ts";
|
|
6
|
-
import { LiveDispatcher } from "./LiveDispatcher.ts";
|
|
7
7
|
|
|
8
8
|
export const openLiveHints = Effect.fn("LiveServer.openLiveHints")(function* (state: Effect.Success<typeof initialize>, keys: readonly Key[]) {
|
|
9
9
|
const reactivity = yield* Reactivity.Reactivity;
|
|
@@ -4,15 +4,11 @@ import { liveServerLayer } from "#live-server/layer.ts";
|
|
|
4
4
|
import { LiveServer } from "#live-server/service.ts";
|
|
5
5
|
import { subscribe as subscribeQueries } from "#live-server/subscribe.ts";
|
|
6
6
|
import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
|
|
7
|
-
import type { QueryUpdate, SubscriptionHint } from "#queryUpdate.ts";
|
|
8
|
-
import type { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
9
7
|
|
|
10
8
|
export const standaloneLiveServer = Effect.gen(function* () {
|
|
11
9
|
const context = yield* Effect.scoped(Layer.build(liveServerLayer));
|
|
12
10
|
const server = Context.get(context, LiveServer);
|
|
13
|
-
function subscribe<E, R>(subscription: QuerySubscription<E, R
|
|
14
|
-
function subscribe<E, R>(subscription: QuerySubscription<E, R>, read: QueryRerun<E, R>): Stream.Stream<QueryUpdate, E | QueryReadDenied, R>;
|
|
15
|
-
function subscribe<E, R>(subscription: QuerySubscription<E, R>, read?: QueryRerun<E, R>) {
|
|
11
|
+
function subscribe<E, R>(subscription: QuerySubscription<E, R>, read: QueryRerun<E, R>) {
|
|
16
12
|
return Stream.unwrap(Effect.provideContext(subscribeQueries(subscription, read), context));
|
|
17
13
|
}
|
|
18
14
|
return { resync: server.resync(), stream: (keys: readonly Key[]) => Stream.unwrap(server.stream(keys)), subscribe };
|
package/src/automaticLive.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect, type Schema, Stream } from "effect";
|
|
2
2
|
import type { Headers } from "effect/http";
|
|
3
3
|
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
4
4
|
import * as Atom from "effect/reactivity/Atom";
|
|
@@ -11,7 +11,6 @@ import { automaticQuery } from "#automaticQuery.ts";
|
|
|
11
11
|
import { clientActivities } from "#clientActivities.ts";
|
|
12
12
|
import { type LiveStatus, live } from "#live.ts";
|
|
13
13
|
import { queryDelivery } from "#queryDelivery.ts";
|
|
14
|
-
import { localQueryKeys, queryHint } from "#queryHints.ts";
|
|
15
14
|
import { queryProgress } from "#queryProgress.ts";
|
|
16
15
|
import { querySignals, shareQuerySignals } from "#querySignals.ts";
|
|
17
16
|
import type { QueryUpdate } from "#queryUpdate.ts";
|
|
@@ -24,7 +23,6 @@ declare const window: ResumeWindow | undefined;
|
|
|
24
23
|
export const LiveConnection = Symbol.for("@shivaedev/contract/LiveConnection");
|
|
25
24
|
export interface LiveBound {
|
|
26
25
|
readonly [LiveConnection]: {
|
|
27
|
-
readonly coalesceWindow: Atom.Writable<Duration.Input>;
|
|
28
26
|
readonly connection: Atom.Atom<unknown>;
|
|
29
27
|
readonly identity?: Atom.Writable<string | undefined>;
|
|
30
28
|
readonly paused: Atom.Writable<boolean>;
|
|
@@ -58,7 +56,6 @@ export function automaticLive(
|
|
|
58
56
|
throw new Error("A session-scoped query belongs to its binding registry");
|
|
59
57
|
}
|
|
60
58
|
}
|
|
61
|
-
const coalesceWindow = Atom.keepAlive(Atom.make<Duration.Input>("20 millis"));
|
|
62
59
|
const paused = pausedByRuntime(service.runtime);
|
|
63
60
|
const statuses = Atom.make<ReadonlyMap<object, LiveStatus<unknown>>>(new Map());
|
|
64
61
|
const status = Atom.map(statuses, (current) => aggregateLiveStatus([...current.values()]));
|
|
@@ -75,7 +72,6 @@ export function automaticLive(
|
|
|
75
72
|
keys: readonly Key[],
|
|
76
73
|
equivalent: (left: A, right: A) => boolean,
|
|
77
74
|
decode: (value: unknown) => A,
|
|
78
|
-
compared: boolean,
|
|
79
75
|
reactive: (source: Atom.Atom<AsyncResult.AsyncResult<A, E>>) => Atom.Atom<AsyncResult.AsyncResult<A, E>>,
|
|
80
76
|
headers?: Headers.Input,
|
|
81
77
|
) {
|
|
@@ -84,30 +80,22 @@ export function automaticLive(
|
|
|
84
80
|
}
|
|
85
81
|
const ready = Atom.make(false);
|
|
86
82
|
const signals = querySignals(atom);
|
|
87
|
-
const delivery = queryDelivery(
|
|
88
|
-
const source =
|
|
83
|
+
const delivery = queryDelivery(decode, paused, signals);
|
|
84
|
+
const source = delivery.atom;
|
|
89
85
|
const subscription = live(service.runtime, {
|
|
90
|
-
initialResync: false,
|
|
91
86
|
keyContext: owner?.context,
|
|
92
|
-
reopenOnResume: compared,
|
|
93
87
|
resume: signal,
|
|
94
|
-
resyncKeys: compared ? [] : localQueryKeys(keys),
|
|
95
88
|
retryKeys: keys,
|
|
96
89
|
stream: Stream.unwrap(service.use((client) => Effect.succeed(client(`${name}.$changes`, { queries: [reference] }, { headers })))).pipe(
|
|
97
90
|
delivery.stream,
|
|
98
|
-
Stream.map((hint) => queryHint(hint, [{ keys }])),
|
|
99
91
|
),
|
|
100
92
|
});
|
|
101
93
|
const gated = automaticQuery({
|
|
102
94
|
assertOwner,
|
|
103
|
-
atom,
|
|
104
95
|
clientStatuses: clientActivities.get(service)?.statuses,
|
|
105
|
-
coalesceWindow,
|
|
106
96
|
equivalent,
|
|
107
97
|
paused,
|
|
108
|
-
pushed: compared,
|
|
109
98
|
ready,
|
|
110
|
-
resultVersion: delivery.version,
|
|
111
99
|
signals,
|
|
112
100
|
source,
|
|
113
101
|
statuses,
|
|
@@ -121,5 +109,5 @@ export function automaticLive(
|
|
|
121
109
|
}
|
|
122
110
|
return gated;
|
|
123
111
|
}
|
|
124
|
-
return { query, subscription: {
|
|
112
|
+
return { query, subscription: { connection, identity: socketIdentity, paused, status } };
|
|
125
113
|
}
|
package/src/automaticQuery.ts
CHANGED
|
@@ -3,8 +3,6 @@ import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
|
3
3
|
import * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import type { AutomaticQueryOptions } from "#AutomaticQueryOptions.ts";
|
|
5
5
|
import type { LiveStatus } from "#live.ts";
|
|
6
|
-
import { queryCoalesceWindow } from "#queryCoalesceWindow.ts";
|
|
7
|
-
import { queryFailure } from "#queryFailure.ts";
|
|
8
6
|
import { recordCompletion, recordSubscription } from "#queryProgress.ts";
|
|
9
7
|
import { queryResult } from "#queryResult.ts";
|
|
10
8
|
|
|
@@ -12,15 +10,11 @@ interface GateOptions<A, E> extends AutomaticQueryOptions<A, E> {
|
|
|
12
10
|
readonly assertOwner: (get: Atom.AtomContext) => void;
|
|
13
11
|
}
|
|
14
12
|
export function automaticQuery<A, E>({
|
|
15
|
-
atom,
|
|
16
13
|
assertOwner,
|
|
17
|
-
coalesceWindow,
|
|
18
14
|
clientStatuses,
|
|
19
15
|
equivalent,
|
|
20
16
|
paused,
|
|
21
|
-
pushed,
|
|
22
17
|
ready,
|
|
23
|
-
resultVersion,
|
|
24
18
|
signals,
|
|
25
19
|
source,
|
|
26
20
|
statuses,
|
|
@@ -31,7 +25,6 @@ export function automaticQuery<A, E>({
|
|
|
31
25
|
(get) => {
|
|
32
26
|
assertOwner(get);
|
|
33
27
|
get.mount(signals);
|
|
34
|
-
get.mount(resultVersion);
|
|
35
28
|
get.mount(subscriptionVersion);
|
|
36
29
|
const openingVersion = get.once(subscriptionVersion);
|
|
37
30
|
get.refresh(subscription.connection);
|
|
@@ -42,8 +35,6 @@ export function automaticQuery<A, E>({
|
|
|
42
35
|
get.addFinalizer(() => stopSource?.());
|
|
43
36
|
get.set(ready, false);
|
|
44
37
|
get.set(signals, { ...get.once(signals), pending: true, subscription: "opening" });
|
|
45
|
-
get.once(queryCoalesceWindow(gated));
|
|
46
|
-
get.subscribe(queryCoalesceWindow(gated), (value) => get.set(queryCoalesceWindow(source), value), { immediate: true });
|
|
47
38
|
const token = {};
|
|
48
39
|
let active = true;
|
|
49
40
|
function observe(value: LiveStatus<unknown>) {
|
|
@@ -73,17 +64,10 @@ export function automaticQuery<A, E>({
|
|
|
73
64
|
}
|
|
74
65
|
});
|
|
75
66
|
get.addFinalizer(registry.subscribe(subscription.status, observe, { immediate: true }));
|
|
76
|
-
get.addFinalizer(registry.subscribe(coalesceWindow, (value) => registry.set(subscription.coalesceWindow, value), { immediate: true }));
|
|
77
|
-
let started = false;
|
|
78
|
-
let deniedVersion = 0;
|
|
79
67
|
function start() {
|
|
80
68
|
if (stopSource !== undefined) {
|
|
81
69
|
return;
|
|
82
70
|
}
|
|
83
|
-
if (!pushed && get.once(resultVersion) === deniedVersion) {
|
|
84
|
-
get.refresh(source);
|
|
85
|
-
}
|
|
86
|
-
started = true;
|
|
87
71
|
const publish = queryResult<A, E>(equivalent, (value) => {
|
|
88
72
|
if (!denied) {
|
|
89
73
|
get.setSelf(value);
|
|
@@ -96,37 +80,41 @@ export function automaticQuery<A, E>({
|
|
|
96
80
|
get.subscribe(ready, (value) => {
|
|
97
81
|
if (value) {
|
|
98
82
|
start();
|
|
83
|
+
} else {
|
|
84
|
+
const previous = get.self<AsyncResult.AsyncResult<A, E>>();
|
|
85
|
+
if (Option.isSome(previous)) {
|
|
86
|
+
const waiting = AsyncResult.waiting(previous.value);
|
|
87
|
+
get.setSelf(waiting);
|
|
88
|
+
get.set(signals, recordCompletion(get.once(signals), waiting));
|
|
89
|
+
}
|
|
99
90
|
}
|
|
100
91
|
});
|
|
101
|
-
|
|
102
|
-
(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
);
|
|
116
|
-
get.subscribe(subscription.status, failed);
|
|
92
|
+
get.subscribe(subscription.status, ({ connection: state, failure }) => {
|
|
93
|
+
if (state !== "failed" || Option.isNone(failure)) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const cause = failure.value;
|
|
97
|
+
if (Option.exists(Cause.findErrorOption(cause), Predicate.isTagged("QueryReadDenied"))) {
|
|
98
|
+
denied = true;
|
|
99
|
+
registry.set(ready, false);
|
|
100
|
+
stopSource?.();
|
|
101
|
+
stopSource = undefined;
|
|
102
|
+
}
|
|
103
|
+
const result = AsyncResult.failure(cause);
|
|
104
|
+
get.setSelf(result);
|
|
105
|
+
get.set(signals, recordCompletion(get.once(signals), result));
|
|
106
|
+
});
|
|
117
107
|
get.addFinalizer(registry.subscribe(paused, (value) => registry.set(subscription.paused, value), { immediate: true }));
|
|
118
108
|
function resume() {
|
|
119
109
|
if (get.once(subscription.status).connection !== "failed") {
|
|
120
110
|
get.refresh(subscription.connection);
|
|
121
111
|
}
|
|
122
112
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
});
|
|
129
|
-
}
|
|
113
|
+
get.subscribe(paused, (value) => {
|
|
114
|
+
if (!value) {
|
|
115
|
+
resume();
|
|
116
|
+
}
|
|
117
|
+
});
|
|
130
118
|
get.mount(subscription.connection);
|
|
131
119
|
if (get.once(ready)) {
|
|
132
120
|
start();
|
|
@@ -135,9 +123,7 @@ export function automaticQuery<A, E>({
|
|
|
135
123
|
return AsyncResult.initial();
|
|
136
124
|
},
|
|
137
125
|
(refresh) => {
|
|
138
|
-
|
|
139
|
-
refresh(atom);
|
|
140
|
-
}
|
|
126
|
+
refresh(ready);
|
|
141
127
|
refresh(subscription.connection);
|
|
142
128
|
},
|
|
143
129
|
).pipe(Atom.setIdleTTL(0));
|
package/src/changesSuccess.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
-
import { LiveHint } from "#live.ts";
|
|
3
2
|
import type { QueryShape } from "#operation.ts";
|
|
4
3
|
import { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
5
4
|
|
|
@@ -13,18 +12,17 @@ type ResultSchema<TQuery extends QueryShape> = TQuery extends QueryShape
|
|
|
13
12
|
queryIndex: Schema.Number;
|
|
14
13
|
value: TQuery["success"];
|
|
15
14
|
}>
|
|
16
|
-
|
|
|
15
|
+
| typeof QueryUnchanged
|
|
17
16
|
: never;
|
|
18
17
|
|
|
19
18
|
export function changesSuccess<const TQueries extends readonly QueryShape[]>(
|
|
20
19
|
queries: TQueries,
|
|
21
|
-
): Schema.Union<readonly [typeof
|
|
20
|
+
): Schema.Union<readonly [typeof QueryReadDenied, ...ResultSchema<TQueries[number]>[]]>;
|
|
22
21
|
export function changesSuccess(queries: readonly QueryShape[]) {
|
|
23
22
|
return Schema.Union([
|
|
24
|
-
LiveHint,
|
|
25
23
|
QueryReadDenied,
|
|
26
24
|
...queries.flatMap((query) => [
|
|
27
|
-
|
|
25
|
+
QueryUnchanged,
|
|
28
26
|
Schema.Struct({
|
|
29
27
|
_tag: Schema.Literal("Result"),
|
|
30
28
|
query: Schema.Literal(query.name),
|
package/src/clientActivities.ts
CHANGED
|
@@ -3,8 +3,7 @@ import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
|
3
3
|
import type * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import { activityState } from "#adapters/activityState.ts";
|
|
5
5
|
import { clientConnection } from "#adapters/clientConnection.ts";
|
|
6
|
-
import {
|
|
7
|
-
import { wrapAtom } from "./wrapAtom.ts";
|
|
6
|
+
import { wrapAtom } from "#wrapAtom.ts";
|
|
8
7
|
|
|
9
8
|
type ActivityTracker = ReturnType<typeof makeActivity>;
|
|
10
9
|
export const clientActivities = new WeakMap<object, ActivityTracker>();
|
|
@@ -12,7 +11,7 @@ export const clientActivities = new WeakMap<object, ActivityTracker>();
|
|
|
12
11
|
export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
13
12
|
const { activity, change, hold, setConnection } = activityState();
|
|
14
13
|
const connection = clientConnection(factory, setConnection, activity);
|
|
15
|
-
function begin(key: "rpcCalls" | "
|
|
14
|
+
function begin(key: "rpcCalls" | "commands") {
|
|
16
15
|
change(key, 1);
|
|
17
16
|
return () => change(key, -1);
|
|
18
17
|
}
|
|
@@ -43,8 +42,6 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
43
42
|
function watch<A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) {
|
|
44
43
|
const watched = wrapAtom(atom, (get) => {
|
|
45
44
|
get.mount(connection.status);
|
|
46
|
-
get.once(queryCoalesceWindow(watched));
|
|
47
|
-
get.subscribe(queryCoalesceWindow(watched), (duration) => get.set(queryCoalesceWindow(atom), duration), { immediate: true });
|
|
48
45
|
const refreshing = counter("refreshingAtoms");
|
|
49
46
|
const initial = counter("initialQueries");
|
|
50
47
|
let received = false;
|
|
@@ -90,7 +87,6 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
90
87
|
),
|
|
91
88
|
true,
|
|
92
89
|
),
|
|
93
|
-
queued: () => begin("liveInvalidations"),
|
|
94
90
|
statuses: connection.statuses,
|
|
95
91
|
track,
|
|
96
92
|
watch,
|
package/src/clientActivity.ts
CHANGED
|
@@ -8,7 +8,6 @@ export interface ClientActivityState {
|
|
|
8
8
|
readonly connectionStatus: ConnectionStatus;
|
|
9
9
|
readonly initialQueries: number;
|
|
10
10
|
readonly liveHints: number;
|
|
11
|
-
readonly liveInvalidations: number;
|
|
12
11
|
readonly refreshingAtoms: number;
|
|
13
12
|
readonly rpcCalls: number;
|
|
14
13
|
}
|
|
@@ -29,7 +28,7 @@ export function clientActivity(client: ActiveClient): ClientActivity {
|
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
export function isClientIdle(state: ClientActivityState): boolean {
|
|
32
|
-
return state.rpcCalls === 0 && state.
|
|
31
|
+
return state.rpcCalls === 0 && state.refreshingAtoms === 0;
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
export function isClientSettled(state: ClientActivityState): boolean {
|
package/src/clientOperations.ts
CHANGED
|
@@ -14,7 +14,6 @@ import { clientInvalidationKeys, clientReadKeys } from "#clientKeys.ts";
|
|
|
14
14
|
import { commandPayload } from "#commandPayload.ts";
|
|
15
15
|
import { waitForConnection } from "#connectionReadiness.ts";
|
|
16
16
|
import type { CommandShape, QueryShape } from "#operation.ts";
|
|
17
|
-
import { queryReactivity } from "#queryReactivity.ts";
|
|
18
17
|
import { shareQuerySignals } from "#querySignals.ts";
|
|
19
18
|
import { retainQuery } from "#retainQuery.ts";
|
|
20
19
|
|
|
@@ -37,7 +36,6 @@ export function clientOperations(
|
|
|
37
36
|
}
|
|
38
37
|
|
|
39
38
|
function boundQuery(tag: string, query: QueryShape) {
|
|
40
|
-
const reactive = queryReactivity(ordinaryClient.runtime, clientActivities.get(ordinaryClient)?.queued);
|
|
41
39
|
const cached = new WeakMap<ReturnType<typeof ordinaryClient.query>, ReturnType<typeof ordinaryClient.query>>();
|
|
42
40
|
return {
|
|
43
41
|
query: (payload: unknown, options: QueryOptions = {}) => {
|
|
@@ -57,8 +55,7 @@ export function clientOperations(
|
|
|
57
55
|
keys,
|
|
58
56
|
Schema.toEquivalence(query.success),
|
|
59
57
|
Schema.decodeUnknownSync(Schema.toType(query.success)),
|
|
60
|
-
|
|
61
|
-
(source) => reactive(source, clientReadKeys(dependencies, owner?.context), owner?.registry),
|
|
58
|
+
(source) => ordinaryClient.runtime.factory.withReactivity(clientReadKeys(dependencies, owner?.context))(source),
|
|
62
59
|
options.headers,
|
|
63
60
|
);
|
|
64
61
|
const tracked = retainQuery(ordinaryClient.runtime, clientActivities.get(ordinaryClient)?.watch(result) ?? result);
|
package/src/compareQueries.ts
CHANGED
|
@@ -2,45 +2,23 @@ import { Effect, Schema } from "effect";
|
|
|
2
2
|
import { Digest } from "effect/crypto";
|
|
3
3
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
4
|
import type { LiveHint } from "#live.ts";
|
|
5
|
-
import { changedHint } from "#queryHints.ts";
|
|
6
5
|
import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
|
|
7
6
|
import type { QueryResult, QueryUnchanged, QueryUpdate } from "#queryUpdate.ts";
|
|
8
7
|
import type { QueryRequest } from "#serverMetadata.ts";
|
|
9
8
|
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
const scopes = subscription.scopes.map((scope, index) => (queries[index]?.query.live === "rerun-and-compare" ? [] : scope));
|
|
19
|
-
if (queries.every(({ query }) => query.live === "rerun-and-compare")) {
|
|
20
|
-
return [];
|
|
21
|
-
}
|
|
22
|
-
if (hint._tag === "Resync") {
|
|
23
|
-
return [hint];
|
|
24
|
-
}
|
|
25
|
-
const keys = new Set(scopes.flatMap((scope) => scope.map((key) => JSON.stringify(key))));
|
|
26
|
-
const visible: readonly Key[] = hint.keys.filter((key) => keys.has(JSON.stringify(key)));
|
|
27
|
-
return visible.length === 0 ? [] : [changedHint(visible, scopes)];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function compareQueries<E, R>(subscription: QuerySubscription<unknown, unknown>, read?: QueryRerun<E, R>, initialRead = true) {
|
|
31
|
-
const queries = subscription.queries ?? [];
|
|
9
|
+
export function compareQueries<E, R>(
|
|
10
|
+
subscription: QuerySubscription<unknown, unknown>,
|
|
11
|
+
read: QueryRerun<E, R>,
|
|
12
|
+
initialRead?: boolean,
|
|
13
|
+
): (hint: LiveHint, scopes?: readonly (readonly Key[])[]) => Effect.Effect<readonly QueryUpdate[], E, R>;
|
|
14
|
+
export function compareQueries<E, R>(subscription: QuerySubscription<unknown, unknown>, read: QueryRerun<E, R>, initialRead = true) {
|
|
15
|
+
const queries = subscription.queries;
|
|
32
16
|
const hashes = new Map<number, string>();
|
|
33
|
-
let initial = initialRead &&
|
|
17
|
+
let initial = initialRead && queries.length > 0;
|
|
34
18
|
const resultFor = Effect.fnUntraced(function* (request: QueryRequest, index: number) {
|
|
35
|
-
if (read === undefined) {
|
|
36
|
-
return yield* Effect.die(new Error("Result subscriptions require an authorized query runner"));
|
|
37
|
-
}
|
|
38
19
|
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
|
-
}
|
|
42
20
|
const encoded = yield* Schema.encodeUnknownEffect(Schema.toCodecJson(request.query.success))(value).pipe(Effect.orDie);
|
|
43
|
-
const digest = yield* Digest.Digest;
|
|
21
|
+
const digest = yield* Digest.Digest.pipe(Effect.provide(Digest.layerWebCrypto));
|
|
44
22
|
const hash = yield* digest.digestHex(Digest.sha256, new TextEncoder().encode(JSON.stringify(encoded)));
|
|
45
23
|
if (hash === hashes.get(index)) {
|
|
46
24
|
return { _tag: "Unchanged", queryIndex: index } satisfies QueryUnchanged;
|
|
@@ -50,19 +28,15 @@ export function compareQueries<E, R>(subscription: QuerySubscription<unknown, un
|
|
|
50
28
|
});
|
|
51
29
|
return Effect.fnUntraced(function* (hint: LiveHint, scopes: readonly (readonly Key[])[] = subscription.scopes) {
|
|
52
30
|
const updates: QueryUpdate[] = [];
|
|
53
|
-
const
|
|
31
|
+
const changed = new Set(hint._tag === "Changed" ? hint.keys.map((key) => JSON.stringify(key)) : []);
|
|
54
32
|
const matching = queries
|
|
55
33
|
.map((request, index) => ({ index, request }))
|
|
56
|
-
.filter(
|
|
57
|
-
({ index, request }) =>
|
|
58
|
-
initial || (request.query.live === "rerun-and-compare" && (affected?._tag !== "Changed" || affected.queryIndexes?.includes(index))),
|
|
59
|
-
);
|
|
34
|
+
.filter(({ index }) => initial || hint._tag === "Resync" || scopes[index]?.some((key) => changed.has(JSON.stringify(key))));
|
|
60
35
|
for (const { index, request } of matching) {
|
|
61
36
|
const result = yield* resultFor(request, index);
|
|
62
37
|
updates.push(result);
|
|
63
38
|
}
|
|
64
|
-
const hints = initial ? [] : hintsFor(hint, { ...subscription, scopes });
|
|
65
39
|
initial = false;
|
|
66
|
-
return
|
|
40
|
+
return updates;
|
|
67
41
|
});
|
|
68
42
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Context, Effect, Layer, Option } from "effect";
|
|
2
2
|
import * as Atom from "effect/reactivity/Atom";
|
|
3
3
|
import { RpcClient } from "effect/rpc";
|
|
4
|
-
import { ConnectionReadiness, connectionReadiness } from "
|
|
4
|
+
import { ConnectionReadiness, connectionReadiness } from "#connectionReadiness.ts";
|
|
5
5
|
|
|
6
6
|
export const protocolConnections = new WeakMap<RpcClient.Protocol["Service"], Effect.Success<typeof connectionReadiness>>();
|
|
7
7
|
|
package/src/live-server/layer.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Layer } from "effect";
|
|
2
2
|
import { LiveDispatcher } from "#adapters/LiveDispatcher.ts";
|
|
3
|
-
import { LiveServer } from "
|
|
3
|
+
import { LiveServer } from "#live-server/service.ts";
|
|
4
4
|
|
|
5
5
|
export const liveServerLayer = LiveServer.layer.pipe(Layer.provide(LiveDispatcher.layer));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
3
3
|
import { liveHintStream } from "#adapters/liveHintStream.ts";
|
|
4
|
-
import type { initialize } from "
|
|
4
|
+
import type { initialize } from "#live-server/initialize.ts";
|
|
5
5
|
|
|
6
6
|
export function register(state: Effect.Success<typeof initialize>) {
|
|
7
7
|
return Effect.fn("LiveServer.register")(function* (keys: readonly Key[]) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect, Ref } from "effect";
|
|
2
2
|
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
3
|
-
import type { initialize } from "
|
|
3
|
+
import type { initialize } from "#live-server/initialize.ts";
|
|
4
4
|
|
|
5
5
|
export function resync(state: Effect.Success<typeof initialize>) {
|
|
6
6
|
return Effect.fn("LiveServer.resync")(function* () {
|
|
@@ -2,10 +2,10 @@ import { Random } from "effect";
|
|
|
2
2
|
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
3
3
|
import { defineService } from "@shivaedev/service/defineService.ts";
|
|
4
4
|
import { LiveDispatcher } from "#adapters/LiveDispatcher.ts";
|
|
5
|
-
import { initialize } from "
|
|
6
|
-
import { register } from "
|
|
7
|
-
import { resync } from "
|
|
8
|
-
import { stream } from "
|
|
5
|
+
import { initialize } from "#live-server/initialize.ts";
|
|
6
|
+
import { register } from "#live-server/register.ts";
|
|
7
|
+
import { resync } from "#live-server/resync.ts";
|
|
8
|
+
import { stream } from "#live-server/stream.ts";
|
|
9
9
|
|
|
10
10
|
export const LiveServer = defineService({
|
|
11
11
|
id: "@shivaedev/contract/LiveServer",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, Stream } from "effect";
|
|
2
2
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
3
3
|
import { liveHintStream } from "#adapters/liveHintStream.ts";
|
|
4
|
-
import type { initialize } from "
|
|
4
|
+
import type { initialize } from "#live-server/initialize.ts";
|
|
5
5
|
|
|
6
6
|
export function stream(state: Effect.Success<typeof initialize>) {
|
|
7
7
|
return Effect.fn("LiveServer.stream")(function* (keys: readonly Key[]) {
|
|
@@ -2,20 +2,16 @@ import { Effect, Stream } from "effect";
|
|
|
2
2
|
import { refreshSubscription } from "#adapters/refreshSubscription.ts";
|
|
3
3
|
import { compareQueries } from "#compareQueries.ts";
|
|
4
4
|
import type { LiveHint } from "#live.ts";
|
|
5
|
+
import { LiveServer } from "#live-server/service.ts";
|
|
5
6
|
import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
|
|
6
|
-
import type { QueryUpdate
|
|
7
|
+
import type { QueryUpdate } from "#queryUpdate.ts";
|
|
7
8
|
import { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
8
|
-
import { LiveServer } from "./service.ts";
|
|
9
9
|
|
|
10
|
-
function subscribeQueries<E, R
|
|
10
|
+
function subscribeQueries<E, R>(
|
|
11
11
|
subscription: QuerySubscription<E, R>,
|
|
12
|
-
read
|
|
13
|
-
): Effect.Effect<
|
|
14
|
-
|
|
15
|
-
never,
|
|
16
|
-
typeof LiveServer.Identifier
|
|
17
|
-
>;
|
|
18
|
-
function subscribeQueries<E, R>(subscription: QuerySubscription<E, R>, read?: QueryRerun<E, R>) {
|
|
12
|
+
read: QueryRerun<E, R>,
|
|
13
|
+
): Effect.Effect<Stream.Stream<QueryUpdate, E | QueryReadDenied, R>, never, typeof LiveServer.Identifier>;
|
|
14
|
+
function subscribeQueries<E, R>(subscription: QuerySubscription<E, R>, read: QueryRerun<E, R>) {
|
|
19
15
|
return Effect.fnUntraced(function* () {
|
|
20
16
|
const server = yield* LiveServer;
|
|
21
17
|
const hints = yield* server.register(subscription.keys);
|