Skip to content

Commit 59ffe5e

Browse files
authored
fix(types/effectScope): re-expose active as readonly property (#6187)
close #6186
1 parent 4e5d9cd commit 59ffe5e

File tree

3 files changed

+89
-77
lines changed

3 files changed

+89
-77
lines changed

packages/reactivity/__tests__/effectScope.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ describe('reactivity/effect/scope', () => {
2626
expect(new EffectScope().run(() => 1)).toBe(1)
2727
})
2828

29+
it('should work w/ active property', () => {
30+
const scope = new EffectScope()
31+
scope.run(() => 1)
32+
expect(scope.active).toBe(true)
33+
scope.stop()
34+
expect(scope.active).toBe(false)
35+
})
36+
2937
it('should collect the effects', () => {
3038
const scope = new EffectScope()
3139
scope.run(() => {

packages/reactivity/src/effectScope.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class EffectScope {
77
/**
88
* @internal
99
*/
10-
active = true
10+
private _active = true
1111
/**
1212
* @internal
1313
*/
@@ -44,8 +44,12 @@ export class EffectScope {
4444
}
4545
}
4646

47+
get active() {
48+
return this._active
49+
}
50+
4751
run<T>(fn: () => T): T | undefined {
48-
if (this.active) {
52+
if (this._active) {
4953
const currentEffectScope = activeEffectScope
5054
try {
5155
activeEffectScope = this
@@ -75,7 +79,7 @@ export class EffectScope {
7579
}
7680

7781
stop(fromParent?: boolean) {
78-
if (this.active) {
82+
if (this._active) {
7983
let i, l
8084
for (i = 0, l = this.effects.length; i < l; i++) {
8185
this.effects[i].stop()
@@ -98,7 +102,7 @@ export class EffectScope {
98102
}
99103
}
100104
this.parent = undefined
101-
this.active = false
105+
this._active = false
102106
}
103107
}
104108
}

test-dts/reactivity.test-d.ts

+73-73
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
1-
import {
2-
ref,
3-
readonly,
4-
shallowReadonly,
5-
describe,
6-
expectError,
7-
expectType,
8-
Ref,
9-
reactive,
10-
markRaw
11-
} from './index'
12-
13-
describe('should support DeepReadonly', () => {
14-
const r = readonly({ obj: { k: 'v' } })
15-
// @ts-expect-error
16-
expectError((r.obj = {}))
17-
// @ts-expect-error
18-
expectError((r.obj.k = 'x'))
19-
})
20-
21-
// #4180
22-
describe('readonly ref', () => {
23-
const r = readonly(ref({ count: 1 }))
24-
expectType<Ref>(r)
25-
})
26-
27-
describe('should support markRaw', () => {
28-
class Test<T> {
29-
item = {} as Ref<T>
30-
}
31-
const test = new Test<number>()
32-
const plain = {
33-
ref: ref(1)
34-
}
35-
36-
const r = reactive({
37-
class: {
38-
raw: markRaw(test),
39-
reactive: test
40-
},
41-
plain: {
42-
raw: markRaw(plain),
43-
reactive: plain
44-
}
45-
})
46-
47-
expectType<Test<number>>(r.class.raw)
48-
// @ts-expect-error it should unwrap
49-
expectType<Test<number>>(r.class.reactive)
50-
51-
expectType<Ref<number>>(r.plain.raw.ref)
52-
// @ts-expect-error it should unwrap
53-
expectType<Ref<number>>(r.plain.reactive.ref)
54-
})
55-
56-
describe('shallowReadonly ref unwrap', () => {
57-
const r = shallowReadonly({ count: { n: ref(1) } })
58-
// @ts-expect-error
59-
r.count = 2
60-
expectType<Ref>(r.count.n)
61-
r.count.n.value = 123
62-
})
63-
64-
// #3819
65-
describe('should unwrap tuple correctly', () => {
66-
const readonlyTuple = [ref(0)] as const
67-
const reactiveReadonlyTuple = reactive(readonlyTuple)
68-
expectType<Ref<number>>(reactiveReadonlyTuple[0])
69-
70-
const tuple: [Ref<number>] = [ref(0)]
71-
const reactiveTuple = reactive(tuple)
72-
expectType<Ref<number>>(reactiveTuple[0])
73-
})
1+
import {
2+
ref,
3+
readonly,
4+
shallowReadonly,
5+
describe,
6+
expectError,
7+
expectType,
8+
Ref,
9+
reactive,
10+
markRaw
11+
} from './index'
12+
13+
describe('should support DeepReadonly', () => {
14+
const r = readonly({ obj: { k: 'v' } })
15+
// @ts-expect-error
16+
expectError((r.obj = {}))
17+
// @ts-expect-error
18+
expectError((r.obj.k = 'x'))
19+
})
20+
21+
// #4180
22+
describe('readonly ref', () => {
23+
const r = readonly(ref({ count: 1 }))
24+
expectType<Ref>(r)
25+
})
26+
27+
describe('should support markRaw', () => {
28+
class Test<T> {
29+
item = {} as Ref<T>
30+
}
31+
const test = new Test<number>()
32+
const plain = {
33+
ref: ref(1)
34+
}
35+
36+
const r = reactive({
37+
class: {
38+
raw: markRaw(test),
39+
reactive: test
40+
},
41+
plain: {
42+
raw: markRaw(plain),
43+
reactive: plain
44+
}
45+
})
46+
47+
expectType<Test<number>>(r.class.raw)
48+
// @ts-expect-error it should unwrap
49+
expectType<Test<number>>(r.class.reactive)
50+
51+
expectType<Ref<number>>(r.plain.raw.ref)
52+
// @ts-expect-error it should unwrap
53+
expectType<Ref<number>>(r.plain.reactive.ref)
54+
})
55+
56+
describe('shallowReadonly ref unwrap', () => {
57+
const r = shallowReadonly({ count: { n: ref(1) } })
58+
// @ts-expect-error
59+
r.count = 2
60+
expectType<Ref>(r.count.n)
61+
r.count.n.value = 123
62+
})
63+
64+
// #3819
65+
describe('should unwrap tuple correctly', () => {
66+
const readonlyTuple = [ref(0)] as const
67+
const reactiveReadonlyTuple = reactive(readonlyTuple)
68+
expectType<Ref<number>>(reactiveReadonlyTuple[0])
69+
70+
const tuple: [Ref<number>] = [ref(0)]
71+
const reactiveTuple = reactive(tuple)
72+
expectType<Ref<number>>(reactiveTuple[0])
73+
})

0 commit comments

Comments
 (0)