]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types/effectScope): re-expose `active` as readonly property (#6187)
authorwebfansplz <308241863@qq.com>
Mon, 14 Nov 2022 01:27:52 +0000 (09:27 +0800)
committerGitHub <noreply@github.com>
Mon, 14 Nov 2022 01:27:52 +0000 (20:27 -0500)
close #6186

packages/reactivity/__tests__/effectScope.spec.ts
packages/reactivity/src/effectScope.ts
test-dts/reactivity.test-d.ts

index 28579fef028327d8616c426cb91b41b00a0ed12d..a670c1ebc07c55746b21fcc76a52e8c50e67ade9 100644 (file)
@@ -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(() => {
index 557a1379492c1aa005d9ec6ab1ebf64ba6647b60..c644d03fae6ca90a33d062b1ed9223d9a3c13f33 100644 (file)
@@ -7,7 +7,7 @@ export class EffectScope {
   /**
    * @internal
    */
-  active = true
+  private _active = true
   /**
    * @internal
    */
@@ -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
@@ -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()
@@ -98,7 +102,7 @@ export class EffectScope {
         }
       }
       this.parent = undefined
-      this.active = false
+      this._active = false
     }
   }
 }
index 8722441ec14a540fa23df412f4df173a1014ed80..337f05e147e41a7e65caa3942e2586d5dca26c03 100644 (file)
@@ -1,73 +1,73 @@
-import {\r
-  ref,\r
-  readonly,\r
-  shallowReadonly,\r
-  describe,\r
-  expectError,\r
-  expectType,\r
-  Ref,\r
-  reactive,\r
-  markRaw\r
-} from './index'\r
-\r
-describe('should support DeepReadonly', () => {\r
-  const r = readonly({ obj: { k: 'v' } })\r
-  // @ts-expect-error\r
-  expectError((r.obj = {}))\r
-  // @ts-expect-error\r
-  expectError((r.obj.k = 'x'))\r
-})\r
-\r
-// #4180\r
-describe('readonly ref', () => {\r
-  const r = readonly(ref({ count: 1 }))\r
-  expectType<Ref>(r)\r
-})\r
-\r
-describe('should support markRaw', () => {\r
-  class Test<T> {\r
-    item = {} as Ref<T>\r
-  }\r
-  const test = new Test<number>()\r
-  const plain = {\r
-    ref: ref(1)\r
-  }\r
-\r
-  const r = reactive({\r
-    class: {\r
-      raw: markRaw(test),\r
-      reactive: test\r
-    },\r
-    plain: {\r
-      raw: markRaw(plain),\r
-      reactive: plain\r
-    }\r
-  })\r
-\r
-  expectType<Test<number>>(r.class.raw)\r
-  // @ts-expect-error it should unwrap\r
-  expectType<Test<number>>(r.class.reactive)\r
-\r
-  expectType<Ref<number>>(r.plain.raw.ref)\r
-  // @ts-expect-error it should unwrap\r
-  expectType<Ref<number>>(r.plain.reactive.ref)\r
-})\r
-\r
-describe('shallowReadonly ref unwrap', () => {\r
-  const r = shallowReadonly({ count: { n: ref(1) } })\r
-  // @ts-expect-error\r
-  r.count = 2\r
-  expectType<Ref>(r.count.n)\r
-  r.count.n.value = 123\r
-})\r
-\r
-// #3819\r
-describe('should unwrap tuple correctly', () => {\r
-  const readonlyTuple = [ref(0)] as const\r
-  const reactiveReadonlyTuple = reactive(readonlyTuple)\r
-  expectType<Ref<number>>(reactiveReadonlyTuple[0])\r
-\r
-  const tuple: [Ref<number>] = [ref(0)]\r
-  const reactiveTuple = reactive(tuple)\r
-  expectType<Ref<number>>(reactiveTuple[0])\r
-})\r
+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])
+})