home-assistant-js-websocket
Advanced tools
Comparing version 6.1.1 to 7.0.0
@@ -9,3 +9,11 @@ import { Store } from "./store.js"; | ||
}; | ||
export declare const getCollection: <State>(conn: Connection, key: string, fetchCollection: (conn: Connection) => Promise<State>, subscribeUpdates?: ((conn: Connection, store: Store<State>) => Promise<UnsubscribeFunc>) | undefined) => Collection<State>; | ||
/** | ||
* | ||
* @param conn connection | ||
* @param key the key to store it on the connection. Must be unique for each collection. | ||
* @param fetchCollection fetch the current state. If undefined assumes subscribeUpdates receives current state | ||
* @param subscribeUpdates subscribe to updates on the current state | ||
* @returns | ||
*/ | ||
export declare const getCollection: <State>(conn: Connection, key: string, fetchCollection: ((conn: Connection) => Promise<State>) | undefined, subscribeUpdates?: ((conn: Connection, store: Store<State>) => Promise<UnsubscribeFunc>) | undefined) => Collection<State>; | ||
export declare const createCollection: <State>(key: string, fetchCollection: (conn: Connection) => Promise<State>, subscribeUpdates: ((conn: Connection, store: Store<State>) => Promise<UnsubscribeFunc>) | undefined, conn: Connection, onChange: (state: State) => void) => UnsubscribeFunc; |
import { createStore } from "./store.js"; | ||
/** | ||
* | ||
* @param conn connection | ||
* @param key the key to store it on the connection. Must be unique for each collection. | ||
* @param fetchCollection fetch the current state. If undefined assumes subscribeUpdates receives current state | ||
* @param subscribeUpdates subscribe to updates on the current state | ||
* @returns | ||
*/ | ||
export const getCollection = (conn, key, fetchCollection, subscribeUpdates) => { | ||
@@ -9,3 +17,8 @@ if (conn[key]) { | ||
let store = createStore(); | ||
const refresh = () => fetchCollection(conn).then((state) => store.setState(state, true)); | ||
const refresh = () => { | ||
if (!fetchCollection) { | ||
throw new Error("Collection does not support refresh"); | ||
} | ||
return fetchCollection(conn).then((state) => store.setState(state, true)); | ||
}; | ||
const refreshSwallow = () => refresh().catch((err) => { | ||
@@ -32,3 +45,5 @@ // Swallow errors if socket is connecting, closing or closed. | ||
conn.addEventListener("ready", refreshSwallow); | ||
refreshSwallow(); | ||
if (fetchCollection) { | ||
refreshSwallow(); | ||
} | ||
} | ||
@@ -35,0 +50,0 @@ const unsub = store.subscribe(subscriber); |
import { HassEntities, UnsubscribeFunc } from "./types.js"; | ||
import { Connection } from "./connection.js"; | ||
export declare const entitiesColl: (conn: Connection) => import("./collection.js").Collection<{}>; | ||
export declare const entitiesColl: (conn: Connection) => import("./collection.js").Collection<HassEntities>; | ||
export declare const subscribeEntities: (conn: Connection, onChange: (state: HassEntities) => void) => UnsubscribeFunc; |
@@ -104,4 +104,4 @@ import { getCollection } from "./collection.js"; | ||
export const entitiesColl = (conn) => atLeastHaVersion(conn.haVersion, 2022, 4, 0) | ||
? getCollection(conn, "_ent", () => Promise.resolve({}), subscribeUpdates) | ||
? getCollection(conn, "_ent", undefined, subscribeUpdates) | ||
: getCollection(conn, "_ent", legacyFetchEntities, legacySubscribeUpdates); | ||
export const subscribeEntities = (conn, onChange) => entitiesColl(conn).subscribe(onChange); |
@@ -781,2 +781,10 @@ (function (global, factory) { | ||
/** | ||
* | ||
* @param conn connection | ||
* @param key the key to store it on the connection. Must be unique for each collection. | ||
* @param fetchCollection fetch the current state. If undefined assumes subscribeUpdates receives current state | ||
* @param subscribeUpdates subscribe to updates on the current state | ||
* @returns | ||
*/ | ||
const getCollection = (conn, key, fetchCollection, subscribeUpdates) => { | ||
@@ -789,3 +797,8 @@ if (conn[key]) { | ||
let store = createStore(); | ||
const refresh = () => fetchCollection(conn).then((state) => store.setState(state, true)); | ||
const refresh = () => { | ||
if (!fetchCollection) { | ||
throw new Error("Collection does not support refresh"); | ||
} | ||
return fetchCollection(conn).then((state) => store.setState(state, true)); | ||
}; | ||
const refreshSwallow = () => refresh().catch((err) => { | ||
@@ -812,3 +825,5 @@ // Swallow errors if socket is connecting, closing or closed. | ||
conn.addEventListener("ready", refreshSwallow); | ||
refreshSwallow(); | ||
if (fetchCollection) { | ||
refreshSwallow(); | ||
} | ||
} | ||
@@ -1001,3 +1016,3 @@ const unsub = store.subscribe(subscriber); | ||
const entitiesColl = (conn) => atLeastHaVersion(conn.haVersion, 2022, 4, 0) | ||
? getCollection(conn, "_ent", () => Promise.resolve({}), subscribeUpdates) | ||
? getCollection(conn, "_ent", undefined, subscribeUpdates) | ||
: getCollection(conn, "_ent", legacyFetchEntities, legacySubscribeUpdates); | ||
@@ -1004,0 +1019,0 @@ const subscribeEntities = (conn, onChange) => entitiesColl(conn).subscribe(onChange); |
@@ -5,3 +5,3 @@ { | ||
"sideEffects": false, | ||
"version": "6.1.1", | ||
"version": "7.0.0", | ||
"description": "Home Assistant websocket client", | ||
@@ -8,0 +8,0 @@ "source": "lib/index.ts", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
168871
3517