Skip to content

Commit 6e88cad

Browse files
committed
refactor: Make background changes compatible with re-executing main()
1 parent bef3b9d commit 6e88cad

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

packages/extension/src/background.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ defineGlobals();
2020
const OFFSCREEN_DOCUMENT_PATH = '/offscreen.html';
2121
const logger = new Logger('background');
2222
let bootPromise: Promise<void> | null = null;
23+
let kernelP: Promise<KernelFacade>;
24+
let ping: () => Promise<void>;
25+
26+
// With this we can click the extension action button to wake up the service worker.
27+
chrome.action.onClicked.addListener(() => {
28+
ping?.().catch(logger.error);
29+
});
2330

2431
// Install/update
2532
chrome.runtime.onInstalled.addListener(() => {
@@ -101,28 +108,13 @@ async function main(): Promise<void> {
101108
});
102109

103110
// Get the kernel remote presence
104-
const kernelP = backgroundCapTP.getKernel();
111+
kernelP = backgroundCapTP.getKernel();
105112

106-
const ping = async (): Promise<void> => {
113+
ping = async () => {
107114
const result = await E(kernelP).ping();
108115
logger.info(result);
109116
};
110117

111-
Object.defineProperties(globalThis.kernel, {
112-
ping: {
113-
value: ping,
114-
},
115-
getKernel: {
116-
value: async () => kernelP,
117-
},
118-
});
119-
harden(globalThis.kernel);
120-
121-
// With this we can click the extension action button to wake up the service worker.
122-
chrome.action.onClicked.addListener(() => {
123-
ping().catch(logger.error);
124-
});
125-
126118
// Handle incoming CapTP messages from the kernel
127119
const drainPromise = offscreenStream.drain((message) => {
128120
if (isCapTPNotification(message)) {
@@ -140,9 +132,11 @@ async function main(): Promise<void> {
140132
try {
141133
await drainPromise;
142134
} catch (error) {
143-
throw new Error('Offscreen connection closed unexpectedly', {
135+
const finalError = new Error('Offscreen connection closed unexpectedly', {
144136
cause: error,
145137
});
138+
backgroundCapTP.abort(finalError);
139+
throw finalError;
146140
}
147141
}
148142

@@ -175,6 +169,16 @@ function defineGlobals(): void {
175169
value: {},
176170
});
177171

172+
Object.defineProperties(globalThis.kernel, {
173+
ping: {
174+
get: () => ping,
175+
},
176+
getKernel: {
177+
value: async () => kernelP,
178+
},
179+
});
180+
harden(globalThis.kernel);
181+
178182
Object.defineProperty(globalThis, 'E', {
179183
value: E,
180184
configurable: false,

packages/omnium-gatherum/src/background.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import {
55
isCapTPNotification,
66
getCapTPMessage,
77
} from '@metamask/kernel-browser-runtime';
8-
import type { CapTPMessage } from '@metamask/kernel-browser-runtime';
8+
import type {
9+
CapTPMessage,
10+
KernelFacade,
11+
} from '@metamask/kernel-browser-runtime';
912
import { delay, isJsonRpcMessage, stringify } from '@metamask/kernel-utils';
1013
import type { JsonRpcMessage } from '@metamask/kernel-utils';
1114
import { Logger } from '@metamask/logger';
@@ -16,6 +19,13 @@ defineGlobals();
1619
const OFFSCREEN_DOCUMENT_PATH = '/offscreen.html';
1720
const logger = new Logger('background');
1821
let bootPromise: Promise<void> | null = null;
22+
let kernelP: Promise<KernelFacade>;
23+
let ping: () => Promise<void>;
24+
25+
// With this we can click the extension action button to wake up the service worker.
26+
chrome.action.onClicked.addListener(() => {
27+
ping?.().catch(logger.error);
28+
});
1929

2030
// Install/update
2131
chrome.runtime.onInstalled.addListener(() => {
@@ -80,13 +90,11 @@ async function main(): Promise<void> {
8090
// Without this delay, sending messages via the chrome.runtime API can fail.
8191
await delay(50);
8292

83-
// Create stream for CapTP messages
8493
const offscreenStream = await ChromeRuntimeDuplexStream.make<
8594
JsonRpcMessage,
8695
JsonRpcMessage
8796
>(chrome.runtime, 'background', 'offscreen', isJsonRpcMessage);
8897

89-
// Set up CapTP for E() based communication with the kernel
9098
const backgroundCapTP = makeBackgroundCapTP({
9199
send: (captpMessage: CapTPMessage) => {
92100
const notification = makeCapTPNotification(captpMessage);
@@ -96,31 +104,14 @@ async function main(): Promise<void> {
96104
},
97105
});
98106

99-
// Get the kernel remote presence
100-
const kernelP = backgroundCapTP.getKernel();
107+
kernelP = backgroundCapTP.getKernel();
101108

102-
const ping = async (): Promise<void> => {
109+
ping = async (): Promise<void> => {
103110
const result = await E(kernelP).ping();
104111
logger.info(result);
105112
};
106113

107-
Object.defineProperties(globalThis.omnium, {
108-
ping: {
109-
value: ping,
110-
},
111-
getKernel: {
112-
value: async () => kernelP,
113-
},
114-
});
115-
harden(globalThis.omnium);
116-
117-
// With this we can click the extension action button to wake up the service worker.
118-
chrome.action.onClicked.addListener(() => {
119-
ping().catch(logger.error);
120-
});
121-
122114
try {
123-
// Handle incoming CapTP messages from the kernel
124115
await offscreenStream.drain((message) => {
125116
if (isCapTPNotification(message)) {
126117
const captpMessage = getCapTPMessage(message);
@@ -130,9 +121,11 @@ async function main(): Promise<void> {
130121
}
131122
});
132123
} catch (error) {
133-
throw new Error('Offscreen connection closed unexpectedly', {
124+
const finalError = new Error('Offscreen connection closed unexpectedly', {
134125
cause: error,
135126
});
127+
backgroundCapTP.abort(finalError);
128+
throw finalError;
136129
}
137130
}
138131

@@ -147,6 +140,16 @@ function defineGlobals(): void {
147140
value: {},
148141
});
149142

143+
Object.defineProperties(globalThis.omnium, {
144+
ping: {
145+
get: () => ping,
146+
},
147+
getKernel: {
148+
value: async () => kernelP,
149+
},
150+
});
151+
harden(globalThis.omnium);
152+
150153
Object.defineProperty(globalThis, 'E', {
151154
configurable: false,
152155
enumerable: true,

vitest.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ export default defineConfig({
8787
lines: 88.13,
8888
},
8989
'packages/kernel-browser-runtime/**': {
90-
statements: 84.63,
90+
statements: 84.4,
9191
functions: 78.3,
9292
branches: 81.11,
93-
lines: 84.87,
93+
lines: 84.63,
9494
},
9595
'packages/kernel-errors/**': {
9696
statements: 99.24,

0 commit comments

Comments
 (0)