Skip to content

Commit

Permalink
fix: use normal watch
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 18, 2023
1 parent ed8ba0c commit 30b449f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 42 deletions.
33 changes: 10 additions & 23 deletions reactive_home/src/composeables/useNewBoolean.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import type { FullfilledUseState } from "./useState.ts";
import {
useDebounceFn,
ref,
watch,
watchPausable,
extendRef,
computed,
nextTick,
} from "../dep.ts";
import { useDebounceFn, ref, watch, extendRef, computed } from "../dep.ts";
import type { MessageBase, HassEntity, UnwrapNestedRefs } from "../dep.ts";
import { connection } from "../hass/connection.ts";
import { stringBoolToBool } from "../lib/util.ts";
Expand Down Expand Up @@ -44,22 +36,20 @@ export function useNewBoolean(state: FullfilledUseState, debug = false) {
const lastChanged = ref(state.value.last_changed);

// Local state changes
const { pause, resume } = watchPausable(
localValue,
(newLocalValue: typeof localValue.value) => {
if (debug) {
console.log(`call(${state.value.entity_id}): updateHASSState`);
}
updateHASSState(newLocalValue);
watch(localValue, (newLocalValue, oldLocalValue) => {
if (newLocalValue === oldLocalValue) {
return;
}
);

resume();
if (debug) {
console.log(`call(${state.value.entity_id}): updateHASSState`);
}
updateHASSState(newLocalValue);
});

// Incoming state changes from hass
watch(
() => state.value,
async (newEntityState) => {
(newEntityState) => {
const contextIndex = skipContexts.findIndex(
(value) => value === newEntityState.context.id
);
Expand All @@ -68,11 +58,8 @@ export function useNewBoolean(state: FullfilledUseState, debug = false) {
skipContexts.splice(contextIndex, 1);
return;
}
pause();
localValue.value = stringBoolToBool(newEntityState.state);
lastChanged.value = newEntityState.last_changed;
await nextTick();
resume();
}
);

Expand Down
43 changes: 24 additions & 19 deletions reactive_home/src/composeables/useNewLight.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type { FullfilledUseState } from "./useState.ts";
import {
useDebounceFn,
reactive,
watch,
watchPausable,
nextTick,
} from "../dep.ts";
import { useDebounceFn, reactive, watch } from "../dep.ts";
import type { MessageBase, HassEntity } from "../dep.ts";
import { connection } from "../hass/connection.ts";
import { stringBoolToBool } from "../lib/util.ts";
Expand Down Expand Up @@ -61,29 +55,43 @@ export function useNewLight(state: FullfilledUseState, debug = false) {
brightness: getBrightnessFromAttribute(state.value),
entity_id: state.value.entity_id,
lastChanged: new Date(state.value.last_changed),
lock: false,
});

// Local state changes
const { pause, resume } = watchPausable(
localValues,
(newLocalValues: typeof localValues) => {
watch(
() => {
return { value: localValues.value, brightness: localValues.brightness };
},
(newLocalValues, oldLocalValues) => {
if (
newLocalValues.value === oldLocalValues.value &&
newLocalValues.brightness === oldLocalValues.brightness
) {
return;
}
if (debug) {
console.log(`call(${state.value.entity_id}): updateHASSState`);
}
updateHASSState(newLocalValues.value, newLocalValues.brightness);
}
);

resume();

// Incoming state changes from hass
watch(
() => state.value,
async (newEntityState) => {
pause();
(newEntityState) => {
if (debug) {
console.log(
`incoming(${state.value.entity_id}): value=${stringBoolToBool(
newEntityState.state
)} brightness=${getBrightnessFromAttribute(
newEntityState
)} lastChanged=${new Date(newEntityState.last_changed)}`
);
}

localValues.lastChanged = new Date(newEntityState.last_changed);
await nextTick();
resume();

const contextIndex = skipContexts.findIndex(
(value) => value === newEntityState.context.id
Expand All @@ -93,11 +101,8 @@ export function useNewLight(state: FullfilledUseState, debug = false) {
skipContexts.splice(contextIndex, 1);
return;
}
pause();
localValues.value = stringBoolToBool(newEntityState.state);
localValues.brightness = getBrightnessFromAttribute(newEntityState);
await nextTick();
resume();
}
);

Expand Down

0 comments on commit 30b449f

Please sign in to comment.