Skip to content

Commit

Permalink
fix(types/effectScope): re-expose active as readonly property (#6187)
Browse files Browse the repository at this point in the history
close #6186
  • Loading branch information
webfansplz authored Nov 14, 2022
1 parent 4e5d9cd commit 59ffe5e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 77 deletions.
8 changes: 8 additions & 0 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ describe('reactivity/effect/scope', () => {
expect(new EffectScope().run(() => 1)).toBe(1)
})

it('should work w/ active property', () => {
const scope = new EffectScope()
scope.run(() => 1)
expect(scope.active).toBe(true)
scope.stop()
expect(scope.active).toBe(false)
})

it('should collect the effects', () => {
const scope = new EffectScope()
scope.run(() => {
Expand Down
12 changes: 8 additions & 4 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class EffectScope {
/**
* @internal
*/
active = true
private _active = true
/**
* @internal
*/
Expand Down Expand Up @@ -44,8 +44,12 @@ export class EffectScope {
}
}

get active() {
return this._active
}

run<T>(fn: () => T): T | undefined {
if (this.active) {
if (this._active) {
const currentEffectScope = activeEffectScope
try {
activeEffectScope = this
Expand Down Expand Up @@ -75,7 +79,7 @@ export class EffectScope {
}

stop(fromParent?: boolean) {
if (this.active) {
if (this._active) {
let i, l
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].stop()
Expand All @@ -98,7 +102,7 @@ export class EffectScope {
}
}
this.parent = undefined
this.active = false
this._active = false
}
}
}
Expand Down
146 changes: 73 additions & 73 deletions test-dts/reactivity.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
import {
ref,
readonly,
shallowReadonly,
describe,
expectError,
expectType,
Ref,
reactive,
markRaw
} from './index'

describe('should support DeepReadonly', () => {
const r = readonly({ obj: { k: 'v' } })
// @ts-expect-error
expectError((r.obj = {}))
// @ts-expect-error
expectError((r.obj.k = 'x'))
})

// #4180
describe('readonly ref', () => {
const r = readonly(ref({ count: 1 }))
expectType<Ref>(r)
})

describe('should support markRaw', () => {
class Test<T> {
item = {} as Ref<T>
}
const test = new Test<number>()
const plain = {
ref: ref(1)
}

const r = reactive({
class: {
raw: markRaw(test),
reactive: test
},
plain: {
raw: markRaw(plain),
reactive: plain
}
})

expectType<Test<number>>(r.class.raw)
// @ts-expect-error it should unwrap
expectType<Test<number>>(r.class.reactive)

expectType<Ref<number>>(r.plain.raw.ref)
// @ts-expect-error it should unwrap
expectType<Ref<number>>(r.plain.reactive.ref)
})

describe('shallowReadonly ref unwrap', () => {
const r = shallowReadonly({ count: { n: ref(1) } })
// @ts-expect-error
r.count = 2
expectType<Ref>(r.count.n)
r.count.n.value = 123
})

// #3819
describe('should unwrap tuple correctly', () => {
const readonlyTuple = [ref(0)] as const
const reactiveReadonlyTuple = reactive(readonlyTuple)
expectType<Ref<number>>(reactiveReadonlyTuple[0])

const tuple: [Ref<number>] = [ref(0)]
const reactiveTuple = reactive(tuple)
expectType<Ref<number>>(reactiveTuple[0])
})
import {
ref,
readonly,
shallowReadonly,
describe,
expectError,
expectType,
Ref,
reactive,
markRaw
} from './index'

describe('should support DeepReadonly', () => {
const r = readonly({ obj: { k: 'v' } })
// @ts-expect-error
expectError((r.obj = {}))
// @ts-expect-error
expectError((r.obj.k = 'x'))
})

// #4180
describe('readonly ref', () => {
const r = readonly(ref({ count: 1 }))
expectType<Ref>(r)
})

describe('should support markRaw', () => {
class Test<T> {
item = {} as Ref<T>
}
const test = new Test<number>()
const plain = {
ref: ref(1)
}

const r = reactive({
class: {
raw: markRaw(test),
reactive: test
},
plain: {
raw: markRaw(plain),
reactive: plain
}
})

expectType<Test<number>>(r.class.raw)
// @ts-expect-error it should unwrap
expectType<Test<number>>(r.class.reactive)

expectType<Ref<number>>(r.plain.raw.ref)
// @ts-expect-error it should unwrap
expectType<Ref<number>>(r.plain.reactive.ref)
})

describe('shallowReadonly ref unwrap', () => {
const r = shallowReadonly({ count: { n: ref(1) } })
// @ts-expect-error
r.count = 2
expectType<Ref>(r.count.n)
r.count.n.value = 123
})

// #3819
describe('should unwrap tuple correctly', () => {
const readonlyTuple = [ref(0)] as const
const reactiveReadonlyTuple = reactive(readonlyTuple)
expectType<Ref<number>>(reactiveReadonlyTuple[0])

const tuple: [Ref<number>] = [ref(0)]
const reactiveTuple = reactive(tuple)
expectType<Ref<number>>(reactiveTuple[0])
})

0 comments on commit 59ffe5e

Please sign in to comment.