@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
package/src/live.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Cause, Duration, Effect, Option, Schema, type Stream } from "effect";
|
|
1
|
+
import { Cause, Duration, Effect, Option, Queue, Schema, type Stream } from "effect";
|
|
2
2
|
import * as Atom from "effect/reactivity/Atom";
|
|
3
3
|
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
4
|
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
@@ -27,6 +27,7 @@ export interface LiveOptions<E, R> {
|
|
|
27
27
|
readonly coalesceWindow?: Duration.Input;
|
|
28
28
|
readonly initialResync?: boolean;
|
|
29
29
|
readonly keyContext?: KeyContext | undefined;
|
|
30
|
+
readonly reopenOnResume?: boolean;
|
|
30
31
|
readonly resume?: Atom.Atom<unknown>;
|
|
31
32
|
readonly resyncKeys: readonly Key[] | ((registry: AtomRegistry.AtomRegistry) => readonly Key[]);
|
|
32
33
|
readonly retryDelay?: Duration.Input;
|
|
@@ -44,6 +45,7 @@ export function live<R, E, TRuntimeError>(runtime: Atom.AtomRuntime<R, TRuntimeE
|
|
|
44
45
|
.atom((get) =>
|
|
45
46
|
Effect.gen(function* () {
|
|
46
47
|
get.mount(status);
|
|
48
|
+
const resumed = yield* Queue.unbounded<void>();
|
|
47
49
|
const reactivity = yield* Reactivity.Reactivity;
|
|
48
50
|
const retryKeys = options.retryKeys ?? (typeof options.resyncKeys === "function" ? options.resyncKeys(get.registry) : options.resyncKeys);
|
|
49
51
|
yield* retryOnChange(status, clientReadKeys(retryKeys, options.keyContext), () => get.refreshSelf());
|
|
@@ -101,10 +103,20 @@ export function live<R, E, TRuntimeError>(runtime: Atom.AtomRuntime<R, TRuntimeE
|
|
|
101
103
|
resync();
|
|
102
104
|
}
|
|
103
105
|
});
|
|
106
|
+
function resume() {
|
|
107
|
+
const current = get.once(status).connection;
|
|
108
|
+
if (current === "failed") {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
resync();
|
|
112
|
+
if (options.reopenOnResume === true || current === "reconnecting") {
|
|
113
|
+
Queue.offerUnsafe(resumed, undefined);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
104
116
|
if (options.resume !== undefined) {
|
|
105
117
|
// A subscription alone does not initialize a derived signal's dependencies.
|
|
106
118
|
get.once(options.resume);
|
|
107
|
-
get.subscribe(options.resume,
|
|
119
|
+
get.subscribe(options.resume, resume);
|
|
108
120
|
}
|
|
109
121
|
const attempt = liveAttempt(options.stream, {
|
|
110
122
|
changed,
|
|
@@ -129,6 +141,7 @@ export function live<R, E, TRuntimeError>(runtime: Atom.AtomRuntime<R, TRuntimeE
|
|
|
129
141
|
update({ connection: retry ? "reconnecting" : "failed", failure, needsResync: true });
|
|
130
142
|
},
|
|
131
143
|
options.retryDelay,
|
|
144
|
+
resumed,
|
|
132
145
|
);
|
|
133
146
|
}),
|
|
134
147
|
)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Schema } from "effect";
|
|
2
|
+
import type { Rpc, RpcSchema } from "effect/rpc";
|
|
3
|
+
import type { QueryServices } from "#contract.ts";
|
|
4
|
+
import type { OperationShape, QueryShape, StreamShape } from "#operation.ts";
|
|
5
|
+
import type { ReadPolicyGuard } from "#ReadPolicyGuard.ts";
|
|
6
|
+
import type { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
7
|
+
|
|
8
|
+
export type Tag<TContract extends string, TName extends string> = `${TContract}.${TName}`;
|
|
9
|
+
|
|
10
|
+
type OperationError<TOperation extends OperationShape> = TOperation extends QueryShape
|
|
11
|
+
? Schema.Union<readonly [TOperation["error"], typeof QueryReadDenied]>
|
|
12
|
+
: TOperation["error"];
|
|
13
|
+
|
|
14
|
+
export type OperationRpc<TContract extends string, TOperation> = TOperation extends OperationShape
|
|
15
|
+
? TOperation extends StreamShape
|
|
16
|
+
? Rpc.Rpc<Tag<TContract, TOperation["name"]>, TOperation["payload"], RpcSchema.Stream<TOperation["success"], TOperation["error"]>, Schema.Never>
|
|
17
|
+
: Rpc.Rpc<
|
|
18
|
+
Tag<TContract, TOperation["name"]>,
|
|
19
|
+
TOperation["payload"],
|
|
20
|
+
TOperation["success"],
|
|
21
|
+
OperationError<TOperation>,
|
|
22
|
+
TOperation extends QueryShape ? typeof ReadPolicyGuard : never,
|
|
23
|
+
QueryServices<TOperation>
|
|
24
|
+
>
|
|
25
|
+
: never;
|
package/src/subject.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Effect, Schema } from "effect";
|
|
2
|
+
import type { Rpc, RpcMiddleware } from "effect/rpc";
|
|
3
|
+
import type { subjectMiddleware } from "#internal/subjectMiddleware.ts";
|
|
4
|
+
|
|
5
|
+
export interface SubjectDeclaration {
|
|
6
|
+
readonly error: Schema.Top;
|
|
7
|
+
readonly middleware: Effect.Effect<RpcMiddleware.Any, never, unknown>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type SubjectProvides<TSubject extends SubjectDeclaration> =
|
|
11
|
+
Effect.Success<TSubject["middleware"]> extends RpcMiddleware.RpcMiddleware<infer TProvides, infer _E, infer _R> ? TProvides : never;
|
|
12
|
+
|
|
13
|
+
export type WithSubject<TRpcs extends Rpc.Any, TSubject extends SubjectDeclaration | undefined> = TSubject extends SubjectDeclaration
|
|
14
|
+
? Rpc.AddMiddleware<TRpcs, ReturnType<typeof subjectMiddleware<TSubject>>>
|
|
15
|
+
: TRpcs;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Context, type Effect, Layer } from "effect";
|
|
2
|
+
import { Rpc, type RpcGroup, type RpcMiddleware } from "effect/rpc";
|
|
3
|
+
import { SUBJECT_TYPE_ID } from "#internal/subjectMiddleware.ts";
|
|
4
|
+
import type { SubjectDeclaration } from "#subject.ts";
|
|
5
|
+
|
|
6
|
+
type Middleware<R extends Rpc.Any> = R extends { readonly middlewares: ReadonlySet<infer TMiddleware> }
|
|
7
|
+
? Extract<TMiddleware, { readonly [SUBJECT_TYPE_ID]: SubjectDeclaration }>
|
|
8
|
+
: never;
|
|
9
|
+
|
|
10
|
+
function isSubject(middleware: RpcMiddleware.AnyServiceWithProps): middleware is RpcMiddleware.AnyServiceWithProps & {
|
|
11
|
+
readonly [SUBJECT_TYPE_ID]: SubjectDeclaration;
|
|
12
|
+
} {
|
|
13
|
+
return SUBJECT_TYPE_ID in middleware;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function subjectLayers<R extends Rpc.Any>(
|
|
17
|
+
group: RpcGroup.RpcGroup<R>,
|
|
18
|
+
): Layer.Layer<Context.Service.Identifier<Middleware<R>>, never, Effect.Services<Middleware<R>[typeof SUBJECT_TYPE_ID]["middleware"]>>;
|
|
19
|
+
export function subjectLayers(group: RpcGroup.RpcGroup<Rpc.Any>) {
|
|
20
|
+
const layers: Layer.Layer<never, never, unknown>[] = [];
|
|
21
|
+
for (const rpc of group.requests.values()) {
|
|
22
|
+
if (!Rpc.isRpc(rpc)) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const middlewares: ReadonlySet<RpcMiddleware.AnyServiceWithProps> = rpc.middlewares;
|
|
26
|
+
for (const middleware of middlewares) {
|
|
27
|
+
if (isSubject(middleware)) {
|
|
28
|
+
layers.push(Layer.effect(middleware, middleware[SUBJECT_TYPE_ID].middleware));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return Layer.mergeAll(Layer.empty, ...layers);
|
|
33
|
+
}
|