-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1465326
commit 837ea11
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ResultAsync, Result } from "neverthrow"; | ||
import { watchEffect, ref, type Ref } from "vue"; | ||
|
||
/** | ||
* Create a computed ref 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 | ||
* @returns [reference: ComputedRef\<T | undefined\>, triggerUpdate: () => void] | ||
*/ | ||
export function computedResult<T>( | ||
getter: () => Result<T, any> | ResultAsync<T, any> | ||
): [reference: Readonly<Ref<T | undefined>>, triggerUpdate: () => void]; | ||
/** | ||
* 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] | ||
*/ | ||
export function computedResult<T>( | ||
getter: () => Result<T, any> | ResultAsync<T, any>, | ||
defaultValue: T | ||
): [reference: Readonly<Ref<T>>, triggerUpdate: () => void]; | ||
export function computedResult<T>( | ||
getter: () => Result<T, any> | ResultAsync<T, any>, | ||
defaultValue?: T | ||
): [reference: Readonly<Ref<T | undefined>>, triggerUpdate: () => void] { | ||
const reference = ref<T>(); | ||
reference.value = defaultValue; | ||
const triggerUpdate = () => getter().map((v) => (reference.value = v)); | ||
watchEffect(triggerUpdate); | ||
return [reference, triggerUpdate]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ export { | |
confirmBeforeAction, | ||
assertConfirm, | ||
} from "./globalModalConfirm"; | ||
export * from "./computedResult"; |