@formvk/reactive 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +186 -0
- package/dist/index.js +530 -0
- package/dist/index.js.map +1 -0
- package/package.json +21 -3
- package/src/__tests__/autorun.spec.ts +177 -0
- package/src/__tests__/batch.spec.ts +325 -0
- package/src/__tests__/computed.spec.ts +99 -0
- package/src/__tests__/decorator.spec.ts +439 -0
- package/src/__tests__/externals.spec.ts +146 -0
- package/src/__tests__/internals.spec.ts +42 -0
- package/src/__tests__/isReactive.spec.ts +38 -0
- package/src/__tests__/lazy.spec.ts +169 -0
- package/src/__tests__/observable.spec.ts +177 -0
- package/src/__tests__/observe.spec.ts +204 -0
- package/src/__tests__/reactive.spec.ts +201 -0
- package/src/__tests__/shallow.spec.ts +138 -0
- package/src/__tests__/shallowRef.spec.ts +129 -0
- package/src/__tests__/tracker.spec.ts +65 -0
- package/src/__tests__/untracked.spec.ts +56 -0
- package/src/action.ts +30 -0
- package/src/autorun.ts +57 -0
- package/src/batch.ts +120 -0
- package/src/computed.ts +42 -0
- package/src/decorator.ts +61 -0
- package/src/externals.ts +68 -0
- package/src/index.ts +25 -0
- package/src/internals.ts +177 -0
- package/src/observable.ts +49 -0
- package/src/observe.ts +25 -0
- package/src/reactive.ts +33 -0
- package/src/shallow.ts +33 -0
- package/src/shallowRef.ts +28 -0
- package/src/tracker.ts +39 -0
- package/src/types.ts +47 -0
- package/src/untracked.ts +10 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +4 -0
- package/vite.config.ts +39 -0
- package/README.md +0 -3
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { effect } from '@vue/reactivity'
|
|
2
|
+
import { expect, test, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { autorun } from '../autorun'
|
|
5
|
+
import { isObservable } from '../externals'
|
|
6
|
+
import { reactive } from '../reactive'
|
|
7
|
+
|
|
8
|
+
test('reactive() 创建深响应式对象,嵌套变更触发 effect', () => {
|
|
9
|
+
const state = reactive({ count: 0, nested: { value: 1 } })
|
|
10
|
+
const seen: number[] = []
|
|
11
|
+
|
|
12
|
+
effect(() => {
|
|
13
|
+
seen.push(state.count + state.nested.value)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
expect(seen).toEqual([1])
|
|
17
|
+
|
|
18
|
+
state.count = 1
|
|
19
|
+
expect(seen).toEqual([1, 2])
|
|
20
|
+
|
|
21
|
+
state.nested.value = 2
|
|
22
|
+
expect(seen).toEqual([1, 2, 3])
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
test('reactive() 数组变更触发 effect', () => {
|
|
26
|
+
const state = reactive({ list: [1, 2] })
|
|
27
|
+
const seen: number[] = []
|
|
28
|
+
|
|
29
|
+
effect(() => {
|
|
30
|
+
seen.push(state.list.reduce((sum, item) => sum + item, 0))
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
expect(seen).toEqual([3])
|
|
34
|
+
|
|
35
|
+
state.list.push(3)
|
|
36
|
+
expect(seen).toEqual([3, 6])
|
|
37
|
+
|
|
38
|
+
state.list[0] = 10
|
|
39
|
+
expect(seen).toEqual([3, 6, 15])
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('reactive() 对 null 与非对象输入原样返回', () => {
|
|
43
|
+
const date = new Date()
|
|
44
|
+
const skipped = { __v_skip: true, count: 1 }
|
|
45
|
+
|
|
46
|
+
expect(reactive(1)).toBe(1)
|
|
47
|
+
expect(reactive(null)).toBe(null)
|
|
48
|
+
expect(reactive(date)).toBe(date)
|
|
49
|
+
expect(reactive(skipped)).toBe(skipped)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('reactive() 不进入 formvk 代理层,isObservable 为 false', () => {
|
|
53
|
+
const state = reactive({ nested: { a: 1 } })
|
|
54
|
+
|
|
55
|
+
expect(isObservable(state)).toBe(false)
|
|
56
|
+
expect(isObservable(state.nested)).toBe(false)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('reactive() 不消费 formvk 的 registerSkip 规则', () => {
|
|
60
|
+
// 纯 Vue reactive 只认 Vue 自身的跳过标记,VNode 这类普通对象仍会被代理
|
|
61
|
+
const vnode = { __v_isVNode: true, type: 'div', props: { children: 'hello' } }
|
|
62
|
+
|
|
63
|
+
expect(reactive(vnode)).not.toBe(vnode)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('@reactive 装饰 accessor 字段:深层变更可响应', () => {
|
|
67
|
+
class Model {
|
|
68
|
+
@reactive accessor state = { count: 1 }
|
|
69
|
+
}
|
|
70
|
+
const model = new Model()
|
|
71
|
+
|
|
72
|
+
const handler = vi.fn()
|
|
73
|
+
autorun(() => handler(model.state.count))
|
|
74
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
75
|
+
|
|
76
|
+
model.state.count = 2
|
|
77
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('@reactive 装饰 accessor 字段:重新赋值后仍具响应性', () => {
|
|
81
|
+
class Model {
|
|
82
|
+
@reactive accessor state = { count: 1 }
|
|
83
|
+
}
|
|
84
|
+
const model = new Model()
|
|
85
|
+
|
|
86
|
+
model.state = { count: 2 }
|
|
87
|
+
|
|
88
|
+
const handler = vi.fn()
|
|
89
|
+
autorun(() => handler(model.state.count))
|
|
90
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
91
|
+
|
|
92
|
+
model.state.count = 3
|
|
93
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('@reactive 装饰普通字段:深层变更可响应', () => {
|
|
97
|
+
class Model {
|
|
98
|
+
@reactive field = { count: 1 }
|
|
99
|
+
}
|
|
100
|
+
const model = new Model()
|
|
101
|
+
|
|
102
|
+
const handler = vi.fn()
|
|
103
|
+
autorun(() => handler(model.field.count))
|
|
104
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
105
|
+
|
|
106
|
+
model.field.count = 2
|
|
107
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test('@reactive 装饰方法与取值器时原样透传', () => {
|
|
111
|
+
// 类型声明只覆盖 field/accessor,运行时对其他 kind 透传,故按既有约定转 any 验证运行时行为
|
|
112
|
+
const reactiveDecorator = reactive as any
|
|
113
|
+
|
|
114
|
+
class Store {
|
|
115
|
+
@reactiveDecorator
|
|
116
|
+
greet() {
|
|
117
|
+
return 'hello'
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@reactiveDecorator
|
|
121
|
+
get name() {
|
|
122
|
+
return 'formvk'
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const store = new Store()
|
|
127
|
+
expect(store.greet()).toBe('hello')
|
|
128
|
+
expect(store.name).toBe('formvk')
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('@reactive accessor 装饰基础类型:整体赋值可响应', () => {
|
|
132
|
+
const sym = Symbol('sym')
|
|
133
|
+
|
|
134
|
+
class PrimitiveStore {
|
|
135
|
+
@reactive accessor num = 1
|
|
136
|
+
@reactive accessor str = 'a'
|
|
137
|
+
@reactive accessor bool = false
|
|
138
|
+
@reactive accessor nil: object | null = null
|
|
139
|
+
@reactive accessor symValue: symbol = sym
|
|
140
|
+
@reactive accessor big = 10n
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const store = new PrimitiveStore()
|
|
144
|
+
expect(store.num).toBe(1)
|
|
145
|
+
expect(store.str).toBe('a')
|
|
146
|
+
expect(store.bool).toBe(false)
|
|
147
|
+
expect(store.nil).toBe(null)
|
|
148
|
+
expect(store.symValue).toBe(sym)
|
|
149
|
+
expect(store.big).toBe(10n)
|
|
150
|
+
|
|
151
|
+
const handler = vi.fn()
|
|
152
|
+
autorun(() => handler(store.num, store.str))
|
|
153
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
154
|
+
|
|
155
|
+
store.num = 2
|
|
156
|
+
store.str = 'b'
|
|
157
|
+
expect(handler).toHaveBeenCalledTimes(3)
|
|
158
|
+
expect(store.num).toBe(2)
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
test('@reactive 普通字段装饰基础类型:原样保留且赋值不具响应性', () => {
|
|
162
|
+
class RawFieldStore {
|
|
163
|
+
@reactive num = 1
|
|
164
|
+
@reactive str = 'hello'
|
|
165
|
+
@reactive flag = true
|
|
166
|
+
@reactive nothing = null
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const store = new RawFieldStore()
|
|
170
|
+
const handler = vi.fn()
|
|
171
|
+
autorun(() => handler(store.num))
|
|
172
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
173
|
+
|
|
174
|
+
// 普通字段装饰器只转换初始值,无法拦截后续赋值
|
|
175
|
+
store.num = 99
|
|
176
|
+
expect(store.num).toBe(99)
|
|
177
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test('@reactive accessor 装饰数组:深层变更可响应', () => {
|
|
181
|
+
class ArrayStore {
|
|
182
|
+
@reactive accessor list = [1, 2]
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const store = new ArrayStore()
|
|
186
|
+
const lengthHandler = vi.fn()
|
|
187
|
+
const itemHandler = vi.fn()
|
|
188
|
+
autorun(() => lengthHandler(store.list.length))
|
|
189
|
+
autorun(() => itemHandler(store.list[0]))
|
|
190
|
+
expect(lengthHandler).toHaveBeenCalledTimes(1)
|
|
191
|
+
expect(itemHandler).toHaveBeenCalledTimes(1)
|
|
192
|
+
|
|
193
|
+
// push 改变 length,但不影响下标 0 的元素
|
|
194
|
+
store.list.push(3)
|
|
195
|
+
expect(lengthHandler).toHaveBeenCalledTimes(2)
|
|
196
|
+
expect(itemHandler).toHaveBeenCalledTimes(1)
|
|
197
|
+
|
|
198
|
+
store.list[0] = 10
|
|
199
|
+
expect(itemHandler).toHaveBeenCalledTimes(2)
|
|
200
|
+
expect(store.list).toEqual([10, 2, 3])
|
|
201
|
+
})
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { effect } from '@vue/reactivity'
|
|
2
|
+
import { expect, test, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { autorun } from '../autorun'
|
|
5
|
+
import { shallow } from '../shallow'
|
|
6
|
+
|
|
7
|
+
test('shallow() 只有顶层变更触发 Vue effect', () => {
|
|
8
|
+
const state = shallow({ top: 0, nested: { count: 0 } })
|
|
9
|
+
const seen: number[] = []
|
|
10
|
+
|
|
11
|
+
effect(() => {
|
|
12
|
+
seen.push(state.top + state.nested.count)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
expect(seen).toEqual([0])
|
|
16
|
+
|
|
17
|
+
state.top = 1
|
|
18
|
+
expect(seen).toEqual([0, 1])
|
|
19
|
+
|
|
20
|
+
state.nested.count = 2
|
|
21
|
+
expect(seen).toEqual([0, 1])
|
|
22
|
+
|
|
23
|
+
state.nested = { count: 3 }
|
|
24
|
+
expect(seen).toEqual([0, 1, 4])
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('shallow() 非对象输入原样返回', () => {
|
|
28
|
+
const date = new Date()
|
|
29
|
+
|
|
30
|
+
expect(shallow(1)).toBe(1)
|
|
31
|
+
expect(shallow(null)).toBe(null)
|
|
32
|
+
expect(shallow(date)).toBe(date)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('shallow() 识别 Vue 原生 __v_skip 标记并原样返回', () => {
|
|
36
|
+
const skipped = { __v_skip: true, count: 1 }
|
|
37
|
+
|
|
38
|
+
expect(shallow(skipped)).toBe(skipped)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('shallow() 不消费 formvk 的 registerSkip 规则', () => {
|
|
42
|
+
// 纯 Vue shallowReactive 只认 Vue 自身的跳过标记,React 元素这类普通对象仍会被代理
|
|
43
|
+
const reactElement = {
|
|
44
|
+
$$typeof: Symbol.for('react.element'),
|
|
45
|
+
_owner: null,
|
|
46
|
+
type: 'div',
|
|
47
|
+
props: { children: 'hello' },
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
expect(shallow(reactElement)).not.toBe(reactElement)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test('@shallow 装饰 accessor 字段:仅整体替换触发,内层对象变更不触发', () => {
|
|
54
|
+
class Model {
|
|
55
|
+
@shallow accessor state = { nested: { count: 1 } }
|
|
56
|
+
}
|
|
57
|
+
const model = new Model()
|
|
58
|
+
const handler = vi.fn()
|
|
59
|
+
autorun(() => handler(model.state))
|
|
60
|
+
|
|
61
|
+
// 浅盒子的 .value 是原始内层对象,内层对象的任何变更(含其顶层属性)都不触发
|
|
62
|
+
model.state.nested.count = 2
|
|
63
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
64
|
+
|
|
65
|
+
model.state.nested = { count: 3 }
|
|
66
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
67
|
+
|
|
68
|
+
// 只有整体替换 accessor 才会写盒子的 value,触发响应;浅盒子不转换写入值,引用保持一致
|
|
69
|
+
const next = { nested: { count: 4 } }
|
|
70
|
+
model.state = next
|
|
71
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
72
|
+
expect(model.state).toBe(next)
|
|
73
|
+
expect(model.state.nested.count).toBe(4)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('@shallow 装饰普通字段只跟踪顶层', () => {
|
|
77
|
+
class Model {
|
|
78
|
+
@shallow field = { nested: { count: 1 } }
|
|
79
|
+
}
|
|
80
|
+
const model = new Model()
|
|
81
|
+
const handler = vi.fn()
|
|
82
|
+
autorun(() => handler(model.field.nested.count))
|
|
83
|
+
|
|
84
|
+
model.field.nested.count = 2
|
|
85
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
86
|
+
|
|
87
|
+
model.field.nested = { count: 3 }
|
|
88
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('@shallow 装饰方法与取值器时原样透传', () => {
|
|
92
|
+
// 类型声明只覆盖 field/accessor,运行时对其他 kind 透传,故按既有约定转 any 验证运行时行为
|
|
93
|
+
const shallowDecorator = shallow as any
|
|
94
|
+
|
|
95
|
+
class Store {
|
|
96
|
+
@shallowDecorator
|
|
97
|
+
ping() {
|
|
98
|
+
return 1
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
@shallowDecorator
|
|
102
|
+
get label() {
|
|
103
|
+
return 'shallow'
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const store = new Store()
|
|
108
|
+
expect(store.ping()).toBe(1)
|
|
109
|
+
expect(store.label).toBe('shallow')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('@shallow accessor 装饰数组:顶层替换响应,内层变更不响应', () => {
|
|
113
|
+
class ShallowStore {
|
|
114
|
+
@shallow accessor list = [{ n: 1 }]
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const store = new ShallowStore()
|
|
118
|
+
const handler = vi.fn()
|
|
119
|
+
autorun(() => handler(store.list.length))
|
|
120
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
121
|
+
|
|
122
|
+
store.list[0].n = 99
|
|
123
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
124
|
+
|
|
125
|
+
store.list = [{ n: 2 }]
|
|
126
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('@shallow 普通字段装饰基础类型时原样返回', () => {
|
|
130
|
+
class ShallowFieldStore {
|
|
131
|
+
@shallow num = 7
|
|
132
|
+
@shallow text = 'shallow'
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const store = new ShallowFieldStore()
|
|
136
|
+
expect(store.num).toBe(7)
|
|
137
|
+
expect(store.text).toBe('shallow')
|
|
138
|
+
})
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { effect, isRef } from '@vue/reactivity'
|
|
2
|
+
import { expect, test, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { autorun } from '../autorun'
|
|
5
|
+
import { shallowRef } from '../shallowRef'
|
|
6
|
+
|
|
7
|
+
test('shallowRef() 返回浅响应盒子并保留初始值', () => {
|
|
8
|
+
const initial = { count: 1 }
|
|
9
|
+
const box = shallowRef(initial)
|
|
10
|
+
|
|
11
|
+
expect(isRef(box)).toBe(true)
|
|
12
|
+
expect(box.value).toBe(initial)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
test('shallowRef() 包装基础类型:整体赋值触发 effect', () => {
|
|
16
|
+
const count = shallowRef(0)
|
|
17
|
+
const seen: number[] = []
|
|
18
|
+
|
|
19
|
+
effect(() => {
|
|
20
|
+
seen.push(count.value)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
count.value = 5
|
|
24
|
+
|
|
25
|
+
expect(seen).toEqual([0, 5])
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('shallowRef() 盒子整体替换触发 effect,深层变更不触发', () => {
|
|
29
|
+
const state = shallowRef({ count: 0 })
|
|
30
|
+
const seen: number[] = []
|
|
31
|
+
|
|
32
|
+
effect(() => {
|
|
33
|
+
seen.push(state.value.count)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
expect(seen).toEqual([0])
|
|
37
|
+
|
|
38
|
+
state.value = { count: 1 }
|
|
39
|
+
expect(seen).toEqual([0, 1])
|
|
40
|
+
|
|
41
|
+
state.value.count = 2
|
|
42
|
+
expect(seen).toEqual([0, 1])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('@shallowRef 装饰 accessor 字段不深层代理', () => {
|
|
46
|
+
const initial = { count: 1 }
|
|
47
|
+
|
|
48
|
+
class Model {
|
|
49
|
+
@shallowRef accessor state = initial
|
|
50
|
+
}
|
|
51
|
+
const model = new Model()
|
|
52
|
+
|
|
53
|
+
expect(model.state).toBe(initial)
|
|
54
|
+
|
|
55
|
+
const handler = vi.fn()
|
|
56
|
+
autorun(() => handler(model.state.count))
|
|
57
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
58
|
+
|
|
59
|
+
// 浅盒子:内部对象的深层变更不具响应性
|
|
60
|
+
model.state.count = 2
|
|
61
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
62
|
+
|
|
63
|
+
const next = { count: 3 }
|
|
64
|
+
model.state = next
|
|
65
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
66
|
+
expect(model.state).toBe(next)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('@shallowRef 装饰普通字段保持原值', () => {
|
|
70
|
+
const initial = { count: 1 }
|
|
71
|
+
|
|
72
|
+
class Model {
|
|
73
|
+
@shallowRef field = initial
|
|
74
|
+
}
|
|
75
|
+
const model = new Model()
|
|
76
|
+
|
|
77
|
+
expect(model.field).toBe(initial)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('@shallowRef 装饰方法与取值器时原样透传', () => {
|
|
81
|
+
// 类型声明只覆盖 field/accessor,运行时对其他 kind 透传,故按既有约定转 any 验证运行时行为
|
|
82
|
+
const shallowRefDecorator = shallowRef as any
|
|
83
|
+
|
|
84
|
+
class Store {
|
|
85
|
+
@shallowRefDecorator
|
|
86
|
+
ping() {
|
|
87
|
+
return 1
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@shallowRefDecorator
|
|
91
|
+
get label() {
|
|
92
|
+
return 'shallowRef'
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const store = new Store()
|
|
97
|
+
expect(store.ping()).toBe(1)
|
|
98
|
+
expect(store.label).toBe('shallowRef')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('@shallowRef accessor 装饰基础类型:整体赋值响应,不做深层处理', () => {
|
|
102
|
+
class RefStore {
|
|
103
|
+
@shallowRef accessor count = 0
|
|
104
|
+
@shallowRef accessor label = 'x'
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const store = new RefStore()
|
|
108
|
+
const handler = vi.fn()
|
|
109
|
+
autorun(() => handler(store.count))
|
|
110
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
111
|
+
|
|
112
|
+
store.count = 5
|
|
113
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
114
|
+
expect(store.count).toBe(5)
|
|
115
|
+
expect(store.label).toBe('x')
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('@shallowRef 普通字段装饰任意值均原样保留', () => {
|
|
119
|
+
const initial = { nested: { count: 1 } }
|
|
120
|
+
|
|
121
|
+
class RefFieldStore {
|
|
122
|
+
@shallowRef box = initial
|
|
123
|
+
@shallowRef num = 42
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const store = new RefFieldStore()
|
|
127
|
+
expect(store.box).toBe(initial)
|
|
128
|
+
expect(store.num).toBe(42)
|
|
129
|
+
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { expect, test, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { reactive } from '../reactive'
|
|
4
|
+
import { Tracker } from '../tracker'
|
|
5
|
+
|
|
6
|
+
test('Tracker.track 立即执行 view、返回结果并收集依赖', () => {
|
|
7
|
+
const state = reactive({ count: 1 })
|
|
8
|
+
const scheduler = vi.fn()
|
|
9
|
+
const tracker = new Tracker(scheduler)
|
|
10
|
+
|
|
11
|
+
const result = tracker.track(() => state.count * 10)
|
|
12
|
+
|
|
13
|
+
expect(result).toBe(10)
|
|
14
|
+
expect(scheduler).not.toHaveBeenCalled()
|
|
15
|
+
|
|
16
|
+
state.count = 2
|
|
17
|
+
expect(scheduler).toHaveBeenCalledTimes(1)
|
|
18
|
+
// 依赖变化只通知 scheduler,view 不会自动重跑
|
|
19
|
+
expect(tracker.track(() => state.count * 10)).toBe(20)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('Tracker 重复 track 时依赖以最近一次为准', () => {
|
|
23
|
+
const state = reactive({ a: 1, b: 10 })
|
|
24
|
+
const scheduler = vi.fn()
|
|
25
|
+
const tracker = new Tracker(scheduler)
|
|
26
|
+
|
|
27
|
+
tracker.track(() => state.a)
|
|
28
|
+
state.b = 20
|
|
29
|
+
expect(scheduler).not.toHaveBeenCalled()
|
|
30
|
+
|
|
31
|
+
// 切换为追踪 b
|
|
32
|
+
tracker.track(() => state.b)
|
|
33
|
+
state.a = 2
|
|
34
|
+
expect(scheduler).not.toHaveBeenCalled()
|
|
35
|
+
state.b = 30
|
|
36
|
+
expect(scheduler).toHaveBeenCalledTimes(1)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('Tracker.dispose 后不再收到通知', () => {
|
|
40
|
+
const state = reactive({ count: 0 })
|
|
41
|
+
const scheduler = vi.fn()
|
|
42
|
+
const tracker = new Tracker(scheduler)
|
|
43
|
+
|
|
44
|
+
tracker.track(() => state.count)
|
|
45
|
+
tracker.dispose()
|
|
46
|
+
state.count = 1
|
|
47
|
+
expect(scheduler).not.toHaveBeenCalled()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('Tracker 未传 scheduler 时依赖变化也不会自动重跑 view', () => {
|
|
51
|
+
const state = reactive({ count: 0 })
|
|
52
|
+
const reads: number[] = []
|
|
53
|
+
const tracker = new Tracker()
|
|
54
|
+
|
|
55
|
+
tracker.track(() => {
|
|
56
|
+
reads.push(state.count)
|
|
57
|
+
return state.count
|
|
58
|
+
})
|
|
59
|
+
state.count = 1
|
|
60
|
+
state.count = 2
|
|
61
|
+
expect(reads).toEqual([0])
|
|
62
|
+
|
|
63
|
+
// 手动再次 track 才能读到最新值
|
|
64
|
+
expect(tracker.track(() => state.count)).toBe(2)
|
|
65
|
+
})
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { expect, test, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { autorun } from '../autorun'
|
|
4
|
+
import { reactive } from '../reactive'
|
|
5
|
+
import { untracked } from '../untracked'
|
|
6
|
+
|
|
7
|
+
test('untracked 内的读取不建立依赖', () => {
|
|
8
|
+
const state = reactive({ tracked: 0, hidden: 0 })
|
|
9
|
+
const values: number[] = []
|
|
10
|
+
|
|
11
|
+
autorun(() => {
|
|
12
|
+
values.push(state.tracked + untracked(() => state.hidden))
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
expect(values).toEqual([0])
|
|
16
|
+
|
|
17
|
+
state.hidden = 10
|
|
18
|
+
expect(values).toEqual([0])
|
|
19
|
+
|
|
20
|
+
state.tracked = 1
|
|
21
|
+
expect(values).toEqual([0, 11])
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('untracked.bound 绑定上下文并透传参数', () => {
|
|
25
|
+
const state = reactive({ count: 1 })
|
|
26
|
+
const context = { multiplier: 3 }
|
|
27
|
+
|
|
28
|
+
const read = untracked.bound(function (this: typeof context, offset: number) {
|
|
29
|
+
return state.count * this.multiplier + offset
|
|
30
|
+
}, context)
|
|
31
|
+
|
|
32
|
+
expect(read(2)).toBe(5)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('untracked 无回调调用时只切换追踪开关,不抛错', () => {
|
|
36
|
+
expect(() => untracked()).not.toThrow()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('untracked 回调抛错时仍会恢复依赖收集', () => {
|
|
40
|
+
const state = reactive({ count: 0 })
|
|
41
|
+
const handler = vi.fn()
|
|
42
|
+
|
|
43
|
+
expect(() =>
|
|
44
|
+
untracked(() => {
|
|
45
|
+
throw new Error('boom')
|
|
46
|
+
})
|
|
47
|
+
).toThrow('boom')
|
|
48
|
+
|
|
49
|
+
autorun(() => {
|
|
50
|
+
handler(state.count)
|
|
51
|
+
})
|
|
52
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
53
|
+
|
|
54
|
+
state.count = 1
|
|
55
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
56
|
+
})
|
package/src/action.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { pauseTracking, resetTracking } from '@vue/reactivity'
|
|
2
|
+
|
|
3
|
+
import { batchEnd, batchScopeEnd, batchScopeStart, batchStart, createBoundary } from './batch'
|
|
4
|
+
import type { IAction } from './types'
|
|
5
|
+
|
|
6
|
+
/** action = batch + untrack:合并通知,同时不收集执行期间的响应式读取 */
|
|
7
|
+
export const action = Object.assign(
|
|
8
|
+
createBoundary(
|
|
9
|
+
() => {
|
|
10
|
+
batchStart()
|
|
11
|
+
pauseTracking()
|
|
12
|
+
},
|
|
13
|
+
() => {
|
|
14
|
+
resetTracking()
|
|
15
|
+
batchEnd()
|
|
16
|
+
}
|
|
17
|
+
),
|
|
18
|
+
{
|
|
19
|
+
scope: createBoundary(
|
|
20
|
+
() => {
|
|
21
|
+
batchScopeStart()
|
|
22
|
+
pauseTracking()
|
|
23
|
+
},
|
|
24
|
+
() => {
|
|
25
|
+
resetTracking()
|
|
26
|
+
batchScopeEnd()
|
|
27
|
+
}
|
|
28
|
+
),
|
|
29
|
+
}
|
|
30
|
+
) as IAction
|
package/src/autorun.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { effect, pauseTracking, resetTracking, stop } from '@vue/reactivity'
|
|
2
|
+
|
|
3
|
+
import { scheduleReaction } from './batch'
|
|
4
|
+
import type { Dispose, IReactionOptions } from './types'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 自动运行:立即执行 tracker,并追踪其中读取的响应式数据,
|
|
8
|
+
* 任意依赖变化都会整体重跑 tracker。
|
|
9
|
+
*/
|
|
10
|
+
export function autorun(tracker: () => void): Dispose {
|
|
11
|
+
const runner = effect(tracker, {
|
|
12
|
+
scheduler: () => scheduleReaction(runner),
|
|
13
|
+
})
|
|
14
|
+
return () => stop(runner)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 自定义运行:tracker 只负责求值并收集依赖,
|
|
19
|
+
* 依赖变化后重新求值,值发生变更才调用 subscriber。
|
|
20
|
+
*/
|
|
21
|
+
export function reaction<T>(
|
|
22
|
+
tracker: () => T,
|
|
23
|
+
subscriber?: (value: T, oldValue: T) => void,
|
|
24
|
+
options?: IReactionOptions<T>
|
|
25
|
+
): Dispose {
|
|
26
|
+
const equals = options?.equals ?? ((oldValue: T, newValue: T) => oldValue === newValue)
|
|
27
|
+
let oldValue = undefined as T
|
|
28
|
+
let firstRun = true
|
|
29
|
+
const runner = effect(
|
|
30
|
+
() => {
|
|
31
|
+
const newValue = tracker()
|
|
32
|
+
if (firstRun) {
|
|
33
|
+
firstRun = false
|
|
34
|
+
oldValue = newValue
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
if (equals(oldValue, newValue)) return
|
|
38
|
+
const prevValue = oldValue
|
|
39
|
+
oldValue = newValue
|
|
40
|
+
if (!subscriber) return
|
|
41
|
+
// subscriber 中的读取不属于 tracker 依赖
|
|
42
|
+
pauseTracking()
|
|
43
|
+
try {
|
|
44
|
+
subscriber(newValue, prevValue)
|
|
45
|
+
} finally {
|
|
46
|
+
resetTracking()
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
scheduler: () => scheduleReaction(runner),
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
if (options?.fireImmediately) {
|
|
54
|
+
subscriber?.(oldValue, oldValue)
|
|
55
|
+
}
|
|
56
|
+
return () => stop(runner)
|
|
57
|
+
}
|