Skip to content

Commit

Permalink
feat: add useNewStateForDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 18, 2023
1 parent 1e971f9 commit d17e31a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions reactive_home/src/composeables/useStateForDuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from "./useState.ts";
import type { FullfilledUseState } from "./useState.ts";
import { subSeconds } from "../dep.ts";
import { useNow } from "../dep.ts";
import { computed } from "../dep.ts";
Expand All @@ -22,3 +23,21 @@ export function useStateForDuration(
);
});
}

export function useNewStateForDuration(
state: FullfilledUseState,
requiredState: string,
duration: number,
defaultState?: string,
interval = 1000
) {
const now = useNow({ interval });

return computed(() => {
return (
(state.value?.state ?? defaultState) === requiredState &&
new Date(state.value?.last_changed ?? new Date()) <
subSeconds(now.value, duration)
);
});
}

0 comments on commit d17e31a

Please sign in to comment.