import { parse } from 'graphql' ; import { useRef , useCallback , EffectCallback , useEffect } from 'react' ; import { Options , Deps } from './types' ; export function isFalsy ( value ) { if ( typeof value === 'boolean' ) { return ! value ; } return value === undefined || value === null ; } export const getDeps = ( options ? ) : Deps => { if ( options && Array . isArray ( options . deps ) ) return options . deps ; return [ ] ; } ; export function isResolve ( arg ) { if ( ! arg ) return true ; if ( typeof arg !== 'function' ) return true ; try { arg ( ) ; return true ; } catch ( error ) { return false ; } } export const getVariables = ( options ) : any => { if ( ! options . variables ) return { } ; if ( typeof options . variables !== 'function' ) return options . variables ; try { return options . variables ( { } ) ; } catch ( error ) { return false ; } } ; param deps export const getDepsMaps = ( deps ) => { return deps . reduce ( ( result , cur , i ) => { result [ i ] = cur ; return result ; } , { } as { [ key ] : any ; } ) ; } ; export function useUnmounted ( ) { const unmountedRef = useRef < boolean > ( false ) ; const isUnmouted = useCallback ( ( ) => unmountedRef . current , [ ] ) ; useEffect ( ( ) => { unmountedRef . current = false ; return ( ) => { unmountedRef . current = true ; } ; } ) ; return isUnmouted ; } export const useEffectOnce = ( effect ) => { useEffect ( effect , [ ] ) ; } ; export const useUnmount = ( fn ) : void => { const fnRef = useRef ( fn ) ; fnRef . current = fn ; useEffectOnce ( ( ) => ( ) => fnRef . current ( ) ) ; } ; export const getOperationName = ( input ) => { try { return ( parse ( input ) . definitions [ 0 ] as any ) . name . value ; } catch ( error ) { return null ; } } ;