@oraichain/customauth
Version:
CustomAuth login with torus to get user private key
66 lines (52 loc) • 1.54 kB
text/typescript
import { EventEmitter } from "events";
import { getPopupFeatures } from "./helpers";
class PopupHandler extends EventEmitter {
url: URL;
target: string;
features: string;
window: Window;
windowTimer: number;
iClosedWindow: boolean;
constructor({ url, target, features }: { url: URL; target?: string; features?: string }) {
super();
this.url = url;
this.target = target || "_blank";
this.features = features || getPopupFeatures();
this.window = undefined;
this.windowTimer = undefined;
this.iClosedWindow = false;
this._setupTimer();
}
_setupTimer(): void {
this.windowTimer = Number(
setInterval(() => {
if (this.window && this.window.closed) {
clearInterval(this.windowTimer);
if (!this.iClosedWindow) {
this.emit("close");
}
this.iClosedWindow = false;
this.window = undefined;
}
if (this.window === undefined) clearInterval(this.windowTimer);
}, 500)
);
}
open(): Promise<void> {
this.window = window.open(this.url.href, this.target, this.features);
if (this.window?.focus) this.window.focus();
return Promise.resolve();
}
close(): void {
this.iClosedWindow = true;
if (this.window) this.window.close();
}
redirect(locationReplaceOnRedirect: boolean): void {
if (locationReplaceOnRedirect) {
window.location.replace(this.url.href);
} else {
window.location.href = this.url.href;
}
}
}
export default PopupHandler;