Skip to content

Commit 4ec28a2

Browse files
committed
use events to know if gateway is ready
1 parent b2ee5e2 commit 4ec28a2

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@xylabs/assert": "~5.0.10",
3434
"@xylabs/delay": "~5.0.10",
3535
"@xylabs/hex": "~5.0.10",
36+
"@xylabs/react-promise": "~7.0.4",
3637
"@xylabs/typeof": "~5.0.10",
3738
"@xyo-network/id-payload-plugin": "~5.0.7",
3839
"@xyo-network/payload-builder": "~5.0.7",

src/getXyoGateway.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,42 @@ import type { GatewayName, XyoGatewayProvider } from '@xyo-network/xl1-protocol'
22

33
const localGatewayName = 'local' as GatewayName
44

5+
const GATEWAY_LISTENER_TIMEOUT = 5000
6+
57
export const hasXyoWalletGateway = () => {
68
return 'client' in globalThis.xyo
79
}
810

9-
export function getXyoGateway(options: { assert: true }): XyoGatewayProvider
10-
export function getXyoGateway(options?: { assert?: boolean }): XyoGatewayProvider | undefined {
11+
export const listenForWalletInjection = (onPluginReady: () => void, onTimeout: () => void) => {
12+
let resolved = false
13+
const listener: EventListener = (e) => {
14+
onPluginReady()
15+
resolved = true
16+
}
17+
globalThis.addEventListener('xyo:plugin-ready', listener)
18+
setTimeout(() => {
19+
if (!resolved) {
20+
onTimeout()
21+
}
22+
}, GATEWAY_LISTENER_TIMEOUT)
23+
}
24+
25+
export async function getXyoGateway(options: { assert: true }): Promise<XyoGatewayProvider>
26+
export async function getXyoGateway(options?: { assert?: boolean }): Promise<XyoGatewayProvider | undefined> {
1127
const { assert } = options ?? {}
1228
if (hasXyoWalletGateway()) {
1329
return globalThis.xyo.client?.gateways?.[localGatewayName]
1430
} else {
15-
console.error('XYO Wallet not installed')
16-
if (assert) {
17-
throw new Error('XYO Wallet not installed')
18-
} else {
19-
return undefined
20-
}
31+
// listen for the XyoWallet to be injected
32+
return await new Promise<XyoGatewayProvider | undefined>((resolve, reject) => {
33+
listenForWalletInjection(
34+
() => {
35+
resolve(globalThis.xyo.client?.gateways?.[localGatewayName])
36+
},
37+
() => {
38+
reject(new Error('XYO Wallet not installed'))
39+
}
40+
)
41+
})
2142
}
2243
}

src/hooks/useDefaultGateway.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import type { XyoGatewayProvider } from '@xyo-network/xl1-protocol'
22
import { useEffect, useState } from 'react'
33

44
import { getXyoGateway } from '../getXyoGateway.ts'
5+
import { usePromise } from '@xylabs/react-promise'
56

67
export const useDefaultGateway = () => {
78
const [gateway, setGateway] = useState<XyoGatewayProvider>()
89
const [error, setError] = useState<Error | null>(null)
910

10-
useEffect(() => {
11+
usePromise(async () => {
1112
try {
12-
const xyoGateway = getXyoGateway({ assert: true })
13+
const xyoGateway = await getXyoGateway({ assert: true })
1314
setGateway(xyoGateway)
1415
} catch {
1516
setError(new Error('XYO Gateway not available'))

0 commit comments

Comments
 (0)