Skip to content

Commit

Permalink
fix: less recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoch committed Dec 5, 2024
1 parent 789c600 commit 6092b3d
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/idos-sdk-js/src/lib/enclave-providers/iframe-enclave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,10 @@ export class IframeEnclave implements EnclaveProvider {
}) as Promise<idOSCredential[]>;
}

async #loadEnclave(): Promise<unknown> {
const container = document.querySelector(this.container);
const iframeElem = document.getElementById(this.iframe.id);

if (iframeElem) {
console.warn("An Iframe already exists in the container");
container?.removeChild(iframeElem);
return new Promise((resolve) => this.#loadEnclave().then(resolve));
}
async #loadEnclave(): Promise<void> {
const container =
document.querySelector(this.container) ||
throwNew(Error, `Can't find container with selector ${this.container}`);

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Permissions-Policy#directives
const permissionsPolicies = ["publickey-credentials-get", "storage-access"];
Expand Down Expand Up @@ -152,11 +147,15 @@ export class IframeEnclave implements EnclaveProvider {
this.iframe.style.setProperty(k, v);
}

if (!container) throw new Error(`Can't find container with selector ${this.container}`);

container.appendChild(this.iframe);
// This was the best way we found at the time to check if we had already
// added the element to the tree.
if (!document.getElementById(this.iframe.id)) {
container.appendChild(this.iframe);
}

return new Promise((resolve) => this.iframe.addEventListener("load", () => resolve()));
return new Promise((resolve) =>
this.iframe.addEventListener("load", () => resolve(), { once: true }),
);
}

#showEnclave() {
Expand Down Expand Up @@ -240,3 +239,7 @@ export class IframeEnclave implements EnclaveProvider {
};
}
}

function throwNew(ErrorClass: ErrorConstructor, ...args: Parameters<ErrorConstructor>): never {
throw new ErrorClass(...args);
}

0 comments on commit 6092b3d

Please sign in to comment.