UNPKG

@hakit/core

Version:

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

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