This repository has been archived on 2023-12-17. You can view files and clone it, but cannot push or open issues or pull requests.
extension/shared/util.ts

40 lines
808 B
TypeScript

import browser from "webextension-polyfill";
import { createClient } from "@lumeweb/kernel-swarm-client";
let swarmClient = createClient();
export async function queryBackground(method: string, data = {}) {
let resp;
try {
resp = await browser.runtime.sendMessage({
method,
data,
});
} catch (e) {
throw e;
}
return resp;
}
export async function waitForBoot() {
await queryBackground("waitForBoot");
}
export async function waitForConnected(cb?: Function) {
await waitForBoot();
await swarmClient.ready();
await cb?.();
}
export async function listenBootStatus(cb: (percent: number) => void) {
const port = browser.runtime.connect();
port.onMessage.addListener((data: any) => {
if (data?.method === "bootStatus") {
cb(data.data);
}
});
}