Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 23, 2024
1 parent 45ff287 commit 1b6b6da
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 31 deletions.
10 changes: 8 additions & 2 deletions reactive_home/src/composeables/useBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import type { Ref, UnwrapNestedRefs, MessageBase, HassEntity } from "../dep.ts";
* @param debug Enable debug prints
* @returns
*/
export function useBoolean(entity: string, debug = false) {
export function useBoolean(
entity: string,
debug = false
): Ref<HassEntity | undefined> & {
bool: boolean;
set: (newValue: boolean) => Promise<void>;
} {
const state = useState(entity);

const localBool: Ref<null | boolean> = refAutoReset(null, 5000);
Expand All @@ -33,7 +39,7 @@ export function useBoolean(entity: string, debug = false) {
localBool.value = null;
}

const bool: Ref<boolean> = computed({
const bool = computed<boolean>({
get() {
return typeof localBool.value === "boolean"
? localBool.value
Expand Down
2 changes: 1 addition & 1 deletion reactive_home/src/composeables/useLightMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function useLightMapping({
debug,
autoEnableTime,
autoEnableTimeBrightness,
}: UseLightMappingOptions) {
}: UseLightMappingOptions): void {
const localEntity = reactive({
value: entity.value,
brightness: entity.brightness,
Expand Down
14 changes: 12 additions & 2 deletions reactive_home/src/composeables/useNewBoolean.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import type { FullfilledUseState } from "./useState.ts";
import { useDebounceFn, ref, watch, extendRef, computed } from "../dep.ts";
import type { MessageBase, HassEntity, UnwrapNestedRefs } from "../dep.ts";
import type {
MessageBase,
HassEntity,
UnwrapNestedRefs,
WritableComputedRef,
} from "../dep.ts";
import { connection } from "../hass/connection.ts";
import { stringBoolToBool } from "../lib/util.ts";

export function useNewBoolean(state: FullfilledUseState, debug = false) {
export function useNewBoolean(
state: FullfilledUseState,
debug = false
): WritableComputedRef<boolean> & {
lastChanged: Date;
} {
const skipContexts: string[] = [];

const updateHASSState = useDebounceFn(async (newState: boolean) => {
Expand Down
22 changes: 16 additions & 6 deletions reactive_home/src/composeables/useNewLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ import {
extendRef,
brightBlue,
italic,
type Ref,
} from "../dep.ts";
import type { MessageBase, HassEntity } from "../dep.ts";
import { connection } from "../hass/connection.ts";
import { stringBoolToBool } from "../lib/util.ts";
import { formatTime } from "../lib/time.ts";

export function useNewLight(state: FullfilledUseState, debug = false) {
import { toRef } from "../dep.ts";

type ExtendedObject = {
brightness: number;
entity_id: string;
lastChanged: Date;
rgbColor: [number, number, number];
};

export function useNewLight(
state: FullfilledUseState,
debug = false
): ExtendedObject & Ref<boolean> {
const skipContexts: string[] = [];

function getBrightnessFromAttribute(currentState: HassEntity) {
Expand Down Expand Up @@ -200,14 +212,12 @@ export function useNewLight(state: FullfilledUseState, debug = false) {
}
);

const extendObject = {
return extendRef(toRef(exposedValue), {
brightness: exposedBrightness,
entity_id: state.value.entity_id,
lastChanged: exposedLastChanged,
rgbColor: exposedRgbColor,
};

return extendRef(exposedValue, extendObject);
});
}

export type UseNewLightEntity = ReturnType<typeof useNewLight>;
14 changes: 11 additions & 3 deletions reactive_home/src/composeables/useState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { connection } from "../hass/connection.ts";
import { getStates, HassEntities, entitiesColl, watch } from "../dep.ts";
import { ref, computed, red, gray, deepEqual } from "../dep.ts";
import {
ref,
computed,
red,
gray,
deepEqual,
type ComputedRef,
type HassEntity,
} from "../dep.ts";
import { formatTime } from "../lib/time.ts";

const collection = entitiesColl(connection);
Expand All @@ -11,7 +19,7 @@ collection.subscribe((newState) => {
currentStates.value = newState;
});

export function useState(entity: string) {
export function useState(entity: string): ComputedRef<HassEntity | undefined> {
return computed(() => currentStates.value?.[entity]);
}

Expand All @@ -27,7 +35,7 @@ export type UseAsyncStateOption = {
export async function useAsyncState(
entity: string,
options: UseAsyncStateOption = {}
) {
): Promise<ComputedRef<HassEntity>> {
const initialStates = await getStates(connection);

const initialState = initialStates.find(
Expand Down
33 changes: 19 additions & 14 deletions reactive_home/src/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,53 @@ export {
unref,
getCurrentScope,
onScopeDispose,
} from "https://esm.noim.io/@vue/[email protected]";
shallowRef,
} from "https://esm.noim.io/@vue/[email protected]";

export type {
Ref,
UnwrapNestedRefs,
ComputedRef,
} from "https://esm.noim.io/@vue/[email protected]";
ShallowRef,
WritableComputedRef,
} from "https://esm.noim.io/@vue/[email protected]";

export * from "https://deno.land/[email protected]/fmt/colors.ts";
export * from "https://deno.land/[email protected]/fmt/colors.ts";

export { join } from "https://deno.land/[email protected]/path/mod.ts";

export * from "https://esm.noim.io/[email protected]";

export { useNow } from "./composeables/useNow.ts";

// Update this manual with inspecting the esm url via curl -I and fixing the `x-typescript-types` url
// @deno-types="https://esm.noim.io/v135/@vueuse/[email protected]/X-YS92dWUtZGVtaTp2dWVAMy40LjE1CmQvQHZ1ZS9zaGFyZWRAMy40LjE1/index.d.ts"
export {
whenever,
extendRef,
refAutoReset,
watchDebounced,
useDebounceFn,
watchPausable,
} from "https://esm.noim.io/@vueuse/[email protected]?deps=@vue/[email protected]&externals=@vue/runtime-dom";
toRef,
tryOnScopeDispose,
} from "https://esm.noim.io/@vueuse/[email protected]?deps=@vue/[email protected]&externals=@vue/runtime-dom&alias=vue-demi:[email protected]";

export { watch } from "https://esm.noim.io/@vue/runtime-core@3.3.11";
export { watch } from "https://esm.noim.io/@vue/runtime-core@3.4.15";

export { config as dotenvConfig } from "https://deno.land/x/[email protected]/mod.ts";

export { Input as CliffyInput } from "https://deno.land/x/[email protected]/prompt/mod.ts";

export {
subMilliseconds,
subSeconds,
subHours,
addHours,
eachMinuteOfInterval,
} from "https://esm.noim.io/[email protected]";
export { subMilliseconds } from "https://esm.noim.io/[email protected]/subMilliseconds";
export { subSeconds } from "https://esm.noim.io/[email protected]/subSeconds";
export { subHours } from "https://esm.noim.io/[email protected]/subHours";
export { addHours } from "https://esm.noim.io/[email protected]/addHours";
export { eachMinuteOfInterval } from "https://esm.noim.io/[email protected]/eachMinuteOfInterval";

// @deno-types="https://raw.githubusercontent.com/Hypnos3/suncalc3/609d315d7787d15ca3f4643f8b121839e8333cee/suncalc.d.ts"
import SunCalc from "https://esm.noim.io/[email protected]";

import parse from "https://esm.noim.io/[email protected]";

export { join } from "https://deno.land/[email protected]/path/mod.ts";

export { SunCalc, parse as parseDuration, colors };
4 changes: 2 additions & 2 deletions reactive_home/src/lib/light.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sunPositionInPercent } from "./sun.ts";
import type { MaybeComputed } from "./types.ts";
import { computed, unref } from "../dep.ts";
import { type ComputedRef, computed, unref } from "../dep.ts";

export type UseBrightnessOptions = {
/**
Expand All @@ -20,7 +20,7 @@ export type UseBrightnessOptions = {
*/
export function useBrightness(
options: MaybeComputed<UseBrightnessOptions> = {}
) {
): ComputedRef<number> {
return computed(() => {
const sunPercent = sunPositionInPercent.value;

Expand Down
10 changes: 9 additions & 1 deletion reactive_home/src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export * from "./public.ts";

export { computed, watch, unref, reactive, ref } from "./dep.ts";
export {
computed,
watch,
unref,
reactive,
ref,
whenever,
toRef,
} from "./dep.ts";

export type { Ref, UnwrapNestedRefs, ComputedRef, HassEntity } from "./dep.ts";

0 comments on commit 1b6b6da

Please sign in to comment.