*Solve bridge loading race condition by using a blocking promised, resolved by a bridgeLoaded command from a bridge

This commit is contained in:
Derrick Hammer 2022-07-31 00:51:02 -04:00
parent f394776a72
commit 1142576ea5
2 changed files with 22 additions and 8 deletions

View File

@ -24,9 +24,15 @@ setTimeout(logLargeObjects, timer);
let authStatus: KernelAuthStatus;
let authStatusKnown = false;
let authStatusResolve: DataFn;
let bridgeLoadedResolve: DataFn;
let blockForBootloader = new Promise((resolve) => {
authStatusResolve = resolve;
});
let blockForBridge = new Promise((resolve) => {
bridgeLoadedResolve = resolve;
});
tldEnum.list.push("localhost");
export function queryKernel(query: any): Promise<any> {
return new Promise((resolve) => {
@ -50,17 +56,16 @@ export function queryKernel(query: any): Promise<any> {
});
}
function handleKernelMessage(event: MessageEvent) {
if (event.origin !== "http://kernel.skynet") {
return;
}
let data = event.data.data;
if (event.data.method === "kernelBridgeVersion") {
for (let [, port] of Object.entries(openPorts)) {
try {
(port as any).postMessage(event.data);
} catch {}
}
blockForBridge.then(() => {
for (let [, port] of Object.entries(openPorts)) {
try {
(port as any).postMessage(event.data);
} catch {}
}
});
return;
}
@ -128,6 +133,11 @@ function handleBridgeMessage(
data: any,
domain: string
) {
if (data.method === "bridgeLoaded") {
bridgeLoadedResolve();
return;
}
if (!("nonce" in data)) {
return;
}
@ -180,4 +190,5 @@ engine.registerContentProvider(new InternalProvider(engine));
// @ts-ignore
let kernelFrame: HTMLIFrameElement = document.createElement("iframe");
kernelFrame.src = "http://kernel.skynet";
kernelFrame.onload = init;
document.body.appendChild(kernelFrame);

View File

@ -130,3 +130,6 @@ function handleMessage(event: MessageEvent) {
// Everything else just gets ignored.
}
window.addEventListener("message", handleMessage);
port.postMessage({
method: "bridgeLoaded",
});