UNPKG

@web3auth/ws-embed

Version:

Embed script

63 lines (60 loc) 1.98 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { getPopupFeatures, FEATURES_DEFAULT_POPUP_WINDOW } from '@toruslabs/base-controllers'; import { SafeEventEmitter } from '@web3auth/auth'; class PopupHandler extends SafeEventEmitter { 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(FEATURES_DEFAULT_POPUP_WINDOW); 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 = this.window) !== null && _this$window !== void 0 && _this$window.focus) this.window.focus(); } 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 as default };