Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): make it possible to get the computed's value in the unmounted hook #3528

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/reactivity/__tests__/effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,30 @@ describe('reactivity/effect', () => {
expect(dummy).toBe(1)
})

// #3099
it('create an effect with allowInactiveRun', () => {
let dummy
const obj = reactive({ prop: 1 })
const queue: (() => void)[] = []
const runner = effect(
() => {
dummy = obj.prop
},
{
scheduler: e => queue.push(e),
allowInactiveRun: true
}
)
obj.prop = 2
expect(dummy).toBe(1)
expect(queue.length).toBe(1)
stop(runner)

// an effect with allowInactiveRun should be executed even after it is stopped
queue.forEach(e => e())
expect(dummy).toBe(2)
})

it('events: onStop', () => {
const onStop = jest.fn()
const runner = effect(() => {}, {
Expand Down
3 changes: 2 additions & 1 deletion packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class ComputedRefImpl<T> {
this._dirty = true
trigger(toRaw(this), TriggerOpTypes.SET, 'value')
}
}
},
allowInactiveRun: true
})

this[ReactiveFlags.IS_READONLY] = isReadonly
Expand Down
10 changes: 7 additions & 3 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TrackOpTypes, TriggerOpTypes } from './operations'
import { EMPTY_OBJ, isArray, isIntegerKey, isMap } from '@vue/shared'
import { isArray, isIntegerKey, isMap } from '@vue/shared'

// The main WeakMap that stores {target -> key -> dep} connections.
// Conceptually, it's easier to think of a dependency as a Dep class
Expand Down Expand Up @@ -27,6 +27,7 @@ export interface ReactiveEffectOptions {
onTrigger?: (event: DebuggerEvent) => void
onStop?: () => void
allowRecurse?: boolean
allowInactiveRun?: boolean
}

export type DebuggerEvent = {
Expand Down Expand Up @@ -54,11 +55,14 @@ export function isEffect(fn: any): fn is ReactiveEffect {

export function effect<T = any>(
fn: () => T,
options: ReactiveEffectOptions = EMPTY_OBJ
options: ReactiveEffectOptions = Object.create(null)
): ReactiveEffect<T> {
if (isEffect(fn)) {
fn = fn.raw
}
if (!('allowInactiveRun' in options)) {
options.allowInactiveRun = !options.scheduler
}
const effect = createReactiveEffect(fn, options)
if (!options.lazy) {
effect()
Expand All @@ -84,7 +88,7 @@ function createReactiveEffect<T = any>(
): ReactiveEffect<T> {
const effect = function reactiveEffect(): unknown {
if (!effect.active) {
return options.scheduler ? undefined : fn()
return options.allowInactiveRun ? fn() : undefined
}
if (!effectStack.includes(effect)) {
cleanup(effect)
Expand Down