Skip to content

Commit

Permalink
code: reuse resume logic
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Apr 24, 2024
1 parent 34a9e83 commit 396cd4d
Showing 1 changed file with 41 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,30 +287,6 @@ export const RadixConnectRelayClient = (input: {
),
)

const resumeRequest = (
session: ActiveSession,
walletInteraction: WalletInteraction,
) => {
const { sessionId } = session
const { interactionId } = walletInteraction

return requestItemClient
.patch(interactionId, { sentToWallet: true })
.mapErr(() => SdkError('FailedToUpdateRequestItem', interactionId))
.andThen(() => sendEncryptedRequest(session, walletInteraction))
.andThen(() =>
deepLinkClient.deepLinkToWallet({
sessionId,
interactionId,
}),
)
.andThen(() =>
waitForWalletResponse(walletInteraction.interactionId).map(
(item) => item.walletResponse!,
),
)
}

const send = (
walletInteraction: WalletInteraction,
callbackFns: Partial<CallbackFns>,
Expand All @@ -323,7 +299,7 @@ export const RadixConnectRelayClient = (input: {
.andThen((session) => {
return session.status === 'Pending'
? handleLinkingRequest(session, walletInteraction)
: resumeRequest(session, walletInteraction)
: resume(walletInteraction.interactionId)
})

const handleWalletCallback = (values: Record<string, string>) => {
Expand Down Expand Up @@ -389,44 +365,49 @@ export const RadixConnectRelayClient = (input: {

deepLinkClient.handleWalletCallback()

const resume = (interactionId: string) => {
sessionClient.findActiveSession().andThen((session) => {
if (session) {
const url = new URL(origin)
url.hash = 'connect'
url.searchParams.set('sessionId', session.sessionId)
url.searchParams.set('interactionId', interactionId)
const childWindow = window.open(url.toString())!

return requestItemClient.getPendingItems().andThen((pendingItems) => {
const pendingItem = pendingItems.find(
(item) => item.interactionId === interactionId,
)
if (pendingItem) {
return requestItemClient
.patch(interactionId, { sentToWallet: true })
.andThen(() =>
sendEncryptedRequest(session, pendingItem.walletInteraction),
)
.andThen(() =>
deepLinkClient.deepLinkToWallet(
{
sessionId: session.sessionId,
interactionId: pendingItem.interactionId,
},
childWindow,
),
)
}
return errAsync(SdkError('PendingItemNotFound', ''))
})
}
return okAsync(undefined)
})
return waitForWalletResponse(interactionId).map(
(item) => item.walletResponse!,
)
}

return {
isSupported: () => isMobile(),
send,
resume: (interactionId: string) => {
sessionClient.findActiveSession().andThen((session) => {
if (session) {
const url = new URL(origin)
url.hash = 'connect'
url.searchParams.set('sessionId', session.sessionId)
url.searchParams.set('interactionId', interactionId)
const childWindow = window.open(url.toString())!

return requestItemClient.getPendingItems().andThen((pendingItems) => {
const pendingItem = pendingItems.find(
(item) => item.interactionId === interactionId,
)
if (pendingItem) {
return requestItemClient
.patch(interactionId, { sentToWallet: true })
.andThen(() =>
sendEncryptedRequest(session, pendingItem.walletInteraction),
)
.andThen(() =>
deepLinkClient.deepLinkToWallet(
{
sessionId: session.sessionId,
interactionId: pendingItem.interactionId,
},
childWindow,
),
)
}
return errAsync(SdkError('PendingItemNotFound', ''))
})
}
return okAsync(undefined)
})
},
resume,
disconnect: () => {},
destroy: () => {
subscriptions.unsubscribe()
Expand Down

0 comments on commit 396cd4d

Please sign in to comment.