@shivaedev/contract 0.36.0 → 0.37.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 +20 -0
- package/README.md +21 -1
- package/dist/SocketClock.d.ts +3 -0
- package/dist/SocketClock.d.ts.map +1 -0
- package/dist/SocketClock.js +5 -0
- package/dist/SocketClock.js.map +1 -0
- package/dist/adapters/activityState.d.ts +1 -0
- package/dist/adapters/activityState.d.ts.map +1 -1
- package/dist/adapters/activityState.js +16 -1
- package/dist/adapters/activityState.js.map +1 -1
- package/dist/adapters/clientConnection.d.ts +5 -2
- package/dist/adapters/clientConnection.d.ts.map +1 -1
- package/dist/adapters/clientConnection.js +11 -2
- package/dist/adapters/clientConnection.js.map +1 -1
- package/dist/adapters/resumeSocket.d.ts +7 -0
- package/dist/adapters/resumeSocket.d.ts.map +1 -0
- package/dist/adapters/resumeSocket.js +57 -0
- package/dist/adapters/resumeSocket.js.map +1 -0
- package/dist/clientActivity.d.ts +1 -1
- package/dist/clientActivity.d.ts.map +1 -1
- package/dist/internal/clientActivity.d.ts +2 -0
- package/dist/internal/clientActivity.d.ts.map +1 -1
- package/dist/internal/clientActivity.js +3 -2
- package/dist/internal/clientActivity.js.map +1 -1
- package/dist/internal/clientOperations.d.ts.map +1 -1
- package/dist/internal/clientOperations.js +2 -1
- package/dist/internal/clientOperations.js.map +1 -1
- package/dist/internal/connectionProtocol.d.ts +17 -0
- package/dist/internal/connectionProtocol.d.ts.map +1 -0
- package/dist/internal/connectionProtocol.js +14 -0
- package/dist/internal/connectionProtocol.js.map +1 -0
- package/dist/internal/connectionReadiness.d.ts +20 -0
- package/dist/internal/connectionReadiness.d.ts.map +1 -0
- package/dist/internal/connectionReadiness.js +23 -0
- package/dist/internal/connectionReadiness.js.map +1 -0
- package/dist/internal/resumeSources.d.ts +3 -0
- package/dist/internal/resumeSources.d.ts.map +1 -0
- package/dist/internal/resumeSources.js +16 -0
- package/dist/internal/resumeSources.js.map +1 -0
- package/dist/internal/rpcClient.d.ts.map +1 -1
- package/dist/internal/rpcClient.js +32 -28
- package/dist/internal/rpcClient.js.map +1 -1
- package/dist/internal/socketProtocol.d.ts +15 -0
- package/dist/internal/socketProtocol.d.ts.map +1 -0
- package/dist/internal/socketProtocol.js +47 -0
- package/dist/internal/socketProtocol.js.map +1 -0
- package/dist/resume.d.ts +2 -2
- package/dist/resume.d.ts.map +1 -1
- package/dist/resume.js +3 -14
- package/dist/resume.js.map +1 -1
- package/package.json +15 -16
- package/src/SocketClock.ts +5 -0
- package/src/adapters/activityState.ts +18 -3
- package/src/adapters/clientConnection.ts +22 -3
- package/src/adapters/resumeSocket.ts +79 -0
- package/src/clientActivity.ts +1 -1
- package/src/internal/clientActivity.ts +10 -2
- package/src/internal/clientOperations.ts +2 -0
- package/src/internal/connectionProtocol.ts +27 -0
- package/src/internal/connectionReadiness.ts +32 -0
- package/src/internal/resumeSources.ts +19 -0
- package/src/internal/rpcClient.ts +36 -42
- package/src/internal/socketProtocol.ts +73 -0
- package/src/resume.ts +4 -16
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect, Layer, Stream } from "effect";
|
|
2
2
|
import * as Atom from "effect/reactivity/Atom";
|
|
3
3
|
import * as AtomRpc from "effect/reactivity/AtomRpc";
|
|
4
|
-
import { RpcClient, RpcGroup
|
|
5
|
-
import * as Socket from "effect/socket/Socket";
|
|
4
|
+
import { RpcClient, RpcGroup } from "effect/rpc";
|
|
6
5
|
import { Activity } from "#clientActivity.ts";
|
|
7
|
-
import { authenticateSocket } from "#socketAuthentication.ts";
|
|
8
6
|
import { clientActivities, makeActivity } from "./clientActivity.js";
|
|
7
|
+
import { connectionProtocol, protocolConnections } from "./connectionProtocol.js";
|
|
8
|
+
import { commandConnections } from "./connectionReadiness.js";
|
|
9
|
+
import { socketProtocol } from "./socketProtocol.js";
|
|
9
10
|
export function rpcClient(name, source, options) {
|
|
10
11
|
const activity = makeActivity(options.runtime ?? Atom.runtime);
|
|
11
12
|
const group = RpcGroup.make(...source.requests.values()).annotateMerge(source.annotations);
|
|
@@ -14,15 +15,27 @@ export function rpcClient(name, source, options) {
|
|
|
14
15
|
bearerToken: options.bearerToken,
|
|
15
16
|
headers: options.headers,
|
|
16
17
|
identity,
|
|
18
|
+
resume: options.resume,
|
|
19
|
+
resumeWindow: options.resumeWindow ?? (typeof window === "undefined" ? undefined : window),
|
|
17
20
|
url: options.url.replace(/^http/u, "ws"),
|
|
18
21
|
webSocketConstructor: options.webSocketConstructor,
|
|
19
22
|
});
|
|
20
23
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
function configuredProtocol() {
|
|
25
|
+
const custom = options.protocol;
|
|
26
|
+
if (custom === undefined) {
|
|
27
|
+
return sharedProtocol;
|
|
28
|
+
}
|
|
29
|
+
if (typeof custom === "function") {
|
|
30
|
+
return Atom.family((identity) => connectionProtocol(custom(identity)));
|
|
31
|
+
}
|
|
32
|
+
const layer = connectionProtocol(custom);
|
|
33
|
+
return () => layer;
|
|
34
|
+
}
|
|
35
|
+
const protocolFor = configuredProtocol();
|
|
36
|
+
const protocol = protocolFor(undefined);
|
|
37
|
+
function observeClient(client, wait) {
|
|
38
|
+
const observed = new Proxy(client, {
|
|
26
39
|
apply(target, receiver, args) {
|
|
27
40
|
const result = Reflect.apply(target, receiver, args);
|
|
28
41
|
if (String(args[0]).endsWith(".$changes") && Stream.isStream(result)) {
|
|
@@ -32,12 +45,20 @@ export function rpcClient(name, source, options) {
|
|
|
32
45
|
return Effect.isEffect(result) && !args[2]?.asQueue ? activity.track(result) : result;
|
|
33
46
|
},
|
|
34
47
|
});
|
|
48
|
+
commandConnections.set(observed, wait);
|
|
49
|
+
return observed;
|
|
35
50
|
}
|
|
36
|
-
const make =
|
|
51
|
+
const make = Effect.gen(function* () {
|
|
52
|
+
const transport = yield* RpcClient.Protocol;
|
|
53
|
+
const connection = protocolConnections.get(transport);
|
|
54
|
+
const wait = connection === undefined ? Effect.void : Effect.suspend(() => (connection.isReady() ? Effect.void : activity.hold(connection.wait)));
|
|
55
|
+
const native = yield* RpcClient.make(group, { flatten: true });
|
|
56
|
+
return observeClient(native, wait);
|
|
57
|
+
});
|
|
37
58
|
const Client = AtomRpc.Service()(`${name}/Client`, {
|
|
38
59
|
group,
|
|
39
60
|
makeEffect: make,
|
|
40
|
-
protocol: typeof
|
|
61
|
+
protocol: typeof options.protocol === "object" ? protocol : (get) => protocolFor(get(socketIdentity)),
|
|
41
62
|
runtime: options.runtime,
|
|
42
63
|
});
|
|
43
64
|
clientActivities.set(Client, activity);
|
|
@@ -46,21 +67,4 @@ export function rpcClient(name, source, options) {
|
|
|
46
67
|
return Object.assign(layer, { [Activity]: activity.activity, Client, client: directClient, runtime: Client.runtime });
|
|
47
68
|
}
|
|
48
69
|
export const socketIdentity = Atom.make(undefined);
|
|
49
|
-
const socketMemoMap = Layer.makeMemoMapUnsafe();
|
|
50
|
-
const socketProtocol = Atom.family((options) => {
|
|
51
|
-
let url = options.url;
|
|
52
|
-
if (options.bearerToken !== undefined) {
|
|
53
|
-
const address = new URL(url);
|
|
54
|
-
address.searchParams.set("auth", "frame");
|
|
55
|
-
url = address.toString();
|
|
56
|
-
}
|
|
57
|
-
const raw = Socket.layerWebSocket(url, { headers: options.headers });
|
|
58
|
-
const socket = (options.bearerToken === undefined ? raw : Layer.effect(Socket.Socket, authenticateSocket(options.bearerToken)).pipe(Layer.provide(raw))).pipe(Layer.provide(options.webSocketConstructor ?? Socket.layerWebSocketConstructorGlobal));
|
|
59
|
-
const protocol = RpcClient.layerProtocolSocket({
|
|
60
|
-
retryPolicy: Schedule.jittered(Schedule.min([Schedule.exponential(500, 1.5), Schedule.spaced(5000)])),
|
|
61
|
-
}).pipe(Layer.provide(socket), Layer.provide(RpcSerialization.layerNdjson),
|
|
62
|
-
// Socket deadlines follow network time even when application time is simulated.
|
|
63
|
-
Layer.provide(Layer.succeed(Clock.Clock, Clock.Clock.defaultValue())));
|
|
64
|
-
return Layer.effectContext(Effect.flatMap(Effect.scope, (scope) => Layer.buildWithMemoMap(protocol, socketMemoMap, scope)));
|
|
65
|
-
});
|
|
66
70
|
//# sourceMappingURL=rpcClient.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpcClient.js","sourceRoot":"","sources":["../../src/internal/rpcClient.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"rpcClient.js","sourceRoot":"","sources":["../../src/internal/rpcClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC7D,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,KAAK,OAAO,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAY,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3D,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAG9C,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAClF,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AASrD,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,MAAiB,EAAE,OAAsB;IAChF,MAAM,QAAQ,GAAG,YAAY,CAAC,OAAO,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC;IAC/D,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAC3F,SAAS,cAAc,CAAC,QAAiB;QAC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AK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IACvE,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;AACvH,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CAAqB,SAAS,CAAC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Layer } from "effect";
|
|
2
|
+
import { RpcClient } from "effect/rpc";
|
|
3
|
+
import type { ClientOptions } from "#clientOptions.ts";
|
|
4
|
+
interface SocketOptions {
|
|
5
|
+
readonly bearerToken: ClientOptions["bearerToken"];
|
|
6
|
+
readonly headers: ClientOptions["headers"];
|
|
7
|
+
readonly identity: string | undefined;
|
|
8
|
+
readonly resume: ClientOptions["resume"];
|
|
9
|
+
readonly resumeWindow: ClientOptions["resumeWindow"];
|
|
10
|
+
readonly url: string;
|
|
11
|
+
readonly webSocketConstructor: ClientOptions["webSocketConstructor"];
|
|
12
|
+
}
|
|
13
|
+
export declare function socketProtocol(options: SocketOptions): Layer.Layer<RpcClient.Protocol, never, never>;
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=socketProtocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"socketProtocol.d.ts","sourceRoot":"","sources":["../../src/internal/socketProtocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,EAAY,MAAM,QAAQ,CAAC;AAE/D,OAAO,EAAE,SAAS,EAAoB,MAAM,YAAY,CAAC;AAGzD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAMvD,UAAU,aAAa;IACtB,QAAQ,CAAC,WAAW,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC;IACnD,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,SAAS,CAAC,CAAC;IAC3C,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,QAAQ,CAAC,CAAC;IACzC,QAAQ,CAAC,YAAY,EAAE,aAAa,CAAC,cAAc,CAAC,CAAC;IACrD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,oBAAoB,EAAE,aAAa,CAAC,sBAAsB,CAAC,CAAC;CACrE;AAMD,wBAAgB,cAAc,CAAC,OAAO,EAAE,aAAa,iDAQpD"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Clock, Effect, Equal, Layer, Schedule } from "effect";
|
|
2
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
import { RpcClient, RpcSerialization } from "effect/rpc";
|
|
4
|
+
import * as Socket from "effect/socket/Socket";
|
|
5
|
+
import { resumeSocket } from "#adapters/resumeSocket.ts";
|
|
6
|
+
import { SocketClock } from "#SocketClock.ts";
|
|
7
|
+
import { authenticateSocket } from "#socketAuthentication.ts";
|
|
8
|
+
import { connectionProtocol } from "./connectionProtocol.js";
|
|
9
|
+
const socketMemoMaps = new WeakMap();
|
|
10
|
+
const windows = new WeakMap();
|
|
11
|
+
export function socketProtocol(options) {
|
|
12
|
+
const target = options.resumeWindow;
|
|
13
|
+
let reference = target === undefined ? undefined : windows.get(target);
|
|
14
|
+
if (target !== undefined && reference === undefined) {
|
|
15
|
+
reference = Equal.byReference({ target });
|
|
16
|
+
windows.set(target, reference);
|
|
17
|
+
}
|
|
18
|
+
return protocols({ ...options, resumeWindow: reference });
|
|
19
|
+
}
|
|
20
|
+
const protocols = Atom.family((options) => {
|
|
21
|
+
let url = options.url;
|
|
22
|
+
if (options.bearerToken !== undefined) {
|
|
23
|
+
const address = new URL(url);
|
|
24
|
+
address.searchParams.set("auth", "frame");
|
|
25
|
+
url = address.toString();
|
|
26
|
+
}
|
|
27
|
+
const raw = Socket.layerWebSocket(url, { headers: options.headers });
|
|
28
|
+
const authenticated = (options.bearerToken === undefined ? raw : Layer.effect(Socket.Socket, authenticateSocket(options.bearerToken)).pipe(Layer.provide(raw))).pipe(Layer.provide(options.webSocketConstructor ?? Socket.layerWebSocketConstructorGlobal));
|
|
29
|
+
const socket = Layer.effect(Socket.Socket, resumeSocket({
|
|
30
|
+
...(options.resumeWindow === undefined ? {} : { window: options.resumeWindow.target }),
|
|
31
|
+
...(options.resume === undefined ? {} : { native: options.resume }),
|
|
32
|
+
})).pipe(Layer.provide(authenticated), Layer.provide(RpcSerialization.layerNdjson));
|
|
33
|
+
const managed = connectionProtocol(RpcClient.layerProtocolSocket({
|
|
34
|
+
retryPolicy: Schedule.jittered(Schedule.min([Schedule.exponential(500, 1.5), Schedule.spaced(5000)])),
|
|
35
|
+
}).pipe(Layer.provide(socket), Layer.provide(RpcSerialization.layerNdjson)));
|
|
36
|
+
return Layer.effectContext(Effect.gen(function* () {
|
|
37
|
+
const clock = yield* SocketClock;
|
|
38
|
+
let memo = socketMemoMaps.get(clock);
|
|
39
|
+
if (memo === undefined) {
|
|
40
|
+
memo = Layer.makeMemoMapUnsafe();
|
|
41
|
+
socketMemoMaps.set(clock, memo);
|
|
42
|
+
}
|
|
43
|
+
const protocol = managed.pipe(Layer.provide(Layer.succeed(Clock.Clock, clock)));
|
|
44
|
+
return yield* Layer.buildWithMemoMap(protocol, memo, yield* Effect.scope);
|
|
45
|
+
}));
|
|
46
|
+
});
|
|
47
|
+
//# sourceMappingURL=socketProtocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"socketProtocol.js","sourceRoot":"","sources":["../../src/internal/socketProtocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC/D,OAAO,KAAK,IAAI,MAAM,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AC,QAAQ,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACrG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC,CAC3E,CAAC;IACF,OAAO,KAAK,CAAC,aAAa,CACzB,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QACnB,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,WAAW,CAAC;QACjC,IAAI,IAAI,GAAG,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACrC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACxB,IAAI,GAAG,KAAK,CAAC,iBAAiB,EAAE,CAAC;YACjC,cAAc,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QACjC,CAAC;QACD,MAAM,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QAChF,OAAO,KAAK,CAAC,CAAC,KAAK,CAAC,gBAAgB,CAAC,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC3E,CAAC,CAAC,CACF,CAAC;AACH,CAAC,CACD,CAAC"}
|
package/dist/resume.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as Atom from "effect/reactivity/Atom";
|
|
2
2
|
export type ResumeSource = (resume: () => void) => () => void;
|
|
3
|
-
type ResumeEvent = "online" | "visibilitychange";
|
|
3
|
+
type ResumeEvent = "online" | "visibilitychange" | "pageshow";
|
|
4
4
|
export interface ResumeWindow {
|
|
5
5
|
readonly addEventListener: (type: ResumeEvent, listener: () => void) => void;
|
|
6
6
|
readonly document: {
|
|
@@ -12,6 +12,6 @@ export interface ResumeOptions {
|
|
|
12
12
|
readonly native?: ResumeSource;
|
|
13
13
|
readonly window?: ResumeWindow;
|
|
14
14
|
}
|
|
15
|
-
export declare function resumeSignal(
|
|
15
|
+
export declare function resumeSignal(options: ResumeOptions): Atom.Atom<number>;
|
|
16
16
|
export {};
|
|
17
17
|
//# sourceMappingURL=resume.d.ts.map
|
package/dist/resume.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resume.d.ts","sourceRoot":"","sources":["../src/resume.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"resume.d.ts","sourceRoot":"","sources":["../src/resume.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAG/C,MAAM,MAAM,YAAY,GAAG,CAAC,MAAM,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC;AAE9D,KAAK,WAAW,GAAG,QAAQ,GAAG,kBAAkB,GAAG,UAAU,CAAC;AAE9D,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,gBAAgB,EAAE,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;IAC7E,QAAQ,CAAC,QAAQ,EAAE;QAAE,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAA;KAAE,CAAC;IACxD,QAAQ,CAAC,mBAAmB,EAAE,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;CAChF;AAED,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC;IAC/B,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC;CAC/B;AAED,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAatE"}
|
package/dist/resume.js
CHANGED
|
@@ -1,18 +1,7 @@
|
|
|
1
1
|
import * as Atom from "effect/reactivity/Atom";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (target.document.visibilityState === "visible") {
|
|
6
|
-
resume();
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
target.addEventListener(type, visible);
|
|
10
|
-
return () => target.removeEventListener(type, visible);
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
export function resumeSignal({ window, native }) {
|
|
14
|
-
const browser = window === undefined ? [] : [whileVisible(window, "visibilitychange"), whileVisible(window, "online")];
|
|
15
|
-
const sources = native === undefined ? browser : [...browser, native];
|
|
2
|
+
import { resumeSources } from "#internal/resumeSources.ts";
|
|
3
|
+
export function resumeSignal(options) {
|
|
4
|
+
const sources = resumeSources(options);
|
|
16
5
|
return Atom.readable((get) => {
|
|
17
6
|
let count = 0;
|
|
18
7
|
function resume() {
|
package/dist/resume.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resume.js","sourceRoot":"","sources":["../src/resume.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"resume.js","sourceRoot":"","sources":["../src/resume.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAiB3D,MAAM,UAAU,YAAY,CAAC,OAAsB;IAClD,MAAM,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IACvC,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,EAAE,EAAE;QAC5B,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,SAAS,MAAM;YACd,KAAK,IAAI,CAAC,CAAC;YACX,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;QACD,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC9B,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;QAClC,CAAC;QACD,OAAO,KAAK,CAAC;IACd,CAAC,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/contract",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.37.0",
|
|
4
4
|
"description": "Typed query and command contracts over native Effect RPC and reactivity",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -43,31 +43,30 @@
|
|
|
43
43
|
"README.md"
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@shivaedev/changes": "0.10.
|
|
47
|
-
"@shivaedev/service": "0.7.
|
|
46
|
+
"@shivaedev/changes": "0.10.1",
|
|
47
|
+
"@shivaedev/service": "0.7.1",
|
|
48
48
|
"@shivaedev/types": "0.4.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@effect/platform-node": "4.0.
|
|
52
|
-
"@effect/vitest": "4.0.
|
|
53
|
-
"@shivaedev/errors": "0.9.
|
|
54
|
-
"@shivaedev/identity": "0.
|
|
55
|
-
"@shivaedev/quality": "0.
|
|
56
|
-
"@shivaedev/server": "0.
|
|
57
|
-
"@shivaedev/sql": "0.
|
|
58
|
-
"@shivaedev/test": "0.
|
|
59
|
-
"@shivaedev/test-
|
|
60
|
-
"@shivaedev/test-
|
|
61
|
-
"@shivaedev/test-story": "0.38.0",
|
|
51
|
+
"@effect/platform-node": "4.0.7",
|
|
52
|
+
"@effect/vitest": "4.0.7",
|
|
53
|
+
"@shivaedev/errors": "0.9.1",
|
|
54
|
+
"@shivaedev/identity": "0.26.2",
|
|
55
|
+
"@shivaedev/quality": "0.63.0",
|
|
56
|
+
"@shivaedev/server": "0.26.0",
|
|
57
|
+
"@shivaedev/sql": "0.41.0",
|
|
58
|
+
"@shivaedev/test-kits": "0.6.0",
|
|
59
|
+
"@shivaedev/test-sql": "0.22.0",
|
|
60
|
+
"@shivaedev/test-story": "0.40.0",
|
|
62
61
|
"@types/node": "24.19.2",
|
|
63
|
-
"effect": "4.0.
|
|
62
|
+
"effect": "4.0.7",
|
|
64
63
|
"esbuild": "0.28.2",
|
|
65
64
|
"happy-dom": "21.0.4",
|
|
66
65
|
"typescript": "7.0.2",
|
|
67
66
|
"vitest": "5.0.5"
|
|
68
67
|
},
|
|
69
68
|
"peerDependencies": {
|
|
70
|
-
"effect": "4.0.
|
|
69
|
+
"effect": "4.0.7"
|
|
71
70
|
},
|
|
72
71
|
"engines": {
|
|
73
72
|
"node": ">=24"
|
|
@@ -2,18 +2,33 @@ import { Effect, SubscriptionRef } from "effect";
|
|
|
2
2
|
import type { ClientActivityState, ConnectionStatus } from "#clientActivity.ts";
|
|
3
3
|
|
|
4
4
|
export function activityState() {
|
|
5
|
+
let held = 0;
|
|
6
|
+
let connection: ConnectionStatus = "live";
|
|
5
7
|
const state = Effect.runSync(
|
|
6
8
|
SubscriptionRef.make<ClientActivityState>({ connectionStatus: "live", liveHints: 0, liveInvalidations: 0, refreshingAtoms: 0, rpcCalls: 0 }),
|
|
7
9
|
);
|
|
10
|
+
function publish() {
|
|
11
|
+
const connectionStatus = held > 0 ? "waiting-for-connection" : connection;
|
|
12
|
+
Effect.runSync(
|
|
13
|
+
SubscriptionRef.update(state, (current) => (current.connectionStatus === connectionStatus ? current : { ...current, connectionStatus })),
|
|
14
|
+
);
|
|
15
|
+
}
|
|
8
16
|
return {
|
|
9
17
|
activity: Object.assign(SubscriptionRef.changes(state), { current: SubscriptionRef.get(state) }),
|
|
10
18
|
change: (key: Exclude<keyof ClientActivityState, "connectionStatus">, delta: number) => {
|
|
11
19
|
Effect.runSync(SubscriptionRef.update(state, (current) => ({ ...current, [key]: current[key] + delta })));
|
|
12
20
|
},
|
|
21
|
+
hold: () => {
|
|
22
|
+
held += 1;
|
|
23
|
+
publish();
|
|
24
|
+
return () => {
|
|
25
|
+
held -= 1;
|
|
26
|
+
publish();
|
|
27
|
+
};
|
|
28
|
+
},
|
|
13
29
|
setConnection: (connectionStatus: ConnectionStatus) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
);
|
|
30
|
+
connection = connectionStatus;
|
|
31
|
+
publish();
|
|
17
32
|
},
|
|
18
33
|
};
|
|
19
34
|
}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { Effect, Layer, Option, Stream } from "effect";
|
|
2
2
|
import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
3
|
import * as Atom from "effect/reactivity/Atom";
|
|
4
|
-
import type { ConnectionStatus } from "#clientActivity.ts";
|
|
4
|
+
import type { ClientActivityState, ConnectionStatus } from "#clientActivity.ts";
|
|
5
5
|
import type { LiveStatus } from "#live.ts";
|
|
6
6
|
|
|
7
|
-
export function clientConnection(
|
|
7
|
+
export function clientConnection(
|
|
8
|
+
factory: Atom.RuntimeFactory,
|
|
9
|
+
publish: (status: ConnectionStatus) => void,
|
|
10
|
+
activity: Stream.Stream<ClientActivityState> & { readonly current: Effect.Effect<ClientActivityState> },
|
|
11
|
+
) {
|
|
8
12
|
const statuses = Atom.make<ReadonlyMap<object, LiveStatus<unknown>>>(new Map());
|
|
9
13
|
const current = Atom.map(
|
|
10
14
|
statuses,
|
|
@@ -12,7 +16,7 @@ export function clientConnection(factory: Atom.RuntimeFactory, publish: (status:
|
|
|
12
16
|
[...values.values()].some(({ connection }) => connection === "connecting" || connection === "reconnecting") ? "reconnecting" : "live",
|
|
13
17
|
);
|
|
14
18
|
const runtime = factory(Layer.empty);
|
|
15
|
-
const
|
|
19
|
+
const debounced = Atom.make((get): ConnectionStatus => {
|
|
16
20
|
const context = AsyncResult.value(get(runtime));
|
|
17
21
|
if (Option.isNone(context)) {
|
|
18
22
|
return "live";
|
|
@@ -33,5 +37,20 @@ export function clientConnection(factory: Atom.RuntimeFactory, publish: (status:
|
|
|
33
37
|
get.addFinalizer(cancel);
|
|
34
38
|
return "live";
|
|
35
39
|
});
|
|
40
|
+
const status = Atom.make((get): ConnectionStatus => {
|
|
41
|
+
get.mount(debounced);
|
|
42
|
+
const context = AsyncResult.value(get(runtime));
|
|
43
|
+
if (Option.isSome(context)) {
|
|
44
|
+
const cancel = Effect.runCallbackWith(context.value)(
|
|
45
|
+
activity.pipe(
|
|
46
|
+
Stream.map((state) => state.connectionStatus),
|
|
47
|
+
Stream.changes,
|
|
48
|
+
Stream.runForEach((value) => Effect.sync(() => get.setSelf(value))),
|
|
49
|
+
),
|
|
50
|
+
);
|
|
51
|
+
get.addFinalizer(cancel);
|
|
52
|
+
}
|
|
53
|
+
return Effect.runSync(activity.current).connectionStatus;
|
|
54
|
+
});
|
|
36
55
|
return { status, statuses };
|
|
37
56
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Deferred, Effect } from "effect";
|
|
2
|
+
import { RpcMessage, RpcSerialization } from "effect/rpc";
|
|
3
|
+
import * as Socket from "effect/socket/Socket";
|
|
4
|
+
import { ConnectionReadiness } from "#internal/connectionReadiness.ts";
|
|
5
|
+
import { resumeSources } from "#internal/resumeSources.ts";
|
|
6
|
+
import type { ResumeOptions } from "#resume.ts";
|
|
7
|
+
|
|
8
|
+
export const resumeSocket = Effect.fnUntraced(function* (options: ResumeOptions) {
|
|
9
|
+
const socket = yield* Socket.Socket;
|
|
10
|
+
const connection = yield* ConnectionReadiness;
|
|
11
|
+
const serialization = yield* RpcSerialization.RpcSerialization;
|
|
12
|
+
const writer = yield* socket.writer;
|
|
13
|
+
return Socket.make({
|
|
14
|
+
reader: Effect.gen(function* () {
|
|
15
|
+
const reader = yield* socket.reader;
|
|
16
|
+
const scope = yield* Effect.scope;
|
|
17
|
+
const context = yield* Effect.context();
|
|
18
|
+
const parser = serialization.makeUnsafe();
|
|
19
|
+
const failed = yield* Deferred.make<never, Socket.SocketError>();
|
|
20
|
+
let pending: Deferred.Deferred<void> | undefined;
|
|
21
|
+
const probe = Effect.suspend(() => {
|
|
22
|
+
if (pending !== undefined) {
|
|
23
|
+
return Effect.void;
|
|
24
|
+
}
|
|
25
|
+
const pong = Deferred.makeUnsafe<void>();
|
|
26
|
+
pending = pong;
|
|
27
|
+
return connection.close.pipe(
|
|
28
|
+
Effect.andThen(
|
|
29
|
+
Effect.suspend(() => {
|
|
30
|
+
const ping = parser.encode(RpcMessage.constPing);
|
|
31
|
+
return ping === undefined ? Effect.void : writer.write(ping);
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
Effect.andThen(Deferred.await(pong)),
|
|
35
|
+
Effect.timeoutOrElse({
|
|
36
|
+
duration: "2500 millis",
|
|
37
|
+
orElse: () => Effect.fail(new Socket.SocketError({ reason: new Socket.SocketReadError({ cause: "Resume probe timed out" }) })),
|
|
38
|
+
}),
|
|
39
|
+
Effect.andThen(connection.open),
|
|
40
|
+
Effect.catch((error) => Deferred.fail(failed, error)),
|
|
41
|
+
Effect.ensuring(
|
|
42
|
+
Effect.sync(() => {
|
|
43
|
+
pending = undefined;
|
|
44
|
+
}),
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
for (const source of resumeSources(options)) {
|
|
49
|
+
yield* Effect.acquireRelease(
|
|
50
|
+
Effect.sync(() =>
|
|
51
|
+
source(() => {
|
|
52
|
+
Effect.runForkWith(context)(Effect.forkIn(probe, scope));
|
|
53
|
+
}),
|
|
54
|
+
),
|
|
55
|
+
(stop) => Effect.sync(stop),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
function observe(frames: ReadonlyArray<Uint8Array | string>) {
|
|
59
|
+
for (const frame of frames) {
|
|
60
|
+
const responses = parser.decode(frame);
|
|
61
|
+
if (pending !== undefined && Array.isArray(responses) && responses.some((response) => response._tag === "Pong")) {
|
|
62
|
+
Deferred.doneUnsafe(pending, Effect.void);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const pull = reader.pull.pipe(
|
|
67
|
+
Effect.tap((frames) =>
|
|
68
|
+
Effect.try({
|
|
69
|
+
catch: (cause) => new Socket.SocketError({ reason: new Socket.SocketReadError({ cause }) }),
|
|
70
|
+
try: () => observe(frames),
|
|
71
|
+
}),
|
|
72
|
+
),
|
|
73
|
+
Effect.raceFirst(Deferred.await(failed)),
|
|
74
|
+
);
|
|
75
|
+
return { ...reader, pull };
|
|
76
|
+
}),
|
|
77
|
+
writer: Effect.succeed(writer),
|
|
78
|
+
});
|
|
79
|
+
});
|
package/src/clientActivity.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, Stream } from "effect";
|
|
2
2
|
import type * as Atom from "effect/reactivity/Atom";
|
|
3
3
|
|
|
4
|
-
export type ConnectionStatus = "live" | "reconnecting";
|
|
4
|
+
export type ConnectionStatus = "live" | "reconnecting" | "waiting-for-connection";
|
|
5
5
|
|
|
6
6
|
export interface ClientActivityState {
|
|
7
7
|
readonly connectionStatus: ConnectionStatus;
|
|
@@ -10,8 +10,8 @@ type ActivityTracker = ReturnType<typeof makeActivity>;
|
|
|
10
10
|
export const clientActivities = new WeakMap<object, ActivityTracker>();
|
|
11
11
|
|
|
12
12
|
export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
13
|
-
const { activity, change, setConnection } = activityState();
|
|
14
|
-
const connection = clientConnection(factory, setConnection);
|
|
13
|
+
const { activity, change, hold, setConnection } = activityState();
|
|
14
|
+
const connection = clientConnection(factory, setConnection, activity);
|
|
15
15
|
function begin(key: "rpcCalls" | "liveInvalidations") {
|
|
16
16
|
change(key, 1);
|
|
17
17
|
return () => change(key, -1);
|
|
@@ -65,6 +65,14 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
65
65
|
return {
|
|
66
66
|
activity: Object.assign(activity, { connectionStatus: connection.status }),
|
|
67
67
|
hint: () => change("liveHints", 1),
|
|
68
|
+
hold: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
69
|
+
track(
|
|
70
|
+
Effect.acquireUseRelease(
|
|
71
|
+
Effect.sync(hold),
|
|
72
|
+
() => effect,
|
|
73
|
+
(end) => Effect.sync(end),
|
|
74
|
+
),
|
|
75
|
+
),
|
|
68
76
|
queued: () => begin("liveInvalidations"),
|
|
69
77
|
statuses: connection.statuses,
|
|
70
78
|
track,
|
|
@@ -12,6 +12,7 @@ import type { QueryOptions } from "#bind.ts";
|
|
|
12
12
|
import { clientInvalidationKeys, clientReadKeys } from "#clientKeys.ts";
|
|
13
13
|
import { clientActivities } from "#internal/clientActivity.ts";
|
|
14
14
|
import { commandPayload } from "#internal/commandPayload.ts";
|
|
15
|
+
import { waitForConnection } from "#internal/connectionReadiness.ts";
|
|
15
16
|
import { queryReactivity } from "#internal/queryReactivity.ts";
|
|
16
17
|
import type { CommandShape, QueryShape } from "#operation.ts";
|
|
17
18
|
import { shareQuerySignals } from "#querySignals.ts";
|
|
@@ -79,6 +80,7 @@ function boundCommand(service: ErasedClient, tag: string, command: CommandShape,
|
|
|
79
80
|
service,
|
|
80
81
|
service.use((client) =>
|
|
81
82
|
commandPayload(command, payload).pipe(
|
|
83
|
+
Effect.tap(() => waitForConnection(client)),
|
|
82
84
|
Effect.flatMap((validated) => client(tag, validated)),
|
|
83
85
|
Effect.tap((result) => invalidate(payload, result)),
|
|
84
86
|
),
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Option } from "effect";
|
|
2
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
import { RpcClient } from "effect/rpc";
|
|
4
|
+
import { ConnectionReadiness, connectionReadiness } from "./connectionReadiness.ts";
|
|
5
|
+
|
|
6
|
+
export const protocolConnections = new WeakMap<RpcClient.Protocol["Service"], Effect.Success<typeof connectionReadiness>>();
|
|
7
|
+
|
|
8
|
+
export const connectionProtocol = Atom.family((protocol: Layer.Layer<RpcClient.Protocol, never, ConnectionReadiness>) =>
|
|
9
|
+
Layer.unwrap(
|
|
10
|
+
Effect.gen(function* () {
|
|
11
|
+
const connection = yield* connectionReadiness;
|
|
12
|
+
const hooks = yield* Effect.serviceOption(RpcClient.ConnectionHooks);
|
|
13
|
+
return protocol.pipe(
|
|
14
|
+
Layer.provide(
|
|
15
|
+
Layer.merge(
|
|
16
|
+
Layer.succeed(ConnectionReadiness, connection),
|
|
17
|
+
Layer.succeed(RpcClient.ConnectionHooks, {
|
|
18
|
+
onConnect: connection.open.pipe(Effect.andThen(Option.isSome(hooks) ? hooks.value.onConnect : Effect.void)),
|
|
19
|
+
onDisconnect: connection.close.pipe(Effect.andThen(Option.isSome(hooks) ? hooks.value.onDisconnect : Effect.void)),
|
|
20
|
+
}),
|
|
21
|
+
),
|
|
22
|
+
),
|
|
23
|
+
Layer.tap((context) => Effect.sync(() => protocolConnections.set(Context.get(context, RpcClient.Protocol), connection))),
|
|
24
|
+
);
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Clock, Context, Effect, Latch } from "effect";
|
|
2
|
+
import { RpcClientError } from "effect/rpc/RpcClientError";
|
|
3
|
+
import { SocketOpenError } from "effect/socket/Socket";
|
|
4
|
+
|
|
5
|
+
export const connectionReadiness = Effect.gen(function* () {
|
|
6
|
+
const clock = yield* Clock.Clock;
|
|
7
|
+
const ready = Latch.makeUnsafe(true);
|
|
8
|
+
return {
|
|
9
|
+
close: Effect.asVoid(ready.close),
|
|
10
|
+
isReady: () => ready.isOpen(),
|
|
11
|
+
open: Effect.asVoid(ready.open),
|
|
12
|
+
wait: Effect.suspend(() =>
|
|
13
|
+
ready.await.pipe(
|
|
14
|
+
Effect.timeoutOrElse({
|
|
15
|
+
duration: "10 seconds",
|
|
16
|
+
orElse: () => Effect.fail(new RpcClientError({ reason: new SocketOpenError({ cause: "Waiting for a live connection", kind: "Timeout" }) })),
|
|
17
|
+
}),
|
|
18
|
+
Effect.provideService(Clock.Clock, clock),
|
|
19
|
+
),
|
|
20
|
+
),
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export class ConnectionReadiness extends Context.Service<ConnectionReadiness, Effect.Success<typeof connectionReadiness>>()(
|
|
25
|
+
"@shivaedev/contract/ConnectionReadiness",
|
|
26
|
+
) {}
|
|
27
|
+
|
|
28
|
+
export const commandConnections = new WeakMap<object, Effect.Effect<void, RpcClientError>>();
|
|
29
|
+
|
|
30
|
+
export function waitForConnection(client: object) {
|
|
31
|
+
return commandConnections.get(client) ?? Effect.void;
|
|
32
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ResumeOptions, ResumeSource, ResumeWindow } from "#resume.ts";
|
|
2
|
+
|
|
3
|
+
function whileVisible(target: ResumeWindow, type: "online" | "visibilitychange" | "pageshow"): ResumeSource {
|
|
4
|
+
return (resume) => {
|
|
5
|
+
function visible() {
|
|
6
|
+
if (target.document.visibilityState === "visible") {
|
|
7
|
+
resume();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
target.addEventListener(type, visible);
|
|
11
|
+
return () => target.removeEventListener(type, visible);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function resumeSources({ window, native }: ResumeOptions): readonly ResumeSource[] {
|
|
16
|
+
const browser =
|
|
17
|
+
window === undefined ? [] : [whileVisible(window, "visibilitychange"), whileVisible(window, "online"), whileVisible(window, "pageshow")];
|
|
18
|
+
return native === undefined ? browser : [...browser, native];
|
|
19
|
+
}
|