refactor: switch to using defer

This commit is contained in:
Derrick Hammer 2023-07-31 12:50:06 -04:00
parent d67cbc0a06
commit 3f57f9ec44
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 8 additions and 12 deletions

View File

@ -19,6 +19,7 @@ import type { DNSResult } from "@lumeweb/libresolver";
import { blake3 } from "@noble/hashes/blake3"; import { blake3 } from "@noble/hashes/blake3";
import "./contentFilters/index.js"; import "./contentFilters/index.js";
import defer from "p-defer";
export default class WebEngine { export default class WebEngine {
private contentProviders: BaseProvider[] = []; private contentProviders: BaseProvider[] = [];
@ -270,37 +271,32 @@ export default class WebEngine {
} }
let dnsResult: boolean | DNSResult = false; let dnsResult: boolean | DNSResult = false;
let resolveRequest: any, rejectRequest: any; let requestDefer = defer();
let promise = new Promise((resolve, reject) => { this.navigations.set(this.getNavigationId(details), requestDefer.promise);
resolveRequest = resolve;
rejectRequest = reject;
});
this.navigations.set(this.getNavigationId(details), promise);
if ("kernel.lume" === queriedHost) { if ("kernel.lume" === queriedHost) {
if (!queriedUrl.includes("://")) { if (!queriedUrl.includes("://")) {
queriedUrl = `http://${queriedUrl}`; queriedUrl = `http://${queriedUrl}`;
} }
rejectRequest(queriedUrl); requestDefer.reject(queriedUrl);
return; return;
} }
if (!getAuthStatus().loginComplete) { if (!getAuthStatus().loginComplete) {
resolveRequest(); requestDefer.resolve();
return; return;
} }
try { try {
dnsResult = await scanRecords(queriedHost); dnsResult = await scanRecords(queriedHost);
} catch (e) { } catch (e) {
resolveRequest(); requestDefer.resolve();
return; return;
} }
if (!dnsResult) { if (!dnsResult) {
resolveRequest(); requestDefer.resolve();
return; return;
} }
@ -308,7 +304,7 @@ export default class WebEngine {
queriedUrl = `http://${queriedUrl}`; queriedUrl = `http://${queriedUrl}`;
} }
rejectRequest(queriedUrl); requestDefer.reject(queriedUrl);
} }
private getNavigationId(details: any) { private getNavigationId(details: any) {