This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "fix: decouple pairing provider (#3753)"
- Loading branch information
Showing
12 changed files
with
100 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getPairingUri } from 'src/logic/wallets/pairing/utils' | ||
import { useEffect, useState } from 'react' | ||
|
||
export const useGetPairingUri = (): string | undefined => { | ||
const onboardUri = getPairingUri() | ||
const [uri, setUri] = useState<string>() | ||
|
||
useEffect(() => { | ||
setTimeout(() => { | ||
setUri(getPairingUri()) | ||
}, 100) | ||
}, [onboardUri]) | ||
|
||
return uri | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,13 @@ | ||
import { useCallback, useEffect, useMemo, useState } from 'react' | ||
import { IEventEmitter } from '@walletconnect/types' | ||
import { useEffect } from 'react' | ||
|
||
import { getPairingUri } from 'src/logic/wallets/pairing/utils' | ||
import onboard from 'src/logic/wallets/onboard' | ||
import { getPairingProvider, PAIRING_MODULE_NAME } from 'src/logic/wallets/pairing/module' | ||
|
||
let defaultEvents: IEventEmitter[] | ||
|
||
// These handful of events relate to the session lifecycle | ||
enum WC_EVENT { | ||
CONNECT = 'connect', | ||
DISCONNECT = 'disconnect', | ||
DISPLAY_URI = 'display_uri', | ||
SESSION_UPDATE = 'wc_sessionUpdate', | ||
} | ||
|
||
const usePairing = (): { uri: string } => { | ||
const provider = useMemo(getPairingProvider, []) | ||
|
||
const [uri, setUri] = useState<string>(getPairingUri(provider.wc.uri)) | ||
|
||
// Pairing session lifecycle | ||
const createPairingSession = useCallback(() => { | ||
if (!provider.wc.connected) { | ||
provider.enable() | ||
provider.wc.createSession() | ||
} | ||
}, [provider]) | ||
import { initPairing, isPairingConnected } from 'src/logic/wallets/pairing/utils' | ||
|
||
const usePairing = (): void => { | ||
useEffect(() => { | ||
if (!defaultEvents) { | ||
defaultEvents = (provider.wc as any)._eventManager._eventEmitters as IEventEmitter[] | ||
if (!isPairingConnected()) { | ||
initPairing() | ||
} | ||
|
||
// Resest event listeners to default | ||
;(provider.wc as any)._eventManager._eventEmitters = defaultEvents | ||
|
||
// Watch for URI change | ||
Object.values(WC_EVENT).forEach((event) => | ||
provider.wc.on(event, () => { | ||
const pairingUri = getPairingUri(provider.wc.uri) | ||
setUri(pairingUri) | ||
}), | ||
) | ||
|
||
// Watch for connection | ||
provider.wc.on(WC_EVENT.CONNECT, () => { | ||
onboard().walletSelect(PAIRING_MODULE_NAME) | ||
}) | ||
|
||
// Handle disconnection (`walletReset()` occurs inside pairing module) | ||
provider.wc.on(WC_EVENT.DISCONNECT, createPairingSession) | ||
provider.wc.on(WC_EVENT.SESSION_UPDATE, (_, { params }) => { | ||
const didRevokeSession = params[0]?.approved === false | ||
if (didRevokeSession) { | ||
createPairingSession() | ||
} | ||
}) | ||
|
||
// Create pairing session if one isn't already active | ||
createPairingSession() | ||
}, [createPairingSession, provider.wc]) | ||
|
||
return { uri } | ||
}, []) | ||
} | ||
|
||
export default usePairing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.