-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
45 lines (40 loc) · 874 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import type { ComputedRef, WatchSource } from 'vue-demi'
import { customRef, ref, watch } from 'vue-demi'
import type { Fn } from '../utils'
/**
* Explicitly define the deps of computed.
*
* @param source
* @param fn
*/
export function computedWithControl<T, S>(source: WatchSource<S> | WatchSource<S>[], fn: () => T) {
let v: T = undefined!
let track: Fn
let trigger: Fn
const dirty = ref(true)
watch(
source,
() => {
dirty.value = true
trigger()
},
{ flush: 'sync' },
)
return customRef<T>((_track, _trigger) => {
track = _track
trigger = _trigger
return {
get() {
if (dirty.value) {
v = fn()
dirty.value = false
}
track()
return v
},
set() {},
}
}) as ComputedRef<T>
}
// alias
export { computedWithControl as controlledComputed }