@formvk/reactive 0.1.0 → 1.0.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.
@@ -1,42 +0,0 @@
1
- import { reactive, ref, shallowReactive } from '@vue/reactivity'
2
- import { expect, test, vi } from 'vitest'
3
-
4
- import { canProxy, registerSkip, wrap } from '../internals'
5
- import { observable } from '../observable'
6
- import { observe } from '../observe'
7
-
8
- test('canProxy 拒绝 ref、内置构造器实例与非对象', () => {
9
- expect(canProxy(ref(1))).toBe(false)
10
- expect(canProxy(new Date())).toBe(false)
11
- expect(canProxy(new Map())).toBe(false)
12
- expect(canProxy(1)).toBe(false)
13
- expect(canProxy(null)).toBe(false)
14
- expect(canProxy({})).toBe(true)
15
- })
16
-
17
- test('canProxy 命中自定义 skip 判定时拒绝,wrap 原样返回不代理', () => {
18
- const dispose = registerSkip(value => (value as any).__custom === true)
19
- const special = reactive({ __custom: true })
20
-
21
- expect(canProxy(special)).toBe(false)
22
- expect(wrap(special)).toBe(special)
23
-
24
- dispose()
25
- })
26
-
27
- test('wrap 的 get 对 ref 只解包不代理', () => {
28
- const count = ref(1)
29
- const proxy = wrap(shallowReactive({ count }))
30
-
31
- expect(proxy.count).toBe(1)
32
- })
33
-
34
- test('删除不存在的 key 不触发变更通知', () => {
35
- const state = observable({ a: 1 }) as any
36
- const handler = vi.fn()
37
- observe(state, handler)
38
-
39
- delete state.nonexistent
40
-
41
- expect(handler).not.toHaveBeenCalled()
42
- })
@@ -1,38 +0,0 @@
1
- import { toRaw } from '@vue/reactivity'
2
- import { expect, test } from 'vitest'
3
-
4
- import { computed, isReactive, reactive, shallow, shallowRef } from '../index'
5
-
6
- test('isReactive 识别 reactive 对象与懒访问产生的嵌套代理', () => {
7
- const state = reactive({ nested: { a: 1 }, list: [{ id: 1 }] })
8
-
9
- expect(isReactive(state)).toBe(true)
10
- expect(isReactive(state.nested)).toBe(true)
11
- expect(isReactive(state.list)).toBe(true)
12
- expect(isReactive(state.list[0])).toBe(true)
13
- })
14
-
15
- test('isReactive 识别 shallowReactive 根对象', () => {
16
- expect(isReactive(shallow({ a: 1 }))).toBe(true)
17
- })
18
-
19
- test('isReactive 对普通对象与原始值返回 false', () => {
20
- expect(isReactive({})).toBe(false)
21
- expect(isReactive([])).toBe(false)
22
- expect(isReactive(1)).toBe(false)
23
- expect(isReactive('x')).toBe(false)
24
- expect(isReactive(true)).toBe(false)
25
- expect(isReactive(null)).toBe(false)
26
- expect(isReactive(undefined)).toBe(false)
27
- })
28
-
29
- test('isReactive 不认可 ref / shallowRef / computed 盒子', () => {
30
- expect(isReactive(shallowRef(1))).toBe(false)
31
- expect(isReactive(computed(() => 1))).toBe(false)
32
- })
33
-
34
- test('isReactive 对 toRaw 还原后的原始对象返回 false', () => {
35
- const state = reactive({ a: 1 })
36
-
37
- expect(isReactive(toRaw(state))).toBe(false)
38
- })
@@ -1,169 +0,0 @@
1
- import { expect, test, vi } from 'vitest'
2
-
3
- import { accessorDecorator } from '../decorator'
4
- import { autorun, observable, observe, reactive, shallowRef } from '../index'
5
-
6
- test('懒加载:构造期不创建盒子,首次读取才创建且只创建一次', () => {
7
- const createBox = vi.fn((initial: unknown) => ({ value: initial }))
8
- const lazy = accessorDecorator(createBox)
9
-
10
- class Model {
11
- @lazy accessor x = 1
12
- }
13
-
14
- expect(createBox).not.toHaveBeenCalled()
15
- const model = new Model()
16
- expect(createBox).not.toHaveBeenCalled()
17
-
18
- expect(model.x).toBe(1)
19
- expect(createBox).toHaveBeenCalledTimes(1)
20
- expect(createBox).toHaveBeenLastCalledWith(1)
21
-
22
- // 后续读取复用同一盒子,不重建
23
- expect(model.x).toBe(1)
24
- expect(createBox).toHaveBeenCalledTimes(1)
25
-
26
- // 写入只走盒子的 value 赋值,也不重建
27
- model.x = 2
28
- expect(createBox).toHaveBeenCalledTimes(1)
29
- expect(model.x).toBe(2)
30
- })
31
-
32
- test('懒加载:首次访问是写入时,直接以新值建盒', () => {
33
- const createBox = vi.fn((initial: unknown) => ({ value: initial }))
34
- const lazy = accessorDecorator(createBox)
35
-
36
- class Model {
37
- @lazy accessor label = 'init'
38
- }
39
-
40
- const model = new Model()
41
- expect(createBox).not.toHaveBeenCalled()
42
-
43
- model.label = 'first-write'
44
- expect(createBox).toHaveBeenCalledTimes(1)
45
- expect(createBox).toHaveBeenLastCalledWith('first-write')
46
- expect(model.label).toBe('first-write')
47
- })
48
-
49
- test('懒加载:normalize 推迟到字段激活时才执行', () => {
50
- const createBox = vi.fn((v: unknown) => ({ value: v }))
51
- const normalize = vi.fn((v: unknown) => `n:${String(v)}`)
52
- const lazy = accessorDecorator(createBox, normalize)
53
-
54
- class Model {
55
- @lazy accessor x = 'a'
56
- }
57
-
58
- const model = new Model()
59
- expect(normalize).not.toHaveBeenCalled()
60
-
61
- expect(model.x).toBe('n:a')
62
- expect(normalize).toHaveBeenCalledTimes(1)
63
- expect(createBox).toHaveBeenLastCalledWith('n:a')
64
-
65
- model.x = 'b'
66
- expect(normalize).toHaveBeenCalledTimes(2)
67
- expect(model.x).toBe('n:b')
68
- })
69
-
70
- test('懒加载:各实例独立激活,盒子互不共享', () => {
71
- const createBox = vi.fn((initial: unknown) => ({ value: initial }))
72
- const lazy = accessorDecorator(createBox)
73
-
74
- class Model {
75
- @lazy accessor x = 0
76
- }
77
-
78
- const a = new Model()
79
- const b = new Model()
80
- expect(createBox).not.toHaveBeenCalled()
81
-
82
- expect(a.x).toBe(0)
83
- expect(createBox).toHaveBeenCalledTimes(1)
84
-
85
- expect(b.x).toBe(0)
86
- expect(createBox).toHaveBeenCalledTimes(2)
87
-
88
- a.x = 10
89
- expect(a.x).toBe(10)
90
- expect(b.x).toBe(0)
91
- })
92
-
93
- test('懒加载:undefined / null 初始值激活后行为正确', () => {
94
- class Model {
95
- @reactive accessor none: { v: number } | undefined = undefined
96
- @observable accessor nil: object | null = null
97
- }
98
-
99
- const model = new Model()
100
- expect(model.none).toBeUndefined()
101
- expect(model.nil).toBeNull()
102
-
103
- model.none = { v: 1 }
104
- expect(model.none).toEqual({ v: 1 })
105
- model.nil = { v: 2 }
106
- expect(model.nil).toEqual({ v: 2 })
107
- })
108
-
109
- test('懒加载:字段初始化器交叉访问装饰字段时即时激活,语义正确', () => {
110
- class Cross {
111
- @reactive accessor a = { v: 1 }
112
- @reactive accessor b = this.a.v + 10
113
- }
114
-
115
- const cross = new Cross()
116
- expect(cross.a).toEqual({ v: 1 })
117
- expect(cross.b).toBe(11)
118
-
119
- cross.a = { v: 5 }
120
- expect(cross.a).toEqual({ v: 5 })
121
- expect(cross.b).toBe(11)
122
- })
123
-
124
- test('懒加载黑盒:reactive accessor 激活后 autorun 订阅与通知正常', () => {
125
- class Model {
126
- @reactive accessor count = 0
127
- }
128
-
129
- const model = new Model()
130
- const handler = vi.fn()
131
- autorun(() => handler(model.count))
132
- expect(handler).toHaveBeenCalledTimes(1)
133
-
134
- model.count = 5
135
- expect(handler).toHaveBeenCalledTimes(2)
136
- expect(handler).toHaveBeenLastCalledWith(5)
137
- })
138
-
139
- test('懒加载黑盒:observable accessor 激活后 observe 链路完整', () => {
140
- class Model {
141
- @observable accessor state = { count: 1 }
142
- }
143
-
144
- const model = new Model()
145
- const events: unknown[] = []
146
- observe(model.state, change => events.push(change))
147
-
148
- model.state.count = 2
149
- expect(events).toHaveLength(1)
150
- expect(events[0]).toMatchObject({ type: 'set', key: 'count', value: 2, path: [] })
151
- })
152
-
153
- test('懒加载黑盒:shallowRef 首次写入保持引用透传,激活后整体替换才通知', () => {
154
- class Model {
155
- @shallowRef accessor data = { a: 1 }
156
- }
157
-
158
- const model = new Model()
159
- const next = { a: 2 }
160
- model.data = next
161
- expect(model.data).toBe(next)
162
-
163
- const handler = vi.fn()
164
- autorun(() => handler(model.data.a))
165
- expect(handler).toHaveBeenLastCalledWith(2)
166
-
167
- model.data = { a: 3 }
168
- expect(handler).toHaveBeenLastCalledWith(3)
169
- })
@@ -1,177 +0,0 @@
1
- import { computed, effect, ref } from '@vue/reactivity'
2
- import { expect, test } from 'vitest'
3
-
4
- import { registerSkip } from '../index'
5
- import { observable } from '../observable'
6
- import { observe } from '../observe'
7
- import type { IChange } from '../types'
8
-
9
- test('保留初始值', () => {
10
- const state = observable({ a: 1, b: 'x', nested: { c: true } })
11
-
12
- expect(state.a).toBe(1)
13
- expect(state.b).toBe('x')
14
- expect(state.nested.c).toBe(true)
15
- })
16
-
17
- test('不代理不可代理的值', () => {
18
- function fn() {}
19
- const date = new Date()
20
- const regExp = /x/
21
- const map = new Map()
22
- const set = new Set()
23
- const weakMap = new WeakMap()
24
- const weakSet = new WeakSet()
25
- const promise = Promise.resolve()
26
- const skipped = { __v_skip: true, value: 1 }
27
-
28
- const state = observable({
29
- fn,
30
- date,
31
- regExp,
32
- map,
33
- set,
34
- weakMap,
35
- weakSet,
36
- promise,
37
- skipped,
38
- nil: null,
39
- num: 1,
40
- })
41
-
42
- expect(state.fn).toBe(fn)
43
- expect(state.date).toBe(date)
44
- expect(state.regExp).toBe(regExp)
45
- expect(state.map).toBeInstanceOf(Map)
46
- expect(state.set).toBeInstanceOf(Set)
47
- expect(state.weakMap).toBeInstanceOf(WeakMap)
48
- expect(state.weakSet).toBeInstanceOf(WeakSet)
49
- expect(state.promise).toBe(promise)
50
- expect(state.skipped).toBe(skipped)
51
- expect(state.nil).toBe(null)
52
- expect(state.num).toBe(1)
53
- })
54
-
55
- test('解包 ref 但不代理', () => {
56
- const count = ref(1)
57
- const double = computed(() => count.value * 2)
58
- const state: any = observable({ count, double })
59
-
60
- expect(state.count).toBe(1)
61
- expect(state.double).toBe(2)
62
-
63
- state.count = 2
64
- expect(state.count).toBe(2)
65
- expect(count.value).toBe(2)
66
- })
67
-
68
- test('非对象输入原样返回', () => {
69
- const date = new Date()
70
- const skipped = { __v_skip: true, count: 1 }
71
- const count = ref(1)
72
-
73
- expect(observable(1)).toBe(1)
74
- expect(observable(null)).toBe(null)
75
- expect(observable(date)).toBe(date)
76
- expect(observable(skipped)).toBe(skipped)
77
- expect(observable(count)).toBe(count)
78
- })
79
-
80
- test('内置跳过对 React 元素与 Vue VNode 的包装', () => {
81
- const reactElement = {
82
- $$typeof: Symbol.for('react.element'),
83
- _owner: null,
84
- type: 'div',
85
- props: { children: 'hello' },
86
- }
87
- const vnode = { __v_isVNode: true, type: 'div', props: { children: 'hello' } }
88
- const state = observable({ reactElement, vnode })
89
-
90
- expect(state.reactElement).toBe(reactElement)
91
- expect(state.reactElement.props).toBe(reactElement.props)
92
- expect(state.vnode).toBe(vnode)
93
- expect(state.vnode.props).toBe(vnode.props)
94
-
95
- expect(observable(reactElement)).toBe(reactElement)
96
- expect(observable(vnode)).toBe(vnode)
97
- })
98
-
99
- test('registerSkip 可注册多个跳过判定并支持销毁', () => {
100
- class Widget {
101
- name = 'widget'
102
- }
103
- const widget = new Widget()
104
- const custom = { __custom: true, nested: { count: 1 } }
105
-
106
- const disposeWidget = registerSkip(value => value instanceof Widget)
107
- const disposeCustom = registerSkip(value => (value as any).__custom === true)
108
-
109
- const state = observable({ widget, custom })
110
-
111
- expect(state.widget).toBe(widget)
112
- expect(state.custom).toBe(custom)
113
- expect(observable(widget)).toBe(widget)
114
-
115
- disposeWidget()
116
- disposeCustom()
117
- disposeWidget() // 重复销毁无副作用
118
-
119
- expect(state.widget).not.toBe(widget)
120
- expect(state.custom).not.toBe(custom)
121
- expect(observable(widget)).not.toBe(widget)
122
- })
123
-
124
- test('observable 忽略不支持的装饰器类型', () => {
125
- function getter() {
126
- return 1
127
- }
128
-
129
- expect((observable as any)(getter, { kind: 'getter' })).toBe(getter)
130
- })
131
-
132
- test('observable 的读取与写入接入 Vue 响应式', () => {
133
- const state = observable({ count: 0, nested: { value: 1 } })
134
- const seen: number[] = []
135
-
136
- const runner = effect(() => {
137
- seen.push(state.count + state.nested.value)
138
- })
139
-
140
- expect(seen).toEqual([1])
141
-
142
- state.count = 1
143
- expect(seen).toEqual([1, 2])
144
-
145
- state.nested.value = 2
146
- expect(seen).toEqual([1, 2, 3])
147
-
148
- runner.effect.stop()
149
- state.count = 2
150
- expect(seen).toEqual([1, 2, 3])
151
- })
152
-
153
- test('observable 数组变更触发 Vue computed', () => {
154
- const state = observable({ list: [1, 2] })
155
- const total = computed(() => state.list.reduce((sum, item) => sum + item, 0))
156
-
157
- expect(total.value).toBe(3)
158
-
159
- state.list.push(3)
160
- expect(total.value).toBe(6)
161
-
162
- state.list[0] = 10
163
- expect(total.value).toBe(15)
164
- })
165
-
166
- test('observable 的变更可被 observe 监听', () => {
167
- const state = observable({ nested: { count: 1 } })
168
- const events: IChange[] = []
169
- observe(state, change => {
170
- events.push(change)
171
- })
172
-
173
- state.nested.count = 2
174
-
175
- expect(events).toHaveLength(1)
176
- expect(events[0]).toMatchObject({ type: 'set', key: 'count', path: ['nested'] })
177
- })
@@ -1,204 +0,0 @@
1
- import { expect, test } from 'vitest'
2
-
3
- import { computed } from '../computed'
4
- import { observable } from '../observable'
5
- import { observe } from '../observe'
6
- import { reactive } from '../reactive'
7
- import { shallow } from '../shallow'
8
- import { shallowRef } from '../shallowRef'
9
- import type { IChange } from '../types'
10
-
11
- const ERROR_MESSAGE =
12
- '[formvk] observe 仅支持 observable() 创建的对象;reactive/shallowRef/shallow/computed 的变更请用 autorun 监听'
13
-
14
- function collect(state: any, deep = true) {
15
- const events: IChange[] = []
16
- observe(
17
- state,
18
- change => {
19
- events.push(change)
20
- },
21
- deep
22
- )
23
- return events
24
- }
25
-
26
- test('监听已有属性赋值', () => {
27
- const state = observable({ a: 1 })
28
- const events = collect(state)
29
-
30
- state.a = 2
31
-
32
- expect(events).toHaveLength(1)
33
- expect(events[0]).toMatchObject({
34
- type: 'set',
35
- key: 'a',
36
- value: 2,
37
- oldValue: 1,
38
- path: [],
39
- })
40
- expect(events[0].target).toBeDefined()
41
- })
42
-
43
- test('监听新增属性', () => {
44
- const state: any = observable({ a: 1 })
45
- const events = collect(state)
46
-
47
- state.b = 2
48
-
49
- expect(events).toHaveLength(1)
50
- expect(events[0]).toMatchObject({
51
- type: 'add',
52
- key: 'b',
53
- value: 2,
54
- oldValue: undefined,
55
- path: [],
56
- })
57
- })
58
-
59
- test('监听删除属性', () => {
60
- const state: any = observable({ a: 1 })
61
- const events = collect(state)
62
-
63
- delete state.a
64
-
65
- expect(events).toHaveLength(1)
66
- expect(events[0]).toMatchObject({
67
- type: 'delete',
68
- key: 'a',
69
- value: undefined,
70
- oldValue: 1,
71
- path: [],
72
- })
73
- })
74
-
75
- test('相同值不触发回调', () => {
76
- const state = observable({ a: 1 })
77
- const events = collect(state)
78
-
79
- state.a = 1
80
-
81
- expect(events).toHaveLength(0)
82
- })
83
-
84
- test('deep 观察者收到嵌套变更及相对路径', () => {
85
- const state = observable({ a: { b: { c: 0 } } })
86
- const events = collect(state)
87
-
88
- state.a.b.c = 1
89
-
90
- expect(events).toHaveLength(1)
91
- expect(events[0]).toMatchObject({
92
- type: 'set',
93
- key: 'c',
94
- value: 1,
95
- path: ['a', 'b'],
96
- })
97
- })
98
-
99
- test('非 deep 观察者只收到直接变更', () => {
100
- const state = observable({ a: { b: 1 } })
101
- const events = collect(state, false)
102
-
103
- state.a.b = 2
104
- expect(events).toHaveLength(0)
105
-
106
- state.a = { b: 2 }
107
- expect(events).toHaveLength(1)
108
- expect(events[0]).toMatchObject({ type: 'set', key: 'a', path: [] })
109
- })
110
-
111
- test('子对象上的观察者与根观察者都会收到变更', () => {
112
- const state = observable({ nested: { a: 1 } })
113
- const rootEvents = collect(state)
114
- const nestedEvents = collect(state.nested)
115
-
116
- state.nested.a = 2
117
-
118
- expect(rootEvents).toHaveLength(1)
119
- expect(rootEvents[0]).toMatchObject({ key: 'a', path: ['nested'] })
120
-
121
- expect(nestedEvents).toHaveLength(1)
122
- expect(nestedEvents[0]).toMatchObject({ key: 'a', path: [] })
123
- })
124
-
125
- test('dispose 后不再触发回调', () => {
126
- const state = observable({ a: 1 })
127
- const events: IChange[] = []
128
- const dispose = observe(state, change => {
129
- events.push(change)
130
- })
131
-
132
- state.a = 2
133
- dispose()
134
- state.a = 3
135
-
136
- expect(events).toHaveLength(1)
137
- expect(events[0]).toMatchObject({ type: 'set', key: 'a', value: 2 })
138
- })
139
-
140
- test('支持多个观察者', () => {
141
- const state = observable({ a: 1 })
142
- const first = collect(state)
143
- const second = collect(state)
144
-
145
- state.a = 2
146
-
147
- expect(first).toHaveLength(1)
148
- expect(second).toHaveLength(1)
149
- })
150
-
151
- test('循环引用不会无限传播', () => {
152
- const source: any = { value: 0 }
153
- source.self = source
154
- const state = observable(source)
155
- const events = collect(state)
156
-
157
- state.self.self.value = 1
158
-
159
- expect(events).toHaveLength(1)
160
- expect(events[0]).toMatchObject({ type: 'set', key: 'value', path: [] })
161
- })
162
-
163
- test('监听数组变更', () => {
164
- const state = observable({ list: [] as number[] })
165
- const events = collect(state)
166
-
167
- state.list.push(1)
168
-
169
- const addEvent = events.find(event => event.key === '0')
170
- expect(addEvent).toMatchObject({
171
- type: 'add',
172
- key: '0',
173
- value: 1,
174
- path: ['list'],
175
- })
176
- })
177
-
178
- test('observe 用于普通对象时抛错', () => {
179
- expect(() => observe({}, () => {})).toThrowError(ERROR_MESSAGE)
180
- })
181
-
182
- test('observe 用于 reactive 对象时抛错', () => {
183
- const state = reactive({ a: 1 })
184
-
185
- expect(() => observe(state, () => {})).toThrowError(ERROR_MESSAGE)
186
- })
187
-
188
- test('observe 用于 shallow 对象时抛错', () => {
189
- const state = shallow({ a: 1 })
190
-
191
- expect(() => observe(state, () => {})).toThrowError(ERROR_MESSAGE)
192
- })
193
-
194
- test('observe 用于 shallowRef 盒子时抛错', () => {
195
- const box = shallowRef({ count: 1 })
196
-
197
- expect(() => observe(box, () => {})).toThrowError(ERROR_MESSAGE)
198
- })
199
-
200
- test('observe 用于 computed 时抛错', () => {
201
- const double = computed(() => 1)
202
-
203
- expect(() => observe(double, () => {})).toThrowError(ERROR_MESSAGE)
204
- })