Skip to content

Commit

Permalink
fix return type and report errors in watchEffect call
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaboud committed Jun 3, 2024
1 parent 3d02a63 commit 7201008
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions houston-common-ui/lib/composables/computedResult.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { reportError } from "@/components/NotificationView.vue";
import { ResultAsync, Result } from "neverthrow";
import { watchEffect, ref, type Ref } from "vue";

Expand All @@ -6,48 +7,48 @@ import { watchEffect, ref, type Ref } from "vue";
* returning computed ref and a manual update trigger function.
* @param getter function that returns a result of T
* @returns [reference: ComputedRef\<T | undefined\>, triggerUpdate: () => void]
*
*
* @example
* const path = ref("/tmp");
*
*
* const listDirectory = () =>
* server.execute(new Command(["ls", path.value])).map((proc) => proc.getStdout().trim().split(RegexSnippets.newlineSplitter))
*
*
* const [dirContents, refetchDirContents] = computedResult(listDirectory);
* // dirContents automatically refreshes whenever path changes, or when refetchDirContents() is called
* // dirContents.value is undefined until listDirectory() finishes
*/
export function computedResult<T>(
getter: () => Result<T, any> | ResultAsync<T, any>
): [reference: Readonly<Ref<T | undefined>>, triggerUpdate: () => void];
): [reference: Ref<T | undefined>, triggerUpdate: typeof getter];
/**
* Create a computed ref with default value that grabs it's value from a {@link Result} or {@link ResultAsync},
* returning computed ref and a manual update trigger function.
* @param getter function that returns a result of T
* @param defaultValue the default value before the Result is determined
* @returns [reference: ComputedRef\<T\>, triggerUpdate: () => void]
*
* @returns [reference: ComputedRef\<T\>, triggerUpdate: typeof getter]
*
* @example
* const path = ref("/tmp");
*
*
* const listDirectory = () =>
* server.execute(new Command(["ls", path.value])).map((proc) => proc.getStdout().trim().split(RegexSnippets.newlineSplitter))
*
*
* const [dirContents, refetchDirContents] = computedResult(listDirectory, []);
* // dirContents automatically refreshes whenever path changes, or when refetchDirContents() is called
* // dirContents.value defaults to [] until listDirectory() finishes
*/
export function computedResult<T>(
getter: () => Result<T, any> | ResultAsync<T, any>,
defaultValue: T
): [reference: Readonly<Ref<T>>, triggerUpdate: () => void];
): [reference: Ref<T>, triggerUpdate: typeof getter];
export function computedResult<T>(
getter: () => Result<T, any> | ResultAsync<T, any>,
defaultValue?: T
): [reference: Readonly<Ref<T | undefined>>, triggerUpdate: () => void] {
): [reference: Ref<T | undefined>, triggerUpdate: typeof getter] {
const reference = ref<T>();
reference.value = defaultValue;
const triggerUpdate = () => getter().map((v) => (reference.value = v));
watchEffect(triggerUpdate);
watchEffect(() => triggerUpdate().mapErr(reportError));
return [reference, triggerUpdate];
}

0 comments on commit 7201008

Please sign in to comment.