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

DesktopIpc.ts

219 lines5.7 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Schema from "effect/Schema";
4import * as Scope from "effect/Scope";
5​
6export interface DesktopIpcInvokeEvent {}
7​
8export interface DesktopIpcSyncEvent {
9 returnValue: unknown;
11​
12export type DesktopIpcHandleListener = (
13 event: DesktopIpcInvokeEvent,
14 raw: unknown,
15) => unknown | Promise<unknown>;
16​
17export type DesktopIpcSyncListener = (event: DesktopIpcSyncEvent) => void;
18​
19export interface DesktopIpcMain {
20 removeHandler(channel: string): void;
21 handle(channel: string, listener: DesktopIpcHandleListener): void;
22 removeAllListeners(channel: string): void;
23 on(channel: string, listener: DesktopIpcSyncListener): void;
25​
26export interface DesktopIpcMethod<E, R> {
27 readonly channel: string;
28 readonly handler: (raw: unknown) => Effect.Effect<unknown, E, R>;
30​
31export interface DesktopSyncIpcMethod<E, R> {
32 readonly channel: string;
33 readonly handler: () => Effect.Effect<unknown, E, R>;
35​
36export interface DesktopIpcShape {
37 readonly handle: <E, R>(
38 input: DesktopIpcMethod<E, R>,
39 ) => Effect.Effect<void, never, R | Scope.Scope>;
40 readonly handleSync: <E, R>(
41 input: DesktopSyncIpcMethod<E, R>,
42 ) => Effect.Effect<void, never, R | Scope.Scope>;
44​
45export class DesktopIpc extends Context.Service<DesktopIpc, DesktopIpcShape>()("t3/desktop/Ipc") {}
46​
47export const make = (ipcMain: DesktopIpcMain): DesktopIpcShape =>
48 DesktopIpc.of({
49 handle: Effect.fn("desktop.ipc.registerInvoke")(function* <E, R>({
50 channel,
51 handler,
52 }: DesktopIpcMethod<E, R>) {
53 yield* Effect.annotateCurrentSpan({ channel });
54 const context = yield* Effect.context<R>();
55 const runPromise = Effect.runPromiseWith(context);
56​
57 yield* Effect.acquireRelease(
58 Effect.sync(() => {
59 ipcMain.removeHandler(channel);
60 ipcMain.handle(channel, (_event, raw) =>
61 runPromise(
62 Effect.gen(function* () {
63 yield* Effect.annotateCurrentSpan({ channel });
64 return yield* handler(raw);
65 }).pipe(Effect.annotateLogs({ channel }), Effect.withSpan("desktop.ipc.invoke")),
66 ),
67 );
68 }),
69 () => Effect.sync(() => ipcMain.removeHandler(channel)),
70 );
71 }),
72​
73 handleSync: Effect.fn("desktop.ipc.registerSync")(function* <E, R>({
74 channel,
75 handler,
76 }: DesktopSyncIpcMethod<E, R>) {
77 yield* Effect.annotateCurrentSpan({ channel });
78 const context = yield* Effect.context<R>();
79 const runSync = Effect.runSyncWith(context);
80​
81 yield* Effect.acquireRelease(
82 Effect.sync(() => {
83 ipcMain.removeAllListeners(channel);
84 ipcMain.on(channel, (event) => {
85 event.returnValue = runSync(
86 Effect.gen(function* () {
87 yield* Effect.annotateCurrentSpan({ channel });
88 return yield* handler();
89 }).pipe(Effect.annotateLogs({ channel }), Effect.withSpan("desktop.ipc.invokeSync")),
90 );
91 });
92 }),
93 () => Effect.sync(() => ipcMain.removeAllListeners(channel)),
94 );
95 }),
96 });
97​
98/**
99 * Convenience helpers for creating IPC methods
100 */
101​
102export interface DesktopIpcMethodRegistration<
103 Payload,
104 EncodedPayload,
105 Result,
106 EncodedResult,
107 E,
108 R,
109 PayloadDecodingServices = never,
110 PayloadEncodingServices = never,
111 ResultDecodingServices = never,
112 ResultEncodingServices = never,
113> {
114 readonly channel: string;
115 readonly payload: Schema.Codec<
116 Payload,
117 EncodedPayload,
118 PayloadDecodingServices,
119 PayloadEncodingServices
120 >;
121 readonly result: Schema.Codec<
122 Result,
123 EncodedResult,
124 ResultDecodingServices,
125 ResultEncodingServices
126 >;
127 readonly handler: (input: Payload) => Effect.Effect<Result, E, R>;
129​
130export const makeIpcMethod = <
131 Payload,
132 EncodedPayload,
133 Result,
134 EncodedResult,
135 E,
136 R,
137 PayloadDecodingServices = never,
138 PayloadEncodingServices = never,
139 ResultDecodingServices = never,
140 ResultEncodingServices = never,
141>(
142 method: DesktopIpcMethodRegistration<
143 Payload,
144 EncodedPayload,
145 Result,
146 EncodedResult,
147 E,
148 R,
149 PayloadDecodingServices,
150 PayloadEncodingServices,
151 ResultDecodingServices,
152 ResultEncodingServices
153 >,
154): DesktopIpcMethod<
155 E | Schema.SchemaError,
156 R | PayloadDecodingServices | ResultEncodingServices
157> => {
158 const decode = Schema.decodeUnknownEffect(method.payload);
159 const encode = Schema.encodeUnknownEffect(method.result);
160​
161 return {
162 channel: method.channel,
163 handler: (raw) =>
164 decode(raw).pipe(
165 Effect.flatMap(method.handler),
166 Effect.flatMap(encode),
167 Effect.withSpan("desktop.ipc.method", { attributes: { channel: method.channel } }),
168 ),
169 };
170};
171​
172export interface DesktopSyncIpcMethodRegistration<
173 Result,
174 EncodedResult,
175 E,
176 R,
177 ResultDecodingServices = never,
178 ResultEncodingServices = never,
179> {
180 readonly channel: string;
181 readonly result: Schema.Codec<
182 Result,
183 EncodedResult,
184 ResultDecodingServices,
185 ResultEncodingServices
186 >;
187 readonly handler: () => Effect.Effect<Result, E, R>;
189​
190export const makeSyncIpcMethod = <
191 Result,
192 EncodedResult,
193 E,
194 R,
195 ResultDecodingServices = never,
196 ResultEncodingServices = never,
197>(
198 method: DesktopSyncIpcMethodRegistration<
199 Result,
200 EncodedResult,
201 E,
202 R,
203 ResultDecodingServices,
204 ResultEncodingServices
205 >,
206): DesktopSyncIpcMethod<E | Schema.SchemaError, R | ResultEncodingServices> => {
207 const encode = Schema.encodeUnknownEffect(method.result);
208​
209 return {
210 channel: method.channel,
211 handler: () =>
212 method
213 .handler()
214 .pipe(
215 Effect.flatMap(encode),
216 Effect.withSpan("desktop.ipc.method", { attributes: { channel: method.channel } }),
217 ),
218 };
219};