@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
49 lines (48 loc) • 1.36 kB
JavaScript
import { useState as p, useRef as E, useCallback as g, useEffect as w, useMemo as y } from "react";
import "lodash";
import { X as F } from "../../../index-BDZyTvOW.js";
import "../useLocale/locales/index.js";
import "home-assistant-js-websocket";
import "../../utils/date.js";
import { u as W } from "../../../index-CPm3GSga.js";
import "@iconify/react";
import { useDebouncedCallback as _ } from "use-debounce";
import { subscribeForecast as k, getForecast as C } from "./helpers.js";
function A(n, v) {
const i = F((r) => r.connection), c = W(n), [a, d] = p(null), [f, h] = p(null), e = E(!1), t = E(void 0), { type: b = "daily" } = v || {}, m = g(
(r, o) => k(i, r, o, (s) => {
e.current && h(s);
}).catch((s) => {
e.current = !1, s instanceof Error && d(s.message);
}),
[i]
), l = _(
async (r, o) => {
if (t.current) {
const u = await t.current;
u && u(), t.current = void 0;
}
e.current = !0, t.current = await m(r, o);
},
100,
{
trailing: !0,
leading: !0
}
);
if (w(() => (l(n, b), () => {
e.current = !1, t.current && t.current();
}), [b, l, m, n]), a)
throw a;
return y(() => {
const r = C(c.attributes, f);
return {
...c,
forecast: r
};
}, [c, f]);
}
export {
A as useWeather
};
//# sourceMappingURL=index.js.map