fixed minor socket connection issues

This commit is contained in:
ari committed 2022-07-29 10:45:42 -04:00
1 parent 9a26fc46d5
commit ea86f7f763
4 files changed
+76 -10

No files matched your search

+22 -1
View File
@@ -8,8 +8,16 @@ export type MockHAEntitiesProps =
}
| {
loading: false;
error?: false;
entities: HassEntities;
entitySource: Record<string, EntitySource>;
}
| {
loading: false;
error: {
entities: any;
entitySource: any;
};
};
export const useMockHAEntities = (args: MockHAEntitiesProps) => {
if (args.loading) {
@@ -23,7 +31,7 @@ export const useMockHAEntities = (args: MockHAEntitiesProps) => {
ready: false,
}
);
} else {
} else if (!args.error) {
return createHAEntitiesState(
{
additional: {},
@@ -36,5 +44,18 @@ export const useMockHAEntities = (args: MockHAEntitiesProps) => {
collection: args.entitySource,
}
);
} else {
return createHAEntitiesState(
{
additional: {},
ready: false,
error: args.error.entities,
},
{
additional: {},
ready: false,
error: args.error.entitySource,
}
);
}
};
+29 -7
View File
@@ -6,6 +6,7 @@ type _HACollectionState<T> =
| {
ready: false;
collection?: undefined;
error?: string;
}
| {
ready: true;
@@ -26,19 +27,23 @@ export const useHassCollection = <T, D extends {} = {}>(
if (conn.status === "loaded") {
return getHACollection(conn.connection);
}
}, [conn.status]);
}, [conn, getHACollection]);
useEffect(() => {
if (collection) {
setState({
ready: true,
collection: collection.state,
});
const unsub = collection.subscribe((d) => {
if (collection.state) {
setState({
ready: true,
collection: d,
collection: collection.state,
});
}
const unsub = collection.subscribe((d) => {
if (d) {
setState({
ready: true,
collection: d,
});
}
});
return () => {
unsub();
@@ -46,6 +51,23 @@ export const useHassCollection = <T, D extends {} = {}>(
}
}, [collection, state.ready]);
useEffect(() => {
if (conn.status === "error") {
if (!state.ready && conn.error !== state.error) {
setState({
ready: false,
error: conn.error,
});
}
}
}, [conn]);
if (state.ready) {
if (!state.collection) {
throw Error("What?");
}
}
return {
...state,
get additional(): Partial<D> {