refactor: rename reactivity package name and APIs
This commit is contained in:
parent
07403c9aba
commit
471899af8b
@ -1,7 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'production') {
|
|
||||||
module.exports = require('./dist/observer.cjs.prod.js')
|
|
||||||
} else {
|
|
||||||
module.exports = require('./dist/observer.cjs.js')
|
|
||||||
}
|
|
@ -1,18 +1,18 @@
|
|||||||
import { observable, effect, unwrap, isObservable } from '../../src'
|
import { state, effect, toRaw, isState } from '../../src'
|
||||||
|
|
||||||
describe('observer/collections', () => {
|
describe('observer/collections', () => {
|
||||||
describe('Map', () => {
|
describe('Map', () => {
|
||||||
test('instanceof', () => {
|
test('instanceof', () => {
|
||||||
const original = new Map()
|
const original = new Map()
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(original instanceof Map).toBe(true)
|
expect(original instanceof Map).toBe(true)
|
||||||
expect(observed instanceof Map).toBe(true)
|
expect(observed instanceof Map).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should observe mutations', () => {
|
it('should observe mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = map.get('key')
|
dummy = map.get('key')
|
||||||
})
|
})
|
||||||
@ -28,7 +28,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe size mutations', () => {
|
it('should observe size mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
effect(() => (dummy = map.size))
|
effect(() => (dummy = map.size))
|
||||||
|
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
@ -43,7 +43,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe for of iteration', () => {
|
it('should observe for of iteration', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
@ -66,7 +66,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe forEach iteration', () => {
|
it('should observe forEach iteration', () => {
|
||||||
let dummy: any
|
let dummy: any
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
map.forEach((num: any) => (dummy += num))
|
map.forEach((num: any) => (dummy += num))
|
||||||
@ -85,7 +85,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe keys iteration', () => {
|
it('should observe keys iteration', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (let key of map.keys()) {
|
for (let key of map.keys()) {
|
||||||
@ -106,7 +106,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe values iteration', () => {
|
it('should observe values iteration', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (let num of map.values()) {
|
for (let num of map.values()) {
|
||||||
@ -127,7 +127,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe entries iteration', () => {
|
it('should observe entries iteration', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
@ -150,7 +150,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should be triggered by clearing', () => {
|
it('should be triggered by clearing', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
effect(() => (dummy = map.get('key')))
|
effect(() => (dummy = map.get('key')))
|
||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
@ -162,7 +162,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not observe custom property mutations', () => {
|
it('should not observe custom property mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map: any = observable(new Map())
|
const map: any = state(new Map())
|
||||||
effect(() => (dummy = map.customProp))
|
effect(() => (dummy = map.customProp))
|
||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
@ -172,7 +172,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not observe non value changing mutations', () => {
|
it('should not observe non value changing mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
const mapSpy = jest.fn(() => (dummy = map.get('key')))
|
const mapSpy = jest.fn(() => (dummy = map.get('key')))
|
||||||
effect(mapSpy)
|
effect(mapSpy)
|
||||||
|
|
||||||
@ -197,8 +197,8 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not observe raw data', () => {
|
it('should not observe raw data', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map = observable(new Map())
|
const map = state(new Map())
|
||||||
effect(() => (dummy = unwrap(map).get('key')))
|
effect(() => (dummy = toRaw(map).get('key')))
|
||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
map.set('key', 'Hello')
|
map.set('key', 'Hello')
|
||||||
@ -209,24 +209,24 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not pollute original Map with Proxies', () => {
|
it('should not pollute original Map with Proxies', () => {
|
||||||
const map = new Map()
|
const map = new Map()
|
||||||
const observed = observable(map)
|
const observed = state(map)
|
||||||
const value = observable({})
|
const value = state({})
|
||||||
observed.set('key', value)
|
observed.set('key', value)
|
||||||
expect(map.get('key')).not.toBe(value)
|
expect(map.get('key')).not.toBe(value)
|
||||||
expect(map.get('key')).toBe(unwrap(value))
|
expect(map.get('key')).toBe(toRaw(value))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return observable versions of contained values', () => {
|
it('should return observable versions of contained values', () => {
|
||||||
const observed = observable(new Map())
|
const observed = state(new Map())
|
||||||
const value = {}
|
const value = {}
|
||||||
observed.set('key', value)
|
observed.set('key', value)
|
||||||
const wrapped = observed.get('key')
|
const wrapped = observed.get('key')
|
||||||
expect(isObservable(wrapped)).toBe(true)
|
expect(isState(wrapped)).toBe(true)
|
||||||
expect(unwrap(wrapped)).toBe(value)
|
expect(toRaw(wrapped)).toBe(value)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should observed nested data', () => {
|
it('should observed nested data', () => {
|
||||||
const observed = observable(new Map())
|
const observed = state(new Map())
|
||||||
observed.set('key', { a: 1 })
|
observed.set('key', { a: 1 })
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
@ -237,12 +237,12 @@ describe('observer/collections', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should observe nested values in iterations (forEach)', () => {
|
it('should observe nested values in iterations (forEach)', () => {
|
||||||
const map = observable(new Map([[1, { foo: 1 }]]))
|
const map = state(new Map([[1, { foo: 1 }]]))
|
||||||
let dummy: any
|
let dummy: any
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
map.forEach(value => {
|
map.forEach(value => {
|
||||||
expect(isObservable(value)).toBe(true)
|
expect(isState(value)).toBe(true)
|
||||||
dummy += value.foo
|
dummy += value.foo
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -252,12 +252,12 @@ describe('observer/collections', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should observe nested values in iterations (values)', () => {
|
it('should observe nested values in iterations (values)', () => {
|
||||||
const map = observable(new Map([[1, { foo: 1 }]]))
|
const map = state(new Map([[1, { foo: 1 }]]))
|
||||||
let dummy: any
|
let dummy: any
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (const value of map.values()) {
|
for (const value of map.values()) {
|
||||||
expect(isObservable(value)).toBe(true)
|
expect(isState(value)).toBe(true)
|
||||||
dummy += value.foo
|
dummy += value.foo
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -268,14 +268,14 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe nested values in iterations (entries)', () => {
|
it('should observe nested values in iterations (entries)', () => {
|
||||||
const key = {}
|
const key = {}
|
||||||
const map = observable(new Map([[key, { foo: 1 }]]))
|
const map = state(new Map([[key, { foo: 1 }]]))
|
||||||
let dummy: any
|
let dummy: any
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (const [key, value] of map.entries()) {
|
for (const [key, value] of map.entries()) {
|
||||||
key
|
key
|
||||||
expect(isObservable(key)).toBe(true)
|
expect(isState(key)).toBe(true)
|
||||||
expect(isObservable(value)).toBe(true)
|
expect(isState(value)).toBe(true)
|
||||||
dummy += value.foo
|
dummy += value.foo
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -286,14 +286,14 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe nested values in iterations (for...of)', () => {
|
it('should observe nested values in iterations (for...of)', () => {
|
||||||
const key = {}
|
const key = {}
|
||||||
const map = observable(new Map([[key, { foo: 1 }]]))
|
const map = state(new Map([[key, { foo: 1 }]]))
|
||||||
let dummy: any
|
let dummy: any
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (const [key, value] of map) {
|
for (const [key, value] of map) {
|
||||||
key
|
key
|
||||||
expect(isObservable(key)).toBe(true)
|
expect(isState(key)).toBe(true)
|
||||||
expect(isObservable(value)).toBe(true)
|
expect(isState(value)).toBe(true)
|
||||||
dummy += value.foo
|
dummy += value.foo
|
||||||
}
|
}
|
||||||
})
|
})
|
@ -1,18 +1,18 @@
|
|||||||
import { observable, effect, isObservable, unwrap } from '../../src'
|
import { state, effect, isState, toRaw } from '../../src'
|
||||||
|
|
||||||
describe('observer/collections', () => {
|
describe('observer/collections', () => {
|
||||||
describe('Set', () => {
|
describe('Set', () => {
|
||||||
it('instanceof', () => {
|
it('instanceof', () => {
|
||||||
const original = new Set()
|
const original = new Set()
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(original instanceof Set).toBe(true)
|
expect(original instanceof Set).toBe(true)
|
||||||
expect(observed instanceof Set).toBe(true)
|
expect(observed instanceof Set).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should observe mutations', () => {
|
it('should observe mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
effect(() => (dummy = set.has('value')))
|
effect(() => (dummy = set.has('value')))
|
||||||
|
|
||||||
expect(dummy).toBe(false)
|
expect(dummy).toBe(false)
|
||||||
@ -24,7 +24,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe for of iteration', () => {
|
it('should observe for of iteration', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set() as Set<number>)
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (let num of set) {
|
for (let num of set) {
|
||||||
@ -44,7 +44,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe forEach iteration', () => {
|
it('should observe forEach iteration', () => {
|
||||||
let dummy: any
|
let dummy: any
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
set.forEach(num => (dummy += num))
|
set.forEach(num => (dummy += num))
|
||||||
@ -62,7 +62,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe values iteration', () => {
|
it('should observe values iteration', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set() as Set<number>)
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (let num of set.values()) {
|
for (let num of set.values()) {
|
||||||
@ -82,7 +82,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe keys iteration', () => {
|
it('should observe keys iteration', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set() as Set<number>)
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (let num of set.keys()) {
|
for (let num of set.keys()) {
|
||||||
@ -102,7 +102,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe entries iteration', () => {
|
it('should observe entries iteration', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set() as Set<number>)
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
@ -124,7 +124,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should be triggered by clearing', () => {
|
it('should be triggered by clearing', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
effect(() => (dummy = set.has('key')))
|
effect(() => (dummy = set.has('key')))
|
||||||
|
|
||||||
expect(dummy).toBe(false)
|
expect(dummy).toBe(false)
|
||||||
@ -136,7 +136,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not observe custom property mutations', () => {
|
it('should not observe custom property mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set: any = observable(new Set())
|
const set: any = state(new Set())
|
||||||
effect(() => (dummy = set.customProp))
|
effect(() => (dummy = set.customProp))
|
||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
@ -146,7 +146,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should observe size mutations', () => {
|
it('should observe size mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
effect(() => (dummy = set.size))
|
effect(() => (dummy = set.size))
|
||||||
|
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
@ -161,7 +161,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not observe non value changing mutations', () => {
|
it('should not observe non value changing mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
const setSpy = jest.fn(() => (dummy = set.has('value')))
|
const setSpy = jest.fn(() => (dummy = set.has('value')))
|
||||||
effect(setSpy)
|
effect(setSpy)
|
||||||
|
|
||||||
@ -186,8 +186,8 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not observe raw data', () => {
|
it('should not observe raw data', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
effect(() => (dummy = unwrap(set).has('value')))
|
effect(() => (dummy = toRaw(set).has('value')))
|
||||||
|
|
||||||
expect(dummy).toBe(false)
|
expect(dummy).toBe(false)
|
||||||
set.add('value')
|
set.add('value')
|
||||||
@ -196,22 +196,22 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not observe raw iterations', () => {
|
it('should not observe raw iterations', () => {
|
||||||
let dummy = 0
|
let dummy = 0
|
||||||
const set = observable(new Set())
|
const set = state(new Set() as Set<number>)
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (let [num] of unwrap(set).entries()) {
|
for (let [num] of toRaw(set).entries()) {
|
||||||
dummy += num
|
dummy += num
|
||||||
}
|
}
|
||||||
for (let num of unwrap(set).keys()) {
|
for (let num of toRaw(set).keys()) {
|
||||||
dummy += num
|
dummy += num
|
||||||
}
|
}
|
||||||
for (let num of unwrap(set).values()) {
|
for (let num of toRaw(set).values()) {
|
||||||
dummy += num
|
dummy += num
|
||||||
}
|
}
|
||||||
unwrap(set).forEach(num => {
|
toRaw(set).forEach(num => {
|
||||||
dummy += num
|
dummy += num
|
||||||
})
|
})
|
||||||
for (let num of unwrap(set)) {
|
for (let num of toRaw(set)) {
|
||||||
dummy += num
|
dummy += num
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -226,23 +226,23 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not be triggered by raw mutations', () => {
|
it('should not be triggered by raw mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
effect(() => (dummy = set.has('value')))
|
effect(() => (dummy = set.has('value')))
|
||||||
|
|
||||||
expect(dummy).toBe(false)
|
expect(dummy).toBe(false)
|
||||||
unwrap(set).add('value')
|
toRaw(set).add('value')
|
||||||
expect(dummy).toBe(false)
|
expect(dummy).toBe(false)
|
||||||
dummy = true
|
dummy = true
|
||||||
unwrap(set).delete('value')
|
toRaw(set).delete('value')
|
||||||
expect(dummy).toBe(true)
|
expect(dummy).toBe(true)
|
||||||
unwrap(set).clear()
|
toRaw(set).clear()
|
||||||
expect(dummy).toBe(true)
|
expect(dummy).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not observe raw size mutations', () => {
|
it('should not observe raw size mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
effect(() => (dummy = unwrap(set).size))
|
effect(() => (dummy = toRaw(set).size))
|
||||||
|
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
set.add('value')
|
set.add('value')
|
||||||
@ -251,18 +251,18 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not be triggered by raw size mutations', () => {
|
it('should not be triggered by raw size mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
effect(() => (dummy = set.size))
|
effect(() => (dummy = set.size))
|
||||||
|
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
unwrap(set).add('value')
|
toRaw(set).add('value')
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support objects as key', () => {
|
it('should support objects as key', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const key = {}
|
const key = {}
|
||||||
const set = observable(new Set())
|
const set = state(new Set())
|
||||||
const setSpy = jest.fn(() => (dummy = set.has(key)))
|
const setSpy = jest.fn(() => (dummy = set.has(key)))
|
||||||
effect(setSpy)
|
effect(setSpy)
|
||||||
|
|
||||||
@ -280,20 +280,20 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not pollute original Set with Proxies', () => {
|
it('should not pollute original Set with Proxies', () => {
|
||||||
const set = new Set()
|
const set = new Set()
|
||||||
const observed = observable(set)
|
const observed = state(set)
|
||||||
const value = observable({})
|
const value = state({})
|
||||||
observed.add(value)
|
observed.add(value)
|
||||||
expect(observed.has(value)).toBe(true)
|
expect(observed.has(value)).toBe(true)
|
||||||
expect(set.has(value)).toBe(false)
|
expect(set.has(value)).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should observe nested values in iterations (forEach)', () => {
|
it('should observe nested values in iterations (forEach)', () => {
|
||||||
const set = observable(new Set([{ foo: 1 }]))
|
const set = state(new Set([{ foo: 1 }]))
|
||||||
let dummy: any
|
let dummy: any
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
set.forEach(value => {
|
set.forEach(value => {
|
||||||
expect(isObservable(value)).toBe(true)
|
expect(isState(value)).toBe(true)
|
||||||
dummy += value.foo
|
dummy += value.foo
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -305,12 +305,12 @@ describe('observer/collections', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should observe nested values in iterations (values)', () => {
|
it('should observe nested values in iterations (values)', () => {
|
||||||
const set = observable(new Set([{ foo: 1 }]))
|
const set = state(new Set([{ foo: 1 }]))
|
||||||
let dummy: any
|
let dummy: any
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (const value of set.values()) {
|
for (const value of set.values()) {
|
||||||
expect(isObservable(value)).toBe(true)
|
expect(isState(value)).toBe(true)
|
||||||
dummy += value.foo
|
dummy += value.foo
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -322,13 +322,13 @@ describe('observer/collections', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should observe nested values in iterations (entries)', () => {
|
it('should observe nested values in iterations (entries)', () => {
|
||||||
const set = observable(new Set([{ foo: 1 }]))
|
const set = state(new Set([{ foo: 1 }]))
|
||||||
let dummy: any
|
let dummy: any
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (const [key, value] of set.entries()) {
|
for (const [key, value] of set.entries()) {
|
||||||
expect(isObservable(key)).toBe(true)
|
expect(isState(key)).toBe(true)
|
||||||
expect(isObservable(value)).toBe(true)
|
expect(isState(value)).toBe(true)
|
||||||
dummy += value.foo
|
dummy += value.foo
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -340,12 +340,12 @@ describe('observer/collections', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should observe nested values in iterations (for...of)', () => {
|
it('should observe nested values in iterations (for...of)', () => {
|
||||||
const set = observable(new Set([{ foo: 1 }]))
|
const set = state(new Set([{ foo: 1 }]))
|
||||||
let dummy: any
|
let dummy: any
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (const value of set) {
|
for (const value of set) {
|
||||||
expect(isObservable(value)).toBe(true)
|
expect(isState(value)).toBe(true)
|
||||||
dummy += value.foo
|
dummy += value.foo
|
||||||
}
|
}
|
||||||
})
|
})
|
@ -1,11 +1,11 @@
|
|||||||
import { observable, effect, unwrap, isObservable } from '../../src'
|
import { state, effect, toRaw, isState } from '../../src'
|
||||||
|
|
||||||
describe('observer/collections', () => {
|
describe('observer/collections', () => {
|
||||||
describe('WeakMap', () => {
|
describe('WeakMap', () => {
|
||||||
test('instanceof', () => {
|
test('instanceof', () => {
|
||||||
const original = new WeakMap()
|
const original = new WeakMap()
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(original instanceof WeakMap).toBe(true)
|
expect(original instanceof WeakMap).toBe(true)
|
||||||
expect(observed instanceof WeakMap).toBe(true)
|
expect(observed instanceof WeakMap).toBe(true)
|
||||||
})
|
})
|
||||||
@ -13,7 +13,7 @@ describe('observer/collections', () => {
|
|||||||
it('should observe mutations', () => {
|
it('should observe mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const key = {}
|
const key = {}
|
||||||
const map = observable(new WeakMap())
|
const map = state(new WeakMap())
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = map.get(key)
|
dummy = map.get(key)
|
||||||
})
|
})
|
||||||
@ -29,7 +29,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not observe custom property mutations', () => {
|
it('should not observe custom property mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const map: any = observable(new WeakMap())
|
const map: any = state(new WeakMap())
|
||||||
effect(() => (dummy = map.customProp))
|
effect(() => (dummy = map.customProp))
|
||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
@ -40,7 +40,7 @@ describe('observer/collections', () => {
|
|||||||
it('should not observe non value changing mutations', () => {
|
it('should not observe non value changing mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const key = {}
|
const key = {}
|
||||||
const map = observable(new WeakMap())
|
const map = state(new WeakMap())
|
||||||
const mapSpy = jest.fn(() => (dummy = map.get(key)))
|
const mapSpy = jest.fn(() => (dummy = map.get(key)))
|
||||||
effect(mapSpy)
|
effect(mapSpy)
|
||||||
|
|
||||||
@ -63,8 +63,8 @@ describe('observer/collections', () => {
|
|||||||
it('should not observe raw data', () => {
|
it('should not observe raw data', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const key = {}
|
const key = {}
|
||||||
const map = observable(new WeakMap())
|
const map = state(new WeakMap())
|
||||||
effect(() => (dummy = unwrap(map).get(key)))
|
effect(() => (dummy = toRaw(map).get(key)))
|
||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
map.set(key, 'Hello')
|
map.set(key, 'Hello')
|
||||||
@ -75,26 +75,26 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not pollute original Map with Proxies', () => {
|
it('should not pollute original Map with Proxies', () => {
|
||||||
const map = new WeakMap()
|
const map = new WeakMap()
|
||||||
const observed = observable(map)
|
const observed = state(map)
|
||||||
const key = {}
|
const key = {}
|
||||||
const value = observable({})
|
const value = state({})
|
||||||
observed.set(key, value)
|
observed.set(key, value)
|
||||||
expect(map.get(key)).not.toBe(value)
|
expect(map.get(key)).not.toBe(value)
|
||||||
expect(map.get(key)).toBe(unwrap(value))
|
expect(map.get(key)).toBe(toRaw(value))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return observable versions of contained values', () => {
|
it('should return observable versions of contained values', () => {
|
||||||
const observed = observable(new WeakMap())
|
const observed = state(new WeakMap())
|
||||||
const key = {}
|
const key = {}
|
||||||
const value = {}
|
const value = {}
|
||||||
observed.set(key, value)
|
observed.set(key, value)
|
||||||
const wrapped = observed.get(key)
|
const wrapped = observed.get(key)
|
||||||
expect(isObservable(wrapped)).toBe(true)
|
expect(isState(wrapped)).toBe(true)
|
||||||
expect(unwrap(wrapped)).toBe(value)
|
expect(toRaw(wrapped)).toBe(value)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should observed nested data', () => {
|
it('should observed nested data', () => {
|
||||||
const observed = observable(new Map())
|
const observed = state(new Map())
|
||||||
const key = {}
|
const key = {}
|
||||||
observed.set(key, { a: 1 })
|
observed.set(key, { a: 1 })
|
||||||
let dummy
|
let dummy
|
@ -1,11 +1,11 @@
|
|||||||
import { observable, isObservable, effect, unwrap } from '../../src'
|
import { state, isState, effect, toRaw } from '../../src'
|
||||||
|
|
||||||
describe('observer/collections', () => {
|
describe('observer/collections', () => {
|
||||||
describe('WeakSet', () => {
|
describe('WeakSet', () => {
|
||||||
it('instanceof', () => {
|
it('instanceof', () => {
|
||||||
const original = new Set()
|
const original = new Set()
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(original instanceof Set).toBe(true)
|
expect(original instanceof Set).toBe(true)
|
||||||
expect(observed instanceof Set).toBe(true)
|
expect(observed instanceof Set).toBe(true)
|
||||||
})
|
})
|
||||||
@ -13,7 +13,7 @@ describe('observer/collections', () => {
|
|||||||
it('should observe mutations', () => {
|
it('should observe mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const value = {}
|
const value = {}
|
||||||
const set = observable(new WeakSet())
|
const set = state(new WeakSet())
|
||||||
effect(() => (dummy = set.has(value)))
|
effect(() => (dummy = set.has(value)))
|
||||||
|
|
||||||
expect(dummy).toBe(false)
|
expect(dummy).toBe(false)
|
||||||
@ -25,7 +25,7 @@ describe('observer/collections', () => {
|
|||||||
|
|
||||||
it('should not observe custom property mutations', () => {
|
it('should not observe custom property mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const set: any = observable(new WeakSet())
|
const set: any = state(new WeakSet())
|
||||||
effect(() => (dummy = set.customProp))
|
effect(() => (dummy = set.customProp))
|
||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
@ -36,7 +36,7 @@ describe('observer/collections', () => {
|
|||||||
it('should not observe non value changing mutations', () => {
|
it('should not observe non value changing mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const value = {}
|
const value = {}
|
||||||
const set = observable(new WeakSet())
|
const set = state(new WeakSet())
|
||||||
const setSpy = jest.fn(() => (dummy = set.has(value)))
|
const setSpy = jest.fn(() => (dummy = set.has(value)))
|
||||||
effect(setSpy)
|
effect(setSpy)
|
||||||
|
|
||||||
@ -59,8 +59,8 @@ describe('observer/collections', () => {
|
|||||||
it('should not observe raw data', () => {
|
it('should not observe raw data', () => {
|
||||||
const value = {}
|
const value = {}
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new WeakSet())
|
const set = state(new WeakSet())
|
||||||
effect(() => (dummy = unwrap(set).has(value)))
|
effect(() => (dummy = toRaw(set).has(value)))
|
||||||
|
|
||||||
expect(dummy).toBe(false)
|
expect(dummy).toBe(false)
|
||||||
set.add(value)
|
set.add(value)
|
||||||
@ -70,18 +70,18 @@ describe('observer/collections', () => {
|
|||||||
it('should not be triggered by raw mutations', () => {
|
it('should not be triggered by raw mutations', () => {
|
||||||
const value = {}
|
const value = {}
|
||||||
let dummy
|
let dummy
|
||||||
const set = observable(new WeakSet())
|
const set = state(new WeakSet())
|
||||||
effect(() => (dummy = set.has(value)))
|
effect(() => (dummy = set.has(value)))
|
||||||
|
|
||||||
expect(dummy).toBe(false)
|
expect(dummy).toBe(false)
|
||||||
unwrap(set).add(value)
|
toRaw(set).add(value)
|
||||||
expect(dummy).toBe(false)
|
expect(dummy).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not pollute original Set with Proxies', () => {
|
it('should not pollute original Set with Proxies', () => {
|
||||||
const set = new WeakSet()
|
const set = new WeakSet()
|
||||||
const observed = observable(set)
|
const observed = state(set)
|
||||||
const value = observable({})
|
const value = state({})
|
||||||
observed.add(value)
|
observed.add(value)
|
||||||
expect(observed.has(value)).toBe(true)
|
expect(observed.has(value)).toBe(true)
|
||||||
expect(set.has(value)).toBe(false)
|
expect(set.has(value)).toBe(false)
|
@ -1,8 +1,8 @@
|
|||||||
import { computed, observable, effect, stop } from '../src'
|
import { computed, state, effect, stop } from '../src'
|
||||||
|
|
||||||
describe('observer/computed', () => {
|
describe('observer/computed', () => {
|
||||||
it('should return updated value', () => {
|
it('should return updated value', () => {
|
||||||
const value: any = observable({})
|
const value: any = state({})
|
||||||
const cValue = computed(() => value.foo)
|
const cValue = computed(() => value.foo)
|
||||||
expect(cValue.value).toBe(undefined)
|
expect(cValue.value).toBe(undefined)
|
||||||
value.foo = 1
|
value.foo = 1
|
||||||
@ -10,7 +10,7 @@ describe('observer/computed', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should compute lazily', () => {
|
it('should compute lazily', () => {
|
||||||
const value: any = observable({})
|
const value: any = state({})
|
||||||
const getter = jest.fn(() => value.foo)
|
const getter = jest.fn(() => value.foo)
|
||||||
const cValue = computed(getter)
|
const cValue = computed(getter)
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ describe('observer/computed', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should trigger effect', () => {
|
it('should trigger effect', () => {
|
||||||
const value: any = observable({})
|
const value: any = state({})
|
||||||
const cValue = computed(() => value.foo)
|
const cValue = computed(() => value.foo)
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
@ -50,7 +50,7 @@ describe('observer/computed', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should work when chained', () => {
|
it('should work when chained', () => {
|
||||||
const value: any = observable({ foo: 0 })
|
const value: any = state({ foo: 0 })
|
||||||
const c1 = computed(() => value.foo)
|
const c1 = computed(() => value.foo)
|
||||||
const c2 = computed(() => c1.value + 1)
|
const c2 = computed(() => c1.value + 1)
|
||||||
expect(c2.value).toBe(1)
|
expect(c2.value).toBe(1)
|
||||||
@ -61,7 +61,7 @@ describe('observer/computed', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should trigger effect when chained', () => {
|
it('should trigger effect when chained', () => {
|
||||||
const value: any = observable({ foo: 0 })
|
const value: any = state({ foo: 0 })
|
||||||
const getter1 = jest.fn(() => value.foo)
|
const getter1 = jest.fn(() => value.foo)
|
||||||
const getter2 = jest.fn(() => {
|
const getter2 = jest.fn(() => {
|
||||||
return c1.value + 1
|
return c1.value + 1
|
||||||
@ -84,7 +84,7 @@ describe('observer/computed', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should trigger effect when chained (mixed invocations)', () => {
|
it('should trigger effect when chained (mixed invocations)', () => {
|
||||||
const value: any = observable({ foo: 0 })
|
const value: any = state({ foo: 0 })
|
||||||
const getter1 = jest.fn(() => value.foo)
|
const getter1 = jest.fn(() => value.foo)
|
||||||
const getter2 = jest.fn(() => {
|
const getter2 = jest.fn(() => {
|
||||||
return c1.value + 1
|
return c1.value + 1
|
||||||
@ -108,7 +108,7 @@ describe('observer/computed', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should no longer update when stopped', () => {
|
it('should no longer update when stopped', () => {
|
||||||
const value: any = observable({})
|
const value: any = state({})
|
||||||
const cValue = computed(() => value.foo)
|
const cValue = computed(() => value.foo)
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
@ -1,8 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
observable,
|
state,
|
||||||
effect,
|
effect,
|
||||||
stop,
|
stop,
|
||||||
unwrap,
|
toRaw,
|
||||||
OperationTypes,
|
OperationTypes,
|
||||||
DebuggerEvent,
|
DebuggerEvent,
|
||||||
markNonReactive
|
markNonReactive
|
||||||
@ -18,7 +18,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe basic properties', () => {
|
it('should observe basic properties', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const counter = observable({ num: 0 })
|
const counter = state({ num: 0 })
|
||||||
effect(() => (dummy = counter.num))
|
effect(() => (dummy = counter.num))
|
||||||
|
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
@ -28,7 +28,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe multiple properties', () => {
|
it('should observe multiple properties', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const counter = observable({ num1: 0, num2: 0 })
|
const counter = state({ num1: 0, num2: 0 })
|
||||||
effect(() => (dummy = counter.num1 + counter.num1 + counter.num2))
|
effect(() => (dummy = counter.num1 + counter.num1 + counter.num2))
|
||||||
|
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
@ -38,7 +38,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should handle multiple effects', () => {
|
it('should handle multiple effects', () => {
|
||||||
let dummy1, dummy2
|
let dummy1, dummy2
|
||||||
const counter = observable({ num: 0 })
|
const counter = state({ num: 0 })
|
||||||
effect(() => (dummy1 = counter.num))
|
effect(() => (dummy1 = counter.num))
|
||||||
effect(() => (dummy2 = counter.num))
|
effect(() => (dummy2 = counter.num))
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe nested properties', () => {
|
it('should observe nested properties', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const counter = observable({ nested: { num: 0 } })
|
const counter = state({ nested: { num: 0 } })
|
||||||
effect(() => (dummy = counter.nested.num))
|
effect(() => (dummy = counter.nested.num))
|
||||||
|
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
@ -61,7 +61,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe delete operations', () => {
|
it('should observe delete operations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const obj = observable({ prop: 'value' })
|
const obj = state({ prop: 'value' })
|
||||||
effect(() => (dummy = obj.prop))
|
effect(() => (dummy = obj.prop))
|
||||||
|
|
||||||
expect(dummy).toBe('value')
|
expect(dummy).toBe('value')
|
||||||
@ -71,7 +71,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe has operations', () => {
|
it('should observe has operations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const obj: any = observable({ prop: 'value' })
|
const obj: any = state({ prop: 'value' })
|
||||||
effect(() => (dummy = 'prop' in obj))
|
effect(() => (dummy = 'prop' in obj))
|
||||||
|
|
||||||
expect(dummy).toBe(true)
|
expect(dummy).toBe(true)
|
||||||
@ -83,8 +83,8 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe properties on the prototype chain', () => {
|
it('should observe properties on the prototype chain', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const counter = observable({ num: 0 })
|
const counter = state({ num: 0 })
|
||||||
const parentCounter = observable({ num: 2 })
|
const parentCounter = state({ num: 2 })
|
||||||
Object.setPrototypeOf(counter, parentCounter)
|
Object.setPrototypeOf(counter, parentCounter)
|
||||||
effect(() => (dummy = counter.num))
|
effect(() => (dummy = counter.num))
|
||||||
|
|
||||||
@ -99,8 +99,8 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe has operations on the prototype chain', () => {
|
it('should observe has operations on the prototype chain', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const counter = observable({ num: 0 })
|
const counter = state({ num: 0 })
|
||||||
const parentCounter = observable({ num: 2 })
|
const parentCounter = state({ num: 2 })
|
||||||
Object.setPrototypeOf(counter, parentCounter)
|
Object.setPrototypeOf(counter, parentCounter)
|
||||||
effect(() => (dummy = 'num' in counter))
|
effect(() => (dummy = 'num' in counter))
|
||||||
|
|
||||||
@ -115,8 +115,8 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe inherited property accessors', () => {
|
it('should observe inherited property accessors', () => {
|
||||||
let dummy, parentDummy, hiddenValue: any
|
let dummy, parentDummy, hiddenValue: any
|
||||||
const obj: any = observable({})
|
const obj: any = state({})
|
||||||
const parent = observable({
|
const parent = state({
|
||||||
set prop(value) {
|
set prop(value) {
|
||||||
hiddenValue = value
|
hiddenValue = value
|
||||||
},
|
},
|
||||||
@ -141,7 +141,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe function call chains', () => {
|
it('should observe function call chains', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const counter = observable({ num: 0 })
|
const counter = state({ num: 0 })
|
||||||
effect(() => (dummy = getNum()))
|
effect(() => (dummy = getNum()))
|
||||||
|
|
||||||
function getNum() {
|
function getNum() {
|
||||||
@ -155,7 +155,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe iteration', () => {
|
it('should observe iteration', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const list = observable(['Hello'])
|
const list = state(['Hello'])
|
||||||
effect(() => (dummy = list.join(' ')))
|
effect(() => (dummy = list.join(' ')))
|
||||||
|
|
||||||
expect(dummy).toBe('Hello')
|
expect(dummy).toBe('Hello')
|
||||||
@ -167,7 +167,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe implicit array length changes', () => {
|
it('should observe implicit array length changes', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const list = observable(['Hello'])
|
const list = state(['Hello'])
|
||||||
effect(() => (dummy = list.join(' ')))
|
effect(() => (dummy = list.join(' ')))
|
||||||
|
|
||||||
expect(dummy).toBe('Hello')
|
expect(dummy).toBe('Hello')
|
||||||
@ -179,7 +179,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe sparse array mutations', () => {
|
it('should observe sparse array mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const list: any[] = observable([])
|
const list: any[] = state([])
|
||||||
list[1] = 'World!'
|
list[1] = 'World!'
|
||||||
effect(() => (dummy = list.join(' ')))
|
effect(() => (dummy = list.join(' ')))
|
||||||
|
|
||||||
@ -192,7 +192,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should observe enumeration', () => {
|
it('should observe enumeration', () => {
|
||||||
let dummy = 0
|
let dummy = 0
|
||||||
const numbers: any = observable({ num1: 3 })
|
const numbers: any = state({ num1: 3 })
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = 0
|
dummy = 0
|
||||||
for (let key in numbers) {
|
for (let key in numbers) {
|
||||||
@ -210,7 +210,7 @@ describe('observer/effect', () => {
|
|||||||
it('should observe symbol keyed properties', () => {
|
it('should observe symbol keyed properties', () => {
|
||||||
const key = Symbol('symbol keyed prop')
|
const key = Symbol('symbol keyed prop')
|
||||||
let dummy, hasDummy
|
let dummy, hasDummy
|
||||||
const obj = observable({ [key]: 'value' })
|
const obj = state({ [key]: 'value' })
|
||||||
effect(() => (dummy = obj[key]))
|
effect(() => (dummy = obj[key]))
|
||||||
effect(() => (hasDummy = key in obj))
|
effect(() => (hasDummy = key in obj))
|
||||||
|
|
||||||
@ -226,7 +226,7 @@ describe('observer/effect', () => {
|
|||||||
it('should not observe well-known symbol keyed properties', () => {
|
it('should not observe well-known symbol keyed properties', () => {
|
||||||
const key = Symbol.isConcatSpreadable
|
const key = Symbol.isConcatSpreadable
|
||||||
let dummy
|
let dummy
|
||||||
const array: any = observable([])
|
const array: any = state([])
|
||||||
effect(() => (dummy = array[key]))
|
effect(() => (dummy = array[key]))
|
||||||
|
|
||||||
expect(array[key]).toBe(undefined)
|
expect(array[key]).toBe(undefined)
|
||||||
@ -241,7 +241,7 @@ describe('observer/effect', () => {
|
|||||||
const newFunc = () => {}
|
const newFunc = () => {}
|
||||||
|
|
||||||
let dummy
|
let dummy
|
||||||
const obj = observable({ func: oldFunc })
|
const obj = state({ func: oldFunc })
|
||||||
effect(() => (dummy = obj.func))
|
effect(() => (dummy = obj.func))
|
||||||
|
|
||||||
expect(dummy).toBe(oldFunc)
|
expect(dummy).toBe(oldFunc)
|
||||||
@ -251,7 +251,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should not observe set operations without a value change', () => {
|
it('should not observe set operations without a value change', () => {
|
||||||
let hasDummy, getDummy
|
let hasDummy, getDummy
|
||||||
const obj = observable({ prop: 'value' })
|
const obj = state({ prop: 'value' })
|
||||||
|
|
||||||
const getSpy = jest.fn(() => (getDummy = obj.prop))
|
const getSpy = jest.fn(() => (getDummy = obj.prop))
|
||||||
const hasSpy = jest.fn(() => (hasDummy = 'prop' in obj))
|
const hasSpy = jest.fn(() => (hasDummy = 'prop' in obj))
|
||||||
@ -269,8 +269,8 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should not observe raw mutations', () => {
|
it('should not observe raw mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const obj: any = observable()
|
const obj: any = state()
|
||||||
effect(() => (dummy = unwrap(obj).prop))
|
effect(() => (dummy = toRaw(obj).prop))
|
||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
obj.prop = 'value'
|
obj.prop = 'value'
|
||||||
@ -279,18 +279,18 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should not be triggered by raw mutations', () => {
|
it('should not be triggered by raw mutations', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const obj: any = observable()
|
const obj: any = state()
|
||||||
effect(() => (dummy = obj.prop))
|
effect(() => (dummy = obj.prop))
|
||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
unwrap(obj).prop = 'value'
|
toRaw(obj).prop = 'value'
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not be triggered by inherited raw setters', () => {
|
it('should not be triggered by inherited raw setters', () => {
|
||||||
let dummy, parentDummy, hiddenValue: any
|
let dummy, parentDummy, hiddenValue: any
|
||||||
const obj: any = observable({})
|
const obj: any = state({})
|
||||||
const parent = observable({
|
const parent = state({
|
||||||
set prop(value) {
|
set prop(value) {
|
||||||
hiddenValue = value
|
hiddenValue = value
|
||||||
},
|
},
|
||||||
@ -304,13 +304,13 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
expect(parentDummy).toBe(undefined)
|
expect(parentDummy).toBe(undefined)
|
||||||
unwrap(obj).prop = 4
|
toRaw(obj).prop = 4
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
expect(parentDummy).toBe(undefined)
|
expect(parentDummy).toBe(undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should avoid implicit infinite recursive loops with itself', () => {
|
it('should avoid implicit infinite recursive loops with itself', () => {
|
||||||
const counter = observable({ num: 0 })
|
const counter = state({ num: 0 })
|
||||||
|
|
||||||
const counterSpy = jest.fn(() => counter.num++)
|
const counterSpy = jest.fn(() => counter.num++)
|
||||||
effect(counterSpy)
|
effect(counterSpy)
|
||||||
@ -322,7 +322,7 @@ describe('observer/effect', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should allow explicitly recursive raw function loops', () => {
|
it('should allow explicitly recursive raw function loops', () => {
|
||||||
const counter = observable({ num: 0 })
|
const counter = state({ num: 0 })
|
||||||
const numSpy = jest.fn(() => {
|
const numSpy = jest.fn(() => {
|
||||||
counter.num++
|
counter.num++
|
||||||
if (counter.num < 10) {
|
if (counter.num < 10) {
|
||||||
@ -335,7 +335,7 @@ describe('observer/effect', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should avoid infinite loops with other effects', () => {
|
it('should avoid infinite loops with other effects', () => {
|
||||||
const nums = observable({ num1: 0, num2: 1 })
|
const nums = state({ num1: 0, num2: 1 })
|
||||||
|
|
||||||
const spy1 = jest.fn(() => (nums.num1 = nums.num2))
|
const spy1 = jest.fn(() => (nums.num1 = nums.num2))
|
||||||
const spy2 = jest.fn(() => (nums.num2 = nums.num1))
|
const spy2 = jest.fn(() => (nums.num2 = nums.num1))
|
||||||
@ -371,7 +371,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should discover new branches while running automatically', () => {
|
it('should discover new branches while running automatically', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const obj = observable({ prop: 'value', run: false })
|
const obj = state({ prop: 'value', run: false })
|
||||||
|
|
||||||
const conditionalSpy = jest.fn(() => {
|
const conditionalSpy = jest.fn(() => {
|
||||||
dummy = obj.run ? obj.prop : 'other'
|
dummy = obj.run ? obj.prop : 'other'
|
||||||
@ -394,7 +394,7 @@ describe('observer/effect', () => {
|
|||||||
it('should discover new branches when running manually', () => {
|
it('should discover new branches when running manually', () => {
|
||||||
let dummy
|
let dummy
|
||||||
let run = false
|
let run = false
|
||||||
const obj = observable({ prop: 'value' })
|
const obj = state({ prop: 'value' })
|
||||||
const runner = effect(() => {
|
const runner = effect(() => {
|
||||||
dummy = run ? obj.prop : 'other'
|
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', () => {
|
it('should not be triggered by mutating a property, which is used in an inactive branch', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const obj = observable({ prop: 'value', run: true })
|
const obj = state({ prop: 'value', run: true })
|
||||||
|
|
||||||
const conditionalSpy = jest.fn(() => {
|
const conditionalSpy = jest.fn(() => {
|
||||||
dummy = obj.run ? obj.prop : 'other'
|
dummy = obj.run ? obj.prop : 'other'
|
||||||
@ -437,7 +437,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('should not run multiple times for a single mutation', () => {
|
it('should not run multiple times for a single mutation', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const obj: any = observable()
|
const obj: any = state()
|
||||||
const fnSpy = jest.fn(() => {
|
const fnSpy = jest.fn(() => {
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
dummy = obj[key]
|
dummy = obj[key]
|
||||||
@ -453,7 +453,7 @@ describe('observer/effect', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should allow nested effects', () => {
|
it('should allow nested effects', () => {
|
||||||
const nums = observable({ num1: 0, num2: 1, num3: 2 })
|
const nums = state({ num1: 0, num2: 1, num3: 2 })
|
||||||
const dummy: any = {}
|
const dummy: any = {}
|
||||||
|
|
||||||
const childSpy = jest.fn(() => (dummy.num1 = nums.num1))
|
const childSpy = jest.fn(() => (dummy.num1 = nums.num1))
|
||||||
@ -495,7 +495,7 @@ describe('observer/effect', () => {
|
|||||||
this.count++
|
this.count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const model = observable(new Model())
|
const model = state(new Model())
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = model.count
|
dummy = model.count
|
||||||
@ -510,7 +510,7 @@ describe('observer/effect', () => {
|
|||||||
const scheduler = jest.fn(_runner => {
|
const scheduler = jest.fn(_runner => {
|
||||||
runner = _runner
|
runner = _runner
|
||||||
})
|
})
|
||||||
const obj = observable({ foo: 1 })
|
const obj = state({ foo: 1 })
|
||||||
effect(
|
effect(
|
||||||
() => {
|
() => {
|
||||||
dummy = obj.foo
|
dummy = obj.foo
|
||||||
@ -536,7 +536,7 @@ describe('observer/effect', () => {
|
|||||||
const onTrack = jest.fn((e: DebuggerEvent) => {
|
const onTrack = jest.fn((e: DebuggerEvent) => {
|
||||||
events.push(e)
|
events.push(e)
|
||||||
})
|
})
|
||||||
const obj = observable({ foo: 1, bar: 2 })
|
const obj = state({ foo: 1, bar: 2 })
|
||||||
const runner = effect(
|
const runner = effect(
|
||||||
() => {
|
() => {
|
||||||
dummy = obj.foo
|
dummy = obj.foo
|
||||||
@ -550,19 +550,19 @@ describe('observer/effect', () => {
|
|||||||
expect(events).toEqual([
|
expect(events).toEqual([
|
||||||
{
|
{
|
||||||
effect: runner,
|
effect: runner,
|
||||||
target: unwrap(obj),
|
target: toRaw(obj),
|
||||||
type: OperationTypes.GET,
|
type: OperationTypes.GET,
|
||||||
key: 'foo'
|
key: 'foo'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
effect: runner,
|
effect: runner,
|
||||||
target: unwrap(obj),
|
target: toRaw(obj),
|
||||||
type: OperationTypes.HAS,
|
type: OperationTypes.HAS,
|
||||||
key: 'bar'
|
key: 'bar'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
effect: runner,
|
effect: runner,
|
||||||
target: unwrap(obj),
|
target: toRaw(obj),
|
||||||
type: OperationTypes.ITERATE,
|
type: OperationTypes.ITERATE,
|
||||||
key: ITERATE_KEY
|
key: ITERATE_KEY
|
||||||
}
|
}
|
||||||
@ -575,7 +575,7 @@ describe('observer/effect', () => {
|
|||||||
const onTrigger = jest.fn((e: DebuggerEvent) => {
|
const onTrigger = jest.fn((e: DebuggerEvent) => {
|
||||||
events.push(e)
|
events.push(e)
|
||||||
})
|
})
|
||||||
const obj = observable({ foo: 1 })
|
const obj = state({ foo: 1 })
|
||||||
const runner = effect(
|
const runner = effect(
|
||||||
() => {
|
() => {
|
||||||
dummy = obj.foo
|
dummy = obj.foo
|
||||||
@ -588,7 +588,7 @@ describe('observer/effect', () => {
|
|||||||
expect(onTrigger).toHaveBeenCalledTimes(1)
|
expect(onTrigger).toHaveBeenCalledTimes(1)
|
||||||
expect(events[0]).toEqual({
|
expect(events[0]).toEqual({
|
||||||
effect: runner,
|
effect: runner,
|
||||||
target: unwrap(obj),
|
target: toRaw(obj),
|
||||||
type: OperationTypes.SET,
|
type: OperationTypes.SET,
|
||||||
key: 'foo',
|
key: 'foo',
|
||||||
oldValue: 1,
|
oldValue: 1,
|
||||||
@ -600,7 +600,7 @@ describe('observer/effect', () => {
|
|||||||
expect(onTrigger).toHaveBeenCalledTimes(2)
|
expect(onTrigger).toHaveBeenCalledTimes(2)
|
||||||
expect(events[1]).toEqual({
|
expect(events[1]).toEqual({
|
||||||
effect: runner,
|
effect: runner,
|
||||||
target: unwrap(obj),
|
target: toRaw(obj),
|
||||||
type: OperationTypes.DELETE,
|
type: OperationTypes.DELETE,
|
||||||
key: 'foo',
|
key: 'foo',
|
||||||
oldValue: 2
|
oldValue: 2
|
||||||
@ -609,7 +609,7 @@ describe('observer/effect', () => {
|
|||||||
|
|
||||||
it('stop', () => {
|
it('stop', () => {
|
||||||
let dummy
|
let dummy
|
||||||
const obj = observable({ prop: 1 })
|
const obj = state({ prop: 1 })
|
||||||
const runner = effect(() => {
|
const runner = effect(() => {
|
||||||
dummy = obj.prop
|
dummy = obj.prop
|
||||||
})
|
})
|
||||||
@ -625,7 +625,7 @@ describe('observer/effect', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('markNonReactive', () => {
|
it('markNonReactive', () => {
|
||||||
const obj = observable({
|
const obj = state({
|
||||||
foo: markNonReactive({
|
foo: markNonReactive({
|
||||||
prop: 0
|
prop: 0
|
||||||
})
|
})
|
@ -1,9 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
observable,
|
state,
|
||||||
immutable,
|
immutableState,
|
||||||
unwrap,
|
toRaw,
|
||||||
isObservable,
|
isState,
|
||||||
isImmutable,
|
isImmutableState,
|
||||||
markNonReactive,
|
markNonReactive,
|
||||||
markImmutable,
|
markImmutable,
|
||||||
lock,
|
lock,
|
||||||
@ -26,16 +26,16 @@ describe('observer/immutable', () => {
|
|||||||
describe('Object', () => {
|
describe('Object', () => {
|
||||||
it('should make nested values immutable', () => {
|
it('should make nested values immutable', () => {
|
||||||
const original = { foo: 1, bar: { baz: 2 } }
|
const original = { foo: 1, bar: { baz: 2 } }
|
||||||
const observed = immutable(original)
|
const observed = immutableState(original)
|
||||||
expect(observed).not.toBe(original)
|
expect(observed).not.toBe(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(isImmutable(observed)).toBe(true)
|
expect(isImmutableState(observed)).toBe(true)
|
||||||
expect(isObservable(original)).toBe(false)
|
expect(isState(original)).toBe(false)
|
||||||
expect(isImmutable(original)).toBe(false)
|
expect(isImmutableState(original)).toBe(false)
|
||||||
expect(isObservable(observed.bar)).toBe(true)
|
expect(isState(observed.bar)).toBe(true)
|
||||||
expect(isImmutable(observed.bar)).toBe(true)
|
expect(isImmutableState(observed.bar)).toBe(true)
|
||||||
expect(isObservable(original.bar)).toBe(false)
|
expect(isState(original.bar)).toBe(false)
|
||||||
expect(isImmutable(original.bar)).toBe(false)
|
expect(isImmutableState(original.bar)).toBe(false)
|
||||||
// get
|
// get
|
||||||
expect(observed.foo).toBe(1)
|
expect(observed.foo).toBe(1)
|
||||||
// has
|
// has
|
||||||
@ -45,7 +45,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should not allow mutation', () => {
|
it('should not allow mutation', () => {
|
||||||
const observed = immutable({ foo: 1, bar: { baz: 2 } })
|
const observed = immutableState({ foo: 1, bar: { baz: 2 } })
|
||||||
observed.foo = 2
|
observed.foo = 2
|
||||||
expect(observed.foo).toBe(1)
|
expect(observed.foo).toBe(1)
|
||||||
expect(warn).toHaveBeenCalledTimes(1)
|
expect(warn).toHaveBeenCalledTimes(1)
|
||||||
@ -61,7 +61,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should allow mutation when unlocked', () => {
|
it('should allow mutation when unlocked', () => {
|
||||||
const observed: any = immutable({ foo: 1, bar: { baz: 2 } })
|
const observed: any = immutableState({ foo: 1, bar: { baz: 2 } })
|
||||||
unlock()
|
unlock()
|
||||||
observed.prop = 2
|
observed.prop = 2
|
||||||
observed.bar.qux = 3
|
observed.bar.qux = 3
|
||||||
@ -76,7 +76,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should not trigger effects when locked', () => {
|
it('should not trigger effects when locked', () => {
|
||||||
const observed = immutable({ a: 1 })
|
const observed = immutableState({ a: 1 })
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = observed.a
|
dummy = observed.a
|
||||||
@ -88,7 +88,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should trigger effects when unlocked', () => {
|
it('should trigger effects when unlocked', () => {
|
||||||
const observed = immutable({ a: 1 })
|
const observed = immutableState({ a: 1 })
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = observed.a
|
dummy = observed.a
|
||||||
@ -105,16 +105,16 @@ describe('observer/immutable', () => {
|
|||||||
describe('Array', () => {
|
describe('Array', () => {
|
||||||
it('should make nested values immutable', () => {
|
it('should make nested values immutable', () => {
|
||||||
const original: any[] = [{ foo: 1 }]
|
const original: any[] = [{ foo: 1 }]
|
||||||
const observed = immutable(original)
|
const observed = immutableState(original)
|
||||||
expect(observed).not.toBe(original)
|
expect(observed).not.toBe(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(isImmutable(observed)).toBe(true)
|
expect(isImmutableState(observed)).toBe(true)
|
||||||
expect(isObservable(original)).toBe(false)
|
expect(isState(original)).toBe(false)
|
||||||
expect(isImmutable(original)).toBe(false)
|
expect(isImmutableState(original)).toBe(false)
|
||||||
expect(isObservable(observed[0])).toBe(true)
|
expect(isState(observed[0])).toBe(true)
|
||||||
expect(isImmutable(observed[0])).toBe(true)
|
expect(isImmutableState(observed[0])).toBe(true)
|
||||||
expect(isObservable(original[0])).toBe(false)
|
expect(isState(original[0])).toBe(false)
|
||||||
expect(isImmutable(original[0])).toBe(false)
|
expect(isImmutableState(original[0])).toBe(false)
|
||||||
// get
|
// get
|
||||||
expect(observed[0].foo).toBe(1)
|
expect(observed[0].foo).toBe(1)
|
||||||
// has
|
// has
|
||||||
@ -124,7 +124,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should not allow mutation', () => {
|
it('should not allow mutation', () => {
|
||||||
const observed: any = immutable([{ foo: 1 }])
|
const observed: any = immutableState([{ foo: 1 }])
|
||||||
observed[0] = 1
|
observed[0] = 1
|
||||||
expect(observed[0]).not.toBe(1)
|
expect(observed[0]).not.toBe(1)
|
||||||
expect(warn).toHaveBeenCalledTimes(1)
|
expect(warn).toHaveBeenCalledTimes(1)
|
||||||
@ -146,7 +146,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should allow mutation when unlocked', () => {
|
it('should allow mutation when unlocked', () => {
|
||||||
const observed: any[] = immutable([{ foo: 1, bar: { baz: 2 } }])
|
const observed: any[] = immutableState([{ foo: 1, bar: { baz: 2 } }])
|
||||||
unlock()
|
unlock()
|
||||||
observed[1] = 2
|
observed[1] = 2
|
||||||
observed.push(3)
|
observed.push(3)
|
||||||
@ -162,7 +162,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should not trigger effects when locked', () => {
|
it('should not trigger effects when locked', () => {
|
||||||
const observed = immutable([{ a: 1 }])
|
const observed = immutableState([{ a: 1 }])
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = observed[0].a
|
dummy = observed[0].a
|
||||||
@ -177,7 +177,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should trigger effects when unlocked', () => {
|
it('should trigger effects when unlocked', () => {
|
||||||
const observed = immutable([{ a: 1 }])
|
const observed = immutableState([{ a: 1 }])
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
dummy = observed[0].a
|
dummy = observed[0].a
|
||||||
@ -208,20 +208,20 @@ describe('observer/immutable', () => {
|
|||||||
const key1 = {}
|
const key1 = {}
|
||||||
const key2 = {}
|
const key2 = {}
|
||||||
const original = new Collection([[key1, {}], [key2, {}]])
|
const original = new Collection([[key1, {}], [key2, {}]])
|
||||||
const observed = immutable(original)
|
const observed = immutableState(original)
|
||||||
expect(observed).not.toBe(original)
|
expect(observed).not.toBe(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(isImmutable(observed)).toBe(true)
|
expect(isImmutableState(observed)).toBe(true)
|
||||||
expect(isObservable(original)).toBe(false)
|
expect(isState(original)).toBe(false)
|
||||||
expect(isImmutable(original)).toBe(false)
|
expect(isImmutableState(original)).toBe(false)
|
||||||
expect(isObservable(observed.get(key1))).toBe(true)
|
expect(isState(observed.get(key1))).toBe(true)
|
||||||
expect(isImmutable(observed.get(key1))).toBe(true)
|
expect(isImmutableState(observed.get(key1))).toBe(true)
|
||||||
expect(isObservable(original.get(key1))).toBe(false)
|
expect(isState(original.get(key1))).toBe(false)
|
||||||
expect(isImmutable(original.get(key1))).toBe(false)
|
expect(isImmutableState(original.get(key1))).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('should not allow mutation & not trigger effect', () => {
|
test('should not allow mutation & not trigger effect', () => {
|
||||||
const map = immutable(new Collection())
|
const map = immutableState(new Collection())
|
||||||
const key = {}
|
const key = {}
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
@ -235,7 +235,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('should allow mutation & trigger effect when unlocked', () => {
|
test('should allow mutation & trigger effect when unlocked', () => {
|
||||||
const map = immutable(new Collection())
|
const map = immutableState(new Collection())
|
||||||
const isWeak = Collection === WeakMap
|
const isWeak = Collection === WeakMap
|
||||||
const key = {}
|
const key = {}
|
||||||
let dummy
|
let dummy
|
||||||
@ -256,16 +256,16 @@ describe('observer/immutable', () => {
|
|||||||
const key1 = {}
|
const key1 = {}
|
||||||
const key2 = {}
|
const key2 = {}
|
||||||
const original = new Collection([[key1, {}], [key2, {}]])
|
const original = new Collection([[key1, {}], [key2, {}]])
|
||||||
const observed = immutable(original)
|
const observed = immutableState(original)
|
||||||
for (const [key, value] of observed) {
|
for (const [key, value] of observed) {
|
||||||
expect(isImmutable(key)).toBe(true)
|
expect(isImmutableState(key)).toBe(true)
|
||||||
expect(isImmutable(value)).toBe(true)
|
expect(isImmutableState(value)).toBe(true)
|
||||||
}
|
}
|
||||||
observed.forEach((value: any) => {
|
observed.forEach((value: any) => {
|
||||||
expect(isImmutable(value)).toBe(true)
|
expect(isImmutableState(value)).toBe(true)
|
||||||
})
|
})
|
||||||
for (const value of observed.values()) {
|
for (const value of observed.values()) {
|
||||||
expect(isImmutable(value)).toBe(true)
|
expect(isImmutableState(value)).toBe(true)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -279,18 +279,18 @@ describe('observer/immutable', () => {
|
|||||||
const key1 = {}
|
const key1 = {}
|
||||||
const key2 = {}
|
const key2 = {}
|
||||||
const original = new Collection([key1, key2])
|
const original = new Collection([key1, key2])
|
||||||
const observed = immutable(original)
|
const observed = immutableState(original)
|
||||||
expect(observed).not.toBe(original)
|
expect(observed).not.toBe(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(isImmutable(observed)).toBe(true)
|
expect(isImmutableState(observed)).toBe(true)
|
||||||
expect(isObservable(original)).toBe(false)
|
expect(isState(original)).toBe(false)
|
||||||
expect(isImmutable(original)).toBe(false)
|
expect(isImmutableState(original)).toBe(false)
|
||||||
expect(observed.has(observable(key1))).toBe(true)
|
expect(observed.has(state(key1))).toBe(true)
|
||||||
expect(original.has(observable(key1))).toBe(false)
|
expect(original.has(state(key1))).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('should not allow mutation & not trigger effect', () => {
|
test('should not allow mutation & not trigger effect', () => {
|
||||||
const set = immutable(new Collection())
|
const set = immutableState(new Collection())
|
||||||
const key = {}
|
const key = {}
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
@ -304,7 +304,7 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('should allow mutation & trigger effect when unlocked', () => {
|
test('should allow mutation & trigger effect when unlocked', () => {
|
||||||
const set = immutable(new Collection())
|
const set = immutableState(new Collection())
|
||||||
const key = {}
|
const key = {}
|
||||||
let dummy
|
let dummy
|
||||||
effect(() => {
|
effect(() => {
|
||||||
@ -322,19 +322,19 @@ describe('observer/immutable', () => {
|
|||||||
if (Collection === Set) {
|
if (Collection === Set) {
|
||||||
test('should retrive immutable values on iteration', () => {
|
test('should retrive immutable values on iteration', () => {
|
||||||
const original = new Collection([{}, {}])
|
const original = new Collection([{}, {}])
|
||||||
const observed = immutable(original)
|
const observed = immutableState(original)
|
||||||
for (const value of observed) {
|
for (const value of observed) {
|
||||||
expect(isImmutable(value)).toBe(true)
|
expect(isImmutableState(value)).toBe(true)
|
||||||
}
|
}
|
||||||
observed.forEach((value: any) => {
|
observed.forEach((value: any) => {
|
||||||
expect(isImmutable(value)).toBe(true)
|
expect(isImmutableState(value)).toBe(true)
|
||||||
})
|
})
|
||||||
for (const value of observed.values()) {
|
for (const value of observed.values()) {
|
||||||
expect(isImmutable(value)).toBe(true)
|
expect(isImmutableState(value)).toBe(true)
|
||||||
}
|
}
|
||||||
for (const [v1, v2] of observed.entries()) {
|
for (const [v1, v2] of observed.entries()) {
|
||||||
expect(isImmutable(v1)).toBe(true)
|
expect(isImmutableState(v1)).toBe(true)
|
||||||
expect(isImmutable(v2)).toBe(true)
|
expect(isImmutableState(v2)).toBe(true)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -342,52 +342,52 @@ describe('observer/immutable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('calling observable on an immutable should return immutable', () => {
|
test('calling observable on an immutable should return immutable', () => {
|
||||||
const a = immutable()
|
const a = immutableState()
|
||||||
const b = observable(a)
|
const b = state(a)
|
||||||
expect(isImmutable(b)).toBe(true)
|
expect(isImmutableState(b)).toBe(true)
|
||||||
// should point to same original
|
// should point to same original
|
||||||
expect(unwrap(a)).toBe(unwrap(b))
|
expect(toRaw(a)).toBe(toRaw(b))
|
||||||
})
|
})
|
||||||
|
|
||||||
test('calling immutable on an observable should return immutable', () => {
|
test('calling immutable on an observable should return immutable', () => {
|
||||||
const a = observable()
|
const a = state()
|
||||||
const b = immutable(a)
|
const b = immutableState(a)
|
||||||
expect(isImmutable(b)).toBe(true)
|
expect(isImmutableState(b)).toBe(true)
|
||||||
// should point to same original
|
// should point to same original
|
||||||
expect(unwrap(a)).toBe(unwrap(b))
|
expect(toRaw(a)).toBe(toRaw(b))
|
||||||
})
|
})
|
||||||
|
|
||||||
test('observing already observed value should return same Proxy', () => {
|
test('observing already observed value should return same Proxy', () => {
|
||||||
const original = { foo: 1 }
|
const original = { foo: 1 }
|
||||||
const observed = immutable(original)
|
const observed = immutableState(original)
|
||||||
const observed2 = immutable(observed)
|
const observed2 = immutableState(observed)
|
||||||
expect(observed2).toBe(observed)
|
expect(observed2).toBe(observed)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('observing the same value multiple times should return same Proxy', () => {
|
test('observing the same value multiple times should return same Proxy', () => {
|
||||||
const original = { foo: 1 }
|
const original = { foo: 1 }
|
||||||
const observed = immutable(original)
|
const observed = immutableState(original)
|
||||||
const observed2 = immutable(original)
|
const observed2 = immutableState(original)
|
||||||
expect(observed2).toBe(observed)
|
expect(observed2).toBe(observed)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('markNonReactive', () => {
|
test('markNonReactive', () => {
|
||||||
const obj = immutable({
|
const obj = immutableState({
|
||||||
foo: { a: 1 },
|
foo: { a: 1 },
|
||||||
bar: markNonReactive({ b: 2 })
|
bar: markNonReactive({ b: 2 })
|
||||||
})
|
})
|
||||||
expect(isObservable(obj.foo)).toBe(true)
|
expect(isState(obj.foo)).toBe(true)
|
||||||
expect(isObservable(obj.bar)).toBe(false)
|
expect(isState(obj.bar)).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('markImmutable', () => {
|
test('markImmutable', () => {
|
||||||
const obj = observable({
|
const obj = state({
|
||||||
foo: { a: 1 },
|
foo: { a: 1 },
|
||||||
bar: markImmutable({ b: 2 })
|
bar: markImmutable({ b: 2 })
|
||||||
})
|
})
|
||||||
expect(isObservable(obj.foo)).toBe(true)
|
expect(isState(obj.foo)).toBe(true)
|
||||||
expect(isObservable(obj.bar)).toBe(true)
|
expect(isState(obj.bar)).toBe(true)
|
||||||
expect(isImmutable(obj.foo)).toBe(false)
|
expect(isImmutableState(obj.foo)).toBe(false)
|
||||||
expect(isImmutable(obj.bar)).toBe(true)
|
expect(isImmutableState(obj.bar)).toBe(true)
|
||||||
})
|
})
|
||||||
})
|
})
|
@ -1,12 +1,12 @@
|
|||||||
import { observable, isObservable, unwrap, markNonReactive } from '../src/index'
|
import { state, isState, toRaw, markNonReactive } from '../src/index'
|
||||||
|
|
||||||
describe('observer/observable', () => {
|
describe('observer/observable', () => {
|
||||||
test('Object', () => {
|
test('Object', () => {
|
||||||
const original = { foo: 1 }
|
const original = { foo: 1 }
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
expect(observed).not.toBe(original)
|
expect(observed).not.toBe(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(isObservable(original)).toBe(false)
|
expect(isState(original)).toBe(false)
|
||||||
// get
|
// get
|
||||||
expect(observed.foo).toBe(1)
|
expect(observed.foo).toBe(1)
|
||||||
// has
|
// has
|
||||||
@ -17,11 +17,11 @@ describe('observer/observable', () => {
|
|||||||
|
|
||||||
test('Array', () => {
|
test('Array', () => {
|
||||||
const original: any[] = [{ foo: 1 }]
|
const original: any[] = [{ foo: 1 }]
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
expect(observed).not.toBe(original)
|
expect(observed).not.toBe(original)
|
||||||
expect(isObservable(observed)).toBe(true)
|
expect(isState(observed)).toBe(true)
|
||||||
expect(isObservable(original)).toBe(false)
|
expect(isState(original)).toBe(false)
|
||||||
expect(isObservable(observed[0])).toBe(true)
|
expect(isState(observed[0])).toBe(true)
|
||||||
// get
|
// get
|
||||||
expect(observed[0].foo).toBe(1)
|
expect(observed[0].foo).toBe(1)
|
||||||
// has
|
// has
|
||||||
@ -32,9 +32,9 @@ describe('observer/observable', () => {
|
|||||||
|
|
||||||
test('cloned observable Array should point to observed values', () => {
|
test('cloned observable Array should point to observed values', () => {
|
||||||
const original = [{ foo: 1 }]
|
const original = [{ foo: 1 }]
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
const clone = observed.slice()
|
const clone = observed.slice()
|
||||||
expect(isObservable(clone[0])).toBe(true)
|
expect(isState(clone[0])).toBe(true)
|
||||||
expect(clone[0]).not.toBe(original[0])
|
expect(clone[0]).not.toBe(original[0])
|
||||||
expect(clone[0]).toBe(observed[0])
|
expect(clone[0]).toBe(observed[0])
|
||||||
})
|
})
|
||||||
@ -46,15 +46,15 @@ describe('observer/observable', () => {
|
|||||||
},
|
},
|
||||||
array: [{ bar: 2 }]
|
array: [{ bar: 2 }]
|
||||||
}
|
}
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
expect(isObservable(observed.nested)).toBe(true)
|
expect(isState(observed.nested)).toBe(true)
|
||||||
expect(isObservable(observed.array)).toBe(true)
|
expect(isState(observed.array)).toBe(true)
|
||||||
expect(isObservable(observed.array[0])).toBe(true)
|
expect(isState(observed.array[0])).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('observed value should proxy mutations to original (Object)', () => {
|
test('observed value should proxy mutations to original (Object)', () => {
|
||||||
const original: any = { foo: 1 }
|
const original: any = { foo: 1 }
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
// set
|
// set
|
||||||
observed.bar = 1
|
observed.bar = 1
|
||||||
expect(observed.bar).toBe(1)
|
expect(observed.bar).toBe(1)
|
||||||
@ -67,10 +67,10 @@ describe('observer/observable', () => {
|
|||||||
|
|
||||||
test('observed value should proxy mutations to original (Array)', () => {
|
test('observed value should proxy mutations to original (Array)', () => {
|
||||||
const original: any[] = [{ foo: 1 }, { bar: 2 }]
|
const original: any[] = [{ foo: 1 }, { bar: 2 }]
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
// set
|
// set
|
||||||
const value = { baz: 3 }
|
const value = { baz: 3 }
|
||||||
const observableValue = observable(value)
|
const observableValue = state(value)
|
||||||
observed[0] = value
|
observed[0] = value
|
||||||
expect(observed[0]).toBe(observableValue)
|
expect(observed[0]).toBe(observableValue)
|
||||||
expect(original[0]).toBe(value)
|
expect(original[0]).toBe(value)
|
||||||
@ -85,32 +85,32 @@ describe('observer/observable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('setting a property with an unobserved value should wrap with observable', () => {
|
test('setting a property with an unobserved value should wrap with observable', () => {
|
||||||
const observed: any = observable({})
|
const observed: any = state({})
|
||||||
const raw = {}
|
const raw = {}
|
||||||
observed.foo = raw
|
observed.foo = raw
|
||||||
expect(observed.foo).not.toBe(raw)
|
expect(observed.foo).not.toBe(raw)
|
||||||
expect(isObservable(observed.foo)).toBe(true)
|
expect(isState(observed.foo)).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('observing already observed value should return same Proxy', () => {
|
test('observing already observed value should return same Proxy', () => {
|
||||||
const original = { foo: 1 }
|
const original = { foo: 1 }
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
const observed2 = observable(observed)
|
const observed2 = state(observed)
|
||||||
expect(observed2).toBe(observed)
|
expect(observed2).toBe(observed)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('observing the same value multiple times should return same Proxy', () => {
|
test('observing the same value multiple times should return same Proxy', () => {
|
||||||
const original = { foo: 1 }
|
const original = { foo: 1 }
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
const observed2 = observable(original)
|
const observed2 = state(original)
|
||||||
expect(observed2).toBe(observed)
|
expect(observed2).toBe(observed)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('should not pollute original object with Proxies', () => {
|
test('should not pollute original object with Proxies', () => {
|
||||||
const original: any = { foo: 1 }
|
const original: any = { foo: 1 }
|
||||||
const original2 = { bar: 2 }
|
const original2 = { bar: 2 }
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
const observed2 = observable(original2)
|
const observed2 = state(original2)
|
||||||
observed.bar = observed2
|
observed.bar = observed2
|
||||||
expect(observed.bar).toBe(observed2)
|
expect(observed.bar).toBe(observed2)
|
||||||
expect(original.bar).toBe(original2)
|
expect(original.bar).toBe(original2)
|
||||||
@ -118,9 +118,9 @@ describe('observer/observable', () => {
|
|||||||
|
|
||||||
test('unwrap', () => {
|
test('unwrap', () => {
|
||||||
const original = { foo: 1 }
|
const original = { foo: 1 }
|
||||||
const observed = observable(original)
|
const observed = state(original)
|
||||||
expect(unwrap(observed)).toBe(original)
|
expect(toRaw(observed)).toBe(original)
|
||||||
expect(unwrap(original)).toBe(original)
|
expect(toRaw(original)).toBe(original)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('unobservable values', () => {
|
test('unobservable values', () => {
|
||||||
@ -132,7 +132,7 @@ describe('observer/observable', () => {
|
|||||||
|
|
||||||
const getMsg = (value: any) => `value is not observable: ${String(value)}`
|
const getMsg = (value: any) => `value is not observable: ${String(value)}`
|
||||||
const assertValue = (value: any) => {
|
const assertValue = (value: any) => {
|
||||||
observable(value)
|
state(value)
|
||||||
expect(lastMsg).toMatch(getMsg(value))
|
expect(lastMsg).toMatch(getMsg(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +146,7 @@ describe('observer/observable', () => {
|
|||||||
assertValue(null)
|
assertValue(null)
|
||||||
// undefined should work because it returns empty object observable
|
// undefined should work because it returns empty object observable
|
||||||
lastMsg = ''
|
lastMsg = ''
|
||||||
observable(undefined)
|
state(undefined)
|
||||||
expect(lastMsg).toBe('')
|
expect(lastMsg).toBe('')
|
||||||
// symbol
|
// symbol
|
||||||
const s = Symbol()
|
const s = Symbol()
|
||||||
@ -156,19 +156,19 @@ describe('observer/observable', () => {
|
|||||||
|
|
||||||
// built-ins should work and return same value
|
// built-ins should work and return same value
|
||||||
const p = Promise.resolve()
|
const p = Promise.resolve()
|
||||||
expect(observable(p)).toBe(p)
|
expect(state(p)).toBe(p)
|
||||||
const r = new RegExp('')
|
const r = new RegExp('')
|
||||||
expect(observable(r)).toBe(r)
|
expect(state(r)).toBe(r)
|
||||||
const d = new Date()
|
const d = new Date()
|
||||||
expect(observable(d)).toBe(d)
|
expect(state(d)).toBe(d)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('markNonReactive', () => {
|
test('markNonReactive', () => {
|
||||||
const obj = observable({
|
const obj = state({
|
||||||
foo: { a: 1 },
|
foo: { a: 1 },
|
||||||
bar: markNonReactive({ b: 2 })
|
bar: markNonReactive({ b: 2 })
|
||||||
})
|
})
|
||||||
expect(isObservable(obj.foo)).toBe(true)
|
expect(isState(obj.foo)).toBe(true)
|
||||||
expect(isObservable(obj.bar)).toBe(false)
|
expect(isState(obj.bar)).toBe(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
@ -1,5 +1,5 @@
|
|||||||
import { value } from '../src/value'
|
import { value } from '../src/value'
|
||||||
import { effect, observable } from '../src/index'
|
import { effect, state } from '../src/index'
|
||||||
|
|
||||||
describe('observer/value', () => {
|
describe('observer/value', () => {
|
||||||
it('should hold a value', () => {
|
it('should hold a value', () => {
|
||||||
@ -35,7 +35,7 @@ describe('observer/value', () => {
|
|||||||
|
|
||||||
it('should work like a normal property when nested in an observable', () => {
|
it('should work like a normal property when nested in an observable', () => {
|
||||||
const a = value(1)
|
const a = value(1)
|
||||||
const obj = observable({
|
const obj = state({
|
||||||
a,
|
a,
|
||||||
b: {
|
b: {
|
||||||
c: a,
|
c: a,
|
7
packages/reactivity/index.js
Normal file
7
packages/reactivity/index.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === 'production') {
|
||||||
|
module.exports = require('./dist/reactivity.cjs.prod.js.js')
|
||||||
|
} else {
|
||||||
|
module.exports = require('./dist/reactivity.cjs.js.js')
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "@vue/observer",
|
"name": "@vue/reactivity",
|
||||||
"version": "3.0.0-alpha.1",
|
"version": "3.0.0-alpha.1",
|
||||||
"description": "@vue/observer",
|
"description": "@vue/reactivity",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"module": "dist/observer.esm-bundler.js",
|
"module": "dist/reactivity.esm-bundler.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"unpkg": "dist/observer.global.js",
|
"unpkg": "dist/reactivity.global.js",
|
||||||
"sideEffects": false,
|
"sideEffects": false,
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -23,5 +23,5 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/vuejs/vue/issues"
|
"url": "https://github.com/vuejs/vue/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/vuejs/vue/tree/dev/packages/observer#readme"
|
"homepage": "https://github.com/vuejs/vue/tree/dev/packages/reactivity#readme"
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { observable, immutable, unwrap } from './index'
|
import { state, immutableState, toRaw } from './index'
|
||||||
import { OperationTypes } from './operations'
|
import { OperationTypes } from './operations'
|
||||||
import { track, trigger } from './effect'
|
import { track, trigger } from './effect'
|
||||||
import { LOCKED } from './lock'
|
import { LOCKED } from './lock'
|
||||||
@ -27,8 +27,8 @@ function createGetter(isImmutable: boolean) {
|
|||||||
? isImmutable
|
? isImmutable
|
||||||
? // need to lazy access immutable and observable here to avoid
|
? // need to lazy access immutable and observable here to avoid
|
||||||
// circular dependency
|
// circular dependency
|
||||||
immutable(res)
|
immutableState(res)
|
||||||
: observable(res)
|
: state(res)
|
||||||
: res
|
: res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ function set(
|
|||||||
value: any,
|
value: any,
|
||||||
receiver: any
|
receiver: any
|
||||||
): boolean {
|
): boolean {
|
||||||
value = unwrap(value)
|
value = toRaw(value)
|
||||||
const hadKey = hasOwnProperty.call(target, key)
|
const hadKey = hasOwnProperty.call(target, key)
|
||||||
const oldValue = target[key]
|
const oldValue = target[key]
|
||||||
if (isValue(oldValue)) {
|
if (isValue(oldValue)) {
|
||||||
@ -48,7 +48,7 @@ function set(
|
|||||||
}
|
}
|
||||||
const result = Reflect.set(target, key, value, receiver)
|
const result = Reflect.set(target, key, value, receiver)
|
||||||
// don't trigger if target is something up in the prototype chain of original
|
// don't trigger if target is something up in the prototype chain of original
|
||||||
if (target === unwrap(receiver)) {
|
if (target === toRaw(receiver)) {
|
||||||
/* istanbul ignore else */
|
/* istanbul ignore else */
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
const extraInfo = { oldValue, newValue: value }
|
const extraInfo = { oldValue, newValue: value }
|
@ -1,16 +1,16 @@
|
|||||||
import { unwrap, observable, immutable } from './index'
|
import { toRaw, state, immutableState } from './index'
|
||||||
import { track, trigger } from './effect'
|
import { track, trigger } from './effect'
|
||||||
import { OperationTypes } from './operations'
|
import { OperationTypes } from './operations'
|
||||||
import { LOCKED } from './lock'
|
import { LOCKED } from './lock'
|
||||||
import { isObject } from '@vue/shared'
|
import { isObject } from '@vue/shared'
|
||||||
|
|
||||||
const toObservable = (value: any) =>
|
const toObservable = (value: any) => (isObject(value) ? state(value) : value)
|
||||||
isObject(value) ? observable(value) : value
|
const toImmutable = (value: any) =>
|
||||||
const toImmutable = (value: any) => (isObject(value) ? immutable(value) : value)
|
isObject(value) ? immutableState(value) : value
|
||||||
|
|
||||||
function get(target: any, key: any, wrap: (t: any) => any): any {
|
function get(target: any, key: any, wrap: (t: any) => any): any {
|
||||||
target = unwrap(target)
|
target = toRaw(target)
|
||||||
key = unwrap(key)
|
key = toRaw(key)
|
||||||
const proto: any = Reflect.getPrototypeOf(target)
|
const proto: any = Reflect.getPrototypeOf(target)
|
||||||
track(target, OperationTypes.GET, key)
|
track(target, OperationTypes.GET, key)
|
||||||
const res = proto.get.call(target, key)
|
const res = proto.get.call(target, key)
|
||||||
@ -18,23 +18,23 @@ function get(target: any, key: any, wrap: (t: any) => any): any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function has(key: any): boolean {
|
function has(key: any): boolean {
|
||||||
const target = unwrap(this)
|
const target = toRaw(this)
|
||||||
key = unwrap(key)
|
key = toRaw(key)
|
||||||
const proto: any = Reflect.getPrototypeOf(target)
|
const proto: any = Reflect.getPrototypeOf(target)
|
||||||
track(target, OperationTypes.HAS, key)
|
track(target, OperationTypes.HAS, key)
|
||||||
return proto.has.call(target, key)
|
return proto.has.call(target, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
function size(target: any) {
|
function size(target: any) {
|
||||||
target = unwrap(target)
|
target = toRaw(target)
|
||||||
const proto = Reflect.getPrototypeOf(target)
|
const proto = Reflect.getPrototypeOf(target)
|
||||||
track(target, OperationTypes.ITERATE)
|
track(target, OperationTypes.ITERATE)
|
||||||
return Reflect.get(proto, 'size', target)
|
return Reflect.get(proto, 'size', target)
|
||||||
}
|
}
|
||||||
|
|
||||||
function add(value: any) {
|
function add(value: any) {
|
||||||
value = unwrap(value)
|
value = toRaw(value)
|
||||||
const target = unwrap(this)
|
const target = toRaw(this)
|
||||||
const proto: any = Reflect.getPrototypeOf(this)
|
const proto: any = Reflect.getPrototypeOf(this)
|
||||||
const hadKey = proto.has.call(target, value)
|
const hadKey = proto.has.call(target, value)
|
||||||
const result = proto.add.call(target, value)
|
const result = proto.add.call(target, value)
|
||||||
@ -50,8 +50,8 @@ function add(value: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function set(key: any, value: any) {
|
function set(key: any, value: any) {
|
||||||
value = unwrap(value)
|
value = toRaw(value)
|
||||||
const target = unwrap(this)
|
const target = toRaw(this)
|
||||||
const proto: any = Reflect.getPrototypeOf(this)
|
const proto: any = Reflect.getPrototypeOf(this)
|
||||||
const hadKey = proto.has.call(target, key)
|
const hadKey = proto.has.call(target, key)
|
||||||
const oldValue = proto.get.call(target, key)
|
const oldValue = proto.get.call(target, key)
|
||||||
@ -77,7 +77,7 @@ function set(key: any, value: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function deleteEntry(key: any) {
|
function deleteEntry(key: any) {
|
||||||
const target = unwrap(this)
|
const target = toRaw(this)
|
||||||
const proto: any = Reflect.getPrototypeOf(this)
|
const proto: any = Reflect.getPrototypeOf(this)
|
||||||
const hadKey = proto.has.call(target, key)
|
const hadKey = proto.has.call(target, key)
|
||||||
const oldValue = proto.get ? proto.get.call(target, key) : undefined
|
const oldValue = proto.get ? proto.get.call(target, key) : undefined
|
||||||
@ -95,7 +95,7 @@ function deleteEntry(key: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function clear() {
|
function clear() {
|
||||||
const target = unwrap(this)
|
const target = toRaw(this)
|
||||||
const proto: any = Reflect.getPrototypeOf(this)
|
const proto: any = Reflect.getPrototypeOf(this)
|
||||||
const hadItems = target.size !== 0
|
const hadItems = target.size !== 0
|
||||||
const oldTarget = target instanceof Map ? new Map(target) : new Set(target)
|
const oldTarget = target instanceof Map ? new Map(target) : new Set(target)
|
||||||
@ -115,7 +115,7 @@ function clear() {
|
|||||||
function createForEach(isImmutable: boolean) {
|
function createForEach(isImmutable: boolean) {
|
||||||
return function forEach(callback: Function, thisArg?: any) {
|
return function forEach(callback: Function, thisArg?: any) {
|
||||||
const observed = this
|
const observed = this
|
||||||
const target = unwrap(observed)
|
const target = toRaw(observed)
|
||||||
const proto: any = Reflect.getPrototypeOf(target)
|
const proto: any = Reflect.getPrototypeOf(target)
|
||||||
const wrap = isImmutable ? toImmutable : toObservable
|
const wrap = isImmutable ? toImmutable : toObservable
|
||||||
track(target, OperationTypes.ITERATE)
|
track(target, OperationTypes.ITERATE)
|
||||||
@ -131,7 +131,7 @@ function createForEach(isImmutable: boolean) {
|
|||||||
|
|
||||||
function createIterableMethod(method: string | symbol, isImmutable: boolean) {
|
function createIterableMethod(method: string | symbol, isImmutable: boolean) {
|
||||||
return function(...args: any[]) {
|
return function(...args: any[]) {
|
||||||
const target = unwrap(this)
|
const target = toRaw(this)
|
||||||
const proto: any = Reflect.getPrototypeOf(target)
|
const proto: any = Reflect.getPrototypeOf(target)
|
||||||
const isPair =
|
const isPair =
|
||||||
method === 'entries' ||
|
method === 'entries' ||
|
||||||
@ -170,7 +170,7 @@ function createImmutableMethod(
|
|||||||
const key = args[0] ? `on key "${args[0]}"` : ``
|
const key = args[0] ? `on key "${args[0]}"` : ``
|
||||||
console.warn(
|
console.warn(
|
||||||
`${type} operation ${key}failed: target is immutable.`,
|
`${type} operation ${key}failed: target is immutable.`,
|
||||||
unwrap(this)
|
toRaw(this)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return type === OperationTypes.DELETE ? false : this
|
return type === OperationTypes.DELETE ? false : this
|
@ -25,7 +25,7 @@ export interface ReactiveEffectOptions {
|
|||||||
|
|
||||||
export type Scheduler = (run: () => any) => void
|
export type Scheduler = (run: () => any) => void
|
||||||
|
|
||||||
export type DebuggerEvent = {
|
export interface DebuggerEvent {
|
||||||
effect: ReactiveEffect
|
effect: ReactiveEffect
|
||||||
target: any
|
target: any
|
||||||
type: OperationTypes
|
type: OperationTypes
|
@ -46,14 +46,14 @@ const canObserve = (value: any): boolean => {
|
|||||||
|
|
||||||
type ObservableFactory = <T>(target?: T) => UnwrapValue<T>
|
type ObservableFactory = <T>(target?: T) => UnwrapValue<T>
|
||||||
|
|
||||||
export const observable = ((target: any = {}): any => {
|
export const state = ((target: any = {}): any => {
|
||||||
// if trying to observe an immutable proxy, return the immutable version.
|
// if trying to observe an immutable proxy, return the immutable version.
|
||||||
if (immutableToRaw.has(target)) {
|
if (immutableToRaw.has(target)) {
|
||||||
return target
|
return target
|
||||||
}
|
}
|
||||||
// target is explicitly marked as immutable by user
|
// target is explicitly marked as immutable by user
|
||||||
if (immutableValues.has(target)) {
|
if (immutableValues.has(target)) {
|
||||||
return immutable(target)
|
return immutableState(target)
|
||||||
}
|
}
|
||||||
return createObservable(
|
return createObservable(
|
||||||
target,
|
target,
|
||||||
@ -64,7 +64,7 @@ export const observable = ((target: any = {}): any => {
|
|||||||
)
|
)
|
||||||
}) as ObservableFactory
|
}) as ObservableFactory
|
||||||
|
|
||||||
export const immutable = ((target: any = {}): any => {
|
export const immutableState = ((target: any = {}): any => {
|
||||||
// value is a mutable observable, retrive its original and return
|
// value is a mutable observable, retrive its original and return
|
||||||
// a readonly version.
|
// a readonly version.
|
||||||
if (observedToRaw.has(target)) {
|
if (observedToRaw.has(target)) {
|
||||||
@ -141,15 +141,15 @@ export function stop(effect: ReactiveEffect) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isObservable(value: any): boolean {
|
export function isState(value: any): boolean {
|
||||||
return observedToRaw.has(value) || immutableToRaw.has(value)
|
return observedToRaw.has(value) || immutableToRaw.has(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isImmutable(value: any): boolean {
|
export function isImmutableState(value: any): boolean {
|
||||||
return immutableToRaw.has(value)
|
return immutableToRaw.has(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unwrap<T>(observed: T): T {
|
export function toRaw<T>(observed: T): T {
|
||||||
return observedToRaw.get(observed) || immutableToRaw.get(observed) || observed
|
return observedToRaw.get(observed) || immutableToRaw.get(observed) || observed
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
import { track, trigger } from './effect'
|
import { track, trigger } from './effect'
|
||||||
import { OperationTypes } from './operations'
|
import { OperationTypes } from './operations'
|
||||||
import { isObject } from '@vue/shared'
|
import { isObject } from '@vue/shared'
|
||||||
import { observable } from './index'
|
import { state } from './index'
|
||||||
|
|
||||||
export const knownValues = new WeakSet()
|
export const knownValues = new WeakSet()
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ export interface Value<T> {
|
|||||||
value: T
|
value: T
|
||||||
}
|
}
|
||||||
|
|
||||||
const convert = (val: any): any => (isObject(val) ? observable(val) : val)
|
const convert = (val: any): any => (isObject(val) ? state(val) : val)
|
||||||
|
|
||||||
export function value<T>(raw: T): Value<T> {
|
export function value<T>(raw: T): Value<T> {
|
||||||
raw = convert(raw)
|
raw = convert(raw)
|
@ -1,5 +1,5 @@
|
|||||||
import { createComponent } from '../src/component'
|
import { createComponent } from '../src/component'
|
||||||
import { value } from '@vue/observer'
|
import { value } from '@vue/reactivity'
|
||||||
import { PropType } from '../src/componentProps'
|
import { PropType } from '../src/componentProps'
|
||||||
|
|
||||||
test('createComponent type inference', () => {
|
test('createComponent type inference', () => {
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
export {
|
export {
|
||||||
value,
|
value,
|
||||||
isValue,
|
isValue,
|
||||||
observable,
|
state,
|
||||||
immutable,
|
isState,
|
||||||
isObservable,
|
immutableState,
|
||||||
isImmutable,
|
isImmutableState,
|
||||||
unwrap,
|
toRaw,
|
||||||
markImmutable,
|
markImmutable,
|
||||||
markNonReactive,
|
markNonReactive,
|
||||||
effect,
|
effect,
|
||||||
@ -17,13 +17,13 @@ export {
|
|||||||
Value,
|
Value,
|
||||||
ComputedValue,
|
ComputedValue,
|
||||||
UnwrapValue
|
UnwrapValue
|
||||||
} from '@vue/observer'
|
} from '@vue/reactivity'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
computed as _computed,
|
computed as _computed,
|
||||||
ComputedValue,
|
ComputedValue,
|
||||||
ReactiveEffect
|
ReactiveEffect
|
||||||
} from '@vue/observer'
|
} from '@vue/reactivity'
|
||||||
|
|
||||||
import { currentInstance } from './component'
|
import { currentInstance } from './component'
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ import {
|
|||||||
isValue,
|
isValue,
|
||||||
Value,
|
Value,
|
||||||
ReactiveEffectOptions
|
ReactiveEffectOptions
|
||||||
} from '@vue/observer'
|
} from '@vue/reactivity'
|
||||||
import { queueJob, queuePostFlushCb } from './scheduler'
|
import { queueJob, queuePostFlushCb } from './scheduler'
|
||||||
import { EMPTY_OBJ, isObject, isArray } from '@vue/shared'
|
import { EMPTY_OBJ, isObject, isArray } from '@vue/shared'
|
||||||
import { recordEffect } from './apiState'
|
import { recordEffect } from './apiState'
|
||||||
|
@ -2,9 +2,9 @@ import { VNode, normalizeVNode, VNodeChild } from './vnode'
|
|||||||
import {
|
import {
|
||||||
ReactiveEffect,
|
ReactiveEffect,
|
||||||
UnwrapValue,
|
UnwrapValue,
|
||||||
observable,
|
state,
|
||||||
immutable
|
immutableState
|
||||||
} from '@vue/observer'
|
} from '@vue/reactivity'
|
||||||
import { EMPTY_OBJ } from '@vue/shared'
|
import { EMPTY_OBJ } from '@vue/shared'
|
||||||
import { RenderProxyHandlers } from './componentProxy'
|
import { RenderProxyHandlers } from './componentProxy'
|
||||||
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'
|
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'
|
||||||
@ -151,9 +151,9 @@ export function setupStatefulComponent(instance: ComponentInstance) {
|
|||||||
// only need to create it if setup() actually expects it
|
// only need to create it if setup() actually expects it
|
||||||
// it will be updated in resolveProps() on updates before render
|
// it will be updated in resolveProps() on updates before render
|
||||||
const propsProxy = (instance.propsProxy = setup.length
|
const propsProxy = (instance.propsProxy = setup.length
|
||||||
? immutable(instance.props)
|
? immutableState(instance.props)
|
||||||
: null)
|
: null)
|
||||||
instance.state = observable(setup.call(proxy, propsProxy))
|
instance.state = state(setup.call(proxy, propsProxy))
|
||||||
currentInstance = null
|
currentInstance = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { immutable, unwrap, lock, unlock } from '@vue/observer'
|
import { immutableState, toRaw, lock, unlock } from '@vue/reactivity'
|
||||||
import {
|
import {
|
||||||
EMPTY_OBJ,
|
EMPTY_OBJ,
|
||||||
camelize,
|
camelize,
|
||||||
@ -140,7 +140,7 @@ export function resolveProps(
|
|||||||
}
|
}
|
||||||
// runtime validation
|
// runtime validation
|
||||||
if (__DEV__ && rawProps) {
|
if (__DEV__ && rawProps) {
|
||||||
validateProp(key, unwrap(rawProps[key]), opt, isAbsent)
|
validateProp(key, toRaw(rawProps[key]), opt, isAbsent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -152,7 +152,7 @@ export function resolveProps(
|
|||||||
// the props proxy
|
// the props proxy
|
||||||
const { patchFlag } = instance.vnode
|
const { patchFlag } = instance.vnode
|
||||||
if (propsProxy !== null && (patchFlag === 0 || patchFlag & FULL_PROPS)) {
|
if (propsProxy !== null && (patchFlag === 0 || patchFlag & FULL_PROPS)) {
|
||||||
const rawInitialProps = unwrap(propsProxy)
|
const rawInitialProps = toRaw(propsProxy)
|
||||||
for (const key in rawInitialProps) {
|
for (const key in rawInitialProps) {
|
||||||
if (!props.hasOwnProperty(key)) {
|
if (!props.hasOwnProperty(key)) {
|
||||||
delete propsProxy[key]
|
delete propsProxy[key]
|
||||||
@ -163,10 +163,10 @@ export function resolveProps(
|
|||||||
// lock immutable
|
// lock immutable
|
||||||
lock()
|
lock()
|
||||||
|
|
||||||
instance.props = __DEV__ ? immutable(props) : props
|
instance.props = __DEV__ ? immutableState(props) : props
|
||||||
instance.attrs = options
|
instance.attrs = options
|
||||||
? __DEV__
|
? __DEV__
|
||||||
? immutable(attrs)
|
? immutableState(attrs)
|
||||||
: attrs
|
: attrs
|
||||||
: instance.props
|
: instance.props
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ import {
|
|||||||
FULL_PROPS
|
FULL_PROPS
|
||||||
} from './patchFlags'
|
} from './patchFlags'
|
||||||
import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler'
|
import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler'
|
||||||
import { effect, stop, ReactiveEffectOptions } from '@vue/observer'
|
import { effect, stop, ReactiveEffectOptions } from '@vue/reactivity'
|
||||||
import { resolveProps } from './componentProps'
|
import { resolveProps } from './componentProps'
|
||||||
import { resolveSlots } from './componentSlots'
|
import { resolveSlots } from './componentSlots'
|
||||||
import {
|
import {
|
||||||
|
0
packages/runtime-core/src/errorHandling.ts
Normal file
0
packages/runtime-core/src/errorHandling.ts
Normal file
@ -23,7 +23,7 @@
|
|||||||
"@vue/runtime-core": ["packages/runtime-core/src"],
|
"@vue/runtime-core": ["packages/runtime-core/src"],
|
||||||
"@vue/runtime-dom": ["packages/runtime-dom/src"],
|
"@vue/runtime-dom": ["packages/runtime-dom/src"],
|
||||||
"@vue/runtime-test": ["packages/runtime-test/src"],
|
"@vue/runtime-test": ["packages/runtime-test/src"],
|
||||||
"@vue/observer": ["packages/observer/src"],
|
"@vue/reactivity": ["packages/reactivity/src"],
|
||||||
"@vue/compiler-core": ["packages/compiler-core/src"],
|
"@vue/compiler-core": ["packages/compiler-core/src"],
|
||||||
"@vue/server-renderer": ["packages/server-renderer/src"]
|
"@vue/server-renderer": ["packages/server-renderer/src"]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user