@@ -88,7 +88,7 @@ export type MultiWatchSources = (WatchSource<unknown> | object)[]
88
88
// overload: single source + cb
89
89
export function watch < T , Immediate extends Readonly < boolean > = false > (
90
90
source : WatchSource < T > ,
91
- cb : WatchCallback < T , MaybeUndefined < T , Immediate > > | null ,
91
+ cb : WatchCallback < T , MaybeUndefined < T , Immediate > > ,
92
92
options ?: WatchOptions < Immediate > ,
93
93
) : WatchHandle
94
94
@@ -98,11 +98,9 @@ export function watch<
98
98
Immediate extends Readonly < boolean > = false ,
99
99
> (
100
100
sources : readonly [ ...T ] | T ,
101
- cb :
102
- | ( [ T ] extends [ ReactiveMarker ]
103
- ? WatchCallback < T , MaybeUndefined < T , Immediate > >
104
- : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > )
105
- | null ,
101
+ cb : [ T ] extends [ ReactiveMarker ]
102
+ ? WatchCallback < T , MaybeUndefined < T , Immediate > >
103
+ : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
106
104
options ?: WatchOptions < Immediate > ,
107
105
) : WatchHandle
108
106
@@ -112,7 +110,7 @@ export function watch<
112
110
Immediate extends Readonly < boolean > = false ,
113
111
> (
114
112
sources : [ ...T ] ,
115
- cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > | null ,
113
+ cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
116
114
options ?: WatchOptions < Immediate > ,
117
115
) : WatchHandle
118
116
@@ -122,17 +120,17 @@ export function watch<
122
120
Immediate extends Readonly < boolean > = false ,
123
121
> (
124
122
source : T ,
125
- cb : WatchCallback < T , MaybeUndefined < T , Immediate > > | null ,
123
+ cb : WatchCallback < T , MaybeUndefined < T , Immediate > > ,
126
124
options ?: WatchOptions < Immediate > ,
127
125
) : WatchHandle
128
126
129
127
// implementation
130
128
export function watch < T = any , Immediate extends Readonly < boolean > = false > (
131
129
source : T | WatchSource < T > ,
132
- cb : WatchCallback | null ,
130
+ cb : WatchCallback ,
133
131
options ?: WatchOptions < Immediate > ,
134
132
) : WatchHandle {
135
- if ( __DEV__ && cb && ! isFunction ( cb ) ) {
133
+ if ( __DEV__ && ! isFunction ( cb ) ) {
136
134
warn (
137
135
`\`watch(fn, options?)\` signature has been moved to a separate API. ` +
138
136
`Use \`watchEffect(fn, options?)\` instead. \`watch\` now only ` +
@@ -146,20 +144,14 @@ class RenderWatcherEffect extends WatcherEffect {
146
144
job : SchedulerJob
147
145
148
146
constructor (
149
- public instance : GenericComponentInstance | null ,
147
+ instance : GenericComponentInstance | null ,
150
148
source : WatchSource | WatchSource [ ] | WatchEffect | object ,
151
149
cb : WatchCallback | null ,
152
150
options : BaseWatchOptions ,
153
- public flush : 'pre' | 'post' | 'sync' ,
151
+ private flush : 'pre' | 'post' | 'sync' ,
154
152
) {
155
153
super ( source , cb , options )
156
154
157
- // important: mark the job as a watcher callback so that scheduler knows
158
- // it is allowed to self-trigger (#1727)
159
- if ( cb ) {
160
- this . flags |= EffectFlags . ALLOW_RECURSE
161
- }
162
-
163
155
const job : SchedulerJob = ( ) => {
164
156
if ( this . dirty ) {
165
157
this . run ( )
@@ -168,10 +160,11 @@ class RenderWatcherEffect extends WatcherEffect {
168
160
// important: mark the job as a watcher callback so that scheduler knows
169
161
// it is allowed to self-trigger (#1727)
170
162
if ( cb ) {
163
+ this . flags |= EffectFlags . ALLOW_RECURSE
171
164
job . flags ! |= SchedulerJobFlags . ALLOW_RECURSE
172
165
}
173
- if ( flush === 'pre' && instance ) {
174
- ; ( job as SchedulerJob ) . i = instance
166
+ if ( instance ) {
167
+ job . i = instance
175
168
}
176
169
this . job = job
177
170
}
@@ -181,13 +174,11 @@ class RenderWatcherEffect extends WatcherEffect {
181
174
if ( ! ( flags & EffectFlags . PAUSED ) ) {
182
175
const flush = this . flush
183
176
if ( flush === 'post' ) {
184
- queuePostRenderEffect (
185
- this . job ,
186
- undefined ,
187
- this . instance && this . instance . suspense ,
188
- )
177
+ const job = this . job
178
+ queuePostRenderEffect ( job , undefined , job . i ? job . i . suspense : null )
189
179
} else if ( flush === 'pre' ) {
190
- queueJob ( this . job , this . instance ? this . instance . uid : undefined , true )
180
+ const job = this . job
181
+ queueJob ( job , job . i ? job . i . uid : undefined , true )
191
182
} else if ( this . dirty ) {
192
183
this . run ( )
193
184
}
0 commit comments