1- import type { FetchBalance , FetchBalanceOptions } from '../types'
2- import type { FetchBalanceResult } from '@wagmi/core'
3- import { reactive , ref , watch } from 'vue'
4- import { fetchBalance as masterFetchBalance } from '@wagmi/core'
5- import { chain } from '../chain'
1+ import type { FetchBalanceResult } from '@wagmi/core'
2+ import { fetchBalance as masterFetchBalance } from '@wagmi/core'
3+ import { reactive , ref , watch } from 'vue'
4+
5+ import { chain } from '../chain'
6+ import type { FetchBalance , FetchBalanceOptions } from '../types'
67
78export function fetchBalance ( data : FetchBalance ) {
8- return masterFetchBalance ( {
9- chainId : data . chainId || chain . value . id ,
10- address : data . address ,
11- token : data . token ,
12- formatUnits : data . formatUnits
13- } )
9+ return masterFetchBalance ( {
10+ chainId : data . chainId || chain . value . id ,
11+ address : data . address ,
12+ token : data . token ,
13+ formatUnits : data . formatUnits
14+ } )
1415}
1516
1617export function useFetchBalance ( params : FetchBalance , options ?: FetchBalanceOptions ) {
17- const loaded = ref ( false )
18- const fetching = ref ( false )
19- const data = reactive < FetchBalanceResult > ( {
20- decimals : 0 ,
21- formatted : '' ,
22- symbol : '' ,
23- value : 0n
24- } )
18+ const loaded = ref ( false )
19+ const fetching = ref ( false )
20+ const data = reactive < FetchBalanceResult > ( {
21+ decimals : 0 ,
22+ formatted : '' ,
23+ symbol : '' ,
24+ value : 0n
25+ } )
2526
26- let timeoutHandler : number
27- let updateHandler : number
28- let currentChain = params . chainId || chain . value . id
29- const fetchOptions : FetchBalanceOptions = {
30- disableAutoFetch : options ?. disableAutoFetch || false ,
31- autoReloadTime : options ?. autoReloadTime || 30000 ,
32- disableAutoReload : options ?. disableAutoReload || false
33- }
27+ let timeoutHandler : number
28+ let updateHandler : number
29+ let currentChain = params . chainId || chain . value . id
30+ const fetchOptions : FetchBalanceOptions = {
31+ disableAutoFetch : options ?. disableAutoFetch || false ,
32+ autoReloadTime : options ?. autoReloadTime || 30000 ,
33+ disableAutoReload : options ?. disableAutoReload || false
34+ }
3435
35- async function fetch ( ) {
36- if ( ! fetching . value || ! loaded . value ) {
37- fetching . value = true
36+ async function fetch ( ) {
37+ if ( ! fetching . value || ! loaded . value ) {
38+ fetching . value = true
3839
39- await fetchBalance ( params )
40- . then ( fetchData => {
41- if ( fetchData . value !== data . value || ! loaded . value ) {
42- data . decimals = fetchData . decimals
43- data . formatted = fetchData . formatted
44- data . symbol = fetchData . symbol
45- data . value = fetchData . value
46- }
47- } )
48- . finally ( ( ) => {
49- loaded . value = true
50- fetching . value = false
40+ await fetchBalance ( params )
41+ . then ( ( fetchData ) => {
42+ if ( fetchData . value !== data . value || ! loaded . value ) {
43+ data . decimals = fetchData . decimals
44+ data . formatted = fetchData . formatted
45+ data . symbol = fetchData . symbol
46+ data . value = fetchData . value
47+ }
48+ } )
49+ . finally ( ( ) => {
50+ loaded . value = true
51+ fetching . value = false
5152
52- runTimeout ( )
53- } )
54- }
53+ runTimeout ( )
54+ } )
5555 }
56+ }
5657
57- function reload ( ) {
58- clearTimeout ( timeoutHandler )
59- return fetch ( )
60- }
58+ function reload ( ) {
59+ clearTimeout ( timeoutHandler )
60+ return fetch ( )
61+ }
6162
62- function disableAutoReload ( ) {
63- fetchOptions . disableAutoReload = true
64- }
63+ function disableAutoReload ( ) {
64+ fetchOptions . disableAutoReload = true
65+ }
6566
66- function runTimeout ( ) {
67- if ( fetchOptions . disableAutoReload !== true ) {
68- // @ts -ignore
69- timeoutHandler = setTimeout ( reload , fetchOptions . autoReloadTime || 30000 )
70- }
67+ function runTimeout ( ) {
68+ if ( fetchOptions . disableAutoReload !== true ) {
69+ // @ts -ignore
70+ timeoutHandler = setTimeout ( reload , fetchOptions . autoReloadTime || 30000 )
7171 }
72+ }
7273
73- function resetData ( ) {
74- loaded . value = false
75- data . decimals = 0
76- data . formatted = ''
77- data . symbol = ''
78- data . value = 0n
79- }
74+ function resetData ( ) {
75+ loaded . value = false
76+ data . decimals = 0
77+ data . formatted = ''
78+ data . symbol = ''
79+ data . value = 0n
80+ }
8081
81- function update ( ) {
82- clearTimeout ( updateHandler )
83- currentChain = chain . value . id
84- resetData ( )
85- reload ( )
86- }
82+ function update ( ) {
83+ clearTimeout ( updateHandler )
84+ currentChain = chain . value . id
85+ resetData ( )
86+ reload ( )
87+ }
8788
88- if ( fetchOptions . disableAutoFetch !== true ) {
89- fetch ( )
90- }
89+ if ( fetchOptions . disableAutoFetch !== true ) {
90+ fetch ( )
91+ }
9192
92- if ( params . chainId === undefined ) {
93- watch ( ( ) => chain . value . id , ( newChainId ) => {
94- if ( newChainId !== currentChain ) {
95- // @ts -ignore
96- updateHandler = setTimeout ( update )
97- }
98- } )
99- }
93+ if ( params . chainId === undefined ) {
94+ watch (
95+ ( ) => chain . value . id ,
96+ ( newChainId ) => {
97+ if ( newChainId !== currentChain ) {
98+ // @ts -ignore
99+ updateHandler = setTimeout ( update )
100+ }
101+ }
102+ )
103+ }
100104
101- return {
102- loaded,
103- fetching,
104- data,
105- fetch,
106- reload,
107- disableAutoReload
108- }
109- }
105+ return {
106+ loaded,
107+ fetching,
108+ data,
109+ fetch,
110+ reload,
111+ disableAutoReload
112+ }
113+ }
0 commit comments