@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.
package/src/batch.ts DELETED
@@ -1,120 +0,0 @@
1
- import type { AnyFunction, BindFunction, Boundary, IBatch } from './types'
2
-
3
- type Runner = () => void
4
-
5
- function isDecoratorContext(context: any): context is { kind: string } {
6
- return !!context && typeof context === 'object' && typeof context.kind === 'string'
7
- }
8
-
9
- let batchCount = 0
10
- let batchScope = false
11
-
12
- const pendingReactions = new Set<Runner>()
13
- const pendingScopeReactions = new Set<Runner>()
14
- const batchEndpoints = new Set<() => void>()
15
-
16
- function runAll(runners: Set<Runner>) {
17
- if (runners.size === 0) return
18
- const queue = Array.from(runners)
19
- runners.clear()
20
- for (const run of queue) {
21
- run()
22
- }
23
- }
24
-
25
- export function batchStart() {
26
- batchCount++
27
- }
28
-
29
- export function batchEnd() {
30
- batchCount--
31
- if (batchCount === 0) {
32
- runAll(pendingReactions)
33
- const endpoints = Array.from(batchEndpoints)
34
- batchEndpoints.clear()
35
- for (const endpoint of endpoints) {
36
- endpoint()
37
- }
38
- }
39
- }
40
-
41
- export function batchScopeStart() {
42
- batchScope = true
43
- // 把批次中尚未刷新的 reaction 转移到 scope,使 scope 结束时能读到最新值
44
- pendingReactions.forEach(runner => pendingScopeReactions.add(runner))
45
- pendingReactions.clear()
46
- }
47
-
48
- export function batchScopeEnd() {
49
- batchScope = false
50
- runAll(pendingScopeReactions)
51
- }
52
-
53
- /** reaction 的调度入口:非批量状态立即执行,否则延迟到批次结束 */
54
- export function scheduleReaction(runner: Runner) {
55
- if (batchScope) {
56
- pendingScopeReactions.add(runner)
57
- } else if (batchCount > 0) {
58
- pendingReactions.add(runner)
59
- } else {
60
- runner()
61
- }
62
- }
63
-
64
- /** 创建在 start / end 之间执行回调的边界函数,可同时作为类成员装饰器 */
65
- export function createBoundary(start: () => void, end: () => void): Boundary {
66
- function invoke<T>(callback?: () => T): T {
67
- let result = undefined as T
68
- try {
69
- start()
70
- if (typeof callback === 'function') {
71
- result = callback()
72
- }
73
- } finally {
74
- end()
75
- }
76
- return result as T
77
- }
78
-
79
- function wrap(callback: AnyFunction) {
80
- return function (this: any, ...args: any[]) {
81
- return invoke(() => callback.apply(this, args))
82
- }
83
- }
84
-
85
- function decorate(callback: any, context: any) {
86
- if (context.kind === 'field') {
87
- return (initialValue: any) => (typeof initialValue === 'function' ? wrap(initialValue) : initialValue)
88
- }
89
- if (typeof callback !== 'function') return callback
90
- return wrap(callback)
91
- }
92
-
93
- const boundary = ((callback?: any, context?: any) => {
94
- if (isDecoratorContext(context)) {
95
- return decorate(callback, context)
96
- }
97
- return invoke(callback)
98
- }) as Boundary
99
-
100
- const bound: BindFunction = (callback, context) =>
101
- ((...args: any[]) => invoke(() => callback?.apply(context, args))) as any
102
-
103
- boundary.bound = bound
104
- return boundary
105
- }
106
-
107
- const scopeBoundary = createBoundary(batchScopeStart, batchScopeEnd)
108
-
109
- /** 批量执行:合并批次内的响应式写入,批次结束时统一刷新 reaction */
110
- export const batch = Object.assign(createBoundary(batchStart, batchEnd), {
111
- scope: scopeBoundary,
112
- endpoint: (callback?: () => void) => {
113
- if (typeof callback !== 'function') return
114
- if (batchCount === 0) {
115
- callback()
116
- } else {
117
- batchEndpoints.add(callback)
118
- }
119
- },
120
- }) as IBatch
package/src/computed.ts DELETED
@@ -1,42 +0,0 @@
1
- import { computed as vueComputed } from '@vue/reactivity'
2
- import type { ComputedRef } from '@vue/reactivity'
3
-
4
- /** computed 函数形态的只读返回值 */
5
- export interface ComputedValue<T> {
6
- readonly value: T
7
- }
8
-
9
- /** computed 函数形态的可写返回值 */
10
- export interface WritableComputedValue<T> {
11
- value: T
12
- }
13
-
14
- export interface IComputed {
15
- <T>(getter: (this: any) => T, context: ClassGetterDecoratorContext): (this: any) => T
16
- <T>(getter: () => T): ComputedValue<T>
17
- <T>(options: { get: () => T }): ComputedValue<T>
18
- <T>(options: { get: () => T; set: (value: T) => void }): WritableComputedValue<T>
19
- }
20
-
21
- function computedImpl(compute: any, context?: any) {
22
- if (!context) return vueComputed(compute)
23
- if (context.kind !== 'getter') {
24
- throw new Error('[formvk] computed 只支持 getter 装饰器')
25
- }
26
- // getter 装饰器在原型上只安装一次,按实例缓存 computed
27
- const cache = new WeakMap<object, ComputedRef<any>>()
28
- return function (this: any) {
29
- let result = cache.get(this)
30
- if (!result) {
31
- result = vueComputed(() => compute.call(this))
32
- cache.set(this, result)
33
- }
34
- return result.value
35
- }
36
- }
37
-
38
- /**
39
- * 创建 Vue 计算属性,或作为 getter 装饰器按实例缓存计算结果。
40
- * 纯 Vue 响应式,不可被 observe。
41
- */
42
- export const computed: IComputed = computedImpl as IComputed
package/src/decorator.ts DELETED
@@ -1,61 +0,0 @@
1
- type AccessorDecorator = (
2
- value: ClassAccessorDecoratorTarget<any, any>,
3
- context: ClassAccessorDecoratorContext
4
- ) => ClassAccessorDecoratorResult<any, any>
5
-
6
- /** 懒加载哨兵:构造期暂存原始值,字段首次读写时才替换成真正的响应式盒子 */
7
- interface LazySlot {
8
- raw: unknown
9
- }
10
-
11
- /**
12
- * accessor 装饰器通用工厂:字段值存进 createBox 返回的盒子,读写都经过盒子,
13
- * 因此重新赋值也能保持响应式(普通 field 装饰器只能改写初始值)。
14
- * normalize 用于写入前归一化(如把对象包成响应式代理),默认原样透传。
15
- *
16
- * 懒加载:构造期只创建轻量哨兵保存初始值,不调用 createBox;
17
- * 直到该字段第一次被读/写时才真正创建响应式盒子并回写私有槽,之后一直复用。
18
- *
19
- * 实现约束:
20
- * 1. 用 WeakSet 识别哨兵而非 symbol 品牌——盒子可能是 Vue Proxy,属性访问会触发
21
- * get trap 并污染依赖收集,WeakSet 按对象 identity 判定,零 trap 副作用;
22
- * 2. WeakSet 的 key 必须是槽位对象而不能是类实例:实例首次作为 WeakCollection key
23
- * 会被 V8 附加 identity hash 并触发 map transition,使实例隐藏类失稳、字段访问
24
- * 内联缓存多态化,实测全激活路径反而慢 4~18 倍;
25
- * 3. 槽位替换后哨兵失去引用即可被 GC,WeakSet 不造成泄漏。
26
- *
27
- * 注意:field 装饰器没有读写拦截点,无法懒加载,仍在构造期即时转换。
28
- */
29
- export function accessorDecorator(
30
- createBox: (initialValue: any) => any,
31
- normalize: (value: any) => any = value => value
32
- ): AccessorDecorator {
33
- // 每个被装饰字段独立一份哨兵集合,随类定义常驻
34
- const lazySlots = new WeakSet<object>()
35
-
36
- return (target, _context) => ({
37
- init(initialValue) {
38
- const slot: LazySlot = { raw: initialValue }
39
- lazySlots.add(slot)
40
- return slot
41
- },
42
- get(this: any) {
43
- const slot = target.get.call(this)
44
- if (lazySlots.has(slot)) {
45
- const box = createBox(normalize(slot.raw))
46
- target.set.call(this, box)
47
- return box.value
48
- }
49
- return slot.value
50
- },
51
- set(this: any, value: any) {
52
- const slot = target.get.call(this)
53
- if (lazySlots.has(slot)) {
54
- // 初始值从未对外暴露,直接以新值建盒,等价于覆盖初始值
55
- target.set.call(this, createBox(normalize(value)))
56
- return
57
- }
58
- slot.value = normalize(value)
59
- },
60
- })
61
- }
package/src/externals.ts DELETED
@@ -1,68 +0,0 @@
1
- import { isReactive, toRaw } from '@vue/reactivity'
2
-
3
- import { parentMap, reverseCache } from './internals'
4
-
5
- /** 判断值是否是 Vue 响应式代理(reactive / shallowReactive 根对象) */
6
- export { isReactive }
7
-
8
- /** 外层 proxy 统一还原为内层 Vue proxy,便于匹配父子关系;入参已由 contains 保证为非空对象 */
9
- function normalize(value: object): object {
10
- return reverseCache.get(value) ?? value
11
- }
12
-
13
- /** 判断值是否是 observable(wrap 出的 formvk 代理;纯 Vue reactive 不在此列) */
14
- export function isObservable(value: any): boolean {
15
- if (value === null || typeof value !== 'object') return false
16
- return reverseCache.has(value)
17
- }
18
-
19
- /** 把可观察/响应式对象深拷贝为普通对象,其余值原样返回 */
20
- export function toJS<T>(values: T): T {
21
- const visited = new WeakSet<object>()
22
-
23
- function convert(value: any): any {
24
- if (value === null || typeof value !== 'object') return value
25
- if (!isObservable(value) && !isReactive(value)) return value
26
- if (visited.has(value)) return toRaw(value)
27
- visited.add(value)
28
- if (Array.isArray(value)) {
29
- const result = value.map((item: any) => convert(item))
30
- visited.delete(value)
31
- return result
32
- }
33
- const result: Record<string, any> = {}
34
- for (const key in value) {
35
- result[key] = convert(value[key])
36
- }
37
- visited.delete(value)
38
- return result
39
- }
40
-
41
- return convert(values)
42
- }
43
-
44
- /** 判断 property 是否就是 target 自身或其后代节点 */
45
- export function contains(target: any, property: any): boolean {
46
- if (target === property) return true
47
- if (target === null || typeof target !== 'object') return false
48
- if (property === null || typeof property !== 'object') return false
49
-
50
- const targetNode = normalize(target)
51
- const propertyNode = normalize(property)
52
- if (targetNode === propertyNode) return true
53
-
54
- const visited = new Set<object>([propertyNode])
55
- const queue: object[] = [propertyNode]
56
- while (queue.length) {
57
- const node = queue.shift()!
58
- const parents = parentMap.get(node)
59
- if (!parents) continue
60
- for (const parent of parents) {
61
- if (parent === targetNode) return true
62
- if (visited.has(parent)) continue
63
- visited.add(parent)
64
- queue.push(parent)
65
- }
66
- }
67
- return false
68
- }
package/src/index.ts DELETED
@@ -1,25 +0,0 @@
1
- export * from './observable'
2
- export * from './reactive'
3
- export * from './shallowRef'
4
- export * from './shallow'
5
- export * from './computed'
6
- export * from './observe'
7
- export * from './autorun'
8
- export * from './tracker'
9
- export { batch } from './batch'
10
- export { action } from './action'
11
- export { contains, isObservable, isReactive, toJS } from './externals'
12
- export { untracked } from './untracked'
13
- export { registerSkip } from './internals'
14
- export type { SkipMatcher } from './internals'
15
- export type {
16
- BindFunction,
17
- Boundary,
18
- DataChange,
19
- Dispose,
20
- IAction,
21
- IBatch,
22
- IChange,
23
- IReactionOptions,
24
- Observer,
25
- } from './types'
package/src/internals.ts DELETED
@@ -1,177 +0,0 @@
1
- import { isRef, toRaw, unref } from '@vue/reactivity'
2
-
3
- import type { IChange, Observer } from './types'
4
-
5
- export interface Entry {
6
- fn: Observer
7
- deep: boolean
8
- }
9
-
10
- const SKIP_CTORS = [Date, RegExp, Map, Set, WeakMap, WeakSet, Promise]
11
-
12
- /** 内层 Vue reactive proxy -> 观察者集合 */
13
- export const handlerMap = new WeakMap<object, Set<Entry>>()
14
- /** 子 -> 所有父 */
15
- export const parentMap = new WeakMap<object, Set<object>>()
16
- /** 子 + 父 -> 在父里的 key */
17
- export const parentKeyMap = new WeakMap<object, Map<object, PropertyKey>>()
18
- /** 内层 proxy -> 外层 proxy */
19
- export const proxyCache = new WeakMap<object, object>()
20
- /** 外层 proxy -> 内层 proxy */
21
- export const reverseCache = new WeakMap<object, object>()
22
-
23
- export type SkipMatcher = (value: object) => boolean
24
-
25
- const skipMatchers = new Set<SkipMatcher>()
26
-
27
- /**
28
- * 注册“跳过包装”的判定,可注册多个,任一命中即跳过;返回销毁函数。
29
- * 命中的值不会生成外层 proxy,元素本身及其嵌套结构保持原始引用。
30
- */
31
- export function registerSkip(matcher: SkipMatcher): () => void {
32
- skipMatchers.add(matcher)
33
- return () => {
34
- skipMatchers.delete(matcher)
35
- }
36
- }
37
-
38
- /** React 元素、Vue VNode 以及自定义跳过判定 */
39
- export function shouldSkip(v: unknown): boolean {
40
- if (v === null || typeof v !== 'object') return false
41
- if ('$$typeof' in v && '_owner' in v) return true
42
- if ((v as any).__v_isVNode === true) return true
43
- for (const matcher of skipMatchers) {
44
- if (matcher(v)) return true
45
- }
46
- return false
47
- }
48
-
49
- export function canProxy(v: unknown): v is object {
50
- if (v === null || typeof v !== 'object') return false
51
- if (isRef(v)) return false
52
- if ((v as any).__v_skip) return false
53
- if (shouldSkip(v)) return false
54
- for (const C of SKIP_CTORS) if (v instanceof C) return false
55
- return true
56
- }
57
-
58
- export function addParent(childVueProxy: object, parentVueProxy: object, key: PropertyKey) {
59
- let parents = parentMap.get(childVueProxy)
60
- if (!parents) {
61
- parents = new Set()
62
- parentMap.set(childVueProxy, parents)
63
- }
64
- parents.add(parentVueProxy)
65
-
66
- let keys = parentKeyMap.get(childVueProxy)
67
- if (!keys) {
68
- keys = new Map()
69
- parentKeyMap.set(childVueProxy, keys)
70
- }
71
- keys.set(parentVueProxy, key)
72
- }
73
-
74
- export function wrap(vueProxy: any, parentVueProxy?: any, parentKey?: PropertyKey, shallow = false): any {
75
- if (!canProxy(vueProxy)) return vueProxy
76
-
77
- let proxy = proxyCache.get(vueProxy)
78
- if (!proxy) {
79
- proxy = new Proxy(vueProxy, {
80
- get(target, key, receiver) {
81
- const raw = Reflect.get(target, key, receiver)
82
- // __v_raw 必须原样返回,否则 toRaw 会再次命中代理,导致 track / trigger 目标不一致
83
- if (key === '__v_raw') return raw
84
- const value = unref(raw)
85
- // ref 只解包,不做代理;嵌套对象按需懒代理,并登记父子关系
86
- if (value !== raw) return value
87
- // 元素需要保持原始引用,Vue 可能已把它的嵌套对象包成 reactive proxy
88
- if (shouldSkip(value)) return toRaw(value)
89
- if (!shallow && canProxy(value)) return wrap(value, target, key)
90
- return value
91
- },
92
- set(target, key, value) {
93
- const raw = toRaw(target) as any
94
- const had = Object.prototype.hasOwnProperty.call(raw, key)
95
- const oldValue = raw[key]
96
-
97
- const result = Reflect.set(target, key, value)
98
-
99
- if (result && !Object.is(oldValue, value)) {
100
- notifyDeep(target, {
101
- type: had ? 'set' : 'add',
102
- key,
103
- value,
104
- oldValue,
105
- target,
106
- path: [],
107
- })
108
- }
109
- return result
110
- },
111
- deleteProperty(target, key) {
112
- const raw = toRaw(target) as any
113
- const had = Object.prototype.hasOwnProperty.call(raw, key)
114
- const oldValue = raw[key]
115
-
116
- const result = Reflect.deleteProperty(target, key)
117
-
118
- if (had && result) {
119
- notifyDeep(target, {
120
- type: 'delete',
121
- key,
122
- value: undefined,
123
- oldValue,
124
- target,
125
- path: [],
126
- })
127
- }
128
- return result
129
- },
130
- })
131
- proxyCache.set(vueProxy, proxy)
132
- reverseCache.set(proxy, vueProxy)
133
- }
134
-
135
- if (parentVueProxy) addParent(vueProxy, parentVueProxy, parentKey!)
136
- return proxy
137
- }
138
-
139
- // 变更点自身通知所有观察者;向上沿父链传播时,只通知 deep=true 的观察者
140
- export function notifyDeep(sourceVueProxy: object, change: IChange) {
141
- // 1) 直接变更的对象:所有观察者都通知,path 为 []
142
- const own = handlerMap.get(sourceVueProxy)
143
- if (own) own.forEach(e => e.fn({ ...change, path: [] }))
144
-
145
- // 2) BFS 向上传播
146
- const visited = new Set<object>([sourceVueProxy])
147
- const queue: Array<{ node: object; path: PropertyKey[] }> = []
148
-
149
- const seedParents = parentMap.get(sourceVueProxy)
150
- if (seedParents) {
151
- for (const p of seedParents) {
152
- const k = parentKeyMap.get(sourceVueProxy)?.get(p)
153
- if (k !== undefined) queue.push({ node: p, path: [k] })
154
- }
155
- }
156
-
157
- while (queue.length) {
158
- const { node, path } = queue.shift()!
159
- if (visited.has(node)) continue
160
- visited.add(node)
161
-
162
- const set = handlerMap.get(node)
163
- if (set) {
164
- set.forEach(e => {
165
- if (e.deep) e.fn({ ...change, path: [...path] })
166
- })
167
- }
168
-
169
- const parents = parentMap.get(node)
170
- if (parents) {
171
- for (const p of parents) {
172
- const k = parentKeyMap.get(node)?.get(p)
173
- if (k !== undefined) queue.push({ node: p, path: [k, ...path] })
174
- }
175
- }
176
- }
177
- }
package/src/observable.ts DELETED
@@ -1,49 +0,0 @@
1
- import { isRef, reactive } from '@vue/reactivity'
2
-
3
- import { accessorDecorator } from './decorator'
4
- import { shouldSkip, wrap } from './internals'
5
-
6
- export interface IObservable {
7
- (
8
- value: ClassAccessorDecoratorTarget<any, any>,
9
- context: ClassAccessorDecoratorContext
10
- ): ClassAccessorDecoratorResult<any, any>
11
- (value: undefined, context: ClassFieldDecoratorContext): (initialValue: any) => any
12
- <T>(source: T): T
13
- }
14
-
15
- const REACTIVE_TAGS = new Set([
16
- '[object Object]',
17
- '[object Array]',
18
- '[object Map]',
19
- '[object Set]',
20
- '[object WeakMap]',
21
- '[object WeakSet]',
22
- ])
23
-
24
- function isReactiveTarget(value: unknown): value is object {
25
- if (value === null || typeof value !== 'object') return false
26
- if (isRef(value)) return false
27
- if ((value as any).__v_skip) return false
28
- if (shouldSkip(value)) return false
29
- return REACTIVE_TAGS.has(Object.prototype.toString.call(value))
30
- }
31
-
32
- function toObservable<T>(source: T): T {
33
- if (!isReactiveTarget(source)) return source
34
- return wrap(reactive(source)) as T
35
- }
36
-
37
- function observableImpl(source: any, context?: any) {
38
- if (!context) return toObservable(source)
39
- if (context.kind === 'field') return (initialValue: any) => toObservable(initialValue)
40
- if (context.kind === 'accessor') return accessorDecorator(value => toObservable({ value }))(source, context)
41
- return source
42
- }
43
-
44
- /**
45
- * 创建可被 observe 的响应式对象(formvk 外层代理 + Vue 深响应式),
46
- * 同时支持 field / accessor 装饰器。
47
- * 这是唯一能被 observe 监听的入口;只需要 autorun 追踪时请改用 reactive/shallowRef/shallow。
48
- */
49
- export const observable: IObservable = observableImpl as IObservable
package/src/observe.ts DELETED
@@ -1,25 +0,0 @@
1
- import type { Entry } from './internals'
2
- import { handlerMap, reverseCache } from './internals'
3
- import type { Observer } from './types'
4
-
5
- /** 监听对象所有写操作,返回 dispose;仅支持 observable() 创建的对象 */
6
- export function observe<T extends object>(target: T, observer: Observer, deep = true): () => void {
7
- const vueProxy = reverseCache.get(target as any)
8
- if (!vueProxy) {
9
- throw new Error(
10
- '[formvk] observe 仅支持 observable() 创建的对象;reactive/shallowRef/shallow/computed 的变更请用 autorun 监听'
11
- )
12
- }
13
- let set = handlerMap.get(vueProxy)
14
- if (!set) {
15
- set = new Set()
16
- handlerMap.set(vueProxy, set)
17
- }
18
-
19
- const entry: Entry = { fn: observer, deep }
20
- set.add(entry)
21
-
22
- return () => {
23
- set!.delete(entry)
24
- }
25
- }
package/src/reactive.ts DELETED
@@ -1,33 +0,0 @@
1
- import { reactive as vueReactive } from '@vue/reactivity'
2
-
3
- import { accessorDecorator } from './decorator'
4
-
5
- export interface IReactive {
6
- (
7
- value: ClassAccessorDecoratorTarget<any, any>,
8
- context: ClassAccessorDecoratorContext
9
- ): ClassAccessorDecoratorResult<any, any>
10
- (value: undefined, context: ClassFieldDecoratorContext): (initialValue: any) => any
11
- <T>(source: T): T
12
- }
13
-
14
- /** 纯 Vue 深响应式:只服务 autorun/effect,不进入 formvk 代理层,不可被 observe */
15
- function toReactive<T>(source: T): T {
16
- if (source === null || typeof source !== 'object') return source
17
- return vueReactive(source) as T
18
- }
19
-
20
- function reactiveImpl(value: any, context?: any) {
21
- if (!context) return toReactive(value)
22
- if (context.kind === 'field') return (initialValue: any) => toReactive(initialValue)
23
- if (context.kind === 'accessor') {
24
- return accessorDecorator(initialValue => toReactive({ value: initialValue }))(value, context)
25
- }
26
- return value
27
- }
28
-
29
- /**
30
- * 创建纯 Vue 响应式对象(深响应),同时支持 field / accessor 装饰器。
31
- * 与 observable 的区别:不包 formvk 外层代理,没有变更路径传播,不能被 observe 监听。
32
- */
33
- export const reactive: IReactive = reactiveImpl as IReactive
package/src/shallow.ts DELETED
@@ -1,33 +0,0 @@
1
- import { shallowReactive } from '@vue/reactivity'
2
-
3
- import { accessorDecorator } from './decorator'
4
-
5
- export interface IShallow {
6
- (
7
- value: ClassAccessorDecoratorTarget<any, any>,
8
- context: ClassAccessorDecoratorContext
9
- ): ClassAccessorDecoratorResult<any, any>
10
- (value: undefined, context: ClassFieldDecoratorContext): (initialValue: any) => any
11
- <T>(source: T): T
12
- }
13
-
14
- /** 纯 Vue 浅响应式:仅顶层属性变化触发通知,嵌套值保持原始引用 */
15
- function toShallow<T>(source: T): T {
16
- if (source === null || typeof source !== 'object') return source
17
- return shallowReactive(source) as T
18
- }
19
-
20
- function shallowImpl(value: any, context?: any) {
21
- if (!context) return toShallow(value)
22
- if (context.kind === 'field') return (initialValue: any) => toShallow(initialValue)
23
- if (context.kind === 'accessor') {
24
- return accessorDecorator(initialValue => toShallow({ value: initialValue }))(value, context)
25
- }
26
- return value
27
- }
28
-
29
- /**
30
- * 创建纯 Vue 浅响应式对象,同时支持 field / accessor 装饰器。
31
- * 不包 formvk 外层代理,不可被 observe,变更请用 autorun 监听。
32
- */
33
- export const shallow: IShallow = shallowImpl as IShallow
package/src/shallowRef.ts DELETED
@@ -1,28 +0,0 @@
1
- import { shallowRef as vueShallowRef } from '@vue/reactivity'
2
- import type { ShallowRef } from '@vue/reactivity'
3
-
4
- import { accessorDecorator } from './decorator'
5
-
6
- export interface IShallowRef {
7
- (
8
- value: ClassAccessorDecoratorTarget<any, any>,
9
- context: ClassAccessorDecoratorContext
10
- ): ClassAccessorDecoratorResult<any, any>
11
- (value: undefined, context: ClassFieldDecoratorContext): (initialValue: any) => any
12
- <T>(value: T): ShallowRef<T>
13
- }
14
-
15
- function shallowRefImpl(value: any, context?: any) {
16
- if (!context) return vueShallowRef(value)
17
- if (context.kind === 'accessor') {
18
- return accessorDecorator(initialValue => vueShallowRef(initialValue))(value, context)
19
- }
20
- // 普通字段无法拦截读写,保持原值;shallowRef 请配合 accessor 使用
21
- return value
22
- }
23
-
24
- /**
25
- * 浅响应式盒子(Vue shallowRef):只有整体替换 .value 才触发通知,
26
- * 盒子内对象的深层变更不具响应性。不可被 observe,变更请用 autorun 监听。
27
- */
28
- export const shallowRef: IShallowRef = shallowRefImpl as IShallowRef