-
Notifications
You must be signed in to change notification settings - Fork 95
fix: premount loader race condition #896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
080eb7b
f2288a0
72aaf7e
10ba32f
0b55322
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,12 +142,13 @@ let make = ( | |
} | ||
} | ||
|
||
let fetchPaymentsList = (mountedIframeRef, componentType) => { | ||
let fetchPaymentsList = (mountedIframeRef, componentType, promiseResolve) => { | ||
let handlePaymentMethodsLoaded = (event: Types.event) => { | ||
let json = event.data->anyTypeToJson | ||
let dict = json->getDictFromJson | ||
let isPaymentMethodsData = dict->getString("data", "") === "payment_methods" | ||
if isPaymentMethodsData { | ||
promiseResolve() | ||
isTaxCalculationEnabled.contents = | ||
dict->getDictFromDict("response")->getBool("is_tax_calculation_enabled", false) | ||
addSmartEventListener("message", onPlaidCallback(mountedIframeRef), "onPlaidCallback") | ||
|
@@ -212,6 +213,7 @@ let make = ( | |
mountedIframeRef, | ||
disableSavedPaymentMethods, | ||
componentType, | ||
promiseResolve, | ||
) => { | ||
if !disableSavedPaymentMethods { | ||
let handleCustomerPaymentMethodsLoaded = (event: Types.event) => { | ||
|
@@ -223,13 +225,16 @@ let make = ( | |
let json = dict->getJsonFromDict("response", JSON.Encode.null) | ||
let msg = [("customerPaymentMethods", json)]->Dict.fromArray | ||
mountedIframeRef->Window.iframePostMessage(msg) | ||
promiseResolve() | ||
} | ||
} | ||
addSmartEventListener( | ||
"message", | ||
handleCustomerPaymentMethodsLoaded, | ||
`onCustomerPaymentMethodsLoaded-${componentType}`, | ||
) | ||
} else { | ||
promiseResolve() | ||
} | ||
let msg = | ||
[ | ||
|
@@ -809,13 +814,14 @@ let make = ( | |
addSmartEventListener("message", handleGooglePayThirdPartyFlow, "onGooglePayThirdParty") | ||
addSmartEventListener("message", handleApplePayThirdPartyFlow, "onApplePayThirdParty") | ||
|
||
let fetchSessionTokens = mountedIframeRef => { | ||
let fetchSessionTokens = (mountedIframeRef, promiseResolve) => { | ||
let handleSessionTokensLoaded = (event: Types.event) => { | ||
let json = event.data->anyTypeToJson | ||
let dict = json->getDictFromJson | ||
let sessionTokensData = dict->getString("data", "") === "session_tokens" | ||
if sessionTokensData { | ||
let json = dict->getJsonFromDict("response", JSON.Encode.null) | ||
promiseResolve() | ||
|
||
{ | ||
let sessionsArr = | ||
|
@@ -1288,15 +1294,34 @@ let make = ( | |
} | ||
preMountLoaderMountedPromise | ||
->then(_ => { | ||
fetchPaymentsList(mountedIframeRef, componentType) | ||
let paymentMethodsPromise = Promise.make((resolve, _) => { | ||
fetchPaymentsList(mountedIframeRef, componentType, resolve) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we have to wrap Wrapping with Promise.make and passing the resolve function seems unnecessary - adding a lot of responsibility on the function caller. It might be simpler if the function returns promise by itself There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved Promise inside the function |
||
let disableSavedPaymentMethods = | ||
newOptions | ||
->getDictFromJson | ||
->getBool("displaySavedPaymentMethods", true) && | ||
!(spmComponents->Array.includes(componentType))->not | ||
fetchCustomerPaymentMethods(mountedIframeRef, disableSavedPaymentMethods, componentType) | ||
fetchSessionTokens(mountedIframeRef) | ||
resolve() | ||
let customerPaymentMethodsPromise = Promise.make((resolve, _) => { | ||
fetchCustomerPaymentMethods( | ||
mountedIframeRef, | ||
disableSavedPaymentMethods, | ||
componentType, | ||
resolve, | ||
) | ||
}) | ||
let sessionTokensPromise = Promise.make((resolve, _) => { | ||
fetchSessionTokens(mountedIframeRef, resolve) | ||
}) | ||
Promise.all([ | ||
paymentMethodsPromise, | ||
customerPaymentMethodsPromise, | ||
sessionTokensPromise, | ||
])->then(_ => { | ||
let msg = [("cleanUpPreMountLoaderIframe", true->JSON.Encode.bool)]->Dict.fromArray | ||
preMountLoaderIframeDiv->Window.iframePostMessage(msg) | ||
resolve() | ||
}) | ||
}) | ||
->catch(_ => resolve()) | ||
->ignore | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,13 +98,19 @@ let make = ( | |
) | ||
}) | ||
|
||
let fetchSavedPaymentMethods = (mountedIframeRef, disableSaveCards, componentType) => { | ||
let fetchSavedPaymentMethods = ( | ||
mountedIframeRef, | ||
disableSaveCards, | ||
componentType, | ||
resolvePromise, | ||
) => { | ||
if !disableSaveCards { | ||
let handleSavedPaymentMethodsLoaded = (event: Types.event) => { | ||
let json = event.data->Identity.anyTypeToJson | ||
let dict = json->getDictFromJson | ||
let isSavedPaymentMethodsData = dict->getString("data", "") === "saved_payment_methods" | ||
if isSavedPaymentMethodsData { | ||
resolvePromise() | ||
let json = dict->getJsonFromDict("response", JSON.Encode.null) | ||
let msg = [("savedPaymentMethods", json)]->Dict.fromArray | ||
mountedIframeRef->Window.iframePostMessage(msg) | ||
|
@@ -115,6 +121,8 @@ let make = ( | |
handleSavedPaymentMethodsLoaded, | ||
`onSavedPaymentMethodsLoaded-${componentType}`, | ||
) | ||
} else { | ||
resolvePromise() | ||
} | ||
let msg = | ||
[("sendSavedPaymentMethodsResponse", !disableSaveCards->JSON.Encode.bool)]->Dict.fromArray | ||
|
@@ -208,7 +216,15 @@ let make = ( | |
disableSavedPaymentMethods && | ||
!(expressCheckoutComponents->Array.includes(componentType)) | ||
) { | ||
fetchSavedPaymentMethods(mountedIframeRef, false, componentType) | ||
Promise.make((resolve, _) => { | ||
fetchSavedPaymentMethods(mountedIframeRef, false, componentType, resolve) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same over here as well. Can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved the promise inside here as well |
||
->then(_ => { | ||
let msg = [("cleanUpPreMountLoaderIframe", true->JSON.Encode.bool)]->Dict.fromArray | ||
preMountLoaderIframeDiv->Window.iframePostMessage(msg) | ||
resolve() | ||
}) | ||
->ignore | ||
} | ||
resolve() | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable used to be called
getPromisesAndHandler
because it used to return promise and message handler. if that is changed to only return handler, then can we rename the function to reflect that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the name