Skip to content

Commit c346af2

Browse files
authored
docs: comments on reactivity functions (fixes #4832) (#4836)
close #4832
1 parent 5261085 commit c346af2

File tree

6 files changed

+408
-28
lines changed

6 files changed

+408
-28
lines changed

packages/compiler-sfc/src/templateUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function parseUrl(url: string): UrlWithStringQuery {
3030

3131
/**
3232
* vuejs/component-compiler-utils#22 Support uri fragment in transformed require
33-
* @param urlString an url as a string
33+
* @param urlString - an url as a string
3434
*/
3535
function parseUriParts(urlString: string): UrlWithStringQuery {
3636
// A TypeError is thrown if urlString is not a string

packages/reactivity/src/computed.ts

+33
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,39 @@ export class ComputedRefImpl<T> {
6868
}
6969
}
7070

71+
/**
72+
* Takes a getter function and returns a readonly reactive ref object for the
73+
* returned value from the getter. It can also take an object with get and set
74+
* functions to create a writable ref object.
75+
*
76+
* @example
77+
* ```js
78+
* // Creating a readonly computed ref:
79+
* const count = ref(1)
80+
* const plusOne = computed(() => count.value + 1)
81+
*
82+
* console.log(plusOne.value) // 2
83+
* plusOne.value++ // error
84+
* ```
85+
*
86+
* ```js
87+
* // Creating a writable computed ref:
88+
* const count = ref(1)
89+
* const plusOne = computed({
90+
* get: () => count.value + 1,
91+
* set: (val) => {
92+
* count.value = val - 1
93+
* }
94+
* })
95+
*
96+
* plusOne.value = 1
97+
* console.log(count.value) // 0
98+
* ```
99+
*
100+
* @param getter - Function that produces the next value.
101+
* @param debugOptions - For debugging. See {@link https://vuejs.org/guide/extras/reactivity-in-depth.html#computed-debugging}.
102+
* @see {@link https://vuejs.org/api/reactivity-core.html#computed}
103+
*/
71104
export function computed<T>(
72105
getter: ComputedGetter<T>,
73106
debugOptions?: DebuggerOptions

packages/reactivity/src/effect.ts

+42
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ export interface ReactiveEffectRunner<T = any> {
167167
effect: ReactiveEffect
168168
}
169169

170+
/**
171+
* Registers the given function to track reactive updates.
172+
*
173+
* The given function will be run once immediately. Every time any reactive
174+
* property that's accessed within it gets updated, the function will run again.
175+
*
176+
* @param fn - The function that will track reactive updates.
177+
* @param options - Allows to control the effect's behaviour.
178+
* @returns A runner that can be used to control the effect after creation.
179+
*/
170180
export function effect<T = any>(
171181
fn: () => T,
172182
options?: ReactiveEffectOptions
@@ -188,28 +198,52 @@ export function effect<T = any>(
188198
return runner
189199
}
190200

201+
/**
202+
* Stops the effect associated with the given runner.
203+
*
204+
* @param runner - Association with the effect to stop tracking.
205+
*/
191206
export function stop(runner: ReactiveEffectRunner) {
192207
runner.effect.stop()
193208
}
194209

195210
export let shouldTrack = true
196211
const trackStack: boolean[] = []
197212

213+
/**
214+
* Temporarily pauses tracking.
215+
*/
198216
export function pauseTracking() {
199217
trackStack.push(shouldTrack)
200218
shouldTrack = false
201219
}
202220

221+
/**
222+
* Re-enables effect tracking (if it was paused).
223+
*/
203224
export function enableTracking() {
204225
trackStack.push(shouldTrack)
205226
shouldTrack = true
206227
}
207228

229+
/**
230+
* Resets the previous global effect tracking state.
231+
*/
208232
export function resetTracking() {
209233
const last = trackStack.pop()
210234
shouldTrack = last === undefined ? true : last
211235
}
212236

237+
/**
238+
* Tracks access to a reactive property.
239+
*
240+
* This will check which effect is running at the moment and record it as dep
241+
* which records all effects that depend on the reactive property.
242+
*
243+
* @param target - Object holding the reactive property.
244+
* @param type - Defines the type of access to the reactive property.
245+
* @param key - Identifier of the reactive property to track.
246+
*/
213247
export function track(target: object, type: TrackOpTypes, key: unknown) {
214248
if (shouldTrack && activeEffect) {
215249
let depsMap = targetMap.get(target)
@@ -260,6 +294,14 @@ export function trackEffects(
260294
}
261295
}
262296

297+
/**
298+
* Finds all deps associated with the target (or a specific property) and
299+
* triggers the effects stored within.
300+
*
301+
* @param target - The reactive object.
302+
* @param type - Defines the type of the operation that needs to trigger effects.
303+
* @param key - Can be used to target a specific reactive property in the target object.
304+
*/
263305
export function trigger(
264306
target: object,
265307
type: TriggerOpTypes,

packages/reactivity/src/effectScope.ts

+21
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ export class EffectScope {
107107
}
108108
}
109109

110+
/**
111+
* Creates an effect scope object which can capture the reactive effects (i.e.
112+
* computed and watchers) created within it so that these effects can be
113+
* disposed together. For detailed use cases of this API, please consult its
114+
* corresponding {@link https://github.com/vuejs/rfcs/blob/master/active-rfcs/0041-reactivity-effect-scope.md | RFC}.
115+
*
116+
* @param detached - Can be used to create a "detached" effect scope.
117+
* @see {@link https://vuejs.org/api/reactivity-advanced.html#effectscope}
118+
*/
110119
export function effectScope(detached?: boolean) {
111120
return new EffectScope(detached)
112121
}
@@ -120,10 +129,22 @@ export function recordEffectScope(
120129
}
121130
}
122131

132+
/**
133+
* Returns the current active effect scope if there is one.
134+
*
135+
* @see {@link https://vuejs.org/api/reactivity-advanced.html#getcurrentscope}
136+
*/
123137
export function getCurrentScope() {
124138
return activeEffectScope
125139
}
126140

141+
/**
142+
* Registers a dispose callback on the current active effect scope. The
143+
* callback will be invoked when the associated effect scope is stopped.
144+
*
145+
* @param fn - The callback function to attach to the scope's cleanup.
146+
* @see {@link https://vuejs.org/api/reactivity-advanced.html#onscopedispose}
147+
*/
127148
export function onScopeDispose(fn: () => void) {
128149
if (activeEffectScope) {
129150
activeEffectScope.cleanups.push(fn)

0 commit comments

Comments
 (0)