Skip to content

Commit

Permalink
Merge e827b87 into 2d16810
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed Jun 12, 2023
2 parents 2d16810 + e827b87 commit be6854d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
20 changes: 11 additions & 9 deletions src/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ export {
* state.value = { count: 2 }
* ```
*
* @param value - The "inner value" for the shallow ref, or a function that returns the inner value.
* @param initialValue - The "inner value" for the shallow ref, or a function that returns the inner value.
* @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowref}
*/
export const useShallowRef = <T>(value: T | (() => T)): ShallowRef<T> => {
export const useShallowRef = <T>(
initialValue: T | (() => T)
): ShallowRef<T> => {
const reactiveRef = useRef<ShallowRef<T> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = shallowRef(
isFunction(value) ? invokeUntracked(value) : value
isFunction(initialValue) ? invokeUntracked(initialValue) : initialValue
);
}
useDebugValue(reactiveRef.current, (ref) => ref.value);
Expand Down Expand Up @@ -116,16 +118,16 @@ export const useCustomRef = <T>(factory: CustomRefFactory<T>): Ref<T> => {
* state.nested.bar++
* ```
*
* @param target - The source object, or a function that returns the source object.
* @param initialValue - The source object, or a function that returns the source object.
* @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowreactive}
*/
export const useShallowReactive = <T extends object>(
target: T | (() => T)
initialValue: T | (() => T)
): T => {
const reactiveRef = useRef<T | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = shallowReactive(
isFunction(target) ? invokeUntracked(target) : target
isFunction(initialValue) ? invokeUntracked(initialValue) : initialValue
);
}
useDebugValue(reactiveRef.current);
Expand Down Expand Up @@ -169,16 +171,16 @@ export const useShallowReactive = <T extends object>(
* state.nested.bar++
* ```
*
* @param target - The source object, or a function that returns the source object.
* @param initialValue - The source object, or a function that returns the source object.
* @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowreadonly}
*/
export const useShallowReadonly = <T extends object>(
target: T | (() => T)
initialValue: T | (() => T)
): Readonly<T> => {
const reactiveRef = useRef<Readonly<T> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = shallowReadonly(
isFunction(target) ? invokeUntracked(target) : target
isFunction(initialValue) ? invokeUntracked(initialValue) : initialValue
);
}
useDebugValue(reactiveRef.current);
Expand Down
20 changes: 11 additions & 9 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ export { ref, computed, reactive, readonly } from '@vue/reactivity';
* const count = useReference(1)
* ```
*
* @param value - The object to wrap in the ref, or a function that returns the object.
* @param initialValue - The object to wrap in the ref, or a function that returns the object.
* @see {@link https://vuejs.org/api/reactivity-core.html#ref}
*/
export const useReference = <T>(value: T | (() => T)): Ref<UnwrapRef<T>> => {
export const useReference = <T>(
initialValue: T | (() => T)
): Ref<UnwrapRef<T>> => {
const reactiveRef = useRef<Ref<UnwrapRef<T>> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = ref(
isFunction(value) ? invokeUntracked(value) : value
isFunction(initialValue) ? invokeUntracked(initialValue) : initialValue
);
}
useDebugValue(reactiveRef.current, (ref) => ref.value);
Expand Down Expand Up @@ -196,16 +198,16 @@ export const useComputed: UseComputed = (<T>(
* const obj = useReactive(() => ({ count: 0 }))
* ```
*
* @param target - The source object, or a function that returns the object.
* @param initialValue - The source object, or a function that returns the object.
* @see {@link https://vuejs.org/api/reactivity-core.html#reactive}
*/
export const useReactive = <T extends object>(
target: T | (() => T)
initialValue: T | (() => T)
): UnwrapNestedRefs<T> => {
const reactiveRef = useRef<UnwrapNestedRefs<T> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = reactive(
isFunction(target) ? invokeUntracked(target) : target
isFunction(initialValue) ? invokeUntracked(initialValue) : initialValue
);
}
useDebugValue(reactiveRef.current);
Expand Down Expand Up @@ -248,16 +250,16 @@ export const useReactive = <T extends object>(
* copy.count++ // warning!
* ```
*
* @param target - The source object, or a function that returns the object.
* @param initialValue - The source object, or a function that returns the object.
* @see {@link https://vuejs.org/api/reactivity-core.html#readonly}
*/
export const useReadonly = <T extends object>(
target: T | (() => T)
initialValue: T | (() => T)
): DeepReadonly<UnwrapNestedRefs<T>> => {
const reactiveRef = useRef<DeepReadonly<UnwrapNestedRefs<T>> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = readonly(
isFunction(target) ? invokeUntracked(target) : target
isFunction(initialValue) ? invokeUntracked(initialValue) : initialValue
);
}
useDebugValue(reactiveRef.current);
Expand Down

0 comments on commit be6854d

Please sign in to comment.