UNPKG

@toruslabs/customauth

Version:

CustomAuth login with torus to get user private key

65 lines (62 loc) 1.97 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { EventEmitter } from 'events'; import { getPopupFeatures } from './helpers.js'; class PopupHandler extends EventEmitter { constructor({ url, target, features, timeout = 30000 }) { super(); _defineProperty(this, "url", void 0); _defineProperty(this, "target", void 0); _defineProperty(this, "features", void 0); _defineProperty(this, "window", void 0); _defineProperty(this, "windowTimer", void 0); _defineProperty(this, "iClosedWindow", void 0); _defineProperty(this, "timeout", void 0); this.url = url; this.target = target || "_blank"; this.features = features || getPopupFeatures(); this.window = undefined; this.windowTimer = undefined; this.iClosedWindow = false; this.timeout = timeout; this._setupTimer(); } _setupTimer() { this.windowTimer = Number(setInterval(() => { if (this.window && this.window.closed) { clearInterval(this.windowTimer); setTimeout(() => { if (!this.iClosedWindow) { this.emit("close"); } this.iClosedWindow = false; this.window = undefined; }, this.timeout); } if (this.window === undefined) clearInterval(this.windowTimer); }, 500)); } open() { var _this$window; this.window = window.open(this.url.href, this.target, this.features); if (!this.window) throw new Error("popup window is blocked"); if ((_this$window = this.window) !== null && _this$window !== void 0 && _this$window.focus) this.window.focus(); return Promise.resolve(); } close() { this.iClosedWindow = true; if (this.window) this.window.close(); } redirect(locationReplaceOnRedirect) { if (locationReplaceOnRedirect) { window.location.replace(this.url.href); } else { window.location.href = this.url.href; } } } export { PopupHandler };