@shivaedev/contract 0.33.0 → 0.35.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 +29 -0
- package/README.md +27 -4
- package/dist/HostVersion.d.ts +31 -0
- package/dist/HostVersion.d.ts.map +1 -0
- package/dist/HostVersion.js +7 -0
- package/dist/HostVersion.js.map +1 -0
- package/dist/adapters/activityState.d.ts +3 -2
- package/dist/adapters/activityState.d.ts.map +1 -1
- package/dist/adapters/activityState.js +4 -1
- package/dist/adapters/activityState.js.map +1 -1
- package/dist/adapters/clientConnection.d.ts +8 -0
- package/dist/adapters/clientConnection.d.ts.map +1 -0
- package/dist/adapters/clientConnection.js +22 -0
- package/dist/adapters/clientConnection.js.map +1 -0
- package/dist/automaticLive.d.ts.map +1 -1
- package/dist/automaticLive.js +3 -1
- package/dist/automaticLive.js.map +1 -1
- package/dist/bind.d.ts +2 -1
- package/dist/bind.d.ts.map +1 -1
- package/dist/bind.js.map +1 -1
- package/dist/clientActivity.d.ts +6 -0
- package/dist/clientActivity.d.ts.map +1 -1
- package/dist/clientActivity.js +1 -1
- package/dist/clientActivity.js.map +1 -1
- package/dist/clientOptions.d.ts +1 -0
- package/dist/clientOptions.d.ts.map +1 -1
- package/dist/composeBindings.d.ts.map +1 -1
- package/dist/composeBindings.js +21 -6
- package/dist/composeBindings.js.map +1 -1
- package/dist/contract.d.ts +7 -10
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +6 -2
- package/dist/contract.js.map +1 -1
- package/dist/internal/automaticQuery.d.ts +2 -2
- package/dist/internal/automaticQuery.d.ts.map +1 -1
- package/dist/internal/automaticQuery.js +12 -10
- package/dist/internal/automaticQuery.js.map +1 -1
- package/dist/internal/clientActivity.d.ts +7 -1
- package/dist/internal/clientActivity.d.ts.map +1 -1
- package/dist/internal/clientActivity.js +13 -3
- package/dist/internal/clientActivity.js.map +1 -1
- package/dist/internal/clientOperations.d.ts.map +1 -1
- package/dist/internal/clientOperations.js +4 -3
- package/dist/internal/clientOperations.js.map +1 -1
- package/dist/internal/commandPayload.d.ts +4 -0
- package/dist/internal/commandPayload.d.ts.map +1 -0
- package/dist/internal/commandPayload.js +9 -0
- package/dist/internal/commandPayload.js.map +1 -0
- package/dist/internal/liveRetry.d.ts +2 -2
- package/dist/internal/liveRetry.d.ts.map +1 -1
- package/dist/internal/liveRetry.js +6 -3
- package/dist/internal/liveRetry.js.map +1 -1
- package/dist/internal/queryReactivity.d.ts.map +1 -1
- package/dist/internal/queryReactivity.js +2 -0
- package/dist/internal/queryReactivity.js.map +1 -1
- package/dist/internal/rpcClient.d.ts +2 -0
- package/dist/internal/rpcClient.d.ts.map +1 -1
- package/dist/internal/rpcClient.js +2 -1
- package/dist/internal/rpcClient.js.map +1 -1
- package/dist/internal/subjectMiddleware.d.ts +16 -0
- package/dist/internal/subjectMiddleware.d.ts.map +1 -0
- package/dist/internal/subjectMiddleware.js +8 -0
- package/dist/internal/subjectMiddleware.js.map +1 -0
- package/dist/live.d.ts +1 -0
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +14 -3
- package/dist/live.js.map +1 -1
- package/dist/operationRpc.d.ts +11 -0
- package/dist/operationRpc.d.ts.map +1 -0
- package/dist/operationRpc.js +2 -0
- package/dist/operationRpc.js.map +1 -0
- package/dist/subject.d.ts +10 -0
- package/dist/subject.d.ts.map +1 -0
- package/dist/subject.js +2 -0
- package/dist/subject.js.map +1 -0
- package/dist/subjectLayers.d.ts +12 -0
- package/dist/subjectLayers.d.ts.map +1 -0
- package/dist/subjectLayers.js +22 -0
- package/dist/subjectLayers.js.map +1 -0
- package/package.json +10 -10
- package/src/HostVersion.ts +7 -0
- package/src/adapters/activityState.ts +10 -3
- package/src/adapters/clientConnection.ts +37 -0
- package/src/automaticLive.ts +3 -1
- package/src/bind.ts +2 -1
- package/src/clientActivity.ts +9 -2
- package/src/clientOptions.ts +1 -0
- package/src/composeBindings.ts +21 -8
- package/src/contract.ts +18 -25
- package/src/internal/automaticQuery.ts +13 -11
- package/src/internal/clientActivity.ts +13 -3
- package/src/internal/clientOperations.ts +13 -3
- package/src/internal/commandPayload.ts +16 -0
- package/src/internal/liveRetry.ts +9 -2
- package/src/internal/queryReactivity.ts +2 -0
- package/src/internal/rpcClient.ts +2 -1
- package/src/internal/subjectMiddleware.ts +22 -0
- package/src/live.ts +15 -2
- package/src/operationRpc.ts +25 -0
- package/src/subject.ts +15 -0
- package/src/subjectLayers.ts +33 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operationRpc.js","sourceRoot":"","sources":["../src/operationRpc.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Effect, Schema } from "effect";
|
|
2
|
+
import type { Rpc, RpcMiddleware } from "effect/rpc";
|
|
3
|
+
import type { subjectMiddleware } from "#internal/subjectMiddleware.ts";
|
|
4
|
+
export interface SubjectDeclaration {
|
|
5
|
+
readonly error: Schema.Top;
|
|
6
|
+
readonly middleware: Effect.Effect<RpcMiddleware.Any, never, unknown>;
|
|
7
|
+
}
|
|
8
|
+
export type SubjectProvides<TSubject extends SubjectDeclaration> = Effect.Success<TSubject["middleware"]> extends RpcMiddleware.RpcMiddleware<infer TProvides, infer _E, infer _R> ? TProvides : never;
|
|
9
|
+
export type WithSubject<TRpcs extends Rpc.Any, TSubject extends SubjectDeclaration | undefined> = TSubject extends SubjectDeclaration ? Rpc.AddMiddleware<TRpcs, ReturnType<typeof subjectMiddleware<TSubject>>> : TRpcs;
|
|
10
|
+
//# sourceMappingURL=subject.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subject.d.ts","sourceRoot":"","sources":["../src/subject.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,KAAK,EAAE,GAAG,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAExE,MAAM,WAAW,kBAAkB;IAClC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;CACtE;AAED,MAAM,MAAM,eAAe,CAAC,QAAQ,SAAS,kBAAkB,IAC9D,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,SAAS,aAAa,CAAC,aAAa,CAAC,MAAM,SAAS,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,GAAG,SAAS,GAAG,KAAK,CAAC;AAErI,MAAM,MAAM,WAAW,CAAC,KAAK,SAAS,GAAG,CAAC,GAAG,EAAE,QAAQ,SAAS,kBAAkB,GAAG,SAAS,IAAI,QAAQ,SAAS,kBAAkB,GAClI,GAAG,CAAC,aAAa,CAAC,KAAK,EAAE,UAAU,CAAC,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAC,CAAC,GACxE,KAAK,CAAC"}
|
package/dist/subject.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subject.js","sourceRoot":"","sources":["../src/subject.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Context, type Effect, Layer } from "effect";
|
|
2
|
+
import { Rpc, type RpcGroup } from "effect/rpc";
|
|
3
|
+
import { SUBJECT_TYPE_ID } from "#internal/subjectMiddleware.ts";
|
|
4
|
+
import type { SubjectDeclaration } from "#subject.ts";
|
|
5
|
+
type Middleware<R extends Rpc.Any> = R extends {
|
|
6
|
+
readonly middlewares: ReadonlySet<infer TMiddleware>;
|
|
7
|
+
} ? Extract<TMiddleware, {
|
|
8
|
+
readonly [SUBJECT_TYPE_ID]: SubjectDeclaration;
|
|
9
|
+
}> : never;
|
|
10
|
+
export declare function subjectLayers<R extends Rpc.Any>(group: RpcGroup.RpcGroup<R>): Layer.Layer<Context.Service.Identifier<Middleware<R>>, never, Effect.Services<Middleware<R>[typeof SUBJECT_TYPE_ID]["middleware"]>>;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=subjectLayers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subjectLayers.d.ts","sourceRoot":"","sources":["../src/subjectLayers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC1D,OAAO,EAAE,GAAG,EAAE,KAAK,QAAQ,EAAsB,MAAM,YAAY,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtD,KAAK,UAAU,CAAC,CAAC,SAAS,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS;IAAE,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC,MAAM,WAAW,CAAC,CAAA;CAAE,GACpG,OAAO,CAAC,WAAW,EAAE;IAAE,QAAQ,CAAC,CAAC,eAAe,CAAC,EAAE,kBAAkB,CAAA;CAAE,CAAC,GACxE,KAAK,CAAC;AAQT,wBAAgB,aAAa,CAAC,CAAC,SAAS,GAAG,CAAC,GAAG,EAC9C,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,GACzB,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,eAAe,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Layer } from "effect";
|
|
2
|
+
import { Rpc } from "effect/rpc";
|
|
3
|
+
import { SUBJECT_TYPE_ID } from "#internal/subjectMiddleware.ts";
|
|
4
|
+
function isSubject(middleware) {
|
|
5
|
+
return SUBJECT_TYPE_ID in middleware;
|
|
6
|
+
}
|
|
7
|
+
export function subjectLayers(group) {
|
|
8
|
+
const layers = [];
|
|
9
|
+
for (const rpc of group.requests.values()) {
|
|
10
|
+
if (!Rpc.isRpc(rpc)) {
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
const middlewares = rpc.middlewares;
|
|
14
|
+
for (const middleware of middlewares) {
|
|
15
|
+
if (isSubject(middleware)) {
|
|
16
|
+
layers.push(Layer.effect(middleware, middleware[SUBJECT_TYPE_ID].middleware));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return Layer.mergeAll(Layer.empty, ...layers);
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=subjectLayers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subjectLayers.js","sourceRoot":"","sources":["../src/subjectLayers.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC1D,OAAO,EAAE,GAAG,EAAqC,MAAM,YAAY,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAOjE,SAAS,SAAS,CAAC,UAA6C;IAG/D,OAAO,eAAe,IAAI,UAAU,CAAC;AACtC,CAAC;AAKD,MAAM,UAAU,aAAa,CAAC,KAAiC;IAC9D,MAAM,MAAM,GAAyC,EAAE,CAAC;IACxD,KAAK,MAAM,GAAG,IAAI,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;QAC3C,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;YACrB,SAAS;QACV,CAAC;QACD,MAAM,WAAW,GAAmD,GAAG,CAAC,WAAW,CAAC;QACpF,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE,CAAC;YACtC,IAAI,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC3B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,EAAE,UAAU,CAAC,eAAe,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;YAC/E,CAAC;QACF,CAAC;IACF,CAAC;IACD,OAAO,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,MAAM,CAAC,CAAC;AAC/C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/contract",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.35.0",
|
|
4
4
|
"description": "Typed query and command contracts over native Effect RPC and reactivity",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -44,21 +44,21 @@
|
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@shivaedev/changes": "0.9.0",
|
|
47
|
-
"@shivaedev/service": "0.6.
|
|
47
|
+
"@shivaedev/service": "0.6.2",
|
|
48
48
|
"@shivaedev/types": "0.3.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@effect/platform-node": "4.0.6",
|
|
52
52
|
"@effect/vitest": "4.0.6",
|
|
53
|
-
"@shivaedev/errors": "0.
|
|
54
|
-
"@shivaedev/identity": "0.
|
|
55
|
-
"@shivaedev/quality": "0.
|
|
56
|
-
"@shivaedev/server": "0.
|
|
57
|
-
"@shivaedev/sql": "0.
|
|
53
|
+
"@shivaedev/errors": "0.8.0",
|
|
54
|
+
"@shivaedev/identity": "0.23.1",
|
|
55
|
+
"@shivaedev/quality": "0.59.0",
|
|
56
|
+
"@shivaedev/server": "0.23.0",
|
|
57
|
+
"@shivaedev/sql": "0.36.1",
|
|
58
58
|
"@shivaedev/test": "0.14.0",
|
|
59
|
-
"@shivaedev/test-kits": "0.
|
|
60
|
-
"@shivaedev/test-sql": "0.
|
|
61
|
-
"@shivaedev/test-story": "0.
|
|
59
|
+
"@shivaedev/test-kits": "0.2.2",
|
|
60
|
+
"@shivaedev/test-sql": "0.19.0",
|
|
61
|
+
"@shivaedev/test-story": "0.37.1",
|
|
62
62
|
"@types/node": "24.19.2",
|
|
63
63
|
"effect": "4.0.6",
|
|
64
64
|
"esbuild": "0.28.2",
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { Effect, SubscriptionRef } from "effect";
|
|
2
|
-
import type { ClientActivityState } from "#clientActivity.ts";
|
|
2
|
+
import type { ClientActivityState, ConnectionStatus } from "#clientActivity.ts";
|
|
3
3
|
|
|
4
4
|
export function activityState() {
|
|
5
|
-
const state = Effect.runSync(
|
|
5
|
+
const state = Effect.runSync(
|
|
6
|
+
SubscriptionRef.make<ClientActivityState>({ connectionStatus: "live", liveHints: 0, liveInvalidations: 0, refreshingAtoms: 0, rpcCalls: 0 }),
|
|
7
|
+
);
|
|
6
8
|
return {
|
|
7
9
|
activity: Object.assign(SubscriptionRef.changes(state), { current: SubscriptionRef.get(state) }),
|
|
8
|
-
change: (key: keyof ClientActivityState, delta: number) => {
|
|
10
|
+
change: (key: Exclude<keyof ClientActivityState, "connectionStatus">, delta: number) => {
|
|
9
11
|
Effect.runSync(SubscriptionRef.update(state, (current) => ({ ...current, [key]: current[key] + delta })));
|
|
10
12
|
},
|
|
13
|
+
setConnection: (connectionStatus: ConnectionStatus) => {
|
|
14
|
+
Effect.runSync(
|
|
15
|
+
SubscriptionRef.update(state, (current) => (current.connectionStatus === connectionStatus ? current : { ...current, connectionStatus })),
|
|
16
|
+
);
|
|
17
|
+
},
|
|
11
18
|
};
|
|
12
19
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Effect, Layer, Option, Stream } from "effect";
|
|
2
|
+
import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
import type { ConnectionStatus } from "#clientActivity.ts";
|
|
5
|
+
import type { LiveStatus } from "#live.ts";
|
|
6
|
+
|
|
7
|
+
export function clientConnection(factory: Atom.RuntimeFactory, publish: (status: ConnectionStatus) => void) {
|
|
8
|
+
const statuses = Atom.make<ReadonlyMap<object, LiveStatus<unknown>>>(new Map());
|
|
9
|
+
const current = Atom.map(
|
|
10
|
+
statuses,
|
|
11
|
+
(values): ConnectionStatus =>
|
|
12
|
+
[...values.values()].some(({ connection }) => connection === "connecting" || connection === "reconnecting") ? "reconnecting" : "live",
|
|
13
|
+
);
|
|
14
|
+
const runtime = factory(Layer.empty);
|
|
15
|
+
const status = Atom.make((get): ConnectionStatus => {
|
|
16
|
+
const context = AsyncResult.value(get(runtime));
|
|
17
|
+
if (Option.isNone(context)) {
|
|
18
|
+
return "live";
|
|
19
|
+
}
|
|
20
|
+
const cancel = Effect.runCallbackWith(context.value)(
|
|
21
|
+
get.stream(current).pipe(
|
|
22
|
+
Stream.changes,
|
|
23
|
+
Stream.debounce("300 millis"),
|
|
24
|
+
Stream.runForEach((value) =>
|
|
25
|
+
Effect.sync(() => {
|
|
26
|
+
publish(value);
|
|
27
|
+
get.setSelf(value);
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
Effect.ensuring(Effect.sync(() => publish("live"))),
|
|
31
|
+
),
|
|
32
|
+
);
|
|
33
|
+
get.addFinalizer(cancel);
|
|
34
|
+
return "live";
|
|
35
|
+
});
|
|
36
|
+
return { status, statuses };
|
|
37
|
+
}
|
package/src/automaticLive.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { Key } from "@shivaedev/changes/keys.ts";
|
|
|
8
8
|
import type { BindingOwner } from "#adapters/bindingOwner.ts";
|
|
9
9
|
import { aggregateLiveStatus } from "#internal/aggregateLiveStatus.ts";
|
|
10
10
|
import { automaticQuery } from "#internal/automaticQuery.ts";
|
|
11
|
+
import { clientActivities } from "#internal/clientActivity.ts";
|
|
11
12
|
import { queryDelivery } from "#internal/queryDelivery.ts";
|
|
12
13
|
import { localQueryKeys, queryHint } from "#internal/queryHints.ts";
|
|
13
14
|
import { queryProgress } from "#internal/querySignals.ts";
|
|
@@ -86,6 +87,7 @@ export function automaticLive(
|
|
|
86
87
|
const subscription = live(service.runtime, {
|
|
87
88
|
initialResync: false,
|
|
88
89
|
keyContext: owner?.context,
|
|
90
|
+
reopenOnResume: decode !== undefined,
|
|
89
91
|
resume: signal,
|
|
90
92
|
resyncKeys: decode === undefined ? localQueryKeys(keys) : [],
|
|
91
93
|
retryKeys: keys,
|
|
@@ -97,12 +99,12 @@ export function automaticLive(
|
|
|
97
99
|
const gated = automaticQuery({
|
|
98
100
|
assertOwner,
|
|
99
101
|
atom,
|
|
102
|
+
clientStatuses: clientActivities.get(service)?.statuses,
|
|
100
103
|
coalesceWindow,
|
|
101
104
|
equivalent,
|
|
102
105
|
paused,
|
|
103
106
|
pushed: decode !== undefined,
|
|
104
107
|
ready,
|
|
105
|
-
signal,
|
|
106
108
|
signals,
|
|
107
109
|
source,
|
|
108
110
|
statuses,
|
package/src/bind.ts
CHANGED
|
@@ -10,10 +10,11 @@ import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
|
10
10
|
import { bindingOwner } from "#adapters/bindingOwner.ts";
|
|
11
11
|
import { automaticLive, type LiveBound, LiveConnection } from "#automaticLive.ts";
|
|
12
12
|
import type { ClientOptions, MadeClient } from "#clientOptions.ts";
|
|
13
|
-
import type { Contract, Declared
|
|
13
|
+
import type { Contract, Declared } from "#contract.ts";
|
|
14
14
|
import { clientOperations } from "#internal/clientOperations.ts";
|
|
15
15
|
import { type RpcSource, rpcClient } from "#internal/rpcClient.ts";
|
|
16
16
|
import type { CommandShape, QueryShape, StreamShape } from "#operation.ts";
|
|
17
|
+
import type { Tag } from "#operationRpc.ts";
|
|
17
18
|
import type { ResumeSource, ResumeWindow } from "#resume.ts";
|
|
18
19
|
|
|
19
20
|
export type Failure<R extends Rpc.Any> =
|
package/src/clientActivity.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { Effect, Stream } from "effect";
|
|
2
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
|
|
4
|
+
export type ConnectionStatus = "live" | "reconnecting";
|
|
2
5
|
|
|
3
6
|
export interface ClientActivityState {
|
|
7
|
+
readonly connectionStatus: ConnectionStatus;
|
|
4
8
|
readonly liveHints: number;
|
|
5
9
|
readonly liveInvalidations: number;
|
|
6
10
|
readonly refreshingAtoms: number;
|
|
7
11
|
readonly rpcCalls: number;
|
|
8
12
|
}
|
|
9
13
|
|
|
10
|
-
export type ClientActivity = Stream.Stream<ClientActivityState> & {
|
|
14
|
+
export type ClientActivity = Stream.Stream<ClientActivityState> & {
|
|
15
|
+
readonly connectionStatus: Atom.Atom<ConnectionStatus>;
|
|
16
|
+
readonly current: Effect.Effect<ClientActivityState>;
|
|
17
|
+
};
|
|
11
18
|
|
|
12
19
|
export const Activity = Symbol.for("@shivaedev/contract/ClientActivity");
|
|
13
20
|
|
|
@@ -25,7 +32,7 @@ export function isClientIdle(state: ClientActivityState): boolean {
|
|
|
25
32
|
|
|
26
33
|
export function activityScreenSignal(activity: ClientActivity) {
|
|
27
34
|
function screenState(state: ClientActivityState) {
|
|
28
|
-
return { hints: state.liveHints, pending: !isClientIdle(state) };
|
|
35
|
+
return { connectionStatus: state.connectionStatus, hints: state.liveHints, pending: !isClientIdle(state) };
|
|
29
36
|
}
|
|
30
37
|
return Object.assign(Stream.map(activity, screenState), { current: Effect.map(activity.current, screenState) });
|
|
31
38
|
}
|
package/src/clientOptions.ts
CHANGED
|
@@ -17,6 +17,7 @@ export interface ClientOptions {
|
|
|
17
17
|
readonly protocol?: Layer.Layer<RpcClient.Protocol> | ((identity: string | undefined) => Layer.Layer<RpcClient.Protocol>);
|
|
18
18
|
readonly resume?: ResumeSource;
|
|
19
19
|
readonly resumeWindow?: ResumeWindow;
|
|
20
|
+
readonly runtime?: Atom.RuntimeFactory;
|
|
20
21
|
readonly url: string;
|
|
21
22
|
readonly webSocketConstructor?: Layer.Layer<Socket.WebSocketConstructor>;
|
|
22
23
|
}
|
package/src/composeBindings.ts
CHANGED
|
@@ -10,18 +10,31 @@ export function composeBindings<TClient extends object>(client: TClient, binding
|
|
|
10
10
|
return { ...client, [ComposedBindings]: bindings };
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
function isLiveBound(client: object): client is LiveBound {
|
|
14
|
+
return LiveConnection in client;
|
|
15
|
+
}
|
|
16
|
+
|
|
13
17
|
function isComposition(client: object): client is BindingComposition {
|
|
14
18
|
return ComposedBindings in client;
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
export function liveBindings(client: unknown): readonly LiveBound[] {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
const pending = [client];
|
|
23
|
+
const visited = new Set<object>();
|
|
24
|
+
const bindings: LiveBound[] = [];
|
|
25
|
+
for (const value of pending) {
|
|
26
|
+
if (typeof value !== "object" || value === null || visited.has(value)) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
visited.add(value);
|
|
30
|
+
if (isComposition(value)) {
|
|
31
|
+
pending.push(...value[ComposedBindings]);
|
|
32
|
+
} else {
|
|
33
|
+
if (isLiveBound(value)) {
|
|
34
|
+
bindings.push(value);
|
|
35
|
+
}
|
|
36
|
+
pending.push(...Object.values(value));
|
|
37
|
+
}
|
|
23
38
|
}
|
|
24
|
-
return
|
|
25
|
-
(value): value is LiveBound => typeof value === "object" && value !== null && LiveConnection in value,
|
|
26
|
-
);
|
|
39
|
+
return bindings;
|
|
27
40
|
}
|
package/src/contract.ts
CHANGED
|
@@ -1,32 +1,16 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
-
import { Rpc, RpcGroup, type RpcMiddleware
|
|
2
|
+
import { Rpc, RpcGroup, type RpcMiddleware } from "effect/rpc";
|
|
3
3
|
import { declarationError, type Unique } from "#internal/declarationError.ts";
|
|
4
|
-
import
|
|
4
|
+
import { subjectMiddleware } from "#internal/subjectMiddleware.ts";
|
|
5
|
+
import type { CommandShape, QueryShape, StreamShape } from "#operation.ts";
|
|
6
|
+
import type { OperationRpc } from "#operationRpc.ts";
|
|
5
7
|
import { ReadPolicyGuard } from "#ReadPolicyGuard.ts";
|
|
6
8
|
import { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
7
9
|
import { changesRpc, KeyScope, operationAnnotations } from "#serverMetadata.ts";
|
|
8
|
-
|
|
9
|
-
export type Tag<TContract extends string, TName extends string> = `${TContract}.${TName}`;
|
|
10
|
+
import type { SubjectDeclaration, WithSubject } from "#subject.ts";
|
|
10
11
|
|
|
11
12
|
export type QueryServices<TQuery> = TQuery extends QueryShape<unknown, infer R> ? R : never;
|
|
12
13
|
|
|
13
|
-
type OperationError<TOperation extends OperationShape> = TOperation extends QueryShape
|
|
14
|
-
? Schema.Union<readonly [TOperation["error"], typeof QueryReadDenied]>
|
|
15
|
-
: TOperation["error"];
|
|
16
|
-
|
|
17
|
-
export type OperationRpc<TContract extends string, TOperation> = TOperation extends OperationShape
|
|
18
|
-
? TOperation extends StreamShape
|
|
19
|
-
? Rpc.Rpc<Tag<TContract, TOperation["name"]>, TOperation["payload"], RpcSchema.Stream<TOperation["success"], TOperation["error"]>, Schema.Never>
|
|
20
|
-
: Rpc.Rpc<
|
|
21
|
-
Tag<TContract, TOperation["name"]>,
|
|
22
|
-
TOperation["payload"],
|
|
23
|
-
TOperation["success"],
|
|
24
|
-
OperationError<TOperation>,
|
|
25
|
-
TOperation extends QueryShape ? typeof ReadPolicyGuard : never,
|
|
26
|
-
QueryServices<TOperation>
|
|
27
|
-
>
|
|
28
|
-
: never;
|
|
29
|
-
|
|
30
14
|
export interface Declared<
|
|
31
15
|
TName extends string,
|
|
32
16
|
TQueries extends readonly QueryShape[],
|
|
@@ -92,10 +76,12 @@ export function contract<
|
|
|
92
76
|
const TCommands extends readonly CommandShape[] = readonly [],
|
|
93
77
|
const TStreams extends readonly StreamShape[] = readonly [],
|
|
94
78
|
const TScope extends "session" | undefined = undefined,
|
|
79
|
+
const TSubject extends SubjectDeclaration | undefined = undefined,
|
|
95
80
|
>(
|
|
96
81
|
name: TName,
|
|
97
82
|
operations: {
|
|
98
83
|
readonly keyScope?: TScope;
|
|
84
|
+
readonly subject?: TSubject;
|
|
99
85
|
readonly queries?: TQueries & Unique<TQueries>;
|
|
100
86
|
readonly commands?: TCommands & Unique<[...TQueries, ...TCommands]>;
|
|
101
87
|
readonly streams?: TStreams & Unique<[...TQueries, ...TCommands, ...TStreams]>;
|
|
@@ -104,8 +90,11 @@ export function contract<
|
|
|
104
90
|
TName,
|
|
105
91
|
TQueries,
|
|
106
92
|
TCommands,
|
|
107
|
-
|
|
108
|
-
|
|
93
|
+
WithSubject<
|
|
94
|
+
| OperationRpc<TName, TQueries[number] | TCommands[number] | TStreams[number]>
|
|
95
|
+
| Rpc.AddMiddleware<ReturnType<typeof changesRpc<TName, TQueries>>, typeof ReadPolicyGuard>,
|
|
96
|
+
TSubject
|
|
97
|
+
>,
|
|
109
98
|
TStreams,
|
|
110
99
|
TScope
|
|
111
100
|
>;
|
|
@@ -113,6 +102,7 @@ export function contract(
|
|
|
113
102
|
name: string,
|
|
114
103
|
operations: {
|
|
115
104
|
readonly keyScope?: "session";
|
|
105
|
+
readonly subject?: SubjectDeclaration;
|
|
116
106
|
readonly queries?: readonly QueryShape[];
|
|
117
107
|
readonly commands?: readonly CommandShape[];
|
|
118
108
|
readonly streams?: readonly StreamShape[];
|
|
@@ -127,11 +117,14 @@ export function contract(
|
|
|
127
117
|
stream: operation.kind === "stream",
|
|
128
118
|
success: operation.success,
|
|
129
119
|
}).annotateMerge(operationAnnotations(operation, keyScope));
|
|
130
|
-
|
|
120
|
+
const guarded = operation.kind === "query" ? rpc.middleware(ReadPolicyGuard) : rpc;
|
|
121
|
+
return operations.subject === undefined ? guarded : guarded.middleware(subjectMiddleware(`${name}.${operation.name}`, operations.subject));
|
|
131
122
|
});
|
|
132
123
|
const application = RpcGroup.make(...rpcs);
|
|
133
124
|
const changes = changesRpc(name, queries).middleware(ReadPolicyGuard);
|
|
134
|
-
|
|
125
|
+
const subjectChanges = operations.subject === undefined ? changes : changes.middleware(subjectMiddleware(`${name}.$changes`, operations.subject));
|
|
126
|
+
const group = application.add(keyScope === undefined ? subjectChanges : subjectChanges.annotate(KeyScope, keyScope));
|
|
127
|
+
return withDeclaration(group, application, {
|
|
135
128
|
commands,
|
|
136
129
|
keyScope,
|
|
137
130
|
name,
|
|
@@ -12,12 +12,12 @@ import type { QuerySignals } from "#querySignals.ts";
|
|
|
12
12
|
interface GateOptions<A, E> {
|
|
13
13
|
readonly assertOwner: (get: Atom.AtomContext) => void;
|
|
14
14
|
readonly atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>;
|
|
15
|
+
readonly clientStatuses: Atom.Writable<ReadonlyMap<object, LiveStatus<unknown>>> | undefined;
|
|
15
16
|
readonly coalesceWindow: Atom.Writable<Duration.Input>;
|
|
16
17
|
readonly equivalent: (left: A, right: A) => boolean;
|
|
17
18
|
readonly paused: Atom.Writable<boolean>;
|
|
18
19
|
readonly pushed: boolean;
|
|
19
20
|
readonly ready: Atom.Writable<boolean>;
|
|
20
|
-
readonly signal: Atom.Atom<unknown>;
|
|
21
21
|
readonly signals: Atom.Writable<QuerySignals>;
|
|
22
22
|
readonly source: Atom.Atom<AsyncResult.AsyncResult<A, E>>;
|
|
23
23
|
readonly statuses: Atom.Writable<ReadonlyMap<object, LiveStatus<unknown>>>;
|
|
@@ -27,11 +27,11 @@ export function automaticQuery<A, E>({
|
|
|
27
27
|
atom,
|
|
28
28
|
assertOwner,
|
|
29
29
|
coalesceWindow,
|
|
30
|
+
clientStatuses,
|
|
30
31
|
equivalent,
|
|
31
32
|
paused,
|
|
32
33
|
pushed,
|
|
33
34
|
ready,
|
|
34
|
-
signal,
|
|
35
35
|
signals,
|
|
36
36
|
source,
|
|
37
37
|
statuses,
|
|
@@ -56,7 +56,9 @@ export function automaticQuery<A, E>({
|
|
|
56
56
|
get.set(signals, recordSubscription(get.once(signals), value));
|
|
57
57
|
dispatcher.scheduleTask(() => {
|
|
58
58
|
if (active) {
|
|
59
|
-
|
|
59
|
+
for (const state of clientStatuses === undefined ? [statuses] : [statuses, clientStatuses]) {
|
|
60
|
+
registry.update(state, (current) => new Map([...current, [token, value]]));
|
|
61
|
+
}
|
|
60
62
|
}
|
|
61
63
|
}, 0);
|
|
62
64
|
if (value.connection === "live") {
|
|
@@ -66,12 +68,14 @@ export function automaticQuery<A, E>({
|
|
|
66
68
|
}
|
|
67
69
|
get.addFinalizer(() => {
|
|
68
70
|
active = false;
|
|
69
|
-
|
|
70
|
-
registry.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
for (const state of clientStatuses === undefined ? [statuses] : [statuses, clientStatuses]) {
|
|
72
|
+
if (registry.getNodes().has(state)) {
|
|
73
|
+
registry.update(state, (current) => {
|
|
74
|
+
const next = new Map(current);
|
|
75
|
+
next.delete(token);
|
|
76
|
+
return next;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
75
79
|
}
|
|
76
80
|
});
|
|
77
81
|
get.addFinalizer(registry.subscribe(subscription.status, observe, { immediate: true }));
|
|
@@ -122,8 +126,6 @@ export function automaticQuery<A, E>({
|
|
|
122
126
|
}
|
|
123
127
|
}
|
|
124
128
|
if (pushed) {
|
|
125
|
-
get.once(signal);
|
|
126
|
-
get.subscribe(signal, resume);
|
|
127
129
|
get.subscribe(paused, (value) => {
|
|
128
130
|
if (!value) {
|
|
129
131
|
resume();
|
|
@@ -2,14 +2,16 @@ import { Effect } from "effect";
|
|
|
2
2
|
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
|
+
import { clientConnection } from "#adapters/clientConnection.ts";
|
|
5
6
|
import { queryCoalesceWindow } from "#queryCoalesceWindow.ts";
|
|
6
7
|
import { wrapAtom } from "./wrapAtom.ts";
|
|
7
8
|
|
|
8
9
|
type ActivityTracker = ReturnType<typeof makeActivity>;
|
|
9
10
|
export const clientActivities = new WeakMap<object, ActivityTracker>();
|
|
10
11
|
|
|
11
|
-
export function makeActivity() {
|
|
12
|
-
const { activity, change } = activityState();
|
|
12
|
+
export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
13
|
+
const { activity, change, setConnection } = activityState();
|
|
14
|
+
const connection = clientConnection(factory, setConnection);
|
|
13
15
|
function begin(key: "rpcCalls" | "liveInvalidations") {
|
|
14
16
|
change(key, 1);
|
|
15
17
|
return () => change(key, -1);
|
|
@@ -23,6 +25,7 @@ export function makeActivity() {
|
|
|
23
25
|
}
|
|
24
26
|
function watch<A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) {
|
|
25
27
|
const watched = wrapAtom(atom, (get) => {
|
|
28
|
+
get.mount(connection.status);
|
|
26
29
|
get.once(queryCoalesceWindow(watched));
|
|
27
30
|
get.subscribe(queryCoalesceWindow(watched), (duration) => get.set(queryCoalesceWindow(atom), duration), { immediate: true });
|
|
28
31
|
let busy = false;
|
|
@@ -59,5 +62,12 @@ export function makeActivity() {
|
|
|
59
62
|
});
|
|
60
63
|
return watched;
|
|
61
64
|
}
|
|
62
|
-
return {
|
|
65
|
+
return {
|
|
66
|
+
activity: Object.assign(activity, { connectionStatus: connection.status }),
|
|
67
|
+
hint: () => change("liveHints", 1),
|
|
68
|
+
queued: () => begin("liveInvalidations"),
|
|
69
|
+
statuses: connection.statuses,
|
|
70
|
+
track,
|
|
71
|
+
watch,
|
|
72
|
+
};
|
|
63
73
|
}
|
|
@@ -10,10 +10,11 @@ import { clientId } from "#adapters/clientId.ts";
|
|
|
10
10
|
import type { automaticLive } from "#automaticLive.ts";
|
|
11
11
|
import type { QueryOptions } from "#bind.ts";
|
|
12
12
|
import { clientInvalidationKeys, clientReadKeys } from "#clientKeys.ts";
|
|
13
|
+
import { clientActivities } from "#internal/clientActivity.ts";
|
|
14
|
+
import { commandPayload } from "#internal/commandPayload.ts";
|
|
15
|
+
import { queryReactivity } from "#internal/queryReactivity.ts";
|
|
13
16
|
import type { CommandShape, QueryShape } from "#operation.ts";
|
|
14
17
|
import { shareQuerySignals } from "#querySignals.ts";
|
|
15
|
-
import { clientActivities } from "./clientActivity.ts";
|
|
16
|
-
import { queryReactivity } from "./queryReactivity.ts";
|
|
17
18
|
|
|
18
19
|
type ErasedClient = AtomRpc.AtomRpcClient<unknown, string, Rpc.Rpc<string, Schema.Top, Schema.Top, Schema.Top>>;
|
|
19
20
|
|
|
@@ -74,7 +75,16 @@ function boundCommand(service: ErasedClient, tag: string, command: CommandShape,
|
|
|
74
75
|
}
|
|
75
76
|
return {
|
|
76
77
|
run: (payload: unknown) =>
|
|
77
|
-
run(
|
|
78
|
+
run(
|
|
79
|
+
service,
|
|
80
|
+
service.use((client) =>
|
|
81
|
+
commandPayload(command, payload).pipe(
|
|
82
|
+
Effect.flatMap((validated) => client(tag, validated)),
|
|
83
|
+
Effect.tap((result) => invalidate(payload, result)),
|
|
84
|
+
),
|
|
85
|
+
),
|
|
86
|
+
owner,
|
|
87
|
+
),
|
|
78
88
|
};
|
|
79
89
|
}
|
|
80
90
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Effect, Predicate, Schema } from "effect";
|
|
2
|
+
import type { CommandShape } from "#operation.ts";
|
|
3
|
+
|
|
4
|
+
export function commandPayload(command: CommandShape, payload: unknown) {
|
|
5
|
+
if (!(Predicate.hasProperty(command.Rejection, "BadRequest") && Schema.isSchema(command.Rejection.BadRequest))) {
|
|
6
|
+
return Effect.succeed(payload);
|
|
7
|
+
}
|
|
8
|
+
const rejection = command.Rejection.BadRequest;
|
|
9
|
+
return command.payload.makeEffect(payload).pipe(
|
|
10
|
+
Effect.mapError((issue) => new Schema.SchemaError(issue)),
|
|
11
|
+
Effect.tap(Schema.encodeUnknownEffect(Schema.toCodecJson(command.payload))),
|
|
12
|
+
Effect.catchTag("SchemaError", (error) =>
|
|
13
|
+
Schema.decodeUnknownEffect(rejection)({ _tag: "BadRequest", message: error.message }).pipe(Effect.orDie, Effect.flatMap(Effect.fail)),
|
|
14
|
+
),
|
|
15
|
+
);
|
|
16
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { Cause, type Duration, Effect, Option, Predicate, Schedule } from "effect";
|
|
1
|
+
import { Cause, type Duration, Effect, Option, Predicate, Queue, Schedule } from "effect";
|
|
2
2
|
|
|
3
3
|
export function liveRetry<E, R>(
|
|
4
4
|
attempt: Effect.Effect<void, E, R>,
|
|
5
5
|
ended: (failure: Option.Option<Cause.Cause<E>>, retry: boolean) => void,
|
|
6
6
|
delay: Duration.Input = "1 second",
|
|
7
|
+
resume?: Queue.Queue<void>,
|
|
7
8
|
) {
|
|
8
9
|
const next = Effect.matchCause(attempt, {
|
|
9
10
|
onFailure: (failure) => {
|
|
@@ -17,8 +18,14 @@ export function liveRetry<E, R>(
|
|
|
17
18
|
return true;
|
|
18
19
|
},
|
|
19
20
|
});
|
|
20
|
-
|
|
21
|
+
const run = Effect.repeat(next, {
|
|
21
22
|
schedule: Schedule.jittered(Schedule.min([Schedule.exponential(delay, 2), Schedule.spaced("30 seconds")])),
|
|
22
23
|
while: (retry) => retry,
|
|
23
24
|
}).pipe(Effect.asVoid);
|
|
25
|
+
return resume === undefined
|
|
26
|
+
? run
|
|
27
|
+
: Effect.suspend(() => Effect.raceFirst(Effect.as(run, false), Effect.as(Queue.take(resume), true))).pipe(
|
|
28
|
+
Effect.repeat({ while: (restarted) => restarted }),
|
|
29
|
+
Effect.asVoid,
|
|
30
|
+
);
|
|
24
31
|
}
|
|
@@ -14,7 +14,7 @@ export interface RpcSource {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export function rpcClient(name: string, source: RpcSource, options: ClientOptions) {
|
|
17
|
-
const activity = makeActivity();
|
|
17
|
+
const activity = makeActivity(options.runtime ?? Atom.runtime);
|
|
18
18
|
const group = RpcGroup.make(...source.requests.values()).annotateMerge(source.annotations);
|
|
19
19
|
function sharedProtocol(identity?: string) {
|
|
20
20
|
return socketProtocol({
|
|
@@ -47,6 +47,7 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
47
47
|
group,
|
|
48
48
|
makeEffect: make,
|
|
49
49
|
protocol: typeof customProtocol === "object" ? customProtocol : (get) => protocolFor(get(socketIdentity)),
|
|
50
|
+
runtime: options.runtime,
|
|
50
51
|
});
|
|
51
52
|
clientActivities.set(Client, activity);
|
|
52
53
|
const directClient = Effect.flatMap(Layer.build(protocol), (context) =>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { RpcMiddleware } from "effect/rpc";
|
|
2
|
+
import type { SubjectDeclaration, SubjectProvides } from "#subject.ts";
|
|
3
|
+
|
|
4
|
+
export const SUBJECT_TYPE_ID = "@shivaedev/contract/SubjectDeclaration";
|
|
5
|
+
|
|
6
|
+
interface SubjectMiddleware<TSubject extends SubjectDeclaration> extends RpcMiddleware.AnyId {
|
|
7
|
+
readonly [RpcMiddleware.TypeId]: {
|
|
8
|
+
readonly provides: SubjectProvides<TSubject>;
|
|
9
|
+
readonly requires: never;
|
|
10
|
+
readonly error: TSubject["error"];
|
|
11
|
+
readonly clientError: never;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function subjectMiddleware<TSubject extends SubjectDeclaration>(name: string, subject: TSubject) {
|
|
16
|
+
const error: TSubject["error"] = subject.error;
|
|
17
|
+
const middleware = RpcMiddleware.Service<SubjectMiddleware<TSubject>, { provides: SubjectProvides<TSubject> }>()(
|
|
18
|
+
`@shivaedev/contract/Subject/${name}`,
|
|
19
|
+
{ error },
|
|
20
|
+
);
|
|
21
|
+
return Object.assign(middleware, { [SUBJECT_TYPE_ID]: subject });
|
|
22
|
+
}
|