forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.ts
80 lines (75 loc) · 1.9 KB
/
debug.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { extend } from '@vue/shared'
import type { DebuggerEventExtraInfo, ReactiveEffectOptions } from './effect'
import { type Link, type Subscriber, SubscriberFlags } from './system'
export const triggerEventInfos: DebuggerEventExtraInfo[] = []
export function onTrack(
sub: Link['sub'],
debugInfo: DebuggerEventExtraInfo,
): void {
if (!__DEV__) {
throw new Error(
`Internal error: onTrack should be called only in development.`,
)
}
if ((sub as ReactiveEffectOptions).onTrack) {
;(sub as ReactiveEffectOptions).onTrack!(
extend(
{
effect: sub,
},
debugInfo,
),
)
}
}
export function onTrigger(sub: Link['sub']): void {
if (!__DEV__) {
throw new Error(
`Internal error: onTrigger should be called only in development.`,
)
}
if ((sub as ReactiveEffectOptions).onTrigger) {
const debugInfo = triggerEventInfos[triggerEventInfos.length - 1]
;(sub as ReactiveEffectOptions).onTrigger!(
extend(
{
effect: sub,
},
debugInfo,
),
)
}
}
export function setupOnTrigger(target: { new (...args: any[]): any }): void {
if (!__DEV__) {
throw new Error(
`Internal error: setupOnTrigger should be called only in development.`,
)
}
Object.defineProperty(target.prototype, 'onTrigger', {
get() {
return this._onTrigger
},
set(val) {
if (!this._onTrigger) setupFlagsHandler(this)
this._onTrigger = val
},
})
}
function setupFlagsHandler(target: Subscriber): void {
;(target as any)._flags = target.flags
Object.defineProperty(target, 'flags', {
get() {
return (target as any)._flags
},
set(value) {
if (
!((target as any)._flags & SubscriberFlags.Propagated) &&
!!(value & SubscriberFlags.Propagated)
) {
onTrigger(this)
}
;(target as any)._flags = value
},
})
}