From 0f4434b754fdff5d9fcbafb16ab7bfe453f97763 Mon Sep 17 00:00:00 2001 From: David Walsh Date: Wed, 14 Apr 2021 17:07:57 -0500 Subject: [PATCH] Use deferred promise solution for onload --- index.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 08f7a30..cf66d75 100644 --- a/index.js +++ b/index.js @@ -195,8 +195,13 @@ class LedgerBridgeKeyring extends EventEmitter { // If the iframe isn't loaded yet, let's store the desired useLedgerLive value and // optimistically return a successful promise if(!this.iframeLoaded) { - this.initialUseLedgerLive = useLedgerLive - return Promise.resolve(true); + return new Promise((delayedResolve, delayedReject) => { + this.delayedPromise = { + resolve: delayedResolve, + reject: delayedReject, + useLedgerLive, + }; + }); } this._sendMessage({ @@ -331,9 +336,16 @@ class LedgerBridgeKeyring extends EventEmitter { // If the ledger live preference was set before the iframe is loaded, // set it after the iframe has loaded this.iframeLoaded = true - if(this.initialUseLedgerLive !== 'undefined') { - await this.updateTransportMethod(this.initialUseLedgerLive) - delete this.initialUseLedgerLive + if(this.delayedPromise) { + try { + const result = await this.updateTransportMethod( + this.delayedPromise.useLedgerLive + ) + this.delayedPromise.resolve(result) + } + catch (e) { + this.delayedPromise.reject(e) + } } } document.head.appendChild(this.iframe) @@ -502,4 +514,4 @@ class LedgerBridgeKeyring extends EventEmitter { } LedgerBridgeKeyring.type = type -module.exports = LedgerBridgeKeyring \ No newline at end of file +module.exports = LedgerBridgeKeyring