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

DesktopIpc.ts

221 lines5.8 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>()(
46 "@t3tools/desktop/ipc/DesktopIpc",
47) {}
48​
49export const make = (ipcMain: DesktopIpcMain): DesktopIpcShape =>
50 DesktopIpc.of({
51 handle: Effect.fn("desktop.ipc.registerInvoke")(function* <E, R>({
52 channel,
53 handler,
54 }: DesktopIpcMethod<E, R>) {
55 yield* Effect.annotateCurrentSpan({ channel });
56 const context = yield* Effect.context<R>();
57 const runPromise = Effect.runPromiseWith(context);
58​
59 yield* Effect.acquireRelease(
60 Effect.sync(() => {
61 ipcMain.removeHandler(channel);
62 ipcMain.handle(channel, (_event, raw) =>
63 runPromise(
64 Effect.gen(function* () {
65 yield* Effect.annotateCurrentSpan({ channel });
66 return yield* handler(raw);
67 }).pipe(Effect.annotateLogs({ channel }), Effect.withSpan("desktop.ipc.invoke")),
68 ),
69 );
70 }),
71 () => Effect.sync(() => ipcMain.removeHandler(channel)),
72 );
73 }),
74​
75 handleSync: Effect.fn("desktop.ipc.registerSync")(function* <E, R>({
76 channel,
77 handler,
78 }: DesktopSyncIpcMethod<E, R>) {
79 yield* Effect.annotateCurrentSpan({ channel });
80 const context = yield* Effect.context<R>();
81 const runSync = Effect.runSyncWith(context);
82​
83 yield* Effect.acquireRelease(
84 Effect.sync(() => {
85 ipcMain.removeAllListeners(channel);
86 ipcMain.on(channel, (event) => {
87 event.returnValue = runSync(
88 Effect.gen(function* () {
89 yield* Effect.annotateCurrentSpan({ channel });
90 return yield* handler();
91 }).pipe(Effect.annotateLogs({ channel }), Effect.withSpan("desktop.ipc.invokeSync")),
92 );
93 });
94 }),
95 () => Effect.sync(() => ipcMain.removeAllListeners(channel)),
96 );
97 }),
98 });
99​
100/**
101 * Convenience helpers for creating IPC methods
102 */
103​
104export interface DesktopIpcMethodRegistration<
105 Payload,
106 EncodedPayload,
107 Result,
108 EncodedResult,
109 E,
110 R,
111 PayloadDecodingServices = never,
112 PayloadEncodingServices = never,
113 ResultDecodingServices = never,
114 ResultEncodingServices = never,
115> {
116 readonly channel: string;
117 readonly payload: Schema.Codec<
118 Payload,
119 EncodedPayload,
120 PayloadDecodingServices,
121 PayloadEncodingServices
122 >;
123 readonly result: Schema.Codec<
124 Result,
125 EncodedResult,
126 ResultDecodingServices,
127 ResultEncodingServices
128 >;
129 readonly handler: (input: Payload) => Effect.Effect<Result, E, R>;
131​
132export const makeIpcMethod = <
133 Payload,
134 EncodedPayload,
135 Result,
136 EncodedResult,
137 E,
138 R,
139 PayloadDecodingServices = never,
140 PayloadEncodingServices = never,
141 ResultDecodingServices = never,
142 ResultEncodingServices = never,
143>(
144 method: DesktopIpcMethodRegistration<
145 Payload,
146 EncodedPayload,
147 Result,
148 EncodedResult,
149 E,
150 R,
151 PayloadDecodingServices,
152 PayloadEncodingServices,
153 ResultDecodingServices,
154 ResultEncodingServices
155 >,
156): DesktopIpcMethod<
157 E | Schema.SchemaError,
158 R | PayloadDecodingServices | ResultEncodingServices
159> => {
160 const decode = Schema.decodeUnknownEffect(method.payload);
161 const encode = Schema.encodeUnknownEffect(method.result);
162​
163 return {
164 channel: method.channel,
165 handler: (raw) =>
166 decode(raw).pipe(
167 Effect.flatMap(method.handler),
168 Effect.flatMap(encode),
169 Effect.withSpan("desktop.ipc.method", { attributes: { channel: method.channel } }),
170 ),
171 };
172};
173​
174export interface DesktopSyncIpcMethodRegistration<
175 Result,
176 EncodedResult,
177 E,
178 R,
179 ResultDecodingServices = never,
180 ResultEncodingServices = never,
181> {
182 readonly channel: string;
183 readonly result: Schema.Codec<
184 Result,
185 EncodedResult,
186 ResultDecodingServices,
187 ResultEncodingServices
188 >;
189 readonly handler: () => Effect.Effect<Result, E, R>;
191​
192export const makeSyncIpcMethod = <
193 Result,
194 EncodedResult,
195 E,
196 R,
197 ResultDecodingServices = never,
198 ResultEncodingServices = never,
199>(
200 method: DesktopSyncIpcMethodRegistration<
201 Result,
202 EncodedResult,
203 E,
204 R,
205 ResultDecodingServices,
206 ResultEncodingServices
207 >,
208): DesktopSyncIpcMethod<E | Schema.SchemaError, R | ResultEncodingServices> => {
209 const encode = Schema.encodeUnknownEffect(method.result);
210​
211 return {
212 channel: method.channel,
213 handler: () =>
214 method
215 .handler()
216 .pipe(
217 Effect.flatMap(encode),
218 Effect.withSpan("desktop.ipc.method", { attributes: { channel: method.channel } }),
219 ),
220 };
221};