Skip to content

Commit

Permalink
PKG -- [fcl] Fixed android polling (#1703)
Browse files Browse the repository at this point in the history
* PKG -- [fcl] Fixed android polling

* DOC -- [fcl] add comments

* DOC -- [fcl] add comments
  • Loading branch information
nialexsan authored Jun 27, 2023
1 parent fefb578 commit 8dcb70c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
25 changes: 22 additions & 3 deletions packages/fcl/src/current-user/exec-service/strategies/http-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,44 @@ export const getExecHttpPost = (execLocal) => async({service, body, config, opts
} else if (resp.status === "REDIRECT") {
return resp
} else if (resp.status === "PENDING") {

// these two flags are required to run polling one more time before it stops
var canContinue = true
var shouldContinue = true

const [_, unmount] = await execLocal(
normalizeLocalView(resp.local),
{
serviceEndpoint,
onClose: () => (canContinue = false)
onClose: () => (shouldContinue = false)
}
)

const close = () => {
try {
unmount()
canContinue = false
shouldContinue = false
} catch (error) {
console.error("Frame Close Error", error)
}
}
/**
* this function is run once per poll call.
* Offsetting canContinue flag to make sure that
* the polling is performed one extra time after canContinue flag is set to false
* to prevent halting on Android when a browser calls window.close
* before FCL receives a successful result from polling
*
* @returns {boolean}
*/
const checkCanContinue = () => {
const offsetCanContinue = canContinue
canContinue = shouldContinue

return offsetCanContinue
}

return poll(resp.updates, () => canContinue)
return poll(resp.updates, checkCanContinue)
.then(serviceResponse => {
close()
return serviceResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ const serviceBody = service => {
return undefined
}

export async function poll(service, canContinue = () => true) {
export async function poll(service, checkCanContinue = () => true) {
invariant(service, "Missing Polling Service", {service})
if (!canContinue()) throw new Error("Externally Halted")
const canContinue = checkCanContinue()
if (!canContinue) throw new Error("Externally Halted")

let resp
try {
Expand All @@ -35,7 +36,7 @@ export async function poll(service, canContinue = () => true) {
document.visibilityState === "hidden"
) {
await new Promise(r => setTimeout(r, 500))
return poll(service, canContinue)
return poll(service, checkCanContinue)
}

resp = await fetchService(service, {
Expand All @@ -52,6 +53,6 @@ export async function poll(service, canContinue = () => true) {
throw new Error(`Declined: ${resp.reason || "No reason supplied."}`)
default:
await new Promise(r => setTimeout(r, 500))
return poll(resp.updates, canContinue)
return poll(resp.updates, checkCanContinue)
}
}
6 changes: 5 additions & 1 deletion packages/fcl/src/utils/react-native/render-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ export function renderBrowser(src, opts = {}) {
const webbrowser = WebBrowser.openAuthSessionAsync(src.toString())

const unmount = () => {
WebBrowser.dismissAuthSession()
try {
WebBrowser.dismissAuthSession()
} catch (error) {
console.log(error)
}
}

// Call onClose when the webbrowser is closed
Expand Down

0 comments on commit 8dcb70c

Please sign in to comment.