UNPKG

3.32 kBSource Map (JSON)View Raw
1{"version":3,"sources":["useGeolocationEvents.js"],"names":["useGeolocationEvents","options","geolocationStandardOptions","watchId","onChangeRef","setOnChangeRef","onErrorRef","setOnErrorRef","isSupported","window","navigator","onSuccess","successEvent","current","onError","err","geolocation","watchPosition","clearWatch","Object","freeze","onChange"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;;;;;;;AAQA,IAAMA,oBAAoB,GAAG,SAAvBA,oBAAuB,GAA0C;AAAA,MAAzCC,OAAyC,uEAA/BC,sCAA+B;AACrE,MAAMC,OAAO,GAAG,oBAAhB;;AADqE,6BAE/B,uCAF+B;AAAA;AAAA,MAE9DC,WAF8D;AAAA,MAEjDC,cAFiD;;AAAA,8BAGjC,uCAHiC;AAAA;AAAA,MAG9DC,UAH8D;AAAA,MAGlDC,aAHkD;;AAIrE,MAAMC,WAAW,GAAG,OAAOC,MAAP,KAAkB,WAAlB,IAAiC,iBAAiBA,MAAM,CAACC,SAA7E;AAEA,wBAAU,YAAM;AACd,QAAMC,SAAS,GAAG,SAAZA,SAAY,CAACC,YAAD,EAAkB;AAClC,UAAIR,WAAW,CAACS,OAAhB,EAAyB;AACvBT,QAAAA,WAAW,CAACS,OAAZ,CAAoBD,YAApB;AACD;AACF,KAJD;;AAKA,QAAME,OAAO,GAAG,SAAVA,OAAU,CAACC,GAAD,EAAS;AACvB,UAAIT,UAAU,CAACO,OAAf,EAAwB;AACtBP,QAAAA,UAAU,CAACO,OAAX,CAAmBE,GAAnB;AACD;AACF,KAJD;;AAMA,QAAIP,WAAJ,EAAiB;AACfL,MAAAA,OAAO,CAACU,OAAR,GAAkBJ,MAAM,CAACC,SAAP,CAAiBM,WAAjB,CAA6BC,aAA7B,CAA2CN,SAA3C,EAAsDG,OAAtD,EAA+Db,OAA/D,CAAlB;AACD;;AAED,WAAO,YAAM;AACX,UAAIO,WAAJ,EAAiB;AACfC,QAAAA,MAAM,CAACC,SAAP,CAAiBM,WAAjB,CAA6BE,UAA7B,CAAwCf,OAAO,CAACU,OAAhD;AACD;AACF,KAJD;AAKD,GArBD,EAqBG,EArBH;AAuBA,SAAO,CAACL,WAAD,GAAe,0CAAyB,sCAAzB,CAAf,GAAkFW,MAAM,CAACC,MAAP,CAAc;AACrGZ,IAAAA,WAAW,EAAXA,WADqG;AAErGa,IAAAA,QAAQ,EAAEhB,cAF2F;AAGrGS,IAAAA,OAAO,EAAEP;AAH4F,GAAd,CAAzF;AAKD,CAlCD;;eAoCeP,oB","sourcesContent":["import { useEffect, useRef } from 'react';\nimport createHandlerSetter from './utils/createHandlerSetter';\nimport createCbSetterErrorProxy from './utils/createCbSetterErrorProxy';\nimport geolocationStandardOptions from './utils/geolocationStandardOptions';\n\n/**\n * Returns a frozen object of callback setters to handle the geolocation events.<br/>\n * So far, the supported methods are: `onChange`, invoked when the position changes and `onError`, invoked when\n * an error occur while retrieving the position.<br/>\n * The returned object also contains the `isSupported` boolean flag reporting whether the geolocation API is supported.\n */\nconst useGeolocationEvents = (options = geolocationStandardOptions) => {\n const watchId = useRef();\n const [onChangeRef, setOnChangeRef] = createHandlerSetter();\n const [onErrorRef, setOnErrorRef] = createHandlerSetter();\n const isSupported = typeof window !== 'undefined' && 'geolocation' in window.navigator;\n\n useEffect(() => {\n const onSuccess = (successEvent) => {\n if (onChangeRef.current) {\n onChangeRef.current(successEvent);\n }\n };\n const onError = (err) => {\n if (onErrorRef.current) {\n onErrorRef.current(err);\n }\n };\n\n if (isSupported) {\n watchId.current = window.navigator.geolocation.watchPosition(onSuccess, onError, options);\n }\n\n return () => {\n if (isSupported) {\n window.navigator.geolocation.clearWatch(watchId.current);\n }\n };\n }, []);\n\n return !isSupported ? createCbSetterErrorProxy('The Geolocation API is not supported') : Object.freeze({\n isSupported,\n onChange: setOnChangeRef,\n onError: setOnErrorRef,\n });\n};\n\nexport default useGeolocationEvents;\n"],"file":"useGeolocationEvents.js"}
\No newline at end of file