-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow name ref to be a lazy function
- Loading branch information
Showing
4 changed files
with
63 additions
and
11 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
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
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 |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import { getCurrentInstance, inject, InjectionKey, ref, Ref, warn as vueWarning, watch } from 'vue'; | ||
import { | ||
computed, | ||
getCurrentInstance, | ||
inject, | ||
InjectionKey, | ||
isRef, | ||
ref, | ||
Ref, | ||
unref, | ||
warn as vueWarning, | ||
watch, | ||
} from 'vue'; | ||
import { klona as deepCopy } from 'klona/full'; | ||
import { isIndex, isNullOrUndefined, isObject, toNumber } from '../../../shared'; | ||
import { isCallable, isIndex, isNullOrUndefined, isObject, toNumber } from '../../../shared'; | ||
import { isContainerValue, isEmptyContainer, isEqual, isNotNestedPath } from './assertions'; | ||
import { PrivateFieldContext } from '../types'; | ||
import { MaybeRefOrLazy, PrivateFieldContext } from '../types'; | ||
|
||
function cleanupNonNestedPath(path: string) { | ||
if (isNotNestedPath(path)) { | ||
|
@@ -299,3 +310,15 @@ export function computedDeep<TValue = unknown>({ get, set }: { get(): TValue; se | |
|
||
return baseRef; | ||
} | ||
|
||
export function unravel<T>(value: MaybeRefOrLazy<T>): T { | ||
This comment has been minimized.
Sorry, something went wrong.
DrJume
|
||
if (isCallable(value)) { | ||
return value(); | ||
} | ||
|
||
return unref(value); | ||
} | ||
|
||
export function lazyToRef<T>(value: MaybeRefOrLazy<T>): Ref<T> { | ||
return computed(() => unravel(value)); | ||
} |
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
VueUse also has a similar type:
https://github.com/vueuse/vueuse/blob/d8e81fcfa1a1d1a1b351e84c268054f9e8f46a7a/packages/shared/utils/types.ts#L34