Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using runtime.onInstalled.addListener #31

Open
flosse opened this issue Sep 27, 2022 · 1 comment
Open

Using runtime.onInstalled.addListener #31

flosse opened this issue Sep 27, 2022 · 1 comment
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@flosse
Copy link
Member

flosse commented Sep 27, 2022

Adding a event listener with

let closure: Closure<dyn Fn(JsValue)> = Closure::new(move |js: JsValue| {
  let reason = js.as_string().expect("reason string"); 
  // ...
});
web_extensions_sys::chrome
  .runtime()
  .on_installed()
  .add_listener(closure.as_ref().unchecked_ref());
closure.forget();

but the event is never fired because the JS API runtime.onInstalled.addListener has to be called synchronously in the background script. And because WASM is asynchronously loaded, the execution of the web-extensions-sys wrapper is called too late

import init from './pkg/background.js';
// here we have to call runtime.onInstalled.addListener
init()
  .then(() => {
    // but it's called by the WASM module here
  })
  .catch(console.error)

There is a method that can init the module synchronously but the fetch is still asynchronous:

fetch("./pkg/background_bg.wasm")
   .then((response) => response.arrayBuffer())
   .then((bytes) => {
     wasm.initSync(bytes);
   })
   .catch(console.error);

So does anybody have an idea how to solve this problem?

@flosse flosse added help wanted Extra attention is needed question Further information is requested labels Sep 27, 2022
@hut8
Copy link
Contributor

hut8 commented Oct 12, 2022

https://groups.google.com/a/chromium.org/g/chromium-extensions/c/HbavKaHwEXU

// in service worker
var promise = WebAssembly.instantiateStreaming(......);
async function onInstall() {
  let obj = await promise;
  // use obj 
}
chrome.runtime.onInstalled.addListener(onInstall);

That makes it seem like the answer is "you can't do it", and it can really only be improved slightly from where we are now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants