Skip to content

Commit 481df0e

Browse files
committed
refactor: minor code adjustments
1 parent 00b1f66 commit 481df0e

File tree

5 files changed

+43
-47
lines changed

5 files changed

+43
-47
lines changed

packages/reactivity/src/effect.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ export class ReactiveEffect<T = any> implements ReactiveEffectOptions {
9696
}
9797

9898
resume(): void {
99-
const flags = this.flags
99+
let flags = this.flags
100100
if (flags & EffectFlags.PAUSED) {
101-
this.flags &= ~EffectFlags.PAUSED
101+
this.flags = flags &= ~EffectFlags.PAUSED
102102
}
103103
if (flags & EffectFlags.NOTIFIED) {
104-
this.flags &= ~EffectFlags.NOTIFIED
104+
this.flags = flags &= ~EffectFlags.NOTIFIED
105105
this.notify()
106106
}
107107
}
@@ -297,9 +297,9 @@ export function onEffectCleanup(fn: () => void, failSilently = false): void {
297297
}
298298

299299
function cleanupEffect(e: ReactiveEffect) {
300-
const { cleanup } = e
301-
e.cleanup = undefined
300+
const cleanup = e.cleanup
302301
if (cleanup !== undefined) {
302+
e.cleanup = undefined
303303
// run cleanup without active effect
304304
const prevSub = activeSub
305305
activeSub = undefined

packages/reactivity/src/ref.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,22 @@ class RefImpl<T = any> implements Dependency {
144144
if (hasChanged(newValue, oldValue)) {
145145
this._rawValue = newValue
146146
this._value =
147-
this._wrap && !useDirectValue ? this._wrap(newValue) : newValue
148-
if (__DEV__) {
149-
triggerEventInfos.push({
150-
target: this,
151-
type: TriggerOpTypes.SET,
152-
key: 'value',
153-
newValue,
154-
oldValue,
155-
})
156-
}
157-
triggerRef(this as unknown as Ref)
158-
if (__DEV__) {
159-
triggerEventInfos.pop()
147+
!useDirectValue && this._wrap ? this._wrap(newValue) : newValue
148+
const subs = this.subs
149+
if (subs !== undefined) {
150+
if (__DEV__) {
151+
triggerEventInfos.push({
152+
target: this,
153+
type: TriggerOpTypes.SET,
154+
key: 'value',
155+
newValue,
156+
oldValue,
157+
})
158+
}
159+
propagate(subs)
160+
if (__DEV__) {
161+
triggerEventInfos.pop()
162+
}
160163
}
161164
}
162165
}

packages/runtime-core/src/renderer.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1599,8 +1599,7 @@ function baseCreateRenderer(
15991599
instance.scope.off()
16001600

16011601
const update = (instance.update = effect.run.bind(effect))
1602-
const job: SchedulerJob = (instance.job = () =>
1603-
effect.dirty && effect.run())
1602+
const job: SchedulerJob = (instance.job = effect.scheduler.bind(effect))
16041603
job.i = instance
16051604
job.id = instance.uid
16061605
effect.scheduler = () => queueJob(job)

packages/runtime-core/src/scheduler.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ function findInsertionIndex(id: number, isPre: boolean) {
9797
* @internal for runtime-vapor only
9898
*/
9999
export function queueJob(job: SchedulerJob, isPre = false): void {
100-
if (!(job.flags! & SchedulerJobFlags.QUEUED)) {
100+
const flags = job.flags!
101+
if (!(flags & SchedulerJobFlags.QUEUED)) {
101102
if (job.id === undefined) {
102103
job.id = isPre ? -1 : Infinity
103104
}
@@ -113,9 +114,7 @@ export function queueJob(job: SchedulerJob, isPre = false): void {
113114
queue.splice(findInsertionIndex(job.id, isPre), 0, job)
114115
}
115116
isPre ? preJobsLength++ : mainJobsLength++
116-
117-
job.flags! |= SchedulerJobFlags.QUEUED
118-
117+
job.flags! = flags | SchedulerJobFlags.QUEUED
119118
queueFlush()
120119
}
121120
}

packages/runtime-vapor/src/renderEffect.ts

+18-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
type EffectScope,
3-
ReactiveEffect,
4-
getCurrentScope,
5-
} from '@vue/reactivity'
1+
import { EffectFlags, type EffectScope, ReactiveEffect } from '@vue/reactivity'
62
import {
73
type SchedulerJob,
84
currentInstance,
@@ -17,24 +13,18 @@ import { invokeArrayFns } from '@vue/shared'
1713

1814
class RenderEffect extends ReactiveEffect {
1915
i: VaporComponentInstance | null
20-
scope: EffectScope | undefined
21-
baseJob: SchedulerJob
22-
postJob: SchedulerJob
16+
job: SchedulerJob
17+
updateJob: SchedulerJob
2318

2419
constructor(public render: () => void) {
2520
super()
2621
const instance = currentInstance as VaporComponentInstance | null
27-
const scope = getCurrentScope()
28-
if (__DEV__ && !__TEST__ && !scope && !isVaporComponent(instance)) {
22+
if (__DEV__ && !__TEST__ && !this.subs && !isVaporComponent(instance)) {
2923
warn('renderEffect called without active EffectScope or Vapor instance.')
3024
}
3125

32-
this.baseJob = () => {
33-
if (this.dirty) {
34-
this.run()
35-
}
36-
}
37-
this.postJob = () => {
26+
const job: SchedulerJob = super.scheduler.bind(this)
27+
this.updateJob = () => {
3828
instance!.isUpdating = false
3929
instance!.u && invokeArrayFns(instance!.u)
4030
}
@@ -48,19 +38,19 @@ class RenderEffect extends ReactiveEffect {
4838
? e => invokeArrayFns(instance.rtg!, e)
4939
: void 0
5040
}
51-
this.baseJob.i = instance
52-
this.baseJob.id = instance.uid
41+
job.i = instance
42+
job.id = instance.uid
5343
}
5444

45+
this.job = job
5546
this.i = instance
56-
this.scope = scope
5747

5848
// TODO recurse handling
5949
}
6050

6151
callback(): void {
6252
const instance = this.i!
63-
const scope = this.scope
53+
const scope = this.subs ? (this.subs.sub as EffectScope) : undefined
6454
// renderEffect is always called after user has registered all hooks
6555
const hasUpdateHooks = instance && (instance.bu || instance.u)
6656
if (__DEV__ && instance) {
@@ -73,7 +63,7 @@ class RenderEffect extends ReactiveEffect {
7363
instance.isUpdating = true
7464
instance.bu && invokeArrayFns(instance.bu)
7565
this.render()
76-
queuePostFlushCb(this.postJob)
66+
queuePostFlushCb(this.updateJob)
7767
} else {
7868
this.render()
7969
}
@@ -84,8 +74,13 @@ class RenderEffect extends ReactiveEffect {
8474
}
8575
}
8676

87-
scheduler(): void {
88-
queueJob(this.baseJob)
77+
notify(): void {
78+
const flags = this.flags
79+
if (!(flags & EffectFlags.PAUSED)) {
80+
queueJob(this.job)
81+
} else {
82+
this.flags = flags | EffectFlags.NOTIFIED
83+
}
8984
}
9085
}
9186

0 commit comments

Comments
 (0)