@shivaedev/contract 0.39.0 → 0.41.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 +39 -32
- 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/LiveDispatcher.d.ts +9 -0
- package/dist/adapters/LiveDispatcher.d.ts.map +1 -0
- package/dist/adapters/LiveDispatcher.js +5 -0
- package/dist/adapters/LiveDispatcher.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/liveHintStream.d.ts +30 -0
- package/dist/adapters/liveHintStream.d.ts.map +1 -0
- package/dist/adapters/liveHintStream.js +10 -0
- package/dist/adapters/liveHintStream.js.map +1 -0
- package/dist/adapters/openLiveHints.d.ts +30 -0
- package/dist/adapters/openLiveHints.d.ts.map +1 -0
- package/dist/adapters/openLiveHints.js +59 -0
- package/dist/adapters/openLiveHints.js.map +1 -0
- package/dist/adapters/refreshSubscription.d.ts +4 -0
- package/dist/adapters/refreshSubscription.d.ts.map +1 -0
- package/dist/adapters/refreshSubscription.js +5 -0
- package/dist/adapters/refreshSubscription.js.map +1 -0
- package/dist/adapters/standaloneLiveServer.d.ts +32 -0
- package/dist/adapters/standaloneLiveServer.d.ts.map +1 -0
- package/dist/adapters/standaloneLiveServer.js +13 -0
- package/dist/adapters/standaloneLiveServer.js.map +1 -0
- package/dist/adapters/withClientProtocol.d.ts +4 -0
- package/dist/adapters/withClientProtocol.d.ts.map +1 -0
- package/dist/adapters/withClientProtocol.js +6 -0
- package/dist/adapters/withClientProtocol.js.map +1 -0
- package/dist/adapters/withPolicyContext.d.ts +4 -0
- package/dist/adapters/withPolicyContext.d.ts.map +1 -0
- package/dist/adapters/withPolicyContext.js +7 -0
- package/dist/adapters/withPolicyContext.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/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.map +1 -1
- package/dist/clientOperations.js +1 -1
- 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/initialize.d.ts +6 -0
- package/dist/live-server/initialize.d.ts.map +1 -0
- package/dist/live-server/initialize.js +7 -0
- package/dist/live-server/initialize.js.map +1 -0
- package/dist/live-server/layer.d.ts +3 -0
- package/dist/live-server/layer.d.ts.map +1 -0
- package/dist/live-server/layer.js +5 -0
- package/dist/live-server/layer.js.map +1 -0
- package/dist/live-server/register.d.ts +26 -0
- package/dist/live-server/register.d.ts.map +1 -0
- package/dist/live-server/register.js +8 -0
- package/dist/live-server/register.js.map +1 -0
- package/dist/live-server/resync.d.ts +4 -3
- package/dist/live-server/resync.d.ts.map +1 -1
- package/dist/live-server/resync.js +8 -2
- package/dist/live-server/resync.js.map +1 -1
- package/dist/live-server/service.d.ts +49 -0
- package/dist/live-server/service.d.ts.map +1 -0
- package/dist/live-server/service.js +19 -0
- package/dist/live-server/service.js.map +1 -0
- package/dist/live-server/stream.d.ts +4 -4
- package/dist/live-server/stream.d.ts.map +1 -1
- package/dist/live-server/stream.js +6 -3
- package/dist/live-server/stream.js.map +1 -1
- package/dist/live-server/subscribe.d.ts +3 -4
- package/dist/live-server/subscribe.d.ts.map +1 -1
- package/dist/live-server/subscribe.js +28 -6
- 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/makeLiveServer.d.ts +5 -10
- package/dist/makeLiveServer.d.ts.map +1 -1
- package/dist/makeLiveServer.js +3 -90
- package/dist/makeLiveServer.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/querySubscription.js +5 -11
- package/dist/querySubscription.js.map +1 -1
- package/dist/readPolicy.d.ts.map +1 -1
- package/dist/readPolicy.js +2 -3
- package/dist/readPolicy.js.map +1 -1
- package/dist/rpcClient.d.ts +1 -1
- package/dist/rpcClient.d.ts.map +1 -1
- package/dist/rpcClient.js +6 -3
- package/dist/rpcClient.js.map +1 -1
- package/package.json +8 -8
- package/src/AutomaticQueryOptions.ts +22 -0
- package/src/adapters/LiveDispatcher.ts +10 -0
- package/src/adapters/activityState.ts +9 -1
- package/src/adapters/liveHintStream.ts +15 -0
- package/src/adapters/openLiveHints.ts +62 -0
- package/src/adapters/refreshSubscription.ts +6 -0
- package/src/adapters/standaloneLiveServer.ts +19 -0
- package/src/adapters/withClientProtocol.ts +10 -0
- package/src/adapters/withPolicyContext.ts +8 -0
- package/src/automaticLive.ts +11 -7
- package/src/automaticQuery.ts +16 -21
- package/src/changesSuccess.ts +11 -14
- package/src/clientActivities.ts +29 -12
- package/src/clientActivity.ts +8 -2
- package/src/clientOperations.ts +4 -2
- package/src/compareQueries.ts +10 -4
- package/src/live-server/initialize.ts +7 -0
- package/src/live-server/layer.ts +5 -0
- package/src/live-server/register.ts +10 -0
- package/src/live-server/resync.ts +9 -4
- package/src/live-server/service.ts +19 -0
- package/src/live-server/stream.ts +7 -4
- package/src/live-server/subscribe.ts +43 -12
- package/src/liveAttempt.ts +10 -2
- package/src/makeLiveServer.ts +3 -104
- package/src/queryDelivery.ts +22 -5
- package/src/queryHints.ts +1 -1
- package/src/querySubscription.ts +8 -11
- package/src/readPolicy.ts +2 -3
- package/src/rpcClient.ts +10 -8
- package/dist/LiveServer.d.ts +0 -27
- package/dist/LiveServer.d.ts.map +0 -1
- package/dist/LiveServer.js +0 -18
- package/dist/LiveServer.js.map +0 -1
- package/src/LiveServer.ts +0 -18
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
|
@@ -45,11 +45,13 @@ function boundQuery(service: ErasedClient, tag: string, query: QueryShape, autom
|
|
|
45
45
|
local.length === 0 ? [...keys, { _tag: "List" as const, collection: `@query/${tag.slice(0, tag.lastIndexOf("."))}` }] : keys;
|
|
46
46
|
const reference = { payload, query: query.name };
|
|
47
47
|
const result = automatic.query(
|
|
48
|
-
|
|
48
|
+
atom,
|
|
49
49
|
reference,
|
|
50
50
|
keys,
|
|
51
51
|
Schema.toEquivalence(query.success),
|
|
52
|
-
|
|
52
|
+
Schema.decodeUnknownSync(Schema.toType(query.success)),
|
|
53
|
+
query.live === "rerun-and-compare",
|
|
54
|
+
(source) => reactive(source, clientReadKeys(dependencies, owner?.context), owner?.registry),
|
|
53
55
|
options.headers,
|
|
54
56
|
);
|
|
55
57
|
const tracked = clientActivities.get(service)?.watch(result) ?? result;
|
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
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
3
|
+
import { liveHintStream } from "#adapters/liveHintStream.ts";
|
|
4
|
+
import type { initialize } from "./initialize.ts";
|
|
5
|
+
|
|
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);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { Effect, Ref } from "effect";
|
|
2
|
+
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
3
|
+
import type { initialize } from "./initialize.ts";
|
|
3
4
|
|
|
4
|
-
export function resync(
|
|
5
|
-
return ()
|
|
5
|
+
export function resync(state: Effect.Success<typeof initialize>) {
|
|
6
|
+
return Effect.fn("LiveServer.resync")(function* () {
|
|
7
|
+
const reactivity = yield* Reactivity.Reactivity;
|
|
8
|
+
yield* Effect.sync(() => reactivity.invalidateUnsafe([state.resyncKey]));
|
|
9
|
+
return yield* Ref.get(state.openStreams);
|
|
10
|
+
});
|
|
6
11
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Random } from "effect";
|
|
2
|
+
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
3
|
+
import { defineService } from "@shivaedev/service/defineService.ts";
|
|
4
|
+
import { LiveDispatcher } from "#adapters/LiveDispatcher.ts";
|
|
5
|
+
import { initialize } from "./initialize.ts";
|
|
6
|
+
import { register } from "./register.ts";
|
|
7
|
+
import { resync } from "./resync.ts";
|
|
8
|
+
import { stream } from "./stream.ts";
|
|
9
|
+
|
|
10
|
+
export const LiveServer = defineService({
|
|
11
|
+
id: "@shivaedev/contract/LiveServer",
|
|
12
|
+
initialize,
|
|
13
|
+
methods: (state) => ({
|
|
14
|
+
register: register(state),
|
|
15
|
+
resync: resync(state),
|
|
16
|
+
stream: stream(state),
|
|
17
|
+
}),
|
|
18
|
+
requires: [Reactivity.Reactivity, LiveDispatcher, Random.Random],
|
|
19
|
+
});
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
1
|
+
import { Effect, Stream } from "effect";
|
|
2
2
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
3
|
-
import
|
|
3
|
+
import { liveHintStream } from "#adapters/liveHintStream.ts";
|
|
4
|
+
import type { initialize } from "./initialize.ts";
|
|
4
5
|
|
|
5
|
-
export function stream(
|
|
6
|
-
return (keys: readonly Key[])
|
|
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));
|
|
9
|
+
});
|
|
7
10
|
}
|
|
@@ -1,16 +1,47 @@
|
|
|
1
|
-
import { Effect,
|
|
2
|
-
import
|
|
1
|
+
import { Effect, Stream } from "effect";
|
|
2
|
+
import { refreshSubscription } from "#adapters/refreshSubscription.ts";
|
|
3
|
+
import { compareQueries } from "#compareQueries.ts";
|
|
4
|
+
import type { LiveHint } from "#live.ts";
|
|
3
5
|
import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
|
|
4
6
|
import type { QueryUpdate, SubscriptionHint } from "#queryUpdate.ts";
|
|
5
|
-
import
|
|
7
|
+
import { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
8
|
+
import { LiveServer } from "./service.ts";
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
function subscribeQueries<E, R, TRead extends QueryRerun<unknown, unknown> | undefined = undefined>(
|
|
11
|
+
subscription: QuerySubscription<E, R>,
|
|
12
|
+
read?: TRead & (QueryRerun<E, R> | undefined),
|
|
13
|
+
): Effect.Effect<
|
|
14
|
+
Stream.Stream<TRead extends undefined ? SubscriptionHint : QueryUpdate, E | QueryReadDenied, R>,
|
|
15
|
+
never,
|
|
16
|
+
typeof LiveServer.Identifier
|
|
17
|
+
>;
|
|
18
|
+
function subscribeQueries<E, R>(subscription: QuerySubscription<E, R>, read?: QueryRerun<E, R>) {
|
|
19
|
+
return Effect.fnUntraced(function* () {
|
|
20
|
+
const server = yield* LiveServer;
|
|
21
|
+
const hints = yield* server.register(subscription.keys);
|
|
22
|
+
return hints.pipe(
|
|
23
|
+
Stream.flatMap((registration) => {
|
|
24
|
+
let scopes = subscription.scopes;
|
|
25
|
+
let compare = compareQueries(subscription, read);
|
|
26
|
+
const next = Effect.fnUntraced(function* (hint: LiveHint) {
|
|
27
|
+
if (!(yield* subscription.authorized)) {
|
|
28
|
+
compare = compareQueries(subscription, read, false);
|
|
29
|
+
return [new QueryReadDenied({ message: "Read no longer allowed for subscribed queries" })];
|
|
30
|
+
}
|
|
31
|
+
const updates = yield* compare(hint, scopes);
|
|
32
|
+
if (subscription.readScopes !== undefined) {
|
|
33
|
+
scopes = yield* subscription.readScopes;
|
|
34
|
+
registration.replaceKeys(scopes.flat());
|
|
35
|
+
}
|
|
36
|
+
return updates;
|
|
37
|
+
});
|
|
38
|
+
return registration.stream.pipe(
|
|
39
|
+
Stream.mapEffect((hint) => refreshSubscription(subscription, next(hint))),
|
|
40
|
+
Stream.flatMap(Stream.fromIterable),
|
|
41
|
+
);
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
})();
|
|
16
45
|
}
|
|
46
|
+
|
|
47
|
+
export const subscribe = Effect.fn("LiveServer.subscribe")(subscribeQueries);
|
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/makeLiveServer.ts
CHANGED
|
@@ -1,105 +1,4 @@
|
|
|
1
|
-
import { Effect
|
|
2
|
-
import
|
|
3
|
-
import { type Key, readKeys } from "@shivaedev/changes/keys.ts";
|
|
4
|
-
import { compareQueries } from "#compareQueries.ts";
|
|
5
|
-
import type { LiveHint } from "#live.ts";
|
|
6
|
-
import { liveHintQueue } from "#liveHintQueue.ts";
|
|
7
|
-
import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
|
|
8
|
-
import type { QueryUpdate, SubscriptionHint } from "#queryUpdate.ts";
|
|
9
|
-
import { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { standaloneLiveServer } from "#adapters/standaloneLiveServer.ts";
|
|
10
3
|
|
|
11
|
-
export const makeLiveServer = Effect.
|
|
12
|
-
const reactivity = yield* Reactivity.Reactivity;
|
|
13
|
-
const dispatcher = (yield* Scheduler.Scheduler).makeDispatcher();
|
|
14
|
-
let openStreams = 0;
|
|
15
|
-
const resyncKey = `@shivaedev/contract/resync/${yield* Random.nextInt}`;
|
|
16
|
-
function open(keys: readonly Key[]) {
|
|
17
|
-
return Effect.gen(function* () {
|
|
18
|
-
const { offer, queue } = yield* liveHintQueue;
|
|
19
|
-
const pending = new Map<string, Key>();
|
|
20
|
-
let scheduled = false;
|
|
21
|
-
let resync = false;
|
|
22
|
-
let active = true;
|
|
23
|
-
function flush() {
|
|
24
|
-
if (scheduled) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
scheduled = true;
|
|
28
|
-
dispatcher.scheduleTask(() => {
|
|
29
|
-
scheduled = false;
|
|
30
|
-
if (active) {
|
|
31
|
-
offer(resync ? { _tag: "Resync" } : { _tag: "Changed", keys: [...pending.values()] });
|
|
32
|
-
}
|
|
33
|
-
pending.clear();
|
|
34
|
-
resync = false;
|
|
35
|
-
}, 0);
|
|
36
|
-
}
|
|
37
|
-
let cancelKeys: (() => void)[] = [];
|
|
38
|
-
function replaceKeys(current: readonly Key[]) {
|
|
39
|
-
for (const stop of cancelKeys) {
|
|
40
|
-
stop();
|
|
41
|
-
}
|
|
42
|
-
cancelKeys = current.map((key) => {
|
|
43
|
-
const own = readKeys([key]);
|
|
44
|
-
return reactivity.registerUnsafe(own, () => {
|
|
45
|
-
pending.set(own[0] ?? "", key);
|
|
46
|
-
flush();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
replaceKeys(keys);
|
|
51
|
-
const cancel = [
|
|
52
|
-
reactivity.registerUnsafe([resyncKey], () => {
|
|
53
|
-
resync = true;
|
|
54
|
-
flush();
|
|
55
|
-
}),
|
|
56
|
-
];
|
|
57
|
-
openStreams += 1;
|
|
58
|
-
yield* Effect.addFinalizer(() =>
|
|
59
|
-
Effect.sync(() => {
|
|
60
|
-
active = false;
|
|
61
|
-
openStreams -= 1;
|
|
62
|
-
for (const stop of [...cancelKeys, ...cancel]) {
|
|
63
|
-
stop();
|
|
64
|
-
}
|
|
65
|
-
}),
|
|
66
|
-
);
|
|
67
|
-
yield* Queue.offer(queue, { _tag: "Resync" });
|
|
68
|
-
return { replaceKeys, stream: Stream.fromQueue(queue) };
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
const resync = Effect.sync(() => {
|
|
72
|
-
reactivity.invalidateUnsafe([resyncKey]);
|
|
73
|
-
return openStreams;
|
|
74
|
-
});
|
|
75
|
-
function subscribe<E, R>(subscription: QuerySubscription<E, R>): Stream.Stream<SubscriptionHint, E | QueryReadDenied, R>;
|
|
76
|
-
function subscribe<E, R>(subscription: QuerySubscription<E, R>, read: QueryRerun<E, R>): Stream.Stream<QueryUpdate, E | QueryReadDenied, R>;
|
|
77
|
-
function subscribe<E, R>(subscription: QuerySubscription<E, R>, read?: QueryRerun<E, R>) {
|
|
78
|
-
return Stream.unwrap(
|
|
79
|
-
Effect.gen(function* () {
|
|
80
|
-
const registration = yield* open(subscription.keys);
|
|
81
|
-
let scopes = subscription.scopes;
|
|
82
|
-
let compare = compareQueries(subscription, read);
|
|
83
|
-
const next = Effect.fnUntraced(function* (hint: LiveHint) {
|
|
84
|
-
if (!(yield* subscription.authorized)) {
|
|
85
|
-
compare = compareQueries(subscription, read);
|
|
86
|
-
return [new QueryReadDenied({ message: "Read no longer allowed for subscribed queries" })];
|
|
87
|
-
}
|
|
88
|
-
const updates = yield* compare(hint, scopes);
|
|
89
|
-
if (subscription.readScopes !== undefined) {
|
|
90
|
-
scopes = yield* subscription.readScopes;
|
|
91
|
-
registration.replaceKeys(scopes.flat());
|
|
92
|
-
}
|
|
93
|
-
return updates;
|
|
94
|
-
});
|
|
95
|
-
return registration.stream.pipe(
|
|
96
|
-
Stream.mapEffect((hint) =>
|
|
97
|
-
Effect.flatMap(subscription.refresh ?? Effect.context<never>(), (current) => Effect.provideContext(next(hint), current)),
|
|
98
|
-
),
|
|
99
|
-
Stream.flatMap(Stream.fromIterable),
|
|
100
|
-
);
|
|
101
|
-
}),
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
return { resync, stream: (keys: readonly Key[]) => Stream.unwrap(Effect.map(open(keys), ({ stream }) => stream)), subscribe };
|
|
105
|
-
});
|
|
4
|
+
export const makeLiveServer = Effect.fn("LiveServer.makeLiveServer")(() => standaloneLiveServer)();
|
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
package/src/querySubscription.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Context, Effect, type Option } from "effect";
|
|
2
2
|
import { Rpc } from "effect/rpc";
|
|
3
3
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
|
-
import {
|
|
4
|
+
import { withPolicyContext } from "#adapters/withPolicyContext.ts";
|
|
5
5
|
import { queryReadKeys } from "#queryReadKeys.ts";
|
|
6
6
|
import { checkQuery, type PolicyChannels, type Viewer } from "#readPolicy.ts";
|
|
7
7
|
import type { MissingReadPolicy, QueryReadDenied } from "#readPolicyErrors.ts";
|
|
@@ -34,17 +34,14 @@ export function querySubscription(rpc: Rpc.Any, references: readonly QueryRefere
|
|
|
34
34
|
const policies = yield* Effect.forEach(
|
|
35
35
|
queries,
|
|
36
36
|
Effect.fnUntraced(function* ({ query, payload }) {
|
|
37
|
-
const
|
|
37
|
+
const policyRpc = Rpc.make(`${prefix}${query.name}`);
|
|
38
38
|
return {
|
|
39
|
-
check:
|
|
40
|
-
keys:
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const current = yield* policyContext(Rpc.make(`${prefix}${query.name}`));
|
|
46
|
-
return yield* Effect.suspend(() => query.readPolicy?.(payload, viewer) ?? Effect.succeed(false)).pipe(Effect.provideContext(current));
|
|
47
|
-
}),
|
|
39
|
+
check: withPolicyContext(policyRpc, checkQuery(query, payload, viewer)),
|
|
40
|
+
keys: withPolicyContext(policyRpc, queryReadKeys(query, payload, viewer)),
|
|
41
|
+
permitted: withPolicyContext(
|
|
42
|
+
policyRpc,
|
|
43
|
+
Effect.suspend(() => query.readPolicy?.(payload, viewer) ?? Effect.succeed(false)),
|
|
44
|
+
),
|
|
48
45
|
};
|
|
49
46
|
}),
|
|
50
47
|
);
|
package/src/readPolicy.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Context, Effect, Option, type Stream, type Types } from "effect";
|
|
2
2
|
import type { Rpc } from "effect/rpc";
|
|
3
|
+
import { withPolicyContext } from "#adapters/withPolicyContext.ts";
|
|
3
4
|
import type { QueryShape } from "#operation.ts";
|
|
4
5
|
import { isPermissionReadPolicy } from "#permissionReadPolicy.ts";
|
|
5
|
-
import { policyContext } from "#policyContext.ts";
|
|
6
6
|
import { MissingReadPolicy, QueryReadDenied } from "#readPolicyErrors.ts";
|
|
7
7
|
import { QueryDefinition } from "#serverMetadata.ts";
|
|
8
8
|
|
|
@@ -49,8 +49,7 @@ export function authorizeReads(
|
|
|
49
49
|
return Effect.fnUntraced(function* () {
|
|
50
50
|
const definition = Context.getOption(request.rpc.annotations, QueryDefinition);
|
|
51
51
|
if (Option.isSome(definition)) {
|
|
52
|
-
|
|
53
|
-
yield* checkQuery(definition.value.definition, request.payload, viewer).pipe(Effect.provideContext(context));
|
|
52
|
+
yield* withPolicyContext(request.rpc, checkQuery(definition.value.definition, request.payload, viewer));
|
|
54
53
|
}
|
|
55
54
|
return yield* Effect.provideService(effect, CurrentViewer, viewer);
|
|
56
55
|
})();
|
package/src/rpcClient.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
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";
|
|
5
5
|
import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
6
|
+
import { withClientProtocol } from "#adapters/withClientProtocol.ts";
|
|
6
7
|
import { Activity } from "#clientActivity.ts";
|
|
7
8
|
import type { ClientOptions } from "#clientOptions.ts";
|
|
8
9
|
import type { ResumeWindow } from "#resume.ts";
|
|
10
|
+
import { CommandInvalidation } from "#serverMetadata.ts";
|
|
9
11
|
import { clientActivities, makeActivity } from "./clientActivities.ts";
|
|
10
12
|
import { connectionProtocol, protocolConnections } from "./connectionProtocol.ts";
|
|
11
13
|
import { commandConnections } from "./connectionReadiness.ts";
|
|
@@ -21,6 +23,9 @@ export interface RpcSource {
|
|
|
21
23
|
export function rpcClient(name: string, source: RpcSource, options: ClientOptions) {
|
|
22
24
|
const activity = makeActivity(options.runtime ?? Atom.runtime);
|
|
23
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
|
+
);
|
|
24
29
|
function sharedProtocol(identity?: string) {
|
|
25
30
|
return socketProtocol({
|
|
26
31
|
bearerToken: options.bearerToken,
|
|
@@ -55,7 +60,7 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
55
60
|
Stream.flatMap((hint) => Stream.concat(Stream.succeed(hint), Stream.fromEffect(Effect.sync(activity.hint)).pipe(Stream.drain))),
|
|
56
61
|
);
|
|
57
62
|
}
|
|
58
|
-
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;
|
|
59
64
|
},
|
|
60
65
|
});
|
|
61
66
|
commandConnections.set(observed, wait);
|
|
@@ -75,12 +80,9 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
75
80
|
runtime: options.runtime,
|
|
76
81
|
});
|
|
77
82
|
clientActivities.set(Client, activity);
|
|
78
|
-
const directClient =
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
Object.fromEntries([...group.requests.keys()].map((tag) => [tag, (payload: never, callOptions: never) => flat(tag, payload, callOptions)])),
|
|
82
|
-
),
|
|
83
|
-
Effect.provideContext(context),
|
|
83
|
+
const directClient = withClientProtocol(protocol, make).pipe(
|
|
84
|
+
Effect.map((flat) =>
|
|
85
|
+
Object.fromEntries([...group.requests.keys()].map((tag) => [tag, (payload: never, callOptions: never) => flat(tag, payload, callOptions)])),
|
|
84
86
|
),
|
|
85
87
|
);
|
|
86
88
|
const layer = Layer.effect(Client, make).pipe(Layer.provide(protocol));
|
package/dist/LiveServer.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
2
|
-
export declare const LiveServer: import("effect/Context").Service<"@shivaedev/contract/LiveServer", Readonly<{
|
|
3
|
-
readonly resync: () => import("effect/Effect").Effect<number, never, never>;
|
|
4
|
-
readonly stream: (keys: readonly import("@shivaedev/changes/keys.ts").Key[]) => import("effect/Effect").Effect<import("effect/Stream").Stream<{
|
|
5
|
-
readonly _tag: "Changed";
|
|
6
|
-
readonly keys: readonly ({
|
|
7
|
-
readonly _tag: "Bucket";
|
|
8
|
-
readonly collection: string;
|
|
9
|
-
readonly field: string;
|
|
10
|
-
readonly value: string | number;
|
|
11
|
-
} | {
|
|
12
|
-
readonly _tag: "List";
|
|
13
|
-
readonly collection: string;
|
|
14
|
-
} | {
|
|
15
|
-
readonly _tag: "Item";
|
|
16
|
-
readonly collection: string;
|
|
17
|
-
readonly id: string | number;
|
|
18
|
-
})[];
|
|
19
|
-
readonly queryIndexes?: readonly number[] | undefined;
|
|
20
|
-
} | {
|
|
21
|
-
readonly _tag: "Resync";
|
|
22
|
-
}, never, never>, never, never>;
|
|
23
|
-
readonly subscribe: <E, R, TRead extends import("./querySubscription.ts").QueryRerun<unknown, unknown> | undefined = undefined>(subscription: import("./querySubscription.ts").QuerySubscription<E, R>, read?: (TRead & (import("./querySubscription.ts").QueryRerun<E, R> | undefined)) | undefined) => import("effect/Effect").Effect<import("effect/Stream").Stream<TRead extends undefined ? import("./queryUpdate.ts").SubscriptionHint : import("./queryUpdate.ts").QueryUpdate, E | import("./readPolicyErrors.ts").QueryReadDenied, R>>;
|
|
24
|
-
}>> & {
|
|
25
|
-
readonly layer: import("effect/Layer").Layer<"@shivaedev/contract/LiveServer", never, Reactivity.Reactivity>;
|
|
26
|
-
};
|
|
27
|
-
//# sourceMappingURL=LiveServer.d.ts.map
|
package/dist/LiveServer.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"LiveServer.d.ts","sourceRoot":"","sources":["../src/LiveServer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,8BAA8B,CAAC;AAQ3D,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;;;;;;;;;;CASrB,CAAC"}
|