@xyo-network/react-chain-provider 1.20.9 → 1.20.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/contexts/gateway/Provider.d.ts.map +1 -1
- package/dist/browser/hooks/client/helpers/findCaveat.d.ts +2 -2
- package/dist/browser/hooks/client/helpers/findCaveat.d.ts.map +1 -1
- package/dist/browser/hooks/client/permissions/usePermissions.d.ts +2 -6
- package/dist/browser/hooks/client/permissions/usePermissions.d.ts.map +1 -1
- package/dist/browser/hooks/client/permissions/usePermissionsAccounts.d.ts +2 -1
- package/dist/browser/hooks/client/permissions/usePermissionsAccounts.d.ts.map +1 -1
- package/dist/browser/hooks/client/useClientFromWallet.d.ts +2 -11
- package/dist/browser/hooks/client/useClientFromWallet.d.ts.map +1 -1
- package/dist/browser/hooks/client/useGatewayFromWallet.d.ts +2 -5
- package/dist/browser/hooks/client/useGatewayFromWallet.d.ts.map +1 -1
- package/dist/browser/hooks/helpers/getXyoClient.d.ts +2 -5
- package/dist/browser/hooks/helpers/getXyoClient.d.ts.map +1 -1
- package/dist/browser/index.mjs +167 -348
- package/dist/browser/index.mjs.map +1 -1
- package/dist/browser/types/GatewayFromWallet.d.ts +2 -7
- package/dist/browser/types/GatewayFromWallet.d.ts.map +1 -1
- package/package.json +19 -17
- package/src/contexts/gateway/Provider.tsx +1 -1
- package/src/hooks/client/helpers/findCaveat.ts +2 -20
- package/src/hooks/client/permissions/usePermissions.ts +2 -14
- package/src/hooks/client/permissions/usePermissionsAccounts.ts +2 -38
- package/src/hooks/client/useClientFromWallet.ts +2 -98
- package/src/hooks/client/useGatewayFromWallet.ts +2 -30
- package/src/hooks/helpers/getXyoClient.ts +2 -46
- package/src/types/GatewayFromWallet.ts +2 -8
|
@@ -1,98 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
useCallback, useEffect, useSyncExternalStore,
|
|
5
|
-
} from 'react'
|
|
6
|
-
|
|
7
|
-
import { getXyoClient } from '../helpers/index.ts'
|
|
8
|
-
|
|
9
|
-
interface ClientState {
|
|
10
|
-
client?: XyoClient | null
|
|
11
|
-
error: Error | null
|
|
12
|
-
isLoading: boolean
|
|
13
|
-
timedout: boolean
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
let currentState: ClientState = {
|
|
17
|
-
client: undefined,
|
|
18
|
-
error: null,
|
|
19
|
-
isLoading: false,
|
|
20
|
-
timedout: false,
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const listeners = new Set<() => void>()
|
|
24
|
-
|
|
25
|
-
const emitChange = () => {
|
|
26
|
-
for (const listener of listeners) listener()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const updateState = (newState: Partial<ClientState>) => {
|
|
30
|
-
currentState = { ...currentState, ...newState }
|
|
31
|
-
emitChange()
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const initializeClient = async (timeout?: number) => {
|
|
35
|
-
if (currentState.isLoading || currentState.client) return
|
|
36
|
-
|
|
37
|
-
updateState({ isLoading: true, error: null })
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
const client = await getXyoClient(timeout)
|
|
41
|
-
if (client === null) {
|
|
42
|
-
updateState({
|
|
43
|
-
client: null, timedout: true, isLoading: false,
|
|
44
|
-
})
|
|
45
|
-
return
|
|
46
|
-
}
|
|
47
|
-
updateState({
|
|
48
|
-
client, isLoading: false, error: null,
|
|
49
|
-
})
|
|
50
|
-
} catch (error) {
|
|
51
|
-
console.error('Error initializing XyoClient', error)
|
|
52
|
-
updateState({ error: error as Error, isLoading: false })
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const subscribe = (listener: () => void, timeout?: number) => {
|
|
57
|
-
listeners.add(listener)
|
|
58
|
-
|
|
59
|
-
void initializeClient(timeout)
|
|
60
|
-
|
|
61
|
-
return () => {
|
|
62
|
-
listeners.delete(listener)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const getSnapshot = (): ClientState => currentState
|
|
67
|
-
|
|
68
|
-
export const useClientFromWallet = (timeout?: number) => {
|
|
69
|
-
const subscribeWithTimeout = useCallback((listener: () => void) => subscribe(listener, timeout), [timeout])
|
|
70
|
-
const clientState = useSyncExternalStore(subscribeWithTimeout, getSnapshot)
|
|
71
|
-
|
|
72
|
-
useEffect(() => {
|
|
73
|
-
// if client appears after timeout
|
|
74
|
-
let listener = () => {
|
|
75
|
-
// Update state when client becomes available
|
|
76
|
-
updateState({
|
|
77
|
-
client: globalThis.xyo?.client,
|
|
78
|
-
isLoading: false,
|
|
79
|
-
error: null,
|
|
80
|
-
})
|
|
81
|
-
// Notify listeners of the change
|
|
82
|
-
emitChange()
|
|
83
|
-
}
|
|
84
|
-
// If we have timed out and still no client, listen for the plugin-ready event
|
|
85
|
-
if (clientState.timedout && isNull(clientState.client) && !clientState.isLoading) {
|
|
86
|
-
globalThis.addEventListener('xyo:plugin-ready', listener)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return () => {
|
|
90
|
-
globalThis.removeEventListener('xyo:plugin-ready', listener)
|
|
91
|
-
}
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
return clientState
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** @deprecated - use useClientFromWallet instead */
|
|
98
|
-
export const useClient = useClientFromWallet
|
|
1
|
+
/** @deprecated Import from `@xyo-network/react-chain-client` instead */
|
|
2
|
+
export { useClient, useClientFromWallet } from '@xyo-network/react-chain-client'
|
|
@@ -1,30 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import type { GatewayFromWallet } from '../../types/index.ts'
|
|
5
|
-
import { useClientFromWallet } from './useClientFromWallet.ts'
|
|
6
|
-
|
|
7
|
-
export const useGatewayFromWallet = (gatewayName?: GatewayName, timeout?: number): GatewayFromWallet => {
|
|
8
|
-
const {
|
|
9
|
-
client, isLoading, error, timedout,
|
|
10
|
-
} = useClientFromWallet(timeout)
|
|
11
|
-
|
|
12
|
-
const resolveGateway = () => {
|
|
13
|
-
// null client means we do not have to wait for timedout
|
|
14
|
-
if (isNull(client)) return null
|
|
15
|
-
if (timedout && !isLoading && isDefined(gatewayName)) {
|
|
16
|
-
return null
|
|
17
|
-
}
|
|
18
|
-
return client?.gateways?.[gatewayName!]
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
gateway: gatewayName ? resolveGateway() : undefined,
|
|
23
|
-
isLoading,
|
|
24
|
-
error,
|
|
25
|
-
timedout,
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** @deprecated - useGatewayFromWallet */
|
|
30
|
-
export const useGateway = useGatewayFromWallet
|
|
1
|
+
/** @deprecated Import from `@xyo-network/react-chain-client` instead */
|
|
2
|
+
export { useGateway, useGatewayFromWallet } from '@xyo-network/react-chain-client'
|
|
@@ -1,46 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const CLIENT_LISTENER_TIMEOUT = 500
|
|
5
|
-
|
|
6
|
-
const hasXyoClient = () => {
|
|
7
|
-
return 'client' in globalThis.xyo
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const listenForClientInjection = (onClientReady: () => void, timeout: number, onTimeout: () => void) => {
|
|
11
|
-
let resolved = false
|
|
12
|
-
const listener: EventListener = () => {
|
|
13
|
-
onClientReady()
|
|
14
|
-
resolved = true
|
|
15
|
-
}
|
|
16
|
-
globalThis.addEventListener('xyo:plugin-ready', listener)
|
|
17
|
-
setTimeout(() => {
|
|
18
|
-
if (!resolved) {
|
|
19
|
-
globalThis.removeEventListener('xyo:plugin-ready', listener)
|
|
20
|
-
onTimeout()
|
|
21
|
-
}
|
|
22
|
-
}, timeout)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
type ReturnType = XyoClient | undefined | null
|
|
26
|
-
|
|
27
|
-
export async function getXyoClient(timeout = CLIENT_LISTENER_TIMEOUT): Promise<ReturnType> {
|
|
28
|
-
// if no xyo object, we can bail early
|
|
29
|
-
if (isUndefined(globalThis.xyo)) {
|
|
30
|
-
return null
|
|
31
|
-
}
|
|
32
|
-
return hasXyoClient()
|
|
33
|
-
? globalThis.xyo.client
|
|
34
|
-
// listen for the XyoWallet to be injected
|
|
35
|
-
: await new Promise<ReturnType>((resolve) => {
|
|
36
|
-
listenForClientInjection(
|
|
37
|
-
() => {
|
|
38
|
-
resolve(globalThis.xyo.client)
|
|
39
|
-
},
|
|
40
|
-
timeout,
|
|
41
|
-
() => {
|
|
42
|
-
resolve(null)
|
|
43
|
-
},
|
|
44
|
-
)
|
|
45
|
-
})
|
|
46
|
-
}
|
|
1
|
+
/** @deprecated Import from `@xyo-network/react-chain-client` instead */
|
|
2
|
+
export { getXyoClient, listenForClientInjection } from '@xyo-network/react-chain-client'
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export type GatewayFromWallet = {
|
|
4
|
-
error?: Error | null
|
|
5
|
-
gateway: XyoGatewayRunner | null | undefined
|
|
6
|
-
isLoading: boolean
|
|
7
|
-
timedout: boolean
|
|
8
|
-
}
|
|
1
|
+
/** @deprecated Import from `@xyo-network/react-chain-client` instead */
|
|
2
|
+
export type { GatewayFromWallet } from '@xyo-network/react-chain-client'
|