UNPKG

@toruslabs/customauth

Version:

CustomAuth login with torus to get user private key

67 lines (63 loc) 2 kB
'use strict'; var _defineProperty = require('@babel/runtime/helpers/defineProperty'); var events = require('events'); var helpers = require('./helpers.js'); class PopupHandler extends events.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 || helpers.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; } } } exports.PopupHandler = PopupHandler;