Skip to content

Commit 4e9df00

Browse files
refactor: Replace handleInternalMessage with mutually exclusive options
Replace the union type parameter with discriminated union options: - `handler`: Direct handler function (for tests) - `handlerPromise`: Promise-based handler (for async initialization) This addresses the anti-pattern of accepting either a value or a promise of that value, making the API more explicit and easier to understand. Co-authored-by: Erik Marks <rekmarks@users.noreply.github.com>
1 parent a5fe04d commit 4e9df00

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

packages/kernel-browser-runtime/src/internal-comms/internal-connections.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe('internal-connections', () => {
183183

184184
it('should handle new internal process connections', async () => {
185185
receiveInternalConnections({
186-
handleInternalMessage: mockHandleMessage,
186+
handler: mockHandleMessage,
187187
logger,
188188
});
189189

@@ -208,7 +208,7 @@ describe('internal-connections', () => {
208208

209209
it('should handle valid message', async () => {
210210
receiveInternalConnections({
211-
handleInternalMessage: mockHandleMessage,
211+
handler: mockHandleMessage,
212212
logger,
213213
});
214214

@@ -257,7 +257,7 @@ describe('internal-connections', () => {
257257

258258
it('should handle JSON-RPC notifications', async () => {
259259
receiveInternalConnections({
260-
handleInternalMessage: mockHandleMessage,
260+
handler: mockHandleMessage,
261261
logger,
262262
});
263263

@@ -300,7 +300,7 @@ describe('internal-connections', () => {
300300

301301
it('should handle multiple simultaneous connections', async () => {
302302
receiveInternalConnections({
303-
handleInternalMessage: mockHandleMessage,
303+
handler: mockHandleMessage,
304304
logger,
305305
});
306306

@@ -335,7 +335,7 @@ describe('internal-connections', () => {
335335

336336
it('should forget ids of closed channels', async () => {
337337
receiveInternalConnections({
338-
handleInternalMessage: mockHandleMessage,
338+
handler: mockHandleMessage,
339339
logger,
340340
});
341341
const controlChannel = MockBroadcastChannel.channels.get(
@@ -376,7 +376,7 @@ describe('internal-connections', () => {
376376

377377
it('should reject duplicate connections', () => {
378378
receiveInternalConnections({
379-
handleInternalMessage: mockHandleMessage,
379+
handler: mockHandleMessage,
380380
logger,
381381
});
382382
const controlChannel = MockBroadcastChannel.channels.get(
@@ -401,7 +401,7 @@ describe('internal-connections', () => {
401401

402402
it('should reject invalid control commands', () => {
403403
receiveInternalConnections({
404-
handleInternalMessage: mockHandleMessage,
404+
handler: mockHandleMessage,
405405
logger,
406406
});
407407
const controlChannel = MockBroadcastChannel.channels.get(
@@ -425,7 +425,7 @@ describe('internal-connections', () => {
425425

426426
it('should handle comms channel message errors', async () => {
427427
receiveInternalConnections({
428-
handleInternalMessage: mockHandleMessage,
428+
handler: mockHandleMessage,
429429
logger,
430430
});
431431

packages/kernel-browser-runtime/src/internal-comms/internal-connections.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,39 +102,50 @@ const connectToInternalProcess = async (
102102
return stream;
103103
};
104104

105-
type ReceiveConnectionsOptions = Omit<Options, 'label'> & {
106-
handleInternalMessage: HandleInternalMessage | Promise<HandleInternalMessage>;
107-
};
105+
type ReceiveConnectionsOptions = Omit<Options, 'label'> &
106+
(
107+
| {
108+
handler: HandleInternalMessage;
109+
handlerPromise?: never;
110+
}
111+
| {
112+
handler?: never;
113+
handlerPromise: Promise<HandleInternalMessage>;
114+
}
115+
);
108116

109117
/**
110118
* Listens for connections between the kernel and an internal process, e.g. a UI instance.
111119
* Should be called exactly once in the kernel, during initialization, before any internal
112120
* processes have attempted to connect.
113121
*
114122
* @param options - The options for the connection.
115-
* @param options.handleInternalMessage - The function to handle the internal message,
116-
* or a promise that resolves to such a function. If a promise is provided, messages will
117-
* be buffered until the handler is ready, then subsequent messages are handled directly.
123+
* @param options.handler - The function to handle internal messages. Mutually exclusive
124+
* with `handlerPromise`.
125+
* @param options.handlerPromise - A promise that resolves to the handler function.
126+
* Messages will be buffered until the handler is ready, then subsequent messages are
127+
* handled directly. Mutually exclusive with `handler`.
118128
* @param options.logger - The logger instance.
119129
* @param options.controlChannelName - The name of the control channel. Must match
120130
* the name used by {@link connectToKernel} on the other end.
121131
*/
122132
export const receiveInternalConnections = ({
123-
handleInternalMessage,
133+
handler: directHandler,
134+
handlerPromise,
124135
logger,
125136
controlChannelName = COMMS_CONTROL_CHANNEL_NAME,
126137
}: ReceiveConnectionsOptions): void => {
127138
// Support both direct handler and promise-based handler
128139
let handler: HandleInternalMessage | null = null;
129140
let handlerReady: Promise<HandleInternalMessage>;
130141

131-
if (typeof handleInternalMessage === 'function') {
142+
if (directHandler !== undefined) {
132143
// Direct handler - use immediately
133-
handler = handleInternalMessage;
134-
handlerReady = Promise.resolve(handleInternalMessage);
144+
handler = directHandler;
145+
handlerReady = Promise.resolve(directHandler);
135146
} else {
136147
// Promise-based handler - cache once resolved
137-
handlerReady = handleInternalMessage.then((resolvedHandler) => {
148+
handlerReady = handlerPromise.then((resolvedHandler) => {
138149
handler = resolvedHandler;
139150
return resolvedHandler;
140151
});

packages/kernel-browser-runtime/src/kernel-worker/kernel-worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function main(): Promise<void> {
6565
});
6666

6767
receiveInternalConnections({
68-
handleInternalMessage: handlerP,
68+
handlerPromise: handlerP,
6969
logger,
7070
});
7171

0 commit comments

Comments
 (0)