@@ -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' ;
912import { delay , isJsonRpcMessage , stringify } from '@metamask/kernel-utils' ;
1013import type { JsonRpcMessage } from '@metamask/kernel-utils' ;
1114import { Logger } from '@metamask/logger' ;
@@ -16,6 +19,13 @@ defineGlobals();
1619const OFFSCREEN_DOCUMENT_PATH = '/offscreen.html' ;
1720const logger = new Logger ( 'background' ) ;
1821let 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
2131chrome . 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 ,
0 commit comments