@@ -13,14 +13,35 @@ let ipfs = null
13
13
* so use-ipfs calls can grab it from there rather than expecting
14
14
* it to be passed in.
15
15
*/
16
- export default function useIpfsFactory ( { commands } ) {
16
+ export default function useIpfsFactory ( ) {
17
17
const [ isIpfsReady , setIpfsReady ] = useState ( Boolean ( ipfs ) )
18
18
const [ ipfsInitError , setIpfsInitError ] = useState ( null )
19
19
20
20
useEffect ( ( ) => {
21
21
// The fn to useEffect should not return anything other than a cleanup fn,
22
22
// So it cannot be marked async, which causes it to return a promise,
23
23
// Hence we delegate to a async fn rather than making the param an async fn.
24
+ async function startIpfs ( ) {
25
+ if ( ipfs ) {
26
+ console . log ( 'IPFS already started' )
27
+ } else if ( window . ipfs && window . ipfs . enable ) {
28
+ console . log ( 'Found window.ipfs' )
29
+ ipfs = await window . ipfs . enable ( { commands : [ 'id' ] } )
30
+ } else {
31
+ try {
32
+ console . time ( 'IPFS Started' )
33
+ ipfs = await Ipfs . create ( )
34
+ console . timeEnd ( 'IPFS Started' )
35
+ } catch ( error ) {
36
+ console . error ( 'IPFS init error:' , error )
37
+ ipfs = null
38
+ setIpfsInitError ( error )
39
+ }
40
+ }
41
+
42
+ setIpfsReady ( Boolean ( ipfs ) )
43
+ }
44
+
24
45
startIpfs ( )
25
46
return function cleanup ( ) {
26
47
if ( ipfs && ipfs . stop ) {
@@ -30,28 +51,7 @@ export default function useIpfsFactory ({ commands }) {
30
51
setIpfsReady ( false )
31
52
}
32
53
}
33
- } )
34
-
35
- async function startIpfs ( ) {
36
- if ( ipfs ) {
37
- console . log ( 'IPFS already started' )
38
- } else if ( window . ipfs && window . ipfs . enable ) {
39
- console . log ( 'Found window.ipfs' )
40
- ipfs = await window . ipfs . enable ( { commands } )
41
- } else {
42
- try {
43
- console . time ( 'IPFS Started' )
44
- ipfs = await Ipfs . create ( )
45
- console . timeEnd ( 'IPFS Started' )
46
- } catch ( error ) {
47
- console . error ( 'IPFS init error:' , error )
48
- ipfs = null
49
- setIpfsInitError ( error )
50
- }
51
- }
52
-
53
- setIpfsReady ( Boolean ( ipfs ) )
54
- }
54
+ } , [ ] )
55
55
56
56
return { ipfs, isIpfsReady, ipfsInitError }
57
57
}
0 commit comments