Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

effect@4.0.0-beta.59.patch

277 lines12.5 KB

1diff --git a/dist/unstable/rpc/RpcClient.d.ts b/dist/unstable/rpc/RpcClient.d.ts
2index 7c5970042df67a25b4145df017960b3e6febed9c..7af3374632d53ad59f24e80835aa6741853b1f2b 100644
3--- a/dist/unstable/rpc/RpcClient.d.ts
4+++ b/dist/unstable/rpc/RpcClient.d.ts
5@@ -2,6 +2,7 @@ import * as Cause from "../../Cause.ts";
6 import * as Context from "../../Context.ts";
7 import type * as Duration from "../../Duration.ts";
8 import * as Effect from "../../Effect.ts";
9+import * as Exit from "../../Exit.ts";
10 import * as Layer from "../../Layer.ts";
11 import * as Queue from "../../Queue.ts";
12 import * as Schedule from "../../Schedule.ts";
13@@ -192,9 +193,40 @@ export declare const layerProtocolWorker: (options: {
14 readonly targetUtilization?: number | undefined;
15 readonly timeToLive: Duration.Input;
16 }) => Layer.Layer<Protocol, WorkerError, Worker.WorkerPlatform | Worker.Spawner>;
17+declare const RequestHooks_base: Context.ServiceClass<RequestHooks, "effect/rpc/RpcClient/RequestHooks", {
18+ readonly onRequestStart?: ((info: {
19+ readonly id: RequestId;
20+ readonly tag: string;
21+ readonly stream: boolean;
22+ }) => Effect.Effect<void>) | undefined;
23+ readonly onRequestChunk?: ((info: {
24+ readonly id: RequestId;
25+ readonly tag: string;
26+ readonly chunkCount: number;
27+ }) => Effect.Effect<void>) | undefined;
28+ readonly onRequestExit?: ((info: {
29+ readonly id: RequestId;
30+ readonly tag: string;
31+ readonly stream: boolean;
32+ readonly exit: Exit.Exit<unknown, unknown>;
33+ }) => Effect.Effect<void>) | undefined;
34+ readonly onRequestInterrupt?: ((info: {
35+ readonly id: RequestId;
36+ readonly tag?: string | undefined;
37+ }) => Effect.Effect<void>) | undefined;
38+}>;
39+/**
40+ * @since 4.0.0
41+ * @category RequestHooks
42+ */
43+export declare class RequestHooks extends RequestHooks_base {
44+}
45 declare const ConnectionHooks_base: Context.ServiceClass<ConnectionHooks, "effect/rpc/RpcClient/ConnectionHooks", {
46 readonly onConnect: Effect.Effect<void>;
47 readonly onDisconnect: Effect.Effect<void>;
48+ readonly onPing?: Effect.Effect<void> | undefined;
49+ readonly onPong?: Effect.Effect<void> | undefined;
50+ readonly onPingTimeout?: Effect.Effect<void> | undefined;
51 }>;
52 /**
53 * @since 4.0.0
54diff --git a/dist/unstable/rpc/RpcClient.js b/dist/unstable/rpc/RpcClient.js
55index 951dfc2d667a9136cfa714dcd3195e6575917fde..599c0345873bedd72bc93afe80eab0c676319421 100644
56--- a/dist/unstable/rpc/RpcClient.js
57+++ b/dist/unstable/rpc/RpcClient.js
58@@ -42,6 +42,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
59 const services = yield* Effect.context();
60 const scope = Context.get(services, Scope.Scope);
61 const entries = new Map();
62+ const hooks = yield* Effect.serviceOption(RequestHooks);
63 let isShutdown = false;
64 yield* Scope.addFinalizer(scope, Effect.withFiber(parent => {
65 isShutdown = true;
66@@ -79,6 +80,11 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
67 return Effect.interrupt;
68 }
69 const id = generateRequestId();
70+ const onStart = Option.isSome(hooks) && hooks.value.onRequestStart ? hooks.value.onRequestStart({
71+ id,
72+ tag: rpc._tag,
73+ stream: false
74+ }) : Effect.void;
75 const send = middleware(message => options.onFromClient({
76 message,
77 context,
78@@ -96,7 +102,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
79 headers: Headers.merge(parentFiber.getRef(CurrentHeaders), headers)
80 });
81 if (discard) {
82- return send;
83+ return onStart.pipe(Effect.andThen(send));
84 }
85 let fiber;
86 return Effect.onInterrupt(Effect.callback(resume => {
87@@ -114,7 +120,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
88 }
89 };
90 entries.set(id, entry);
91- fiber = send.pipe(span ? Effect.withParentSpan(span, {
92+ fiber = onStart.pipe(Effect.andThen(send), span ? Effect.withParentSpan(span, {
93 captureStackTrace: false
94 }) : identity, Effect.runForkWith(parentFiber.context));
95 fiber.addObserver(exit => {
96@@ -123,8 +129,9 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
97 }
98 });
99 }), interruptors => {
100+ const entry = entries.get(id);
101 entries.delete(id);
102- return Effect.andThen(Fiber.interrupt(fiber), sendInterrupt(id, Array.from(interruptors), context));
103+ return Effect.andThen(Fiber.interrupt(fiber), sendInterrupt(id, Array.from(interruptors), context, entry?.rpc._tag));
104 });
105 });
106 const onStreamRequest = Effect.fnUntraced(function* (rpc, middleware, payload, headers, streamBufferSize, context) {
107@@ -136,11 +143,17 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
108 });
109 const fiber = Fiber.getCurrent();
110 const id = generateRequestId();
111+ const onStart = Option.isSome(hooks) && hooks.value.onRequestStart ? hooks.value.onRequestStart({
112+ id,
113+ tag: rpc._tag,
114+ stream: true
115+ }) : Effect.void;
116 const scope = Context.getUnsafe(fiber.context, Scope.Scope);
117 yield* Scope.addFinalizerExit(scope, exit => {
118- if (!entries.has(id)) return Effect.void;
119+ const entry = entries.get(id);
120+ if (!entry) return Effect.void;
121 entries.delete(id);
122- return sendInterrupt(id, Exit.isFailure(exit) ? Array.from(Cause.interruptors(exit.cause)) : [], context);
123+ return sendInterrupt(id, Exit.isFailure(exit) ? Array.from(Cause.interruptors(exit.cause)) : [], context, entry.rpc._tag);
124 });
125 const queue = yield* Queue.bounded(streamBufferSize);
126 entries.set(id, {
127@@ -148,9 +161,10 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
128 rpc,
129 queue,
130 scope,
131- context
132+ context,
133+ chunkCount: 0
134 });
135- yield* middleware(message => options.onFromClient({
136+ yield* onStart.pipe(Effect.andThen(middleware(message => options.onFromClient({
137 message,
138 context,
139 discard: false
140@@ -165,7 +179,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
141 sampled: span.sampled
142 } : {}),
143 headers: Headers.merge(fiber.getRef(CurrentHeaders), headers)
144- }).pipe(span ? Effect.withParentSpan(span, {
145+ }))).pipe(span ? Effect.withParentSpan(span, {
146 captureStackTrace: false
147 }) : identity, Effect.catchCause(error => Queue.failCause(queue, error)), Effect.interruptible, Effect.forkIn(scope, {
148 startImmediately: true
149@@ -195,7 +209,10 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
150 });
151 };
152 };
153- const sendInterrupt = (requestId, interruptors, context) => Effect.callback(resume => {
154+ const sendInterrupt = (requestId, interruptors, context, tag) => (Option.isSome(hooks) && hooks.value.onRequestInterrupt ? hooks.value.onRequestInterrupt({
155+ id: requestId,
156+ tag
157+ }) : Effect.void).pipe(Effect.andThen(Effect.callback(resume => {
158 const parentFiber = Fiber.getCurrent();
159 const fiber = options.onFromClient({
160 message: {
161@@ -209,7 +226,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
162 fiber.addObserver(() => {
163 resume(Effect.void);
164 });
165- });
166+ })));
167 const write = message => {
168 switch (message._tag) {
169 case "Chunk":
170@@ -217,7 +234,13 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
171 const requestId = message.requestId;
172 const entry = entries.get(requestId);
173 if (!entry || entry._tag !== "Queue") return Effect.void;
174- return Queue.offerAll(entry.queue, message.values).pipe(supportsAck ? Effect.flatMap(() => options.onFromClient({
175+ const chunkCount = entry.chunkCount += message.values.length;
176+ const onChunk = Option.isSome(hooks) && hooks.value.onRequestChunk ? hooks.value.onRequestChunk({
177+ id: requestId,
178+ tag: entry.rpc._tag,
179+ chunkCount
180+ }) : Effect.void;
181+ return Queue.offerAll(entry.queue, message.values).pipe(Effect.andThen(onChunk), supportsAck ? Effect.flatMap(() => options.onFromClient({
182 message: {
183 _tag: "Ack",
184 requestId: message.requestId
185@@ -232,11 +255,18 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
186 const entry = entries.get(requestId);
187 if (!entry) return Effect.void;
188 entries.delete(requestId);
189+ const onExit = Option.isSome(hooks) && hooks.value.onRequestExit ? hooks.value.onRequestExit({
190+ id: requestId,
191+ tag: entry.rpc._tag,
192+ stream: entry._tag === "Queue",
193+ exit: message.exit
194+ }) : Effect.void;
195 if (entry._tag === "Effect") {
196- entry.resume(message.exit);
197- return Effect.void;
198+ return onExit.pipe(Effect.andThen(Effect.sync(() => {
199+ entry.resume(message.exit);
200+ })));
201 }
202- return message.exit._tag === "Success" ? Queue.end(entry.queue) : Queue.failCause(entry.queue, message.exit.cause);
203+ return onExit.pipe(Effect.andThen(message.exit._tag === "Success" ? Queue.end(entry.queue) : Queue.failCause(entry.queue, message.exit.cause)));
204 }
205 case "Defect":
206 {
207@@ -530,7 +560,7 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
208 const requestClientMap = new Map();
209 const write = yield* socket.writer;
210 let parser = serialization.makeUnsafe();
211- const pinger = yield* makePinger(write(parser.encode(constPing)));
212+ const pinger = yield* makePinger(write(parser.encode(constPing)), Option.isSome(hooks) ? hooks.value : undefined);
213 let currentError;
214 const onOpen = Effect.suspend(() => {
215 currentError = undefined;
216@@ -550,8 +580,7 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
217 body: () => {
218 const response = responses[i++];
219 if (response._tag === "Pong") {
220- pinger.onPong();
221- return Effect.void;
222+ return pinger.onPong;
223 }
224 if ("requestId" in response) {
225 const clientId = requestClientMap.get(response.requestId);
226@@ -579,12 +608,12 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
227 }
228 }, {
229 onOpen
230- }).pipe(Effect.raceFirst(Effect.flatMap(pinger.timeout, () => Effect.fail(new Socket.SocketError({
231+ }).pipe(Effect.raceFirst(Effect.flatMap(pinger.timeout, () => (Option.isSome(hooks) && hooks.value.onPingTimeout ? hooks.value.onPingTimeout : Effect.void).pipe(Effect.andThen(Effect.fail(new Socket.SocketError({
232 reason: new Socket.SocketOpenError({
233 kind: "Timeout",
234 cause: new Error("ping timeout")
235 })
236- })))));
237+ })))))));
238 }).pipe(Effect.flatMap(() => Effect.fail(new Socket.SocketError({
239 reason: new Socket.SocketCloseError({
240 code: 1000
241@@ -626,20 +655,20 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
242 };
243 }));
244 const defaultRetryPolicy = /*#__PURE__*/Schedule.exponential(500, 1.5).pipe(/*#__PURE__*/Schedule.either(/*#__PURE__*/Schedule.spaced(5000)));
245-const makePinger = /*#__PURE__*/Effect.fnUntraced(function* (writePing) {
246+const makePinger = /*#__PURE__*/Effect.fnUntraced(function* (writePing, hooks) {
247 let recievedPong = true;
248 const latch = Latch.makeUnsafe();
249 const reset = () => {
250 recievedPong = true;
251 latch.closeUnsafe();
252 };
253- const onPong = () => {
254+ const onPong = Effect.sync(() => {
255 recievedPong = true;
256- };
257+ }).pipe(Effect.andThen(hooks?.onPong ?? Effect.void));
258 yield* Effect.suspend(() => {
259 if (!recievedPong) return latch.open;
260 recievedPong = false;
261- return writePing;
262+ return (hooks?.onPing ?? Effect.void).pipe(Effect.andThen(writePing));
263 }).pipe(Effect.delay("5 seconds"), Effect.ignore, Effect.forever, Effect.interruptible, Effect.forkScoped);
264 return {
265 timeout: latch.await,
266@@ -773,6 +802,11 @@ export const makeProtocolWorker = options => Protocol.make(Effect.fnUntraced(fun
267 * @category protocol
268 */
269 export const layerProtocolWorker = /*#__PURE__*/flow(makeProtocolWorker, /*#__PURE__*/Layer.effect(Protocol));
270+/**
271+ * @since 4.0.0
272+ * @category RequestHooks
273+ */
274+export class RequestHooks extends /*#__PURE__*/Context.Service()("effect/rpc/RpcClient/RequestHooks") {}
275 /**
276 * @since 4.0.0
277 * @category ConnectionHooks