Skip to content

Commit

Permalink
Use deferred promise solution for onload
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwing committed Apr 14, 2021
1 parent 6b44012 commit 0f4434b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -502,4 +514,4 @@ class LedgerBridgeKeyring extends EventEmitter {
}

LedgerBridgeKeyring.type = type
module.exports = LedgerBridgeKeyring
module.exports = LedgerBridgeKeyring

0 comments on commit 0f4434b

Please sign in to comment.