]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: state -> reactive, value -> ref
authorEvan You <yyx990803@gmail.com>
Fri, 16 Aug 2019 13:42:46 +0000 (09:42 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 16 Aug 2019 13:42:46 +0000 (09:42 -0400)
17 files changed:
packages/reactivity/__tests__/collections/Map.spec.ts
packages/reactivity/__tests__/collections/Set.spec.ts
packages/reactivity/__tests__/collections/WeakMap.spec.ts
packages/reactivity/__tests__/collections/WeakSet.spec.ts
packages/reactivity/__tests__/computed.spec.ts
packages/reactivity/__tests__/effect.spec.ts
packages/reactivity/__tests__/immutableState.spec.ts
packages/reactivity/__tests__/reactive.spec.ts [moved from packages/reactivity/__tests__/state.spec.ts with 72% similarity]
packages/reactivity/__tests__/ref.spec.ts [moved from packages/reactivity/__tests__/value.spec.ts with 86% similarity]
packages/reactivity/src/baseHandlers.ts
packages/reactivity/src/collectionHandlers.ts
packages/reactivity/src/computed.ts
packages/reactivity/src/effect.ts
packages/reactivity/src/index.ts
packages/reactivity/src/reactive.ts [moved from packages/reactivity/src/state.ts with 91% similarity]
packages/reactivity/src/ref.ts [new file with mode: 0644]
packages/reactivity/src/value.ts [deleted file]

index 090e40e6d20ec6ea21ddf1a9dd0bef9a5ec7a0ea..14558270abde18d057aa6ed46b5346cc968c2d7e 100644 (file)
@@ -1,18 +1,18 @@
-import { state, effect, toRaw, isState } from '../../src'
+import { reactive, effect, toRaw, isReactive } from '../../src'
 
 describe('observer/collections', () => {
   describe('Map', () => {
     test('instanceof', () => {
       const original = new Map()
-      const observed = state(original)
-      expect(isState(observed)).toBe(true)
+      const observed = reactive(original)
+      expect(isReactive(observed)).toBe(true)
       expect(original instanceof Map).toBe(true)
       expect(observed instanceof Map).toBe(true)
     })
 
     it('should observe mutations', () => {
       let dummy
-      const map = state(new Map())
+      const map = reactive(new Map())
       effect(() => {
         dummy = map.get('key')
       })
@@ -28,7 +28,7 @@ describe('observer/collections', () => {
 
     it('should observe size mutations', () => {
       let dummy
-      const map = state(new Map())
+      const map = reactive(new Map())
       effect(() => (dummy = map.size))
 
       expect(dummy).toBe(0)
@@ -43,7 +43,7 @@ describe('observer/collections', () => {
 
     it('should observe for of iteration', () => {
       let dummy
-      const map = state(new Map())
+      const map = reactive(new Map())
       effect(() => {
         dummy = 0
         // eslint-disable-next-line no-unused-vars
@@ -66,7 +66,7 @@ describe('observer/collections', () => {
 
     it('should observe forEach iteration', () => {
       let dummy: any
-      const map = state(new Map())
+      const map = reactive(new Map())
       effect(() => {
         dummy = 0
         map.forEach((num: any) => (dummy += num))
@@ -85,7 +85,7 @@ describe('observer/collections', () => {
 
     it('should observe keys iteration', () => {
       let dummy
-      const map = state(new Map())
+      const map = reactive(new Map())
       effect(() => {
         dummy = 0
         for (let key of map.keys()) {
@@ -106,7 +106,7 @@ describe('observer/collections', () => {
 
     it('should observe values iteration', () => {
       let dummy
-      const map = state(new Map())
+      const map = reactive(new Map())
       effect(() => {
         dummy = 0
         for (let num of map.values()) {
@@ -127,7 +127,7 @@ describe('observer/collections', () => {
 
     it('should observe entries iteration', () => {
       let dummy
-      const map = state(new Map())
+      const map = reactive(new Map())
       effect(() => {
         dummy = 0
         // eslint-disable-next-line no-unused-vars
@@ -150,7 +150,7 @@ describe('observer/collections', () => {
 
     it('should be triggered by clearing', () => {
       let dummy
-      const map = state(new Map())
+      const map = reactive(new Map())
       effect(() => (dummy = map.get('key')))
 
       expect(dummy).toBe(undefined)
@@ -162,7 +162,7 @@ describe('observer/collections', () => {
 
     it('should not observe custom property mutations', () => {
       let dummy
-      const map: any = state(new Map())
+      const map: any = reactive(new Map())
       effect(() => (dummy = map.customProp))
 
       expect(dummy).toBe(undefined)
@@ -172,7 +172,7 @@ describe('observer/collections', () => {
 
     it('should not observe non value changing mutations', () => {
       let dummy
-      const map = state(new Map())
+      const map = reactive(new Map())
       const mapSpy = jest.fn(() => (dummy = map.get('key')))
       effect(mapSpy)
 
@@ -197,7 +197,7 @@ describe('observer/collections', () => {
 
     it('should not observe raw data', () => {
       let dummy
-      const map = state(new Map())
+      const map = reactive(new Map())
       effect(() => (dummy = toRaw(map).get('key')))
 
       expect(dummy).toBe(undefined)
@@ -209,24 +209,24 @@ describe('observer/collections', () => {
 
     it('should not pollute original Map with Proxies', () => {
       const map = new Map()
-      const observed = state(map)
-      const value = state({})
+      const observed = reactive(map)
+      const value = reactive({})
       observed.set('key', value)
       expect(map.get('key')).not.toBe(value)
       expect(map.get('key')).toBe(toRaw(value))
     })
 
     it('should return observable versions of contained values', () => {
-      const observed = state(new Map())
+      const observed = reactive(new Map())
       const value = {}
       observed.set('key', value)
       const wrapped = observed.get('key')
-      expect(isState(wrapped)).toBe(true)
+      expect(isReactive(wrapped)).toBe(true)
       expect(toRaw(wrapped)).toBe(value)
     })
 
     it('should observed nested data', () => {
-      const observed = state(new Map())
+      const observed = reactive(new Map())
       observed.set('key', { a: 1 })
       let dummy
       effect(() => {
@@ -237,12 +237,12 @@ describe('observer/collections', () => {
     })
 
     it('should observe nested values in iterations (forEach)', () => {
-      const map = state(new Map([[1, { foo: 1 }]]))
+      const map = reactive(new Map([[1, { foo: 1 }]]))
       let dummy: any
       effect(() => {
         dummy = 0
         map.forEach(value => {
-          expect(isState(value)).toBe(true)
+          expect(isReactive(value)).toBe(true)
           dummy += value.foo
         })
       })
@@ -252,12 +252,12 @@ describe('observer/collections', () => {
     })
 
     it('should observe nested values in iterations (values)', () => {
-      const map = state(new Map([[1, { foo: 1 }]]))
+      const map = reactive(new Map([[1, { foo: 1 }]]))
       let dummy: any
       effect(() => {
         dummy = 0
         for (const value of map.values()) {
-          expect(isState(value)).toBe(true)
+          expect(isReactive(value)).toBe(true)
           dummy += value.foo
         }
       })
@@ -268,14 +268,14 @@ describe('observer/collections', () => {
 
     it('should observe nested values in iterations (entries)', () => {
       const key = {}
-      const map = state(new Map([[key, { foo: 1 }]]))
+      const map = reactive(new Map([[key, { foo: 1 }]]))
       let dummy: any
       effect(() => {
         dummy = 0
         for (const [key, value] of map.entries()) {
           key
-          expect(isState(key)).toBe(true)
-          expect(isState(value)).toBe(true)
+          expect(isReactive(key)).toBe(true)
+          expect(isReactive(value)).toBe(true)
           dummy += value.foo
         }
       })
@@ -286,14 +286,14 @@ describe('observer/collections', () => {
 
     it('should observe nested values in iterations (for...of)', () => {
       const key = {}
-      const map = state(new Map([[key, { foo: 1 }]]))
+      const map = reactive(new Map([[key, { foo: 1 }]]))
       let dummy: any
       effect(() => {
         dummy = 0
         for (const [key, value] of map) {
           key
-          expect(isState(key)).toBe(true)
-          expect(isState(value)).toBe(true)
+          expect(isReactive(key)).toBe(true)
+          expect(isReactive(value)).toBe(true)
           dummy += value.foo
         }
       })
index 9abc56a2dafce94ff509070a40170448140842af..1fc4bfda582cd934f7c2012e210b814399387c3a 100644 (file)
@@ -1,18 +1,18 @@
-import { state, effect, isState, toRaw } from '../../src'
+import { reactive, effect, isReactive, toRaw } from '../../src'
 
 describe('observer/collections', () => {
   describe('Set', () => {
     it('instanceof', () => {
       const original = new Set()
-      const observed = state(original)
-      expect(isState(observed)).toBe(true)
+      const observed = reactive(original)
+      expect(isReactive(observed)).toBe(true)
       expect(original instanceof Set).toBe(true)
       expect(observed instanceof Set).toBe(true)
     })
 
     it('should observe mutations', () => {
       let dummy
-      const set = state(new Set())
+      const set = reactive(new Set())
       effect(() => (dummy = set.has('value')))
 
       expect(dummy).toBe(false)
@@ -24,7 +24,7 @@ describe('observer/collections', () => {
 
     it('should observe for of iteration', () => {
       let dummy
-      const set = state(new Set() as Set<number>)
+      const set = reactive(new Set() as Set<number>)
       effect(() => {
         dummy = 0
         for (let num of set) {
@@ -44,7 +44,7 @@ describe('observer/collections', () => {
 
     it('should observe forEach iteration', () => {
       let dummy: any
-      const set = state(new Set())
+      const set = reactive(new Set())
       effect(() => {
         dummy = 0
         set.forEach(num => (dummy += num))
@@ -62,7 +62,7 @@ describe('observer/collections', () => {
 
     it('should observe values iteration', () => {
       let dummy
-      const set = state(new Set() as Set<number>)
+      const set = reactive(new Set() as Set<number>)
       effect(() => {
         dummy = 0
         for (let num of set.values()) {
@@ -82,7 +82,7 @@ describe('observer/collections', () => {
 
     it('should observe keys iteration', () => {
       let dummy
-      const set = state(new Set() as Set<number>)
+      const set = reactive(new Set() as Set<number>)
       effect(() => {
         dummy = 0
         for (let num of set.keys()) {
@@ -102,7 +102,7 @@ describe('observer/collections', () => {
 
     it('should observe entries iteration', () => {
       let dummy
-      const set = state(new Set() as Set<number>)
+      const set = reactive(new Set() as Set<number>)
       effect(() => {
         dummy = 0
         // eslint-disable-next-line no-unused-vars
@@ -124,7 +124,7 @@ describe('observer/collections', () => {
 
     it('should be triggered by clearing', () => {
       let dummy
-      const set = state(new Set())
+      const set = reactive(new Set())
       effect(() => (dummy = set.has('key')))
 
       expect(dummy).toBe(false)
@@ -136,7 +136,7 @@ describe('observer/collections', () => {
 
     it('should not observe custom property mutations', () => {
       let dummy
-      const set: any = state(new Set())
+      const set: any = reactive(new Set())
       effect(() => (dummy = set.customProp))
 
       expect(dummy).toBe(undefined)
@@ -146,7 +146,7 @@ describe('observer/collections', () => {
 
     it('should observe size mutations', () => {
       let dummy
-      const set = state(new Set())
+      const set = reactive(new Set())
       effect(() => (dummy = set.size))
 
       expect(dummy).toBe(0)
@@ -161,7 +161,7 @@ describe('observer/collections', () => {
 
     it('should not observe non value changing mutations', () => {
       let dummy
-      const set = state(new Set())
+      const set = reactive(new Set())
       const setSpy = jest.fn(() => (dummy = set.has('value')))
       effect(setSpy)
 
@@ -186,7 +186,7 @@ describe('observer/collections', () => {
 
     it('should not observe raw data', () => {
       let dummy
-      const set = state(new Set())
+      const set = reactive(new Set())
       effect(() => (dummy = toRaw(set).has('value')))
 
       expect(dummy).toBe(false)
@@ -196,7 +196,7 @@ describe('observer/collections', () => {
 
     it('should not observe raw iterations', () => {
       let dummy = 0
-      const set = state(new Set() as Set<number>)
+      const set = reactive(new Set() as Set<number>)
       effect(() => {
         dummy = 0
         for (let [num] of toRaw(set).entries()) {
@@ -226,7 +226,7 @@ describe('observer/collections', () => {
 
     it('should not be triggered by raw mutations', () => {
       let dummy
-      const set = state(new Set())
+      const set = reactive(new Set())
       effect(() => (dummy = set.has('value')))
 
       expect(dummy).toBe(false)
@@ -241,7 +241,7 @@ describe('observer/collections', () => {
 
     it('should not observe raw size mutations', () => {
       let dummy
-      const set = state(new Set())
+      const set = reactive(new Set())
       effect(() => (dummy = toRaw(set).size))
 
       expect(dummy).toBe(0)
@@ -251,7 +251,7 @@ describe('observer/collections', () => {
 
     it('should not be triggered by raw size mutations', () => {
       let dummy
-      const set = state(new Set())
+      const set = reactive(new Set())
       effect(() => (dummy = set.size))
 
       expect(dummy).toBe(0)
@@ -262,7 +262,7 @@ describe('observer/collections', () => {
     it('should support objects as key', () => {
       let dummy
       const key = {}
-      const set = state(new Set())
+      const set = reactive(new Set())
       const setSpy = jest.fn(() => (dummy = set.has(key)))
       effect(setSpy)
 
@@ -280,20 +280,20 @@ describe('observer/collections', () => {
 
     it('should not pollute original Set with Proxies', () => {
       const set = new Set()
-      const observed = state(set)
-      const value = state({})
+      const observed = reactive(set)
+      const value = reactive({})
       observed.add(value)
       expect(observed.has(value)).toBe(true)
       expect(set.has(value)).toBe(false)
     })
 
     it('should observe nested values in iterations (forEach)', () => {
-      const set = state(new Set([{ foo: 1 }]))
+      const set = reactive(new Set([{ foo: 1 }]))
       let dummy: any
       effect(() => {
         dummy = 0
         set.forEach(value => {
-          expect(isState(value)).toBe(true)
+          expect(isReactive(value)).toBe(true)
           dummy += value.foo
         })
       })
@@ -305,12 +305,12 @@ describe('observer/collections', () => {
     })
 
     it('should observe nested values in iterations (values)', () => {
-      const set = state(new Set([{ foo: 1 }]))
+      const set = reactive(new Set([{ foo: 1 }]))
       let dummy: any
       effect(() => {
         dummy = 0
         for (const value of set.values()) {
-          expect(isState(value)).toBe(true)
+          expect(isReactive(value)).toBe(true)
           dummy += value.foo
         }
       })
@@ -322,13 +322,13 @@ describe('observer/collections', () => {
     })
 
     it('should observe nested values in iterations (entries)', () => {
-      const set = state(new Set([{ foo: 1 }]))
+      const set = reactive(new Set([{ foo: 1 }]))
       let dummy: any
       effect(() => {
         dummy = 0
         for (const [key, value] of set.entries()) {
-          expect(isState(key)).toBe(true)
-          expect(isState(value)).toBe(true)
+          expect(isReactive(key)).toBe(true)
+          expect(isReactive(value)).toBe(true)
           dummy += value.foo
         }
       })
@@ -340,12 +340,12 @@ describe('observer/collections', () => {
     })
 
     it('should observe nested values in iterations (for...of)', () => {
-      const set = state(new Set([{ foo: 1 }]))
+      const set = reactive(new Set([{ foo: 1 }]))
       let dummy: any
       effect(() => {
         dummy = 0
         for (const value of set) {
-          expect(isState(value)).toBe(true)
+          expect(isReactive(value)).toBe(true)
           dummy += value.foo
         }
       })
index 8bad5f429d22615afce05e3884d3c824b692700c..0ad7b93a26da1b5569e53cbcda2a95d62195a7aa 100644 (file)
@@ -1,11 +1,11 @@
-import { state, effect, toRaw, isState } from '../../src'
+import { reactive, effect, toRaw, isReactive } from '../../src'
 
 describe('observer/collections', () => {
   describe('WeakMap', () => {
     test('instanceof', () => {
       const original = new WeakMap()
-      const observed = state(original)
-      expect(isState(observed)).toBe(true)
+      const observed = reactive(original)
+      expect(isReactive(observed)).toBe(true)
       expect(original instanceof WeakMap).toBe(true)
       expect(observed instanceof WeakMap).toBe(true)
     })
@@ -13,7 +13,7 @@ describe('observer/collections', () => {
     it('should observe mutations', () => {
       let dummy
       const key = {}
-      const map = state(new WeakMap())
+      const map = reactive(new WeakMap())
       effect(() => {
         dummy = map.get(key)
       })
@@ -29,7 +29,7 @@ describe('observer/collections', () => {
 
     it('should not observe custom property mutations', () => {
       let dummy
-      const map: any = state(new WeakMap())
+      const map: any = reactive(new WeakMap())
       effect(() => (dummy = map.customProp))
 
       expect(dummy).toBe(undefined)
@@ -40,7 +40,7 @@ describe('observer/collections', () => {
     it('should not observe non value changing mutations', () => {
       let dummy
       const key = {}
-      const map = state(new WeakMap())
+      const map = reactive(new WeakMap())
       const mapSpy = jest.fn(() => (dummy = map.get(key)))
       effect(mapSpy)
 
@@ -63,7 +63,7 @@ describe('observer/collections', () => {
     it('should not observe raw data', () => {
       let dummy
       const key = {}
-      const map = state(new WeakMap())
+      const map = reactive(new WeakMap())
       effect(() => (dummy = toRaw(map).get(key)))
 
       expect(dummy).toBe(undefined)
@@ -75,26 +75,26 @@ describe('observer/collections', () => {
 
     it('should not pollute original Map with Proxies', () => {
       const map = new WeakMap()
-      const observed = state(map)
+      const observed = reactive(map)
       const key = {}
-      const value = state({})
+      const value = reactive({})
       observed.set(key, value)
       expect(map.get(key)).not.toBe(value)
       expect(map.get(key)).toBe(toRaw(value))
     })
 
     it('should return observable versions of contained values', () => {
-      const observed = state(new WeakMap())
+      const observed = reactive(new WeakMap())
       const key = {}
       const value = {}
       observed.set(key, value)
       const wrapped = observed.get(key)
-      expect(isState(wrapped)).toBe(true)
+      expect(isReactive(wrapped)).toBe(true)
       expect(toRaw(wrapped)).toBe(value)
     })
 
     it('should observed nested data', () => {
-      const observed = state(new Map())
+      const observed = reactive(new Map())
       const key = {}
       observed.set(key, { a: 1 })
       let dummy
index 45e27896dfa19693d9d1a9599c841cf31a83b3ec..e26d40294cba47ef3fbb56ded220586dfe21bfc6 100644 (file)
@@ -1,11 +1,11 @@
-import { state, isState, effect, toRaw } from '../../src'
+import { reactive, isReactive, effect, toRaw } from '../../src'
 
 describe('observer/collections', () => {
   describe('WeakSet', () => {
     it('instanceof', () => {
       const original = new Set()
-      const observed = state(original)
-      expect(isState(observed)).toBe(true)
+      const observed = reactive(original)
+      expect(isReactive(observed)).toBe(true)
       expect(original instanceof Set).toBe(true)
       expect(observed instanceof Set).toBe(true)
     })
@@ -13,7 +13,7 @@ describe('observer/collections', () => {
     it('should observe mutations', () => {
       let dummy
       const value = {}
-      const set = state(new WeakSet())
+      const set = reactive(new WeakSet())
       effect(() => (dummy = set.has(value)))
 
       expect(dummy).toBe(false)
@@ -25,7 +25,7 @@ describe('observer/collections', () => {
 
     it('should not observe custom property mutations', () => {
       let dummy
-      const set: any = state(new WeakSet())
+      const set: any = reactive(new WeakSet())
       effect(() => (dummy = set.customProp))
 
       expect(dummy).toBe(undefined)
@@ -36,7 +36,7 @@ describe('observer/collections', () => {
     it('should not observe non value changing mutations', () => {
       let dummy
       const value = {}
-      const set = state(new WeakSet())
+      const set = reactive(new WeakSet())
       const setSpy = jest.fn(() => (dummy = set.has(value)))
       effect(setSpy)
 
@@ -59,7 +59,7 @@ describe('observer/collections', () => {
     it('should not observe raw data', () => {
       const value = {}
       let dummy
-      const set = state(new WeakSet())
+      const set = reactive(new WeakSet())
       effect(() => (dummy = toRaw(set).has(value)))
 
       expect(dummy).toBe(false)
@@ -70,7 +70,7 @@ describe('observer/collections', () => {
     it('should not be triggered by raw mutations', () => {
       const value = {}
       let dummy
-      const set = state(new WeakSet())
+      const set = reactive(new WeakSet())
       effect(() => (dummy = set.has(value)))
 
       expect(dummy).toBe(false)
@@ -80,8 +80,8 @@ describe('observer/collections', () => {
 
     it('should not pollute original Set with Proxies', () => {
       const set = new WeakSet()
-      const observed = state(set)
-      const value = state({})
+      const observed = reactive(set)
+      const value = reactive({})
       observed.add(value)
       expect(observed.has(value)).toBe(true)
       expect(set.has(value)).toBe(false)
index 37955661064d0d20ab10278ce0f13490ef690a64..abde4e0848a5af6f0031b87cc757fe2ba94ca97a 100644 (file)
@@ -1,8 +1,8 @@
-import { computed, state, effect, stop } from '../src'
+import { computed, reactive, effect, stop } from '../src'
 
 describe('observer/computed', () => {
   it('should return updated value', () => {
-    const value: any = state({})
+    const value: any = reactive({})
     const cValue = computed(() => value.foo)
     expect(cValue.value).toBe(undefined)
     value.foo = 1
@@ -10,7 +10,7 @@ describe('observer/computed', () => {
   })
 
   it('should compute lazily', () => {
-    const value: any = state({})
+    const value: any = reactive({})
     const getter = jest.fn(() => value.foo)
     const cValue = computed(getter)
 
@@ -38,7 +38,7 @@ describe('observer/computed', () => {
   })
 
   it('should trigger effect', () => {
-    const value: any = state({})
+    const value: any = reactive({})
     const cValue = computed(() => value.foo)
     let dummy
     effect(() => {
@@ -50,7 +50,7 @@ describe('observer/computed', () => {
   })
 
   it('should work when chained', () => {
-    const value: any = state({ foo: 0 })
+    const value: any = reactive({ foo: 0 })
     const c1 = computed(() => value.foo)
     const c2 = computed(() => c1.value + 1)
     expect(c2.value).toBe(1)
@@ -61,7 +61,7 @@ describe('observer/computed', () => {
   })
 
   it('should trigger effect when chained', () => {
-    const value: any = state({ foo: 0 })
+    const value: any = reactive({ foo: 0 })
     const getter1 = jest.fn(() => value.foo)
     const getter2 = jest.fn(() => {
       return c1.value + 1
@@ -84,7 +84,7 @@ describe('observer/computed', () => {
   })
 
   it('should trigger effect when chained (mixed invocations)', () => {
-    const value: any = state({ foo: 0 })
+    const value: any = reactive({ foo: 0 })
     const getter1 = jest.fn(() => value.foo)
     const getter2 = jest.fn(() => {
       return c1.value + 1
@@ -108,7 +108,7 @@ describe('observer/computed', () => {
   })
 
   it('should no longer update when stopped', () => {
-    const value: any = state({})
+    const value: any = reactive({})
     const cValue = computed(() => value.foo)
     let dummy
     effect(() => {
index 042900a7711c31a4f7061720ea24e0dab0bf6614..a99e9a40b4a084a75a7270ecdbc4e48fd1a01030 100644 (file)
@@ -1,5 +1,5 @@
 import {
-  state,
+  reactive,
   effect,
   stop,
   toRaw,
@@ -18,7 +18,7 @@ describe('observer/effect', () => {
 
   it('should observe basic properties', () => {
     let dummy
-    const counter = state({ num: 0 })
+    const counter = reactive({ num: 0 })
     effect(() => (dummy = counter.num))
 
     expect(dummy).toBe(0)
@@ -28,7 +28,7 @@ describe('observer/effect', () => {
 
   it('should observe multiple properties', () => {
     let dummy
-    const counter = state({ num1: 0, num2: 0 })
+    const counter = reactive({ num1: 0, num2: 0 })
     effect(() => (dummy = counter.num1 + counter.num1 + counter.num2))
 
     expect(dummy).toBe(0)
@@ -38,7 +38,7 @@ describe('observer/effect', () => {
 
   it('should handle multiple effects', () => {
     let dummy1, dummy2
-    const counter = state({ num: 0 })
+    const counter = reactive({ num: 0 })
     effect(() => (dummy1 = counter.num))
     effect(() => (dummy2 = counter.num))
 
@@ -51,7 +51,7 @@ describe('observer/effect', () => {
 
   it('should observe nested properties', () => {
     let dummy
-    const counter = state({ nested: { num: 0 } })
+    const counter = reactive({ nested: { num: 0 } })
     effect(() => (dummy = counter.nested.num))
 
     expect(dummy).toBe(0)
@@ -61,7 +61,7 @@ describe('observer/effect', () => {
 
   it('should observe delete operations', () => {
     let dummy
-    const obj = state({ prop: 'value' })
+    const obj = reactive({ prop: 'value' })
     effect(() => (dummy = obj.prop))
 
     expect(dummy).toBe('value')
@@ -71,7 +71,7 @@ describe('observer/effect', () => {
 
   it('should observe has operations', () => {
     let dummy
-    const obj: any = state({ prop: 'value' })
+    const obj: any = reactive({ prop: 'value' })
     effect(() => (dummy = 'prop' in obj))
 
     expect(dummy).toBe(true)
@@ -83,8 +83,8 @@ describe('observer/effect', () => {
 
   it('should observe properties on the prototype chain', () => {
     let dummy
-    const counter = state({ num: 0 })
-    const parentCounter = state({ num: 2 })
+    const counter = reactive({ num: 0 })
+    const parentCounter = reactive({ num: 2 })
     Object.setPrototypeOf(counter, parentCounter)
     effect(() => (dummy = counter.num))
 
@@ -99,8 +99,8 @@ describe('observer/effect', () => {
 
   it('should observe has operations on the prototype chain', () => {
     let dummy
-    const counter = state({ num: 0 })
-    const parentCounter = state({ num: 2 })
+    const counter = reactive({ num: 0 })
+    const parentCounter = reactive({ num: 2 })
     Object.setPrototypeOf(counter, parentCounter)
     effect(() => (dummy = 'num' in counter))
 
@@ -115,8 +115,8 @@ describe('observer/effect', () => {
 
   it('should observe inherited property accessors', () => {
     let dummy, parentDummy, hiddenValue: any
-    const obj: any = state({})
-    const parent = state({
+    const obj: any = reactive({})
+    const parent = reactive({
       set prop(value) {
         hiddenValue = value
       },
@@ -141,7 +141,7 @@ describe('observer/effect', () => {
 
   it('should observe function call chains', () => {
     let dummy
-    const counter = state({ num: 0 })
+    const counter = reactive({ num: 0 })
     effect(() => (dummy = getNum()))
 
     function getNum() {
@@ -155,7 +155,7 @@ describe('observer/effect', () => {
 
   it('should observe iteration', () => {
     let dummy
-    const list = state(['Hello'])
+    const list = reactive(['Hello'])
     effect(() => (dummy = list.join(' ')))
 
     expect(dummy).toBe('Hello')
@@ -167,7 +167,7 @@ describe('observer/effect', () => {
 
   it('should observe implicit array length changes', () => {
     let dummy
-    const list = state(['Hello'])
+    const list = reactive(['Hello'])
     effect(() => (dummy = list.join(' ')))
 
     expect(dummy).toBe('Hello')
@@ -179,7 +179,7 @@ describe('observer/effect', () => {
 
   it('should observe sparse array mutations', () => {
     let dummy
-    const list: any[] = state([])
+    const list: any[] = reactive([])
     list[1] = 'World!'
     effect(() => (dummy = list.join(' ')))
 
@@ -192,7 +192,7 @@ describe('observer/effect', () => {
 
   it('should observe enumeration', () => {
     let dummy = 0
-    const numbers: any = state({ num1: 3 })
+    const numbers: any = reactive({ num1: 3 })
     effect(() => {
       dummy = 0
       for (let key in numbers) {
@@ -210,7 +210,7 @@ describe('observer/effect', () => {
   it('should observe symbol keyed properties', () => {
     const key = Symbol('symbol keyed prop')
     let dummy, hasDummy
-    const obj = state({ [key]: 'value' })
+    const obj = reactive({ [key]: 'value' })
     effect(() => (dummy = obj[key]))
     effect(() => (hasDummy = key in obj))
 
@@ -226,7 +226,7 @@ describe('observer/effect', () => {
   it('should not observe well-known symbol keyed properties', () => {
     const key = Symbol.isConcatSpreadable
     let dummy
-    const array: any = state([])
+    const array: any = reactive([])
     effect(() => (dummy = array[key]))
 
     expect(array[key]).toBe(undefined)
@@ -241,7 +241,7 @@ describe('observer/effect', () => {
     const newFunc = () => {}
 
     let dummy
-    const obj = state({ func: oldFunc })
+    const obj = reactive({ func: oldFunc })
     effect(() => (dummy = obj.func))
 
     expect(dummy).toBe(oldFunc)
@@ -251,7 +251,7 @@ describe('observer/effect', () => {
 
   it('should not observe set operations without a value change', () => {
     let hasDummy, getDummy
-    const obj = state({ prop: 'value' })
+    const obj = reactive({ prop: 'value' })
 
     const getSpy = jest.fn(() => (getDummy = obj.prop))
     const hasSpy = jest.fn(() => (hasDummy = 'prop' in obj))
@@ -269,7 +269,7 @@ describe('observer/effect', () => {
 
   it('should not observe raw mutations', () => {
     let dummy
-    const obj: any = state()
+    const obj: any = reactive()
     effect(() => (dummy = toRaw(obj).prop))
 
     expect(dummy).toBe(undefined)
@@ -279,7 +279,7 @@ describe('observer/effect', () => {
 
   it('should not be triggered by raw mutations', () => {
     let dummy
-    const obj: any = state()
+    const obj: any = reactive()
     effect(() => (dummy = obj.prop))
 
     expect(dummy).toBe(undefined)
@@ -289,8 +289,8 @@ describe('observer/effect', () => {
 
   it('should not be triggered by inherited raw setters', () => {
     let dummy, parentDummy, hiddenValue: any
-    const obj: any = state({})
-    const parent = state({
+    const obj: any = reactive({})
+    const parent = reactive({
       set prop(value) {
         hiddenValue = value
       },
@@ -310,7 +310,7 @@ describe('observer/effect', () => {
   })
 
   it('should avoid implicit infinite recursive loops with itself', () => {
-    const counter = state({ num: 0 })
+    const counter = reactive({ num: 0 })
 
     const counterSpy = jest.fn(() => counter.num++)
     effect(counterSpy)
@@ -322,7 +322,7 @@ describe('observer/effect', () => {
   })
 
   it('should allow explicitly recursive raw function loops', () => {
-    const counter = state({ num: 0 })
+    const counter = reactive({ num: 0 })
     const numSpy = jest.fn(() => {
       counter.num++
       if (counter.num < 10) {
@@ -335,7 +335,7 @@ describe('observer/effect', () => {
   })
 
   it('should avoid infinite loops with other effects', () => {
-    const nums = state({ num1: 0, num2: 1 })
+    const nums = reactive({ num1: 0, num2: 1 })
 
     const spy1 = jest.fn(() => (nums.num1 = nums.num2))
     const spy2 = jest.fn(() => (nums.num2 = nums.num1))
@@ -371,7 +371,7 @@ describe('observer/effect', () => {
 
   it('should discover new branches while running automatically', () => {
     let dummy
-    const obj = state({ prop: 'value', run: false })
+    const obj = reactive({ prop: 'value', run: false })
 
     const conditionalSpy = jest.fn(() => {
       dummy = obj.run ? obj.prop : 'other'
@@ -394,7 +394,7 @@ describe('observer/effect', () => {
   it('should discover new branches when running manually', () => {
     let dummy
     let run = false
-    const obj = state({ prop: 'value' })
+    const obj = reactive({ prop: 'value' })
     const runner = effect(() => {
       dummy = run ? obj.prop : 'other'
     })
@@ -411,7 +411,7 @@ describe('observer/effect', () => {
 
   it('should not be triggered by mutating a property, which is used in an inactive branch', () => {
     let dummy
-    const obj = state({ prop: 'value', run: true })
+    const obj = reactive({ prop: 'value', run: true })
 
     const conditionalSpy = jest.fn(() => {
       dummy = obj.run ? obj.prop : 'other'
@@ -437,7 +437,7 @@ describe('observer/effect', () => {
 
   it('should not run multiple times for a single mutation', () => {
     let dummy
-    const obj: any = state()
+    const obj: any = reactive()
     const fnSpy = jest.fn(() => {
       for (const key in obj) {
         dummy = obj[key]
@@ -453,7 +453,7 @@ describe('observer/effect', () => {
   })
 
   it('should allow nested effects', () => {
-    const nums = state({ num1: 0, num2: 1, num3: 2 })
+    const nums = reactive({ num1: 0, num2: 1, num3: 2 })
     const dummy: any = {}
 
     const childSpy = jest.fn(() => (dummy.num1 = nums.num1))
@@ -495,7 +495,7 @@ describe('observer/effect', () => {
         this.count++
       }
     }
-    const model = state(new Model())
+    const model = reactive(new Model())
     let dummy
     effect(() => {
       dummy = model.count
@@ -510,7 +510,7 @@ describe('observer/effect', () => {
     const scheduler = jest.fn(_runner => {
       runner = _runner
     })
-    const obj = state({ foo: 1 })
+    const obj = reactive({ foo: 1 })
     effect(
       () => {
         dummy = obj.foo
@@ -536,7 +536,7 @@ describe('observer/effect', () => {
     const onTrack = jest.fn((e: DebuggerEvent) => {
       events.push(e)
     })
-    const obj = state({ foo: 1, bar: 2 })
+    const obj = reactive({ foo: 1, bar: 2 })
     const runner = effect(
       () => {
         dummy = obj.foo
@@ -575,7 +575,7 @@ describe('observer/effect', () => {
     const onTrigger = jest.fn((e: DebuggerEvent) => {
       events.push(e)
     })
-    const obj = state({ foo: 1 })
+    const obj = reactive({ foo: 1 })
     const runner = effect(
       () => {
         dummy = obj.foo
@@ -609,7 +609,7 @@ describe('observer/effect', () => {
 
   it('stop', () => {
     let dummy
-    const obj = state({ prop: 1 })
+    const obj = reactive({ prop: 1 })
     const runner = effect(() => {
       dummy = obj.prop
     })
@@ -625,7 +625,7 @@ describe('observer/effect', () => {
   })
 
   it('markNonReactive', () => {
-    const obj = state({
+    const obj = reactive({
       foo: markNonReactive({
         prop: 0
       })
index 51facb30f4ac836cd3183a5c1c575a9388e60b49..e30d027a18fa2a4029f4209f0734c26615a0d72b 100644 (file)
@@ -1,9 +1,9 @@
 import {
-  state,
-  immutableState,
+  reactive,
+  immutable,
   toRaw,
-  isState,
-  isImmutableState,
+  isReactive,
+  isImmutable,
   markNonReactive,
   markImmutable,
   lock,
@@ -26,16 +26,16 @@ describe('observer/immutable', () => {
   describe('Object', () => {
     it('should make nested values immutable', () => {
       const original = { foo: 1, bar: { baz: 2 } }
-      const observed = immutableState(original)
+      const observed = immutable(original)
       expect(observed).not.toBe(original)
-      expect(isState(observed)).toBe(true)
-      expect(isImmutableState(observed)).toBe(true)
-      expect(isState(original)).toBe(false)
-      expect(isImmutableState(original)).toBe(false)
-      expect(isState(observed.bar)).toBe(true)
-      expect(isImmutableState(observed.bar)).toBe(true)
-      expect(isState(original.bar)).toBe(false)
-      expect(isImmutableState(original.bar)).toBe(false)
+      expect(isReactive(observed)).toBe(true)
+      expect(isImmutable(observed)).toBe(true)
+      expect(isReactive(original)).toBe(false)
+      expect(isImmutable(original)).toBe(false)
+      expect(isReactive(observed.bar)).toBe(true)
+      expect(isImmutable(observed.bar)).toBe(true)
+      expect(isReactive(original.bar)).toBe(false)
+      expect(isImmutable(original.bar)).toBe(false)
       // get
       expect(observed.foo).toBe(1)
       // has
@@ -45,7 +45,7 @@ describe('observer/immutable', () => {
     })
 
     it('should not allow mutation', () => {
-      const observed = immutableState({ foo: 1, bar: { baz: 2 } })
+      const observed = immutable({ foo: 1, bar: { baz: 2 } })
       observed.foo = 2
       expect(observed.foo).toBe(1)
       expect(warn).toHaveBeenCalledTimes(1)
@@ -61,7 +61,7 @@ describe('observer/immutable', () => {
     })
 
     it('should allow mutation when unlocked', () => {
-      const observed: any = immutableState({ foo: 1, bar: { baz: 2 } })
+      const observed: any = immutable({ foo: 1, bar: { baz: 2 } })
       unlock()
       observed.prop = 2
       observed.bar.qux = 3
@@ -76,7 +76,7 @@ describe('observer/immutable', () => {
     })
 
     it('should not trigger effects when locked', () => {
-      const observed = immutableState({ a: 1 })
+      const observed = immutable({ a: 1 })
       let dummy
       effect(() => {
         dummy = observed.a
@@ -88,7 +88,7 @@ describe('observer/immutable', () => {
     })
 
     it('should trigger effects when unlocked', () => {
-      const observed = immutableState({ a: 1 })
+      const observed = immutable({ a: 1 })
       let dummy
       effect(() => {
         dummy = observed.a
@@ -105,16 +105,16 @@ describe('observer/immutable', () => {
   describe('Array', () => {
     it('should make nested values immutable', () => {
       const original: any[] = [{ foo: 1 }]
-      const observed = immutableState(original)
+      const observed = immutable(original)
       expect(observed).not.toBe(original)
-      expect(isState(observed)).toBe(true)
-      expect(isImmutableState(observed)).toBe(true)
-      expect(isState(original)).toBe(false)
-      expect(isImmutableState(original)).toBe(false)
-      expect(isState(observed[0])).toBe(true)
-      expect(isImmutableState(observed[0])).toBe(true)
-      expect(isState(original[0])).toBe(false)
-      expect(isImmutableState(original[0])).toBe(false)
+      expect(isReactive(observed)).toBe(true)
+      expect(isImmutable(observed)).toBe(true)
+      expect(isReactive(original)).toBe(false)
+      expect(isImmutable(original)).toBe(false)
+      expect(isReactive(observed[0])).toBe(true)
+      expect(isImmutable(observed[0])).toBe(true)
+      expect(isReactive(original[0])).toBe(false)
+      expect(isImmutable(original[0])).toBe(false)
       // get
       expect(observed[0].foo).toBe(1)
       // has
@@ -124,7 +124,7 @@ describe('observer/immutable', () => {
     })
 
     it('should not allow mutation', () => {
-      const observed: any = immutableState([{ foo: 1 }])
+      const observed: any = immutable([{ foo: 1 }])
       observed[0] = 1
       expect(observed[0]).not.toBe(1)
       expect(warn).toHaveBeenCalledTimes(1)
@@ -146,7 +146,7 @@ describe('observer/immutable', () => {
     })
 
     it('should allow mutation when unlocked', () => {
-      const observed: any[] = immutableState([{ foo: 1, bar: { baz: 2 } }])
+      const observed: any[] = immutable([{ foo: 1, bar: { baz: 2 } }])
       unlock()
       observed[1] = 2
       observed.push(3)
@@ -162,7 +162,7 @@ describe('observer/immutable', () => {
     })
 
     it('should not trigger effects when locked', () => {
-      const observed = immutableState([{ a: 1 }])
+      const observed = immutable([{ a: 1 }])
       let dummy
       effect(() => {
         dummy = observed[0].a
@@ -177,7 +177,7 @@ describe('observer/immutable', () => {
     })
 
     it('should trigger effects when unlocked', () => {
-      const observed = immutableState([{ a: 1 }])
+      const observed = immutable([{ a: 1 }])
       let dummy
       effect(() => {
         dummy = observed[0].a
@@ -208,20 +208,20 @@ describe('observer/immutable', () => {
         const key1 = {}
         const key2 = {}
         const original = new Collection([[key1, {}], [key2, {}]])
-        const observed = immutableState(original)
+        const observed = immutable(original)
         expect(observed).not.toBe(original)
-        expect(isState(observed)).toBe(true)
-        expect(isImmutableState(observed)).toBe(true)
-        expect(isState(original)).toBe(false)
-        expect(isImmutableState(original)).toBe(false)
-        expect(isState(observed.get(key1))).toBe(true)
-        expect(isImmutableState(observed.get(key1))).toBe(true)
-        expect(isState(original.get(key1))).toBe(false)
-        expect(isImmutableState(original.get(key1))).toBe(false)
+        expect(isReactive(observed)).toBe(true)
+        expect(isImmutable(observed)).toBe(true)
+        expect(isReactive(original)).toBe(false)
+        expect(isImmutable(original)).toBe(false)
+        expect(isReactive(observed.get(key1))).toBe(true)
+        expect(isImmutable(observed.get(key1))).toBe(true)
+        expect(isReactive(original.get(key1))).toBe(false)
+        expect(isImmutable(original.get(key1))).toBe(false)
       })
 
       test('should not allow mutation & not trigger effect', () => {
-        const map = immutableState(new Collection())
+        const map = immutable(new Collection())
         const key = {}
         let dummy
         effect(() => {
@@ -235,7 +235,7 @@ describe('observer/immutable', () => {
       })
 
       test('should allow mutation & trigger effect when unlocked', () => {
-        const map = immutableState(new Collection())
+        const map = immutable(new Collection())
         const isWeak = Collection === WeakMap
         const key = {}
         let dummy
@@ -256,16 +256,16 @@ describe('observer/immutable', () => {
           const key1 = {}
           const key2 = {}
           const original = new Collection([[key1, {}], [key2, {}]])
-          const observed = immutableState(original)
+          const observed = immutable(original)
           for (const [key, value] of observed) {
-            expect(isImmutableState(key)).toBe(true)
-            expect(isImmutableState(value)).toBe(true)
+            expect(isImmutable(key)).toBe(true)
+            expect(isImmutable(value)).toBe(true)
           }
           observed.forEach((value: any) => {
-            expect(isImmutableState(value)).toBe(true)
+            expect(isImmutable(value)).toBe(true)
           })
           for (const value of observed.values()) {
-            expect(isImmutableState(value)).toBe(true)
+            expect(isImmutable(value)).toBe(true)
           }
         })
       }
@@ -279,18 +279,18 @@ describe('observer/immutable', () => {
         const key1 = {}
         const key2 = {}
         const original = new Collection([key1, key2])
-        const observed = immutableState(original)
+        const observed = immutable(original)
         expect(observed).not.toBe(original)
-        expect(isState(observed)).toBe(true)
-        expect(isImmutableState(observed)).toBe(true)
-        expect(isState(original)).toBe(false)
-        expect(isImmutableState(original)).toBe(false)
-        expect(observed.has(state(key1))).toBe(true)
-        expect(original.has(state(key1))).toBe(false)
+        expect(isReactive(observed)).toBe(true)
+        expect(isImmutable(observed)).toBe(true)
+        expect(isReactive(original)).toBe(false)
+        expect(isImmutable(original)).toBe(false)
+        expect(observed.has(reactive(key1))).toBe(true)
+        expect(original.has(reactive(key1))).toBe(false)
       })
 
       test('should not allow mutation & not trigger effect', () => {
-        const set = immutableState(new Collection())
+        const set = immutable(new Collection())
         const key = {}
         let dummy
         effect(() => {
@@ -304,7 +304,7 @@ describe('observer/immutable', () => {
       })
 
       test('should allow mutation & trigger effect when unlocked', () => {
-        const set = immutableState(new Collection())
+        const set = immutable(new Collection())
         const key = {}
         let dummy
         effect(() => {
@@ -322,19 +322,19 @@ describe('observer/immutable', () => {
       if (Collection === Set) {
         test('should retrive immutable values on iteration', () => {
           const original = new Collection([{}, {}])
-          const observed = immutableState(original)
+          const observed = immutable(original)
           for (const value of observed) {
-            expect(isImmutableState(value)).toBe(true)
+            expect(isImmutable(value)).toBe(true)
           }
           observed.forEach((value: any) => {
-            expect(isImmutableState(value)).toBe(true)
+            expect(isImmutable(value)).toBe(true)
           })
           for (const value of observed.values()) {
-            expect(isImmutableState(value)).toBe(true)
+            expect(isImmutable(value)).toBe(true)
           }
           for (const [v1, v2] of observed.entries()) {
-            expect(isImmutableState(v1)).toBe(true)
-            expect(isImmutableState(v2)).toBe(true)
+            expect(isImmutable(v1)).toBe(true)
+            expect(isImmutable(v2)).toBe(true)
           }
         })
       }
@@ -342,52 +342,52 @@ describe('observer/immutable', () => {
   })
 
   test('calling observable on an immutable should return immutable', () => {
-    const a = immutableState()
-    const b = state(a)
-    expect(isImmutableState(b)).toBe(true)
+    const a = immutable()
+    const b = reactive(a)
+    expect(isImmutable(b)).toBe(true)
     // should point to same original
     expect(toRaw(a)).toBe(toRaw(b))
   })
 
   test('calling immutable on an observable should return immutable', () => {
-    const a = state()
-    const b = immutableState(a)
-    expect(isImmutableState(b)).toBe(true)
+    const a = reactive()
+    const b = immutable(a)
+    expect(isImmutable(b)).toBe(true)
     // should point to same original
     expect(toRaw(a)).toBe(toRaw(b))
   })
 
   test('observing already observed value should return same Proxy', () => {
     const original = { foo: 1 }
-    const observed = immutableState(original)
-    const observed2 = immutableState(observed)
+    const observed = immutable(original)
+    const observed2 = immutable(observed)
     expect(observed2).toBe(observed)
   })
 
   test('observing the same value multiple times should return same Proxy', () => {
     const original = { foo: 1 }
-    const observed = immutableState(original)
-    const observed2 = immutableState(original)
+    const observed = immutable(original)
+    const observed2 = immutable(original)
     expect(observed2).toBe(observed)
   })
 
   test('markNonReactive', () => {
-    const obj = immutableState({
+    const obj = immutable({
       foo: { a: 1 },
       bar: markNonReactive({ b: 2 })
     })
-    expect(isState(obj.foo)).toBe(true)
-    expect(isState(obj.bar)).toBe(false)
+    expect(isReactive(obj.foo)).toBe(true)
+    expect(isReactive(obj.bar)).toBe(false)
   })
 
   test('markImmutable', () => {
-    const obj = state({
+    const obj = reactive({
       foo: { a: 1 },
       bar: markImmutable({ b: 2 })
     })
-    expect(isState(obj.foo)).toBe(true)
-    expect(isState(obj.bar)).toBe(true)
-    expect(isImmutableState(obj.foo)).toBe(false)
-    expect(isImmutableState(obj.bar)).toBe(true)
+    expect(isReactive(obj.foo)).toBe(true)
+    expect(isReactive(obj.bar)).toBe(true)
+    expect(isImmutable(obj.foo)).toBe(false)
+    expect(isImmutable(obj.bar)).toBe(true)
   })
 })
similarity index 72%
rename from packages/reactivity/__tests__/state.spec.ts
rename to packages/reactivity/__tests__/reactive.spec.ts
index d5f3a00d4f46dd71d25db45644513bc81d7166c7..0fe68e73592597ee2fc8b642c1d520f34e1ef79f 100644 (file)
@@ -1,12 +1,12 @@
-import { state, isState, toRaw, markNonReactive } from '../src/index'
+import { reactive, isReactive, toRaw, markNonReactive } from '../src/reactive'
 
 describe('observer/observable', () => {
   test('Object', () => {
     const original = { foo: 1 }
-    const observed = state(original)
+    const observed = reactive(original)
     expect(observed).not.toBe(original)
-    expect(isState(observed)).toBe(true)
-    expect(isState(original)).toBe(false)
+    expect(isReactive(observed)).toBe(true)
+    expect(isReactive(original)).toBe(false)
     // get
     expect(observed.foo).toBe(1)
     // has
@@ -17,11 +17,11 @@ describe('observer/observable', () => {
 
   test('Array', () => {
     const original: any[] = [{ foo: 1 }]
-    const observed = state(original)
+    const observed = reactive(original)
     expect(observed).not.toBe(original)
-    expect(isState(observed)).toBe(true)
-    expect(isState(original)).toBe(false)
-    expect(isState(observed[0])).toBe(true)
+    expect(isReactive(observed)).toBe(true)
+    expect(isReactive(original)).toBe(false)
+    expect(isReactive(observed[0])).toBe(true)
     // get
     expect(observed[0].foo).toBe(1)
     // has
@@ -32,9 +32,9 @@ describe('observer/observable', () => {
 
   test('cloned observable Array should point to observed values', () => {
     const original = [{ foo: 1 }]
-    const observed = state(original)
+    const observed = reactive(original)
     const clone = observed.slice()
-    expect(isState(clone[0])).toBe(true)
+    expect(isReactive(clone[0])).toBe(true)
     expect(clone[0]).not.toBe(original[0])
     expect(clone[0]).toBe(observed[0])
   })
@@ -46,15 +46,15 @@ describe('observer/observable', () => {
       },
       array: [{ bar: 2 }]
     }
-    const observed = state(original)
-    expect(isState(observed.nested)).toBe(true)
-    expect(isState(observed.array)).toBe(true)
-    expect(isState(observed.array[0])).toBe(true)
+    const observed = reactive(original)
+    expect(isReactive(observed.nested)).toBe(true)
+    expect(isReactive(observed.array)).toBe(true)
+    expect(isReactive(observed.array[0])).toBe(true)
   })
 
   test('observed value should proxy mutations to original (Object)', () => {
     const original: any = { foo: 1 }
-    const observed = state(original)
+    const observed = reactive(original)
     // set
     observed.bar = 1
     expect(observed.bar).toBe(1)
@@ -67,10 +67,10 @@ describe('observer/observable', () => {
 
   test('observed value should proxy mutations to original (Array)', () => {
     const original: any[] = [{ foo: 1 }, { bar: 2 }]
-    const observed = state(original)
+    const observed = reactive(original)
     // set
     const value = { baz: 3 }
-    const observableValue = state(value)
+    const observableValue = reactive(value)
     observed[0] = value
     expect(observed[0]).toBe(observableValue)
     expect(original[0]).toBe(value)
@@ -85,32 +85,32 @@ describe('observer/observable', () => {
   })
 
   test('setting a property with an unobserved value should wrap with observable', () => {
-    const observed: any = state({})
+    const observed: any = reactive({})
     const raw = {}
     observed.foo = raw
     expect(observed.foo).not.toBe(raw)
-    expect(isState(observed.foo)).toBe(true)
+    expect(isReactive(observed.foo)).toBe(true)
   })
 
   test('observing already observed value should return same Proxy', () => {
     const original = { foo: 1 }
-    const observed = state(original)
-    const observed2 = state(observed)
+    const observed = reactive(original)
+    const observed2 = reactive(observed)
     expect(observed2).toBe(observed)
   })
 
   test('observing the same value multiple times should return same Proxy', () => {
     const original = { foo: 1 }
-    const observed = state(original)
-    const observed2 = state(original)
+    const observed = reactive(original)
+    const observed2 = reactive(original)
     expect(observed2).toBe(observed)
   })
 
   test('should not pollute original object with Proxies', () => {
     const original: any = { foo: 1 }
     const original2 = { bar: 2 }
-    const observed = state(original)
-    const observed2 = state(original2)
+    const observed = reactive(original)
+    const observed2 = reactive(original2)
     observed.bar = observed2
     expect(observed.bar).toBe(observed2)
     expect(original.bar).toBe(original2)
@@ -118,7 +118,7 @@ describe('observer/observable', () => {
 
   test('unwrap', () => {
     const original = { foo: 1 }
-    const observed = state(original)
+    const observed = reactive(original)
     expect(toRaw(observed)).toBe(original)
     expect(toRaw(original)).toBe(original)
   })
@@ -132,7 +132,7 @@ describe('observer/observable', () => {
 
     const getMsg = (value: any) => `value is not observable: ${String(value)}`
     const assertValue = (value: any) => {
-      state(value)
+      reactive(value)
       expect(lastMsg).toMatch(getMsg(value))
     }
 
@@ -146,7 +146,7 @@ describe('observer/observable', () => {
     assertValue(null)
     // undefined should work because it returns empty object observable
     lastMsg = ''
-    state(undefined)
+    reactive(undefined)
     expect(lastMsg).toBe('')
     // symbol
     const s = Symbol()
@@ -156,19 +156,19 @@ describe('observer/observable', () => {
 
     // built-ins should work and return same value
     const p = Promise.resolve()
-    expect(state(p)).toBe(p)
+    expect(reactive(p)).toBe(p)
     const r = new RegExp('')
-    expect(state(r)).toBe(r)
+    expect(reactive(r)).toBe(r)
     const d = new Date()
-    expect(state(d)).toBe(d)
+    expect(reactive(d)).toBe(d)
   })
 
   test('markNonReactive', () => {
-    const obj = state({
+    const obj = reactive({
       foo: { a: 1 },
       bar: markNonReactive({ b: 2 })
     })
-    expect(isState(obj.foo)).toBe(true)
-    expect(isState(obj.bar)).toBe(false)
+    expect(isReactive(obj.foo)).toBe(true)
+    expect(isReactive(obj.bar)).toBe(false)
   })
 })
similarity index 86%
rename from packages/reactivity/__tests__/value.spec.ts
rename to packages/reactivity/__tests__/ref.spec.ts
index 3ad687fd16ee5e27875e06785ef133454570dbc2..94600eb1c403b4bf879484fb33a2940c12ff6a91 100644 (file)
@@ -1,15 +1,15 @@
-import { value, effect, state } from '../src/index'
+import { ref, effect, reactive } from '../src/index'
 
 describe('observer/value', () => {
   it('should hold a value', () => {
-    const a = value(1)
+    const a = ref(1)
     expect(a.value).toBe(1)
     a.value = 2
     expect(a.value).toBe(2)
   })
 
   it('should be reactive', () => {
-    const a = value(1)
+    const a = ref(1)
     let dummy
     effect(() => {
       dummy = a.value
@@ -20,7 +20,7 @@ describe('observer/value', () => {
   })
 
   it('should make nested properties reactive', () => {
-    const a = value({
+    const a = ref({
       count: 1
     })
     let dummy
@@ -33,8 +33,8 @@ describe('observer/value', () => {
   })
 
   it('should work like a normal property when nested in an observable', () => {
-    const a = value(1)
-    const obj = state({
+    const a = ref(1)
+    const obj = reactive({
       a,
       b: {
         c: a,
index eaa2b1ed0dfd2be1732d07ead41611b3953ec457..5724352cc8c3c004adda5d4d96b7c65c7979ec11 100644 (file)
@@ -1,9 +1,9 @@
-import { state, immutableState, toRaw } from './index'
+import { reactive, immutable, toRaw } from './reactive'
 import { OperationTypes } from './operations'
 import { track, trigger } from './effect'
 import { LOCKED } from './lock'
 import { isObject } from '@vue/shared'
-import { isValue } from './value'
+import { isRef } from './ref'
 
 const hasOwnProperty = Object.prototype.hasOwnProperty
 
@@ -19,16 +19,16 @@ function createGetter(isImmutable: boolean) {
     if (typeof key === 'symbol' && builtInSymbols.has(key)) {
       return res
     }
-    if (isValue(res)) {
+    if (isRef(res)) {
       return res.value
     }
     track(target, OperationTypes.GET, key)
     return isObject(res)
       ? isImmutable
-        ? // need to lazy access immutable and observable here to avoid
+        ? // need to lazy access immutable and reactive here to avoid
           // circular dependency
-          immutableState(res)
-        : state(res)
+          immutable(res)
+        : reactive(res)
       : res
   }
 }
@@ -42,7 +42,7 @@ function set(
   value = toRaw(value)
   const hadKey = hasOwnProperty.call(target, key)
   const oldValue = target[key]
-  if (isValue(oldValue) && !isValue(value)) {
+  if (isRef(oldValue) && !isRef(value)) {
     oldValue.value = value
     return true
   }
index d781dae061f007ef2a12a6144b5057163fb08d11..02c1f8e96ac66f2d7a510c8e75f453792ddde0c0 100644 (file)
@@ -1,12 +1,11 @@
-import { toRaw, state, immutableState } from './index'
+import { toRaw, reactive, immutable } from './reactive'
 import { track, trigger } from './effect'
 import { OperationTypes } from './operations'
 import { LOCKED } from './lock'
 import { isObject } from '@vue/shared'
 
-const toObservable = (value: any) => (isObject(value) ? state(value) : value)
-const toImmutable = (value: any) =>
-  isObject(value) ? immutableState(value) : value
+const toReactive = (value: any) => (isObject(value) ? reactive(value) : value)
+const toImmutable = (value: any) => (isObject(value) ? immutable(value) : value)
 
 function get(target: any, key: any, wrap: (t: any) => any): any {
   target = toRaw(target)
@@ -117,7 +116,7 @@ function createForEach(isImmutable: boolean) {
     const observed = this
     const target = toRaw(observed)
     const proto: any = Reflect.getPrototypeOf(target)
-    const wrap = isImmutable ? toImmutable : toObservable
+    const wrap = isImmutable ? toImmutable : toReactive
     track(target, OperationTypes.ITERATE)
     // important: create sure the callback is
     // 1. invoked with the observable map as `this` and 3rd arg
@@ -137,7 +136,7 @@ function createIterableMethod(method: string | symbol, isImmutable: boolean) {
       method === 'entries' ||
       (method === Symbol.iterator && target instanceof Map)
     const innerIterator = proto[method].apply(target, args)
-    const wrap = isImmutable ? toImmutable : toObservable
+    const wrap = isImmutable ? toImmutable : toReactive
     track(target, OperationTypes.ITERATE)
     // return a wrapped iterator which returns observed versions of the
     // values emitted from the real iterator
@@ -182,7 +181,7 @@ function createImmutableMethod(
 
 const mutableInstrumentations: any = {
   get(key: any) {
-    return get(this, key, toObservable)
+    return get(this, key, toReactive)
   },
   get size() {
     return size(this)
index 81a6f7efb49e6b4f868f9c69913443188a0a0b7e..b23bdfcde822a18e9b89897636b064c1050bef21 100644 (file)
@@ -1,8 +1,7 @@
-import { effect } from './index'
-import { ReactiveEffect, activeReactiveEffectStack } from './effect'
-import { knownValues } from './value'
+import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
+import { knownValues } from './ref'
 
-export interface ComputedValue<T> {
+export interface ComputedRef<T> {
   readonly value: T
   readonly effect: ReactiveEffect
 }
@@ -10,7 +9,7 @@ export interface ComputedValue<T> {
 export function computed<T>(
   getter: () => T,
   setter?: (v: T) => void
-): ComputedValue<T> {
+): ComputedRef<T> {
   let dirty: boolean = true
   let value: any = undefined
   const runner = effect(getter, {
index 51adc4a5ce30b9211da354192e94ba529b2cf225..c9847c1eac79fbdae2f4182825d1b1a45582c79f 100644 (file)
@@ -1,5 +1,5 @@
 import { OperationTypes } from './operations'
-import { Dep, targetMap } from './state'
+import { Dep, targetMap } from './reactive'
 import { EMPTY_OBJ } from '@vue/shared'
 
 export interface ReactiveEffect {
index 3bc0572012135b46ba5b7c847931c1d3b22bcf1a..f790a70a9817bc588805eb474b557a386a313474 100644 (file)
@@ -1,14 +1,14 @@
-export { value, isValue, Value, UnwrapValue } from './value'
+export { ref, isRef, Ref, UnwrapRef } from './ref'
 export {
-  state,
-  isState,
-  immutableState,
-  isImmutableState,
+  reactive,
+  isReactive,
+  immutable,
+  isImmutable,
   toRaw,
   markImmutable,
   markNonReactive
-} from './state'
-export { computed, ComputedValue } from './computed'
+} from './reactive'
+export { computed, ComputedRef } from './computed'
 export {
   effect,
   stop,
similarity index 91%
rename from packages/reactivity/src/state.ts
rename to packages/reactivity/src/reactive.ts
index 0f990ebbeb56bd2d52b4f9b18b7920310db49026..a965ceac57aad6dc1e0c6c055567f5039b35d155 100644 (file)
@@ -6,7 +6,7 @@ import {
   immutableCollectionHandlers
 } from './collectionHandlers'
 
-import { UnwrapValue } from './value'
+import { UnwrapRef } from './ref'
 import { ReactiveEffect } from './effect'
 
 // The main WeakMap that stores {target -> key -> dep} connections.
@@ -40,16 +40,16 @@ const canObserve = (value: any): boolean => {
   )
 }
 
-type ObservableFactory = <T>(target?: T) => UnwrapValue<T>
+type ObservableFactory = <T>(target?: T) => UnwrapRef<T>
 
-export const state = ((target: any = {}): any => {
+export const reactive = ((target: any = {}): any => {
   // if trying to observe an immutable proxy, return the immutable version.
   if (immutableToRaw.has(target)) {
     return target
   }
   // target is explicitly marked as immutable by user
   if (immutableValues.has(target)) {
-    return immutableState(target)
+    return immutable(target)
   }
   return createObservable(
     target,
@@ -60,7 +60,7 @@ export const state = ((target: any = {}): any => {
   )
 }) as ObservableFactory
 
-export const immutableState = ((target: any = {}): any => {
+export const immutable = ((target: any = {}): any => {
   // value is a mutable observable, retrive its original and return
   // a readonly version.
   if (observedToRaw.has(target)) {
@@ -113,11 +113,11 @@ function createObservable(
   return observed
 }
 
-export function isState(value: any): boolean {
+export function isReactive(value: any): boolean {
   return observedToRaw.has(value) || immutableToRaw.has(value)
 }
 
-export function isImmutableState(value: any): boolean {
+export function isImmutable(value: any): boolean {
   return immutableToRaw.has(value)
 }
 
diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts
new file mode 100644 (file)
index 0000000..a3a67b8
--- /dev/null
@@ -0,0 +1,118 @@
+import { track, trigger } from './effect'
+import { OperationTypes } from './operations'
+import { isObject } from '@vue/shared'
+import { reactive } from './reactive'
+
+export const knownValues = new WeakSet()
+
+export interface Ref<T> {
+  value: T
+}
+
+const convert = (val: any): any => (isObject(val) ? reactive(val) : val)
+
+export function ref<T>(raw: T): Ref<T> {
+  raw = convert(raw)
+  const v = {
+    get value() {
+      track(v, OperationTypes.GET, '')
+      return raw
+    },
+    set value(newVal) {
+      raw = convert(newVal)
+      trigger(v, OperationTypes.SET, '')
+    }
+  }
+  knownValues.add(v)
+  return v
+}
+
+export function isRef(v: any): v is Ref<any> {
+  return knownValues.has(v)
+}
+
+type BailTypes =
+  | Function
+  | Map<any, any>
+  | Set<any>
+  | WeakMap<any, any>
+  | WeakSet<any>
+
+// Recursively unwraps nested value bindings.
+// Unfortunately TS cannot do recursive types, but this should be enough for
+// practical use cases...
+export type UnwrapRef<T> = T extends Ref<infer V>
+  ? UnwrapRef2<V>
+  : T extends Array<infer V>
+    ? Array<UnwrapRef2<V>>
+    : T extends BailTypes
+      ? T // bail out on types that shouldn't be unwrapped
+      : T extends object ? { [K in keyof T]: UnwrapRef2<T[K]> } : T
+
+type UnwrapRef2<T> = T extends Ref<infer V>
+  ? UnwrapRef3<V>
+  : T extends Array<infer V>
+    ? Array<UnwrapRef3<V>>
+    : T extends BailTypes
+      ? T
+      : T extends object ? { [K in keyof T]: UnwrapRef3<T[K]> } : T
+
+type UnwrapRef3<T> = T extends Ref<infer V>
+  ? UnwrapRef4<V>
+  : T extends Array<infer V>
+    ? Array<UnwrapRef4<V>>
+    : T extends BailTypes
+      ? T
+      : T extends object ? { [K in keyof T]: UnwrapRef4<T[K]> } : T
+
+type UnwrapRef4<T> = T extends Ref<infer V>
+  ? UnwrapRef5<V>
+  : T extends Array<infer V>
+    ? Array<UnwrapRef5<V>>
+    : T extends BailTypes
+      ? T
+      : T extends object ? { [K in keyof T]: UnwrapRef5<T[K]> } : T
+
+type UnwrapRef5<T> = T extends Ref<infer V>
+  ? UnwrapRef6<V>
+  : T extends Array<infer V>
+    ? Array<UnwrapRef6<V>>
+    : T extends BailTypes
+      ? T
+      : T extends object ? { [K in keyof T]: UnwrapRef6<T[K]> } : T
+
+type UnwrapRef6<T> = T extends Ref<infer V>
+  ? UnwrapRef7<V>
+  : T extends Array<infer V>
+    ? Array<UnwrapRef7<V>>
+    : T extends BailTypes
+      ? T
+      : T extends object ? { [K in keyof T]: UnwrapRef7<T[K]> } : T
+
+type UnwrapRef7<T> = T extends Ref<infer V>
+  ? UnwrapRef8<V>
+  : T extends Array<infer V>
+    ? Array<UnwrapRef8<V>>
+    : T extends BailTypes
+      ? T
+      : T extends object ? { [K in keyof T]: UnwrapRef8<T[K]> } : T
+
+type UnwrapRef8<T> = T extends Ref<infer V>
+  ? UnwrapRef9<V>
+  : T extends Array<infer V>
+    ? Array<UnwrapRef9<V>>
+    : T extends BailTypes
+      ? T
+      : T extends object ? { [K in keyof T]: UnwrapRef9<T[K]> } : T
+
+type UnwrapRef9<T> = T extends Ref<infer V>
+  ? UnwrapRef10<V>
+  : T extends Array<infer V>
+    ? Array<UnwrapRef10<V>>
+    : T extends BailTypes
+      ? T
+      : T extends object ? { [K in keyof T]: UnwrapRef10<T[K]> } : T
+
+type UnwrapRef10<T> = T extends Ref<infer V>
+  ? V // stop recursion
+  : T
diff --git a/packages/reactivity/src/value.ts b/packages/reactivity/src/value.ts
deleted file mode 100644 (file)
index 761d4eb..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-import { track, trigger } from './effect'
-import { OperationTypes } from './operations'
-import { isObject } from '@vue/shared'
-import { state } from './index'
-
-export const knownValues = new WeakSet()
-
-export interface Value<T> {
-  value: T
-}
-
-const convert = (val: any): any => (isObject(val) ? state(val) : val)
-
-export function value<T>(raw: T): Value<T> {
-  raw = convert(raw)
-  const v = {
-    get value() {
-      track(v, OperationTypes.GET, '')
-      return raw
-    },
-    set value(newVal) {
-      raw = convert(newVal)
-      trigger(v, OperationTypes.SET, '')
-    }
-  }
-  knownValues.add(v)
-  return v
-}
-
-export function isValue(v: any): v is Value<any> {
-  return knownValues.has(v)
-}
-
-type BailTypes =
-  | Function
-  | Map<any, any>
-  | Set<any>
-  | WeakMap<any, any>
-  | WeakSet<any>
-
-// Recursively unwraps nested value bindings.
-// Unfortunately TS cannot do recursive types, but this should be enough for
-// practical use cases...
-export type UnwrapValue<T> = T extends Value<infer V>
-  ? UnwrapValue2<V>
-  : T extends Array<infer V>
-    ? Array<UnwrapValue2<V>>
-    : T extends BailTypes
-      ? T // bail out on types that shouldn't be unwrapped
-      : T extends object ? { [K in keyof T]: UnwrapValue2<T[K]> } : T
-
-type UnwrapValue2<T> = T extends Value<infer V>
-  ? UnwrapValue3<V>
-  : T extends Array<infer V>
-    ? Array<UnwrapValue3<V>>
-    : T extends BailTypes
-      ? T
-      : T extends object ? { [K in keyof T]: UnwrapValue3<T[K]> } : T
-
-type UnwrapValue3<T> = T extends Value<infer V>
-  ? UnwrapValue4<V>
-  : T extends Array<infer V>
-    ? Array<UnwrapValue4<V>>
-    : T extends BailTypes
-      ? T
-      : T extends object ? { [K in keyof T]: UnwrapValue4<T[K]> } : T
-
-type UnwrapValue4<T> = T extends Value<infer V>
-  ? UnwrapValue5<V>
-  : T extends Array<infer V>
-    ? Array<UnwrapValue5<V>>
-    : T extends BailTypes
-      ? T
-      : T extends object ? { [K in keyof T]: UnwrapValue5<T[K]> } : T
-
-type UnwrapValue5<T> = T extends Value<infer V>
-  ? UnwrapValue6<V>
-  : T extends Array<infer V>
-    ? Array<UnwrapValue6<V>>
-    : T extends BailTypes
-      ? T
-      : T extends object ? { [K in keyof T]: UnwrapValue6<T[K]> } : T
-
-type UnwrapValue6<T> = T extends Value<infer V>
-  ? UnwrapValue7<V>
-  : T extends Array<infer V>
-    ? Array<UnwrapValue7<V>>
-    : T extends BailTypes
-      ? T
-      : T extends object ? { [K in keyof T]: UnwrapValue7<T[K]> } : T
-
-type UnwrapValue7<T> = T extends Value<infer V>
-  ? UnwrapValue8<V>
-  : T extends Array<infer V>
-    ? Array<UnwrapValue8<V>>
-    : T extends BailTypes
-      ? T
-      : T extends object ? { [K in keyof T]: UnwrapValue8<T[K]> } : T
-
-type UnwrapValue8<T> = T extends Value<infer V>
-  ? UnwrapValue9<V>
-  : T extends Array<infer V>
-    ? Array<UnwrapValue9<V>>
-    : T extends BailTypes
-      ? T
-      : T extends object ? { [K in keyof T]: UnwrapValue9<T[K]> } : T
-
-type UnwrapValue9<T> = T extends Value<infer V>
-  ? UnwrapValue10<V>
-  : T extends Array<infer V>
-    ? Array<UnwrapValue10<V>>
-    : T extends BailTypes
-      ? T
-      : T extends object ? { [K in keyof T]: UnwrapValue10<T[K]> } : T
-
-type UnwrapValue10<T> = T extends Value<infer V>
-  ? V // stop recursion
-  : T