reactivity.d.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. import { IfAny } from '@vue/shared';
  2. export declare enum TrackOpTypes {
  3. GET = "get",
  4. HAS = "has",
  5. ITERATE = "iterate"
  6. }
  7. export declare enum TriggerOpTypes {
  8. SET = "set",
  9. ADD = "add",
  10. DELETE = "delete",
  11. CLEAR = "clear"
  12. }
  13. export declare enum ReactiveFlags {
  14. SKIP = "__v_skip",
  15. IS_REACTIVE = "__v_isReactive",
  16. IS_READONLY = "__v_isReadonly",
  17. IS_SHALLOW = "__v_isShallow",
  18. RAW = "__v_raw",
  19. IS_REF = "__v_isRef"
  20. }
  21. export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRefSimple<T>;
  22. declare const ReactiveMarkerSymbol: unique symbol;
  23. export interface ReactiveMarker {
  24. [ReactiveMarkerSymbol]?: void;
  25. }
  26. export type Reactive<T> = UnwrapNestedRefs<T> & (T extends readonly any[] ? ReactiveMarker : {});
  27. /**
  28. * Returns a reactive proxy of the object.
  29. *
  30. * The reactive conversion is "deep": it affects all nested properties. A
  31. * reactive object also deeply unwraps any properties that are refs while
  32. * maintaining reactivity.
  33. *
  34. * @example
  35. * ```js
  36. * const obj = reactive({ count: 0 })
  37. * ```
  38. *
  39. * @param target - The source object.
  40. * @see {@link https://vuejs.org/api/reactivity-core.html#reactive}
  41. */
  42. export declare function reactive<T extends object>(target: T): Reactive<T>;
  43. declare const ShallowReactiveMarker: unique symbol;
  44. export type ShallowReactive<T> = T & {
  45. [ShallowReactiveMarker]?: true;
  46. };
  47. /**
  48. * Shallow version of {@link reactive()}.
  49. *
  50. * Unlike {@link reactive()}, there is no deep conversion: only root-level
  51. * properties are reactive for a shallow reactive object. Property values are
  52. * stored and exposed as-is - this also means properties with ref values will
  53. * not be automatically unwrapped.
  54. *
  55. * @example
  56. * ```js
  57. * const state = shallowReactive({
  58. * foo: 1,
  59. * nested: {
  60. * bar: 2
  61. * }
  62. * })
  63. *
  64. * // mutating state's own properties is reactive
  65. * state.foo++
  66. *
  67. * // ...but does not convert nested objects
  68. * isReactive(state.nested) // false
  69. *
  70. * // NOT reactive
  71. * state.nested.bar++
  72. * ```
  73. *
  74. * @param target - The source object.
  75. * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowreactive}
  76. */
  77. export declare function shallowReactive<T extends object>(target: T): ShallowReactive<T>;
  78. type Primitive = string | number | boolean | bigint | symbol | undefined | null;
  79. type Builtin = Primitive | Function | Date | Error | RegExp;
  80. export type DeepReadonly<T> = T extends Builtin ? T : T extends Map<infer K, infer V> ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> : T extends WeakMap<infer K, infer V> ? WeakMap<DeepReadonly<K>, DeepReadonly<V>> : T extends Set<infer U> ? ReadonlySet<DeepReadonly<U>> : T extends ReadonlySet<infer U> ? ReadonlySet<DeepReadonly<U>> : T extends WeakSet<infer U> ? WeakSet<DeepReadonly<U>> : T extends Promise<infer U> ? Promise<DeepReadonly<U>> : T extends Ref<infer U, unknown> ? Readonly<Ref<DeepReadonly<U>>> : T extends {} ? {
  81. readonly [K in keyof T]: DeepReadonly<T[K]>;
  82. } : Readonly<T>;
  83. /**
  84. * Takes an object (reactive or plain) or a ref and returns a readonly proxy to
  85. * the original.
  86. *
  87. * A readonly proxy is deep: any nested property accessed will be readonly as
  88. * well. It also has the same ref-unwrapping behavior as {@link reactive()},
  89. * except the unwrapped values will also be made readonly.
  90. *
  91. * @example
  92. * ```js
  93. * const original = reactive({ count: 0 })
  94. *
  95. * const copy = readonly(original)
  96. *
  97. * watchEffect(() => {
  98. * // works for reactivity tracking
  99. * console.log(copy.count)
  100. * })
  101. *
  102. * // mutating original will trigger watchers relying on the copy
  103. * original.count++
  104. *
  105. * // mutating the copy will fail and result in a warning
  106. * copy.count++ // warning!
  107. * ```
  108. *
  109. * @param target - The source object.
  110. * @see {@link https://vuejs.org/api/reactivity-core.html#readonly}
  111. */
  112. export declare function readonly<T extends object>(target: T): DeepReadonly<UnwrapNestedRefs<T>>;
  113. /**
  114. * Shallow version of {@link readonly()}.
  115. *
  116. * Unlike {@link readonly()}, there is no deep conversion: only root-level
  117. * properties are made readonly. Property values are stored and exposed as-is -
  118. * this also means properties with ref values will not be automatically
  119. * unwrapped.
  120. *
  121. * @example
  122. * ```js
  123. * const state = shallowReadonly({
  124. * foo: 1,
  125. * nested: {
  126. * bar: 2
  127. * }
  128. * })
  129. *
  130. * // mutating state's own properties will fail
  131. * state.foo++
  132. *
  133. * // ...but works on nested objects
  134. * isReadonly(state.nested) // false
  135. *
  136. * // works
  137. * state.nested.bar++
  138. * ```
  139. *
  140. * @param target - The source object.
  141. * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowreadonly}
  142. */
  143. export declare function shallowReadonly<T extends object>(target: T): Readonly<T>;
  144. /**
  145. * Checks if an object is a proxy created by {@link reactive()} or
  146. * {@link shallowReactive()} (or {@link ref()} in some cases).
  147. *
  148. * @example
  149. * ```js
  150. * isReactive(reactive({})) // => true
  151. * isReactive(readonly(reactive({}))) // => true
  152. * isReactive(ref({}).value) // => true
  153. * isReactive(readonly(ref({})).value) // => true
  154. * isReactive(ref(true)) // => false
  155. * isReactive(shallowRef({}).value) // => false
  156. * isReactive(shallowReactive({})) // => true
  157. * ```
  158. *
  159. * @param value - The value to check.
  160. * @see {@link https://vuejs.org/api/reactivity-utilities.html#isreactive}
  161. */
  162. export declare function isReactive(value: unknown): boolean;
  163. /**
  164. * Checks whether the passed value is a readonly object. The properties of a
  165. * readonly object can change, but they can't be assigned directly via the
  166. * passed object.
  167. *
  168. * The proxies created by {@link readonly()} and {@link shallowReadonly()} are
  169. * both considered readonly, as is a computed ref without a set function.
  170. *
  171. * @param value - The value to check.
  172. * @see {@link https://vuejs.org/api/reactivity-utilities.html#isreadonly}
  173. */
  174. export declare function isReadonly(value: unknown): boolean;
  175. export declare function isShallow(value: unknown): boolean;
  176. /**
  177. * Checks if an object is a proxy created by {@link reactive},
  178. * {@link readonly}, {@link shallowReactive} or {@link shallowReadonly()}.
  179. *
  180. * @param value - The value to check.
  181. * @see {@link https://vuejs.org/api/reactivity-utilities.html#isproxy}
  182. */
  183. export declare function isProxy(value: any): boolean;
  184. /**
  185. * Returns the raw, original object of a Vue-created proxy.
  186. *
  187. * `toRaw()` can return the original object from proxies created by
  188. * {@link reactive()}, {@link readonly()}, {@link shallowReactive()} or
  189. * {@link shallowReadonly()}.
  190. *
  191. * This is an escape hatch that can be used to temporarily read without
  192. * incurring proxy access / tracking overhead or write without triggering
  193. * changes. It is **not** recommended to hold a persistent reference to the
  194. * original object. Use with caution.
  195. *
  196. * @example
  197. * ```js
  198. * const foo = {}
  199. * const reactiveFoo = reactive(foo)
  200. *
  201. * console.log(toRaw(reactiveFoo) === foo) // true
  202. * ```
  203. *
  204. * @param observed - The object for which the "raw" value is requested.
  205. * @see {@link https://vuejs.org/api/reactivity-advanced.html#toraw}
  206. */
  207. export declare function toRaw<T>(observed: T): T;
  208. export type Raw<T> = T & {
  209. [RawSymbol]?: true;
  210. };
  211. /**
  212. * Marks an object so that it will never be converted to a proxy. Returns the
  213. * object itself.
  214. *
  215. * @example
  216. * ```js
  217. * const foo = markRaw({})
  218. * console.log(isReactive(reactive(foo))) // false
  219. *
  220. * // also works when nested inside other reactive objects
  221. * const bar = reactive({ foo })
  222. * console.log(isReactive(bar.foo)) // false
  223. * ```
  224. *
  225. * **Warning:** `markRaw()` together with the shallow APIs such as
  226. * {@link shallowReactive()} allow you to selectively opt-out of the default
  227. * deep reactive/readonly conversion and embed raw, non-proxied objects in your
  228. * state graph.
  229. *
  230. * @param value - The object to be marked as "raw".
  231. * @see {@link https://vuejs.org/api/reactivity-advanced.html#markraw}
  232. */
  233. export declare function markRaw<T extends object>(value: T): Raw<T>;
  234. /**
  235. * Returns a reactive proxy of the given value (if possible).
  236. *
  237. * If the given value is not an object, the original value itself is returned.
  238. *
  239. * @param value - The value for which a reactive proxy shall be created.
  240. */
  241. export declare const toReactive: <T extends unknown>(value: T) => T;
  242. /**
  243. * Returns a readonly proxy of the given value (if possible).
  244. *
  245. * If the given value is not an object, the original value itself is returned.
  246. *
  247. * @param value - The value for which a readonly proxy shall be created.
  248. */
  249. export declare const toReadonly: <T extends unknown>(value: T) => DeepReadonly<T>;
  250. export type EffectScheduler = (...args: any[]) => any;
  251. export type DebuggerEvent = {
  252. effect: Subscriber;
  253. } & DebuggerEventExtraInfo;
  254. export type DebuggerEventExtraInfo = {
  255. target: object;
  256. type: TrackOpTypes | TriggerOpTypes;
  257. key: any;
  258. newValue?: any;
  259. oldValue?: any;
  260. oldTarget?: Map<any, any> | Set<any>;
  261. };
  262. export interface DebuggerOptions {
  263. onTrack?: (event: DebuggerEvent) => void;
  264. onTrigger?: (event: DebuggerEvent) => void;
  265. }
  266. export interface ReactiveEffectOptions extends DebuggerOptions {
  267. scheduler?: EffectScheduler;
  268. allowRecurse?: boolean;
  269. onStop?: () => void;
  270. }
  271. export declare enum EffectFlags {
  272. /**
  273. * ReactiveEffect only
  274. */
  275. ACTIVE = 1,
  276. RUNNING = 2,
  277. TRACKING = 4,
  278. NOTIFIED = 8,
  279. DIRTY = 16,
  280. ALLOW_RECURSE = 32,
  281. PAUSED = 64
  282. }
  283. /**
  284. * Subscriber is a type that tracks (or subscribes to) a list of deps.
  285. */
  286. interface Subscriber extends DebuggerOptions {
  287. }
  288. export declare class ReactiveEffect<T = any> implements Subscriber, ReactiveEffectOptions {
  289. fn: () => T;
  290. scheduler?: EffectScheduler;
  291. onStop?: () => void;
  292. onTrack?: (event: DebuggerEvent) => void;
  293. onTrigger?: (event: DebuggerEvent) => void;
  294. constructor(fn: () => T);
  295. pause(): void;
  296. resume(): void;
  297. run(): T;
  298. stop(): void;
  299. trigger(): void;
  300. get dirty(): boolean;
  301. }
  302. export interface ReactiveEffectRunner<T = any> {
  303. (): T;
  304. effect: ReactiveEffect;
  305. }
  306. export interface ReactiveEffectRunner<T = any> {
  307. (): T;
  308. effect: ReactiveEffect;
  309. }
  310. export declare function effect<T = any>(fn: () => T, options?: ReactiveEffectOptions): ReactiveEffectRunner<T>;
  311. /**
  312. * Stops the effect associated with the given runner.
  313. *
  314. * @param runner - Association with the effect to stop tracking.
  315. */
  316. export declare function stop(runner: ReactiveEffectRunner): void;
  317. /**
  318. * Temporarily pauses tracking.
  319. */
  320. export declare function pauseTracking(): void;
  321. /**
  322. * Re-enables effect tracking (if it was paused).
  323. */
  324. export declare function enableTracking(): void;
  325. /**
  326. * Resets the previous global effect tracking state.
  327. */
  328. export declare function resetTracking(): void;
  329. /**
  330. * Registers a cleanup function for the current active effect.
  331. * The cleanup function is called right before the next effect run, or when the
  332. * effect is stopped.
  333. *
  334. * Throws a warning if there is no current active effect. The warning can be
  335. * suppressed by passing `true` to the second argument.
  336. *
  337. * @param fn - the cleanup function to be registered
  338. * @param failSilently - if `true`, will not throw warning when called without
  339. * an active effect.
  340. */
  341. export declare function onEffectCleanup(fn: () => void, failSilently?: boolean): void;
  342. declare const ComputedRefSymbol: unique symbol;
  343. declare const WritableComputedRefSymbol: unique symbol;
  344. interface BaseComputedRef<T, S = T> extends Ref<T, S> {
  345. [ComputedRefSymbol]: true;
  346. /**
  347. * @deprecated computed no longer uses effect
  348. */
  349. effect: ComputedRefImpl;
  350. }
  351. export interface ComputedRef<T = any> extends BaseComputedRef<T> {
  352. readonly value: T;
  353. }
  354. export interface WritableComputedRef<T, S = T> extends BaseComputedRef<T, S> {
  355. [WritableComputedRefSymbol]: true;
  356. }
  357. export type ComputedGetter<T> = (oldValue?: T) => T;
  358. export type ComputedSetter<T> = (newValue: T) => void;
  359. export interface WritableComputedOptions<T, S = T> {
  360. get: ComputedGetter<T>;
  361. set: ComputedSetter<S>;
  362. }
  363. /**
  364. * @private exported by @vue/reactivity for Vue core use, but not exported from
  365. * the main vue package
  366. */
  367. export declare class ComputedRefImpl<T = any> implements Subscriber {
  368. fn: ComputedGetter<T>;
  369. private readonly setter;
  370. effect: this;
  371. onTrack?: (event: DebuggerEvent) => void;
  372. onTrigger?: (event: DebuggerEvent) => void;
  373. constructor(fn: ComputedGetter<T>, setter: ComputedSetter<T> | undefined, isSSR: boolean);
  374. get value(): T;
  375. set value(newValue: T);
  376. }
  377. /**
  378. * Takes a getter function and returns a readonly reactive ref object for the
  379. * returned value from the getter. It can also take an object with get and set
  380. * functions to create a writable ref object.
  381. *
  382. * @example
  383. * ```js
  384. * // Creating a readonly computed ref:
  385. * const count = ref(1)
  386. * const plusOne = computed(() => count.value + 1)
  387. *
  388. * console.log(plusOne.value) // 2
  389. * plusOne.value++ // error
  390. * ```
  391. *
  392. * ```js
  393. * // Creating a writable computed ref:
  394. * const count = ref(1)
  395. * const plusOne = computed({
  396. * get: () => count.value + 1,
  397. * set: (val) => {
  398. * count.value = val - 1
  399. * }
  400. * })
  401. *
  402. * plusOne.value = 1
  403. * console.log(count.value) // 0
  404. * ```
  405. *
  406. * @param getter - Function that produces the next value.
  407. * @param debugOptions - For debugging. See {@link https://vuejs.org/guide/extras/reactivity-in-depth.html#computed-debugging}.
  408. * @see {@link https://vuejs.org/api/reactivity-core.html#computed}
  409. */
  410. export declare function computed<T>(getter: ComputedGetter<T>, debugOptions?: DebuggerOptions): ComputedRef<T>;
  411. export declare function computed<T, S = T>(options: WritableComputedOptions<T, S>, debugOptions?: DebuggerOptions): WritableComputedRef<T, S>;
  412. declare const RefSymbol: unique symbol;
  413. declare const RawSymbol: unique symbol;
  414. export interface Ref<T = any, S = T> {
  415. get value(): T;
  416. set value(_: S);
  417. /**
  418. * Type differentiator only.
  419. * We need this to be in public d.ts but don't want it to show up in IDE
  420. * autocomplete, so we use a private Symbol instead.
  421. */
  422. [RefSymbol]: true;
  423. }
  424. /**
  425. * Checks if a value is a ref object.
  426. *
  427. * @param r - The value to inspect.
  428. * @see {@link https://vuejs.org/api/reactivity-utilities.html#isref}
  429. */
  430. export declare function isRef<T>(r: Ref<T> | unknown): r is Ref<T>;
  431. /**
  432. * Takes an inner value and returns a reactive and mutable ref object, which
  433. * has a single property `.value` that points to the inner value.
  434. *
  435. * @param value - The object to wrap in the ref.
  436. * @see {@link https://vuejs.org/api/reactivity-core.html#ref}
  437. */
  438. export declare function ref<T>(value: T): [T] extends [Ref] ? IfAny<T, Ref<T>, T> : Ref<UnwrapRef<T>, UnwrapRef<T> | T>;
  439. export declare function ref<T = any>(): Ref<T | undefined>;
  440. declare const ShallowRefMarker: unique symbol;
  441. export type ShallowRef<T = any, S = T> = Ref<T, S> & {
  442. [ShallowRefMarker]?: true;
  443. };
  444. /**
  445. * Shallow version of {@link ref()}.
  446. *
  447. * @example
  448. * ```js
  449. * const state = shallowRef({ count: 1 })
  450. *
  451. * // does NOT trigger change
  452. * state.value.count = 2
  453. *
  454. * // does trigger change
  455. * state.value = { count: 2 }
  456. * ```
  457. *
  458. * @param value - The "inner value" for the shallow ref.
  459. * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowref}
  460. */
  461. export declare function shallowRef<T>(value: T): Ref extends T ? T extends Ref ? IfAny<T, ShallowRef<T>, T> : ShallowRef<T> : ShallowRef<T>;
  462. export declare function shallowRef<T = any>(): ShallowRef<T | undefined>;
  463. /**
  464. * Force trigger effects that depends on a shallow ref. This is typically used
  465. * after making deep mutations to the inner value of a shallow ref.
  466. *
  467. * @example
  468. * ```js
  469. * const shallow = shallowRef({
  470. * greet: 'Hello, world'
  471. * })
  472. *
  473. * // Logs "Hello, world" once for the first run-through
  474. * watchEffect(() => {
  475. * console.log(shallow.value.greet)
  476. * })
  477. *
  478. * // This won't trigger the effect because the ref is shallow
  479. * shallow.value.greet = 'Hello, universe'
  480. *
  481. * // Logs "Hello, universe"
  482. * triggerRef(shallow)
  483. * ```
  484. *
  485. * @param ref - The ref whose tied effects shall be executed.
  486. * @see {@link https://vuejs.org/api/reactivity-advanced.html#triggerref}
  487. */
  488. export declare function triggerRef(ref: Ref): void;
  489. export type MaybeRef<T = any> = T | Ref<T> | ShallowRef<T> | WritableComputedRef<T>;
  490. export type MaybeRefOrGetter<T = any> = MaybeRef<T> | ComputedRef<T> | (() => T);
  491. /**
  492. * Returns the inner value if the argument is a ref, otherwise return the
  493. * argument itself. This is a sugar function for
  494. * `val = isRef(val) ? val.value : val`.
  495. *
  496. * @example
  497. * ```js
  498. * function useFoo(x: number | Ref<number>) {
  499. * const unwrapped = unref(x)
  500. * // unwrapped is guaranteed to be number now
  501. * }
  502. * ```
  503. *
  504. * @param ref - Ref or plain value to be converted into the plain value.
  505. * @see {@link https://vuejs.org/api/reactivity-utilities.html#unref}
  506. */
  507. export declare function unref<T>(ref: MaybeRef<T> | ComputedRef<T>): T;
  508. /**
  509. * Normalizes values / refs / getters to values.
  510. * This is similar to {@link unref()}, except that it also normalizes getters.
  511. * If the argument is a getter, it will be invoked and its return value will
  512. * be returned.
  513. *
  514. * @example
  515. * ```js
  516. * toValue(1) // 1
  517. * toValue(ref(1)) // 1
  518. * toValue(() => 1) // 1
  519. * ```
  520. *
  521. * @param source - A getter, an existing ref, or a non-function value.
  522. * @see {@link https://vuejs.org/api/reactivity-utilities.html#tovalue}
  523. */
  524. export declare function toValue<T>(source: MaybeRefOrGetter<T>): T;
  525. /**
  526. * Returns a proxy for the given object that shallowly unwraps properties that
  527. * are refs. If the object already is reactive, it's returned as-is. If not, a
  528. * new reactive proxy is created.
  529. *
  530. * @param objectWithRefs - Either an already-reactive object or a simple object
  531. * that contains refs.
  532. */
  533. export declare function proxyRefs<T extends object>(objectWithRefs: T): ShallowUnwrapRef<T>;
  534. export type CustomRefFactory<T> = (track: () => void, trigger: () => void) => {
  535. get: () => T;
  536. set: (value: T) => void;
  537. };
  538. /**
  539. * Creates a customized ref with explicit control over its dependency tracking
  540. * and updates triggering.
  541. *
  542. * @param factory - The function that receives the `track` and `trigger` callbacks.
  543. * @see {@link https://vuejs.org/api/reactivity-advanced.html#customref}
  544. */
  545. export declare function customRef<T>(factory: CustomRefFactory<T>): Ref<T>;
  546. export type ToRefs<T = any> = {
  547. [K in keyof T]: ToRef<T[K]>;
  548. };
  549. /**
  550. * Converts a reactive object to a plain object where each property of the
  551. * resulting object is a ref pointing to the corresponding property of the
  552. * original object. Each individual ref is created using {@link toRef()}.
  553. *
  554. * @param object - Reactive object to be made into an object of linked refs.
  555. * @see {@link https://vuejs.org/api/reactivity-utilities.html#torefs}
  556. */
  557. export declare function toRefs<T extends object>(object: T): ToRefs<T>;
  558. export type ToRef<T> = IfAny<T, Ref<T>, [T] extends [Ref] ? T : Ref<T>>;
  559. /**
  560. * Used to normalize values / refs / getters into refs.
  561. *
  562. * @example
  563. * ```js
  564. * // returns existing refs as-is
  565. * toRef(existingRef)
  566. *
  567. * // creates a ref that calls the getter on .value access
  568. * toRef(() => props.foo)
  569. *
  570. * // creates normal refs from non-function values
  571. * // equivalent to ref(1)
  572. * toRef(1)
  573. * ```
  574. *
  575. * Can also be used to create a ref for a property on a source reactive object.
  576. * The created ref is synced with its source property: mutating the source
  577. * property will update the ref, and vice-versa.
  578. *
  579. * @example
  580. * ```js
  581. * const state = reactive({
  582. * foo: 1,
  583. * bar: 2
  584. * })
  585. *
  586. * const fooRef = toRef(state, 'foo')
  587. *
  588. * // mutating the ref updates the original
  589. * fooRef.value++
  590. * console.log(state.foo) // 2
  591. *
  592. * // mutating the original also updates the ref
  593. * state.foo++
  594. * console.log(fooRef.value) // 3
  595. * ```
  596. *
  597. * @param source - A getter, an existing ref, a non-function value, or a
  598. * reactive object to create a property ref from.
  599. * @param [key] - (optional) Name of the property in the reactive object.
  600. * @see {@link https://vuejs.org/api/reactivity-utilities.html#toref}
  601. */
  602. export declare function toRef<T>(value: T): T extends () => infer R ? Readonly<Ref<R>> : T extends Ref ? T : Ref<UnwrapRef<T>>;
  603. export declare function toRef<T extends object, K extends keyof T>(object: T, key: K): ToRef<T[K]>;
  604. export declare function toRef<T extends object, K extends keyof T>(object: T, key: K, defaultValue: T[K]): ToRef<Exclude<T[K], undefined>>;
  605. /**
  606. * This is a special exported interface for other packages to declare
  607. * additional types that should bail out for ref unwrapping. For example
  608. * \@vue/runtime-dom can declare it like so in its d.ts:
  609. *
  610. * ``` ts
  611. * declare module '@vue/reactivity' {
  612. * export interface RefUnwrapBailTypes {
  613. * runtimeDOMBailTypes: Node | Window
  614. * }
  615. * }
  616. * ```
  617. */
  618. export interface RefUnwrapBailTypes {
  619. }
  620. export type ShallowUnwrapRef<T> = {
  621. [K in keyof T]: DistributeRef<T[K]>;
  622. };
  623. type DistributeRef<T> = T extends Ref<infer V, unknown> ? V : T;
  624. export type UnwrapRef<T> = T extends ShallowRef<infer V, unknown> ? V : T extends Ref<infer V, unknown> ? UnwrapRefSimple<V> : UnwrapRefSimple<T>;
  625. type UnwrapRefSimple<T> = T extends Builtin | Ref | RefUnwrapBailTypes[keyof RefUnwrapBailTypes] | {
  626. [RawSymbol]?: true;
  627. } ? T : T extends Map<infer K, infer V> ? Map<K, UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Map<any, any>>> : T extends WeakMap<infer K, infer V> ? WeakMap<K, UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof WeakMap<any, any>>> : T extends Set<infer V> ? Set<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Set<any>>> : T extends WeakSet<infer V> ? WeakSet<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof WeakSet<any>>> : T extends ReadonlyArray<any> ? {
  628. [K in keyof T]: UnwrapRefSimple<T[K]>;
  629. } : T extends object & {
  630. [ShallowReactiveMarker]?: never;
  631. } ? {
  632. [P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>;
  633. } : T;
  634. export declare const ITERATE_KEY: unique symbol;
  635. export declare const MAP_KEY_ITERATE_KEY: unique symbol;
  636. export declare const ARRAY_ITERATE_KEY: unique symbol;
  637. /**
  638. * Tracks access to a reactive property.
  639. *
  640. * This will check which effect is running at the moment and record it as dep
  641. * which records all effects that depend on the reactive property.
  642. *
  643. * @param target - Object holding the reactive property.
  644. * @param type - Defines the type of access to the reactive property.
  645. * @param key - Identifier of the reactive property to track.
  646. */
  647. export declare function track(target: object, type: TrackOpTypes, key: unknown): void;
  648. /**
  649. * Finds all deps associated with the target (or a specific property) and
  650. * triggers the effects stored within.
  651. *
  652. * @param target - The reactive object.
  653. * @param type - Defines the type of the operation that needs to trigger effects.
  654. * @param key - Can be used to target a specific reactive property in the target object.
  655. */
  656. export declare function trigger(target: object, type: TriggerOpTypes, key?: unknown, newValue?: unknown, oldValue?: unknown, oldTarget?: Map<unknown, unknown> | Set<unknown>): void;
  657. export declare class EffectScope {
  658. detached: boolean;
  659. private _isPaused;
  660. constructor(detached?: boolean);
  661. get active(): boolean;
  662. pause(): void;
  663. /**
  664. * Resumes the effect scope, including all child scopes and effects.
  665. */
  666. resume(): void;
  667. run<T>(fn: () => T): T | undefined;
  668. stop(fromParent?: boolean): void;
  669. }
  670. /**
  671. * Creates an effect scope object which can capture the reactive effects (i.e.
  672. * computed and watchers) created within it so that these effects can be
  673. * disposed together. For detailed use cases of this API, please consult its
  674. * corresponding {@link https://github.com/vuejs/rfcs/blob/master/active-rfcs/0041-reactivity-effect-scope.md | RFC}.
  675. *
  676. * @param detached - Can be used to create a "detached" effect scope.
  677. * @see {@link https://vuejs.org/api/reactivity-advanced.html#effectscope}
  678. */
  679. export declare function effectScope(detached?: boolean): EffectScope;
  680. /**
  681. * Returns the current active effect scope if there is one.
  682. *
  683. * @see {@link https://vuejs.org/api/reactivity-advanced.html#getcurrentscope}
  684. */
  685. export declare function getCurrentScope(): EffectScope | undefined;
  686. /**
  687. * Registers a dispose callback on the current active effect scope. The
  688. * callback will be invoked when the associated effect scope is stopped.
  689. *
  690. * @param fn - The callback function to attach to the scope's cleanup.
  691. * @see {@link https://vuejs.org/api/reactivity-advanced.html#onscopedispose}
  692. */
  693. export declare function onScopeDispose(fn: () => void, failSilently?: boolean): void;
  694. /**
  695. * Track array iteration and return:
  696. * - if input is reactive: a cloned raw array with reactive values
  697. * - if input is non-reactive or shallowReactive: the original raw array
  698. */
  699. export declare function reactiveReadArray<T>(array: T[]): T[];
  700. /**
  701. * Track array iteration and return raw array
  702. */
  703. export declare function shallowReadArray<T>(arr: T[]): T[];
  704. export declare enum WatchErrorCodes {
  705. WATCH_GETTER = 2,
  706. WATCH_CALLBACK = 3,
  707. WATCH_CLEANUP = 4
  708. }
  709. export type WatchEffect = (onCleanup: OnCleanup) => void;
  710. export type WatchSource<T = any> = Ref<T, any> | ComputedRef<T> | (() => T);
  711. export type WatchCallback<V = any, OV = any> = (value: V, oldValue: OV, onCleanup: OnCleanup) => any;
  712. export type OnCleanup = (cleanupFn: () => void) => void;
  713. export interface WatchOptions<Immediate = boolean> extends DebuggerOptions {
  714. immediate?: Immediate;
  715. deep?: boolean | number;
  716. once?: boolean;
  717. scheduler?: WatchScheduler;
  718. onWarn?: (msg: string, ...args: any[]) => void;
  719. }
  720. export type WatchStopHandle = () => void;
  721. export interface WatchHandle extends WatchStopHandle {
  722. pause: () => void;
  723. resume: () => void;
  724. stop: () => void;
  725. }
  726. export type WatchScheduler = (job: () => void, isFirstRun: boolean) => void;
  727. /**
  728. * Returns the current active effect if there is one.
  729. */
  730. export declare function getCurrentWatcher(): ReactiveEffect<any> | undefined;
  731. /**
  732. * Registers a cleanup callback on the current active effect. This
  733. * registered cleanup callback will be invoked right before the
  734. * associated effect re-runs.
  735. *
  736. * @param cleanupFn - The callback function to attach to the effect's cleanup.
  737. * @param failSilently - if `true`, will not throw warning when called without
  738. * an active effect.
  739. * @param owner - The effect that this cleanup function should be attached to.
  740. * By default, the current active effect.
  741. */
  742. export declare function onWatcherCleanup(cleanupFn: () => void, failSilently?: boolean, owner?: ReactiveEffect | undefined): void;
  743. export declare function watch(source: WatchSource | WatchSource[] | WatchEffect | object, cb?: WatchCallback | null, options?: WatchOptions): WatchHandle;
  744. export declare function traverse(value: unknown, depth?: number, seen?: Set<unknown>): unknown;