@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,12 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type Context, 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 { type Rpc, RpcClient, RpcGroup
|
|
5
|
-
import
|
|
4
|
+
import { type Rpc, RpcClient, RpcGroup } from "effect/rpc";
|
|
5
|
+
import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
6
6
|
import { Activity } from "#clientActivity.ts";
|
|
7
7
|
import type { ClientOptions } from "#clientOptions.ts";
|
|
8
|
-
import {
|
|
8
|
+
import type { ResumeWindow } from "#resume.ts";
|
|
9
9
|
import { clientActivities, makeActivity } from "./clientActivity.ts";
|
|
10
|
+
import { connectionProtocol, protocolConnections } from "./connectionProtocol.ts";
|
|
11
|
+
import { commandConnections } from "./connectionReadiness.ts";
|
|
12
|
+
import { socketProtocol } from "./socketProtocol.ts";
|
|
13
|
+
|
|
14
|
+
declare const window: ResumeWindow | undefined;
|
|
10
15
|
|
|
11
16
|
export interface RpcSource {
|
|
12
17
|
readonly annotations: Context.Context<never>;
|
|
@@ -21,15 +26,27 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
21
26
|
bearerToken: options.bearerToken,
|
|
22
27
|
headers: options.headers,
|
|
23
28
|
identity,
|
|
29
|
+
resume: options.resume,
|
|
30
|
+
resumeWindow: options.resumeWindow ?? (typeof window === "undefined" ? undefined : window),
|
|
24
31
|
url: options.url.replace(/^http/u, "ws"),
|
|
25
32
|
webSocketConstructor: options.webSocketConstructor,
|
|
26
33
|
});
|
|
27
34
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
function configuredProtocol() {
|
|
36
|
+
const custom = options.protocol;
|
|
37
|
+
if (custom === undefined) {
|
|
38
|
+
return sharedProtocol;
|
|
39
|
+
}
|
|
40
|
+
if (typeof custom === "function") {
|
|
41
|
+
return Atom.family((identity: string | undefined) => connectionProtocol(custom(identity)));
|
|
42
|
+
}
|
|
43
|
+
const layer = connectionProtocol(custom);
|
|
44
|
+
return () => layer;
|
|
45
|
+
}
|
|
46
|
+
const protocolFor = configuredProtocol();
|
|
47
|
+
const protocol = protocolFor(undefined);
|
|
48
|
+
function observeClient(client: RpcClient.RpcClient.Flat<Rpc.Any, RpcClientError>, wait: Effect.Effect<void, RpcClientError>) {
|
|
49
|
+
const observed = new Proxy(client, {
|
|
33
50
|
apply(target, receiver, args) {
|
|
34
51
|
const result = Reflect.apply(target, receiver, args);
|
|
35
52
|
if (String(args[0]).endsWith(".$changes") && Stream.isStream(result)) {
|
|
@@ -41,12 +58,20 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
41
58
|
return Effect.isEffect(result) && !args[2]?.asQueue ? activity.track(result) : result;
|
|
42
59
|
},
|
|
43
60
|
});
|
|
61
|
+
commandConnections.set(observed, wait);
|
|
62
|
+
return observed;
|
|
44
63
|
}
|
|
45
|
-
const make =
|
|
64
|
+
const make = Effect.gen(function* () {
|
|
65
|
+
const transport = yield* RpcClient.Protocol;
|
|
66
|
+
const connection = protocolConnections.get(transport);
|
|
67
|
+
const wait = connection === undefined ? Effect.void : Effect.suspend(() => (connection.isReady() ? Effect.void : activity.hold(connection.wait)));
|
|
68
|
+
const native = yield* RpcClient.make<Rpc.Any, true>(group, { flatten: true });
|
|
69
|
+
return observeClient(native, wait);
|
|
70
|
+
});
|
|
46
71
|
const Client = AtomRpc.Service<unknown>()(`${name}/Client`, {
|
|
47
72
|
group,
|
|
48
73
|
makeEffect: make,
|
|
49
|
-
protocol: typeof
|
|
74
|
+
protocol: typeof options.protocol === "object" ? protocol : (get) => protocolFor(get(socketIdentity)),
|
|
50
75
|
runtime: options.runtime,
|
|
51
76
|
});
|
|
52
77
|
clientActivities.set(Client, activity);
|
|
@@ -63,34 +88,3 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
63
88
|
}
|
|
64
89
|
|
|
65
90
|
export const socketIdentity = Atom.make<string | undefined>(undefined);
|
|
66
|
-
|
|
67
|
-
const socketMemoMap = Layer.makeMemoMapUnsafe();
|
|
68
|
-
const socketProtocol = Atom.family(
|
|
69
|
-
(options: {
|
|
70
|
-
readonly bearerToken: ClientOptions["bearerToken"];
|
|
71
|
-
readonly url: string;
|
|
72
|
-
readonly headers: ClientOptions["headers"];
|
|
73
|
-
readonly identity: string | undefined;
|
|
74
|
-
readonly webSocketConstructor: ClientOptions["webSocketConstructor"];
|
|
75
|
-
}) => {
|
|
76
|
-
let url = options.url;
|
|
77
|
-
if (options.bearerToken !== undefined) {
|
|
78
|
-
const address = new URL(url);
|
|
79
|
-
address.searchParams.set("auth", "frame");
|
|
80
|
-
url = address.toString();
|
|
81
|
-
}
|
|
82
|
-
const raw = Socket.layerWebSocket(url, { headers: options.headers });
|
|
83
|
-
const socket = (
|
|
84
|
-
options.bearerToken === undefined ? raw : Layer.effect(Socket.Socket, authenticateSocket(options.bearerToken)).pipe(Layer.provide(raw))
|
|
85
|
-
).pipe(Layer.provide(options.webSocketConstructor ?? Socket.layerWebSocketConstructorGlobal));
|
|
86
|
-
const protocol = RpcClient.layerProtocolSocket({
|
|
87
|
-
retryPolicy: Schedule.jittered(Schedule.min([Schedule.exponential(500, 1.5), Schedule.spaced(5000)])),
|
|
88
|
-
}).pipe(
|
|
89
|
-
Layer.provide(socket),
|
|
90
|
-
Layer.provide(RpcSerialization.layerNdjson),
|
|
91
|
-
// Socket deadlines follow network time even when application time is simulated.
|
|
92
|
-
Layer.provide(Layer.succeed(Clock.Clock, Clock.Clock.defaultValue())),
|
|
93
|
-
);
|
|
94
|
-
return Layer.effectContext(Effect.flatMap(Effect.scope, (scope) => Layer.buildWithMemoMap(protocol, socketMemoMap, scope)));
|
|
95
|
-
},
|
|
96
|
-
);
|
|
@@ -0,0 +1,73 @@
|
|
|
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 type { ClientOptions } from "#clientOptions.ts";
|
|
7
|
+
import type { ResumeWindow } from "#resume.ts";
|
|
8
|
+
import { SocketClock } from "#SocketClock.ts";
|
|
9
|
+
import { authenticateSocket } from "#socketAuthentication.ts";
|
|
10
|
+
import { connectionProtocol } from "./connectionProtocol.ts";
|
|
11
|
+
|
|
12
|
+
interface SocketOptions {
|
|
13
|
+
readonly bearerToken: ClientOptions["bearerToken"];
|
|
14
|
+
readonly headers: ClientOptions["headers"];
|
|
15
|
+
readonly identity: string | undefined;
|
|
16
|
+
readonly resume: ClientOptions["resume"];
|
|
17
|
+
readonly resumeWindow: ClientOptions["resumeWindow"];
|
|
18
|
+
readonly url: string;
|
|
19
|
+
readonly webSocketConstructor: ClientOptions["webSocketConstructor"];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const socketMemoMaps = new WeakMap<Clock.Clock, Layer.MemoMap>();
|
|
23
|
+
|
|
24
|
+
const windows = new WeakMap<ResumeWindow, { readonly target: ResumeWindow }>();
|
|
25
|
+
|
|
26
|
+
export function socketProtocol(options: SocketOptions) {
|
|
27
|
+
const target = options.resumeWindow;
|
|
28
|
+
let reference = target === undefined ? undefined : windows.get(target);
|
|
29
|
+
if (target !== undefined && reference === undefined) {
|
|
30
|
+
reference = Equal.byReference({ target });
|
|
31
|
+
windows.set(target, reference);
|
|
32
|
+
}
|
|
33
|
+
return protocols({ ...options, resumeWindow: reference });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const protocols = Atom.family(
|
|
37
|
+
(options: Omit<SocketOptions, "resumeWindow"> & { readonly resumeWindow: { readonly target: ResumeWindow } | undefined }) => {
|
|
38
|
+
let url = options.url;
|
|
39
|
+
if (options.bearerToken !== undefined) {
|
|
40
|
+
const address = new URL(url);
|
|
41
|
+
address.searchParams.set("auth", "frame");
|
|
42
|
+
url = address.toString();
|
|
43
|
+
}
|
|
44
|
+
const raw = Socket.layerWebSocket(url, { headers: options.headers });
|
|
45
|
+
const authenticated = (
|
|
46
|
+
options.bearerToken === undefined ? raw : Layer.effect(Socket.Socket, authenticateSocket(options.bearerToken)).pipe(Layer.provide(raw))
|
|
47
|
+
).pipe(Layer.provide(options.webSocketConstructor ?? Socket.layerWebSocketConstructorGlobal));
|
|
48
|
+
const socket = Layer.effect(
|
|
49
|
+
Socket.Socket,
|
|
50
|
+
resumeSocket({
|
|
51
|
+
...(options.resumeWindow === undefined ? {} : { window: options.resumeWindow.target }),
|
|
52
|
+
...(options.resume === undefined ? {} : { native: options.resume }),
|
|
53
|
+
}),
|
|
54
|
+
).pipe(Layer.provide(authenticated), Layer.provide(RpcSerialization.layerNdjson));
|
|
55
|
+
const managed = connectionProtocol(
|
|
56
|
+
RpcClient.layerProtocolSocket({
|
|
57
|
+
retryPolicy: Schedule.jittered(Schedule.min([Schedule.exponential(500, 1.5), Schedule.spaced(5000)])),
|
|
58
|
+
}).pipe(Layer.provide(socket), Layer.provide(RpcSerialization.layerNdjson)),
|
|
59
|
+
);
|
|
60
|
+
return Layer.effectContext(
|
|
61
|
+
Effect.gen(function* () {
|
|
62
|
+
const clock = yield* SocketClock;
|
|
63
|
+
let memo = socketMemoMaps.get(clock);
|
|
64
|
+
if (memo === undefined) {
|
|
65
|
+
memo = Layer.makeMemoMapUnsafe();
|
|
66
|
+
socketMemoMaps.set(clock, memo);
|
|
67
|
+
}
|
|
68
|
+
const protocol = managed.pipe(Layer.provide(Layer.succeed(Clock.Clock, clock)));
|
|
69
|
+
return yield* Layer.buildWithMemoMap(protocol, memo, yield* Effect.scope);
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
},
|
|
73
|
+
);
|
package/src/resume.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as Atom from "effect/reactivity/Atom";
|
|
2
|
+
import { resumeSources } from "#internal/resumeSources.ts";
|
|
2
3
|
|
|
3
4
|
export type ResumeSource = (resume: () => void) => () => void;
|
|
4
5
|
|
|
5
|
-
type ResumeEvent = "online" | "visibilitychange";
|
|
6
|
+
type ResumeEvent = "online" | "visibilitychange" | "pageshow";
|
|
6
7
|
|
|
7
8
|
export interface ResumeWindow {
|
|
8
9
|
readonly addEventListener: (type: ResumeEvent, listener: () => void) => void;
|
|
@@ -15,21 +16,8 @@ export interface ResumeOptions {
|
|
|
15
16
|
readonly window?: ResumeWindow;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
function visible() {
|
|
21
|
-
if (target.document.visibilityState === "visible") {
|
|
22
|
-
resume();
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
target.addEventListener(type, visible);
|
|
26
|
-
return () => target.removeEventListener(type, visible);
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function resumeSignal({ window, native }: ResumeOptions): Atom.Atom<number> {
|
|
31
|
-
const browser = window === undefined ? [] : [whileVisible(window, "visibilitychange"), whileVisible(window, "online")];
|
|
32
|
-
const sources = native === undefined ? browser : [...browser, native];
|
|
19
|
+
export function resumeSignal(options: ResumeOptions): Atom.Atom<number> {
|
|
20
|
+
const sources = resumeSources(options);
|
|
33
21
|
return Atom.readable((get) => {
|
|
34
22
|
let count = 0;
|
|
35
23
|
function resume() {
|