Skip to content

Commit 7f7107a

Browse files
feat: effect is always fired once on recovery
1 parent 287c182 commit 7f7107a

File tree

6 files changed

+17
-48
lines changed

6 files changed

+17
-48
lines changed

packages/reactivity/__tests__/effect.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,11 +1089,11 @@ describe('reactivity/effect', () => {
10891089
expect(obj.foo).toBe(2)
10901090

10911091
runner.effect.resume()
1092-
expect(fnSpy).toHaveBeenCalledTimes(1)
1092+
expect(fnSpy).toHaveBeenCalledTimes(2)
10931093
expect(obj.foo).toBe(2)
10941094

10951095
obj.foo++
1096-
expect(fnSpy).toHaveBeenCalledTimes(2)
1096+
expect(fnSpy).toHaveBeenCalledTimes(3)
10971097
expect(obj.foo).toBe(3)
10981098
})
10991099

@@ -1110,12 +1110,12 @@ describe('reactivity/effect', () => {
11101110
expect(fnSpy).toHaveBeenCalledTimes(1)
11111111
expect(obj.foo).toBe(2)
11121112

1113-
runner.effect.resume(true)
1114-
expect(fnSpy).toHaveBeenCalledTimes(2)
1115-
expect(obj.foo).toBe(2)
1116-
11171113
obj.foo++
1118-
expect(fnSpy).toHaveBeenCalledTimes(3)
1114+
expect(fnSpy).toHaveBeenCalledTimes(1)
1115+
expect(obj.foo).toBe(3)
1116+
1117+
runner.effect.resume()
1118+
expect(fnSpy).toHaveBeenCalledTimes(2)
11191119
expect(obj.foo).toBe(3)
11201120
})
11211121
})

packages/reactivity/__tests__/effectScope.spec.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -316,39 +316,11 @@ describe('reactivity/effect/scope', () => {
316316
await nextTick()
317317
expect(fnSpy).toHaveBeenCalledTimes(2)
318318

319-
scope.resume()
320-
await nextTick()
321-
expect(fnSpy).toHaveBeenCalledTimes(2)
322-
323-
counter.num++
324-
await nextTick()
325-
expect(fnSpy).toHaveBeenCalledTimes(3)
326-
})
327-
328-
it('should execute all saved run methods in effects immediately upon resuming', async () => {
329-
const counter = reactive({ num: 0 })
330-
const fnSpy = vi.fn(() => counter.num)
331-
const scope = new EffectScope()
332-
scope.run(() => {
333-
effect(fnSpy)
334-
})
335-
336-
expect(fnSpy).toHaveBeenCalledTimes(1)
337-
338319
counter.num++
339320
await nextTick()
340321
expect(fnSpy).toHaveBeenCalledTimes(2)
341322

342-
scope.pause()
343-
counter.num++
344-
await nextTick()
345-
expect(fnSpy).toHaveBeenCalledTimes(2)
346-
347-
scope.resume(true)
323+
scope.resume()
348324
expect(fnSpy).toHaveBeenCalledTimes(3)
349-
350-
counter.num++
351-
await nextTick()
352-
expect(fnSpy).toHaveBeenCalledTimes(4)
353325
})
354326
})

packages/reactivity/src/effect.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,15 @@ export class ReactiveEffect<T = any> {
103103

104104
/**
105105
* Resumes the execution of the reactive effect.
106-
* @param {boolean} immediate - If true, executes the saved run method immediately upon resuming.
107106
*/
108-
resume(immediate: boolean = false) {
107+
resume() {
109108
if (!this._isStopped) {
110109
this.active = true
111110
if (pausedQueueEffects.has(this)) {
112111
pausedQueueEffects.delete(this)
113112
queueEffectSchedulers.push(this.scheduler!)
114-
if (immediate) {
115-
pauseScheduling()
116-
resetScheduling()
117-
}
113+
pauseScheduling()
114+
resetScheduling()
118115
}
119116
}
120117
}

packages/reactivity/src/effectScope.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ export class EffectScope {
7575
this._isPaused = false
7676
if (this.scopes) {
7777
for (let i = 0, l = this.scopes.length; i < l; i++) {
78-
this.scopes[i].resume(immediate)
78+
this.scopes[i].resume()
7979
}
8080
}
8181
for (let i = 0, l = this.effects.length; i < l; i++) {
82-
this.effects[i].resume(immediate)
82+
this.effects[i].resume()
8383
}
8484
}
8585
}

packages/runtime-core/__tests__/apiWatch.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ describe('api: watch', () => {
13121312
expect(cb).toHaveBeenCalledTimes(3)
13131313
expect(cb).toHaveBeenLastCalledWith(4, 3, expect.any(Function))
13141314

1315-
resume(true)
1315+
resume()
13161316
await nextTick()
13171317
expect(cb).toHaveBeenCalledTimes(4)
13181318
expect(cb).toHaveBeenLastCalledWith(5, 4, expect.any(Function))

packages/runtime-core/src/apiWatch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export type WatchStopHandle = () => void
8282

8383
export interface WatchHandle extends WatchStopHandle {
8484
pause: () => void
85-
resume: (immediate?: boolean) => void
85+
resume: () => void
8686
stop: () => void
8787
}
8888

@@ -393,8 +393,8 @@ function doWatch(
393393
}
394394

395395
const watchHandle: WatchHandle = () => unwatch()
396-
watchHandle.pause = () => effect.pause()
397-
watchHandle.resume = immediate => effect.resume(immediate)
396+
watchHandle.pause = effect.pause.bind(effect)
397+
watchHandle.resume = effect.resume.bind(effect)
398398
watchHandle.stop = unwatch
399399

400400
if (__DEV__) {

0 commit comments

Comments
 (0)