-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reactivity): add support for
customRef
API
- Loading branch information
Showing
4 changed files
with
68 additions
and
4 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 |
---|---|---|
|
@@ -70,6 +70,31 @@ export function unref<T>(ref: T): T extends Ref<infer V> ? V : T { | |
return isRef(ref) ? (ref.value as any) : ref | ||
} | ||
|
||
export type CustomRefFactory<T> = ( | ||
track: () => void, | ||
trigger: () => void | ||
) => { | ||
get: () => T | ||
set: (value: T) => void | ||
} | ||
|
||
export function customRef<T>(factory: CustomRefFactory<T>): Ref<T> { | ||
const { get, set } = factory( | ||
() => track(r, TrackOpTypes.GET, 'value'), | ||
() => trigger(r, TriggerOpTypes.SET, 'value') | ||
) | ||
const r = { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
yyx990803
Author
Member
|
||
_isRef: true, | ||
get value() { | ||
return get() | ||
}, | ||
set value(v) { | ||
set(v) | ||
} | ||
} | ||
return r as any | ||
} | ||
|
||
export function toRefs<T extends object>( | ||
object: T | ||
): { [K in keyof T]: Ref<T[K]> } { | ||
|
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
Ah, you made me brush up my knowledge of temporal dead zone.
I was surprised you could reference
r
before it's defined! (For other readers: you can from a function, as long as the function isn't invoked before it's defined.)Random thought: would this result in two less functions and more direct access? (get/set functions are the native implementation of properties, rather than a nested call)