MCPcopy Create free account
hub / github.com/cn-docs/vueuse / computedWithControl

Function computedWithControl

packages/shared/computedWithControl/index.ts:31–73  ·  view source on GitHub ↗
(
  source: WatchSource<S> | WatchSource<S>[],
  fn: ComputedGetter<T> | WritableComputedOptions<T>,
)

Source from the content-addressed store, hash-verified

29 * @param fn
30 */
31export function computedWithControl<T, S>(
32 source: WatchSource<S> | WatchSource<S>[],
33 fn: ComputedGetter<T> | WritableComputedOptions<T>,
34) {
35 let v: T = undefined!
36 let track: Fn
37 let trigger: Fn
38 const dirty = shallowRef(true)
39
40 const update = () => {
41 dirty.value = true
42 trigger()
43 }
44
45 watch(source, update, { flush: 'sync' })
46
47 const get = typeof fn === 'function' ? fn : fn.get
48 const set = typeof fn === 'function' ? undefined : fn.set
49
50 const result = customRef<T>((_track, _trigger) => {
51 track = _track
52 trigger = _trigger
53
54 return {
55 get() {
56 if (dirty.value) {
57 v = get(v)
58 dirty.value = false
59 }
60 track()
61 return v
62 },
63 set(v) {
64 set?.(v)
65 },
66 }
67 }) as ComputedRefWithControl<T>
68
69 if (Object.isExtensible(result))
70 result.trigger = update
71
72 return result
73}
74
75// alias
76export { computedWithControl as controlledComputed }

Callers 2

useCurrentElementFunction · 0.90
index.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected