UNPKG

@hakit/core

Version:

A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.

50 lines (49 loc) 1.44 kB
import { useState as p, useRef as E, useCallback as w, useEffect as y, useMemo as F } from "react"; import "lodash"; import "../../utils/light/index.js"; import "../useLocale/locales/index.js"; import { u as S } from "../../../index-DnHwXFlq.js"; import "../../HassConnect/HassContext.js"; import { u as W } from "../../../index-4TfmyaLW.js"; import "@iconify/react"; import { useDebouncedCallback as _ } from "use-debounce"; import "home-assistant-js-websocket"; import { subscribeForecast as k, getForecast as C } from "./helpers.js"; function J(n, d) { const { useStore: h } = S(), i = h((e) => e.connection), c = W(n), [a, g] = p(null), [f, v] = p(null), t = E(!1), r = E(void 0), { type: b = "daily" } = d || {}, m = w( (e, o) => k(i, e, o, (s) => { t.current && v(s); }).catch((s) => { t.current = !1, s instanceof Error && g(s.message); }), [i] ), l = _( async (e, o) => { if (r.current) { const u = await r.current; u && u(), r.current = void 0; } t.current = !0, r.current = await m(e, o); }, 100, { trailing: !0, leading: !0 } ); if (y(() => (l(n, b), () => { t.current = !1, r.current && r.current(); }), [b, l, m, n]), a) throw a; return F(() => { const e = C(c.attributes, f); return { ...c, forecast: e }; }, [c, f]); } export { J as useWeather }; //# sourceMappingURL=index.js.map