Skip to content

Commit

Permalink
feat: effect is always fired once on recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue committed Dec 6, 2023
1 parent 287c182 commit 7f7107a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 48 deletions.
14 changes: 7 additions & 7 deletions packages/reactivity/__tests__/effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1089,11 +1089,11 @@ describe('reactivity/effect', () => {
expect(obj.foo).toBe(2)

runner.effect.resume()
expect(fnSpy).toHaveBeenCalledTimes(1)
expect(fnSpy).toHaveBeenCalledTimes(2)
expect(obj.foo).toBe(2)

obj.foo++
expect(fnSpy).toHaveBeenCalledTimes(2)
expect(fnSpy).toHaveBeenCalledTimes(3)
expect(obj.foo).toBe(3)
})

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

runner.effect.resume(true)
expect(fnSpy).toHaveBeenCalledTimes(2)
expect(obj.foo).toBe(2)

obj.foo++
expect(fnSpy).toHaveBeenCalledTimes(3)
expect(fnSpy).toHaveBeenCalledTimes(1)
expect(obj.foo).toBe(3)

runner.effect.resume()
expect(fnSpy).toHaveBeenCalledTimes(2)
expect(obj.foo).toBe(3)
})
})
30 changes: 1 addition & 29 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,39 +316,11 @@ describe('reactivity/effect/scope', () => {
await nextTick()
expect(fnSpy).toHaveBeenCalledTimes(2)

scope.resume()
await nextTick()
expect(fnSpy).toHaveBeenCalledTimes(2)

counter.num++
await nextTick()
expect(fnSpy).toHaveBeenCalledTimes(3)
})

it('should execute all saved run methods in effects immediately upon resuming', async () => {
const counter = reactive({ num: 0 })
const fnSpy = vi.fn(() => counter.num)
const scope = new EffectScope()
scope.run(() => {
effect(fnSpy)
})

expect(fnSpy).toHaveBeenCalledTimes(1)

counter.num++
await nextTick()
expect(fnSpy).toHaveBeenCalledTimes(2)

scope.pause()
counter.num++
await nextTick()
expect(fnSpy).toHaveBeenCalledTimes(2)

scope.resume(true)
scope.resume()
expect(fnSpy).toHaveBeenCalledTimes(3)

counter.num++
await nextTick()
expect(fnSpy).toHaveBeenCalledTimes(4)
})
})
9 changes: 3 additions & 6 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,15 @@ export class ReactiveEffect<T = any> {

/**
* Resumes the execution of the reactive effect.
* @param {boolean} immediate - If true, executes the saved run method immediately upon resuming.
*/
resume(immediate: boolean = false) {
resume() {
if (!this._isStopped) {
this.active = true
if (pausedQueueEffects.has(this)) {
pausedQueueEffects.delete(this)
queueEffectSchedulers.push(this.scheduler!)
if (immediate) {
pauseScheduling()
resetScheduling()
}
pauseScheduling()
resetScheduling()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ export class EffectScope {
this._isPaused = false
if (this.scopes) {
for (let i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].resume(immediate)
this.scopes[i].resume()
}
}
for (let i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].resume(immediate)
this.effects[i].resume()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ describe('api: watch', () => {
expect(cb).toHaveBeenCalledTimes(3)
expect(cb).toHaveBeenLastCalledWith(4, 3, expect.any(Function))

resume(true)
resume()
await nextTick()
expect(cb).toHaveBeenCalledTimes(4)
expect(cb).toHaveBeenLastCalledWith(5, 4, expect.any(Function))
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export type WatchStopHandle = () => void

export interface WatchHandle extends WatchStopHandle {
pause: () => void
resume: (immediate?: boolean) => void
resume: () => void
stop: () => void
}

Expand Down Expand Up @@ -393,8 +393,8 @@ function doWatch(
}

const watchHandle: WatchHandle = () => unwatch()
watchHandle.pause = () => effect.pause()
watchHandle.resume = immediate => effect.resume(immediate)
watchHandle.pause = effect.pause.bind(effect)
watchHandle.resume = effect.resume.bind(effect)
watchHandle.stop = unwatch

if (__DEV__) {
Expand Down

0 comments on commit 7f7107a

Please sign in to comment.