*Replace variables with more getter/setters

This commit is contained in:
Derrick Hammer 2022-08-22 01:49:58 -04:00
parent 3122bbaf2c
commit eac9e7b579
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 35 additions and 19 deletions

View File

@ -1,4 +1,3 @@
import type { DataFn, KernelAuthStatus } from "libskynet";
import tldEnum from "@lumeweb/tld-enum"; import tldEnum from "@lumeweb/tld-enum";
import WebEngine from "../webEngine.js"; import WebEngine from "../webEngine.js";
import InternalProvider from "../contentProviders/internalProvider.js"; import InternalProvider from "../contentProviders/internalProvider.js";
@ -9,10 +8,10 @@ import IpfsProvider from "../contentProviders/ipfsProvider.js";
import { ready as dnsReady } from "@lumeweb/kernel-dns-client"; import { ready as dnsReady } from "@lumeweb/kernel-dns-client";
import { import {
addQuery, addQuery,
authStatusResolve, getAuthStatusResolve,
blockForBootloader, getBlockForBootloader,
blockForBridge, getBlockForBridge,
bridgeLoadedResolve, getBridgeLoadedResolve,
clearOpenPorts, clearOpenPorts,
deleteOpenPort, deleteOpenPort,
deleteQuery, deleteQuery,
@ -27,9 +26,9 @@ import {
getTimer, getTimer,
increasePortsNonce, increasePortsNonce,
increaseQueriesNonce, increaseQueriesNonce,
kernelFrame,
setAuthStatus, setAuthStatus,
setAuthStatusKnown, setAuthStatusKnown,
setDnsSetupPromise,
setKernelIframe, setKernelIframe,
setOpenPort, setOpenPort,
setTimer, setTimer,
@ -58,13 +57,16 @@ export function queryKernel(query: any): Promise<any> {
resolve(data.data); resolve(data.data);
}; };
blockForBootloader.then(() => { getBlockForBootloader().then(() => {
let nonce = getQueriesNonce(); let nonce = getQueriesNonce();
increaseQueriesNonce(); increaseQueriesNonce();
query.nonce = nonce; query.nonce = nonce;
addQuery(nonce, receiveResponse); addQuery(nonce, receiveResponse);
if (kernelFrame.contentWindow !== null) { if (getKernelIframe().contentWindow !== null) {
kernelFrame.contentWindow.postMessage(query, "http://kernel.skynet"); (getKernelIframe() as any).contentWindow.postMessage(
query,
"http://kernel.skynet"
);
} else { } else {
console.error( console.error(
"kernelFrame.contentWindow was null, cannot send message!" "kernelFrame.contentWindow was null, cannot send message!"
@ -77,7 +79,7 @@ function handleKernelMessage(event: MessageEvent) {
let data = event.data.data; let data = event.data.data;
if (event.data.method === "kernelBridgeVersion") { if (event.data.method === "kernelBridgeVersion") {
blockForBridge.then(() => { getBlockForBridge().then(() => {
for (let [, port] of Object.entries(getOpenPorts())) { for (let [, port] of Object.entries(getOpenPorts())) {
try { try {
(port as any).postMessage(event.data); (port as any).postMessage(event.data);
@ -104,7 +106,7 @@ function handleKernelMessage(event: MessageEvent) {
if (event.data.method === "kernelAuthStatus") { if (event.data.method === "kernelAuthStatus") {
setAuthStatus(data); setAuthStatus(data);
if (getAuthStatusKnown() === false) { if (getAuthStatusKnown() === false) {
authStatusResolve(); getAuthStatusResolve()();
setAuthStatusKnown(true); setAuthStatusKnown(true);
console.log("bootloader is now initialized"); console.log("bootloader is now initialized");
if (getAuthStatus().loginComplete !== true) { if (getAuthStatus().loginComplete !== true) {
@ -151,7 +153,7 @@ function handleBridgeMessage(
domain: string domain: string
) { ) {
if (data.method === "bridgeLoaded") { if (data.method === "bridgeLoaded") {
bridgeLoadedResolve(); getBridgeLoadedResolve()();
return; return;
} }
@ -172,7 +174,7 @@ function handleBridgeMessage(
}); });
data["domain"] = domain; data["domain"] = domain;
} }
kernelFrame.contentWindow!.postMessage(data, "http://kernel.skynet"); getKernelIframe().contentWindow!.postMessage(data, "http://kernel.skynet");
} }
function bridgeListener(port: any) { function bridgeListener(port: any) {
let portNonce = getPortsNonce(); let portNonce = getPortsNonce();
@ -189,7 +191,7 @@ function bridgeListener(port: any) {
handleBridgeMessage(port, portNonce, data, domain); handleBridgeMessage(port, portNonce, data, domain);
}); });
blockForBootloader.then(() => { getBlockForBootloader().then(() => {
port.postMessage({ port.postMessage({
method: "kernelAuthStatus", method: "kernelAuthStatus",
data: getAuthStatus(), data: getAuthStatus(),

View File

@ -8,15 +8,15 @@ export let timer = 20000;
let authStatus: KernelAuthStatus; let authStatus: KernelAuthStatus;
let authStatusKnown = false; let authStatusKnown = false;
export let authStatusResolve: DataFn; let authStatusResolve: DataFn;
export let bridgeLoadedResolve: DataFn; let bridgeLoadedResolve: DataFn;
export let blockForBootloader = new Promise((resolve) => { let blockForBootloader = new Promise((resolve) => {
authStatusResolve = resolve; authStatusResolve = resolve;
}); });
export let blockForBridge = new Promise((resolve) => { let blockForBridge = new Promise((resolve) => {
bridgeLoadedResolve = resolve; bridgeLoadedResolve = resolve;
}); });
export let kernelFrame: HTMLIFrameElement; let kernelFrame: HTMLIFrameElement;
export function getAuthStatusKnown() { export function getAuthStatusKnown() {
return authStatusKnown; return authStatusKnown;
@ -93,3 +93,17 @@ export function getKernelIframe(): HTMLIFrameElement {
export function setKernelIframe(iframe: HTMLIFrameElement) { export function setKernelIframe(iframe: HTMLIFrameElement) {
kernelFrame = iframe; kernelFrame = iframe;
} }
export function getAuthStatusResolve(): DataFn {
return authStatusResolve;
}
export function getBlockForBootloader(): Promise<unknown> {
return blockForBootloader;
}
export function getBlockForBridge(): Promise<unknown> {
return blockForBridge;
}
export function getBridgeLoadedResolve(): DataFn {
return bridgeLoadedResolve;
}