📄 refWithControl¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 7 |
| 📦 Imports | 3 |
| 📊 Variables & Constants | 1 |
| 📐 Interfaces | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/shared/refWithControl/index.ts
📦 Imports¶
| Name | Source |
|---|---|
Fn |
../utils |
customRef |
vue |
extendRef |
../extendRef |
Variables & Constants¶
| Name | Type | Kind | Value | Exported |
|---|---|---|---|---|
controlledRef |
<T>(initial: T, options?: ControlledR... |
const | refWithControl |
✓ |
Functions¶
refWithControl(initial: T, options: ControlledRefOptions<T>): any¶
Fine-grained controls over ref and its reactivity.
Tags: @__NO_SIDE_EFFECTS__
Calls:
customRef (from vue)getsettrackoptions.onBeforeChangeoptions.onChangedtriggerextendRef (from ../extendRef)
Internal Comments:
/**
* Get the value without tracked in the reactivity system
*/ (x2)
/**
* Set the value without triggering the reactivity system
*/ (x2)
/**
* Get the value without tracked in the reactivity system.
*
* Alias for `untrackedGet()`
*/ (x2)
/**
* Set the value without triggering the reactivity system
*
* Alias for `silentSet(v)`
*/ (x2)
Code
export function refWithControl<T>(
initial: T,
options: ControlledRefOptions<T> = {},
) {
let source = initial
let track: Fn
let trigger: Fn
const ref = customRef<T>((_track, _trigger) => {
track = _track
trigger = _trigger
return {
get() {
return get()
},
set(v) {
set(v)
},
}
})
function get(tracking = true) {
if (tracking)
track()
return source
}
function set(value: T, triggering = true) {
if (value === source)
return
const old = source
if (options.onBeforeChange?.(value, old) === false)
return // dismissed
source = value
options.onChanged?.(value, old)
if (triggering)
trigger()
}
/**
* Get the value without tracked in the reactivity system
*/
const untrackedGet = () => get(false)
/**
* Set the value without triggering the reactivity system
*/
const silentSet = (v: T) => set(v, false)
/**
* Get the value without tracked in the reactivity system.
*
* Alias for `untrackedGet()`
*/
const peek = () => get(false)
/**
* Set the value without triggering the reactivity system
*
* Alias for `silentSet(v)`
*/
const lay = (v: T) => set(v, false)
return extendRef(
ref,
{
get,
set,
untrackedGet,
silentSet,
peek,
lay,
},
{ enumerable: true },
)
}
Internal helpers¶
Declared inside another function in this file.
get(tracking: boolean): T¶
Parameters:
trackingboolean
Returns: T
Calls:
track
set(value: T, triggering: boolean): void¶
Parameters:
valueTtriggeringboolean
Returns: void
Calls:
options.onBeforeChangeoptions.onChangedtrigger
Code
untrackedGet(): T¶
Returns: T
Calls:
get
silentSet(v: T): void¶
Parameters:
vT
Returns: void
Calls:
set
peek(): T¶
Returns: T
Calls:
get
lay(v: T): void¶
Parameters:
vT
Returns: void
Calls:
set
Interfaces¶
ControlledRefOptions<T>¶
Interface Code
export interface ControlledRefOptions<T> {
/**
* Callback function before the ref changing.
*
* Returning `false` to dismiss the change.
*/
onBeforeChange?: (value: T, oldValue: T) => void | boolean
/**
* Callback function after the ref changed
*
* This happens synchronously, with less overhead compare to `watch`
*/
onChanged?: (value: T, oldValue: T) => void
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
onBeforeChange |
(value: T, oldValue: T) => void \| boolean |
✓ | not shown |
onChanged |
(value: T, oldValue: T) => void |
✓ | not shown |
Generated by Syntax Scribe