@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
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { isReactive } from '@vue/reactivity';
|
|
2
|
+
import { ShallowRef } from '@vue/reactivity';
|
|
3
|
+
|
|
4
|
+
/** action = batch + untrack:合并通知,同时不收集执行期间的响应式读取 */
|
|
5
|
+
export declare const action: IAction;
|
|
6
|
+
|
|
7
|
+
declare type AnyFunction = (...args: any[]) => any;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 自动运行:立即执行 tracker,并追踪其中读取的响应式数据,
|
|
11
|
+
* 任意依赖变化都会整体重跑 tracker。
|
|
12
|
+
*/
|
|
13
|
+
export declare function autorun(tracker: () => void): Dispose;
|
|
14
|
+
|
|
15
|
+
/** 批量执行:合并批次内的响应式写入,批次结束时统一刷新 reaction */
|
|
16
|
+
export declare const batch: IBatch;
|
|
17
|
+
|
|
18
|
+
export declare type BindFunction<F = (...args: any[]) => any> = (callback?: F, context?: any) => F;
|
|
19
|
+
|
|
20
|
+
/** 既是边界函数(直接调用),也可作为类成员装饰器使用 */
|
|
21
|
+
export declare type Boundary = {
|
|
22
|
+
<F extends AnyFunction>(value: F, context: ClassMethodDecoratorContext | ClassGetterDecoratorContext | ClassSetterDecoratorContext): F;
|
|
23
|
+
(value: undefined, context: ClassFieldDecoratorContext): (initialValue: any) => any;
|
|
24
|
+
<T>(callback?: () => T): T;
|
|
25
|
+
bound: BindFunction;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 创建 Vue 计算属性,或作为 getter 装饰器按实例缓存计算结果。
|
|
30
|
+
* 纯 Vue 响应式,不可被 observe。
|
|
31
|
+
*/
|
|
32
|
+
export declare const computed: IComputed;
|
|
33
|
+
|
|
34
|
+
/** computed 函数形态的只读返回值 */
|
|
35
|
+
export declare interface ComputedValue<T> {
|
|
36
|
+
readonly value: T;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 判断 property 是否就是 target 自身或其后代节点 */
|
|
40
|
+
export declare function contains(target: any, property: any): boolean;
|
|
41
|
+
|
|
42
|
+
/** observe 回调中的变更描述,与 formily 的 DataChange 对齐 */
|
|
43
|
+
export declare type DataChange = IChange;
|
|
44
|
+
|
|
45
|
+
export declare type Dispose = () => void;
|
|
46
|
+
|
|
47
|
+
export declare interface IAction extends Boundary {
|
|
48
|
+
scope: Boundary;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export declare interface IBatch extends IAction {
|
|
52
|
+
endpoint: (callback?: () => void) => void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export declare interface IChange {
|
|
56
|
+
type: 'add' | 'set' | 'delete';
|
|
57
|
+
key: PropertyKey;
|
|
58
|
+
value?: any;
|
|
59
|
+
oldValue?: any;
|
|
60
|
+
/** 内层 Vue reactive 对象,即变更发生的那个对象 */
|
|
61
|
+
target: any;
|
|
62
|
+
/** 相对当前观察者视角的路径 */
|
|
63
|
+
path: PropertyKey[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export declare interface IComputed {
|
|
67
|
+
<T>(getter: (this: any) => T, context: ClassGetterDecoratorContext): (this: any) => T;
|
|
68
|
+
<T>(getter: () => T): ComputedValue<T>;
|
|
69
|
+
<T>(options: {
|
|
70
|
+
get: () => T;
|
|
71
|
+
}): ComputedValue<T>;
|
|
72
|
+
<T>(options: {
|
|
73
|
+
get: () => T;
|
|
74
|
+
set: (value: T) => void;
|
|
75
|
+
}): WritableComputedValue<T>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export declare interface IObservable {
|
|
79
|
+
(value: ClassAccessorDecoratorTarget<any, any>, context: ClassAccessorDecoratorContext): ClassAccessorDecoratorResult<any, any>;
|
|
80
|
+
(value: undefined, context: ClassFieldDecoratorContext): (initialValue: any) => any;
|
|
81
|
+
<T>(source: T): T;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export declare interface IReactionOptions<T> {
|
|
85
|
+
/** 返回 true 表示新旧值相等,不触发 subscriber */
|
|
86
|
+
equals?: (oldValue: T, newValue: T) => boolean;
|
|
87
|
+
/** 初始化后立即触发一次 subscriber */
|
|
88
|
+
fireImmediately?: boolean;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export declare interface IReactive {
|
|
92
|
+
(value: ClassAccessorDecoratorTarget<any, any>, context: ClassAccessorDecoratorContext): ClassAccessorDecoratorResult<any, any>;
|
|
93
|
+
(value: undefined, context: ClassFieldDecoratorContext): (initialValue: any) => any;
|
|
94
|
+
<T>(source: T): T;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export declare interface IShallow {
|
|
98
|
+
(value: ClassAccessorDecoratorTarget<any, any>, context: ClassAccessorDecoratorContext): ClassAccessorDecoratorResult<any, any>;
|
|
99
|
+
(value: undefined, context: ClassFieldDecoratorContext): (initialValue: any) => any;
|
|
100
|
+
<T>(source: T): T;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export declare interface IShallowRef {
|
|
104
|
+
(value: ClassAccessorDecoratorTarget<any, any>, context: ClassAccessorDecoratorContext): ClassAccessorDecoratorResult<any, any>;
|
|
105
|
+
(value: undefined, context: ClassFieldDecoratorContext): (initialValue: any) => any;
|
|
106
|
+
<T>(value: T): ShallowRef<T>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 判断值是否是 observable(wrap 出的 formvk 代理;纯 Vue reactive 不在此列) */
|
|
110
|
+
export declare function isObservable(value: any): boolean;
|
|
111
|
+
|
|
112
|
+
export { isReactive }
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 创建可被 observe 的响应式对象(formvk 外层代理 + Vue 深响应式),
|
|
116
|
+
* 同时支持 field / accessor 装饰器。
|
|
117
|
+
* 这是唯一能被 observe 监听的入口;只需要 autorun 追踪时请改用 reactive/shallowRef/shallow。
|
|
118
|
+
*/
|
|
119
|
+
export declare const observable: IObservable;
|
|
120
|
+
|
|
121
|
+
/** 监听对象所有写操作,返回 dispose;仅支持 observable() 创建的对象 */
|
|
122
|
+
export declare function observe<T extends object>(target: T, observer: Observer, deep?: boolean): () => void;
|
|
123
|
+
|
|
124
|
+
export declare type Observer = (change: IChange) => void;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 自定义运行:tracker 只负责求值并收集依赖,
|
|
128
|
+
* 依赖变化后重新求值,值发生变更才调用 subscriber。
|
|
129
|
+
*/
|
|
130
|
+
export declare function reaction<T>(tracker: () => T, subscriber?: (value: T, oldValue: T) => void, options?: IReactionOptions<T>): Dispose;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 创建纯 Vue 响应式对象(深响应),同时支持 field / accessor 装饰器。
|
|
134
|
+
* 与 observable 的区别:不包 formvk 外层代理,没有变更路径传播,不能被 observe 监听。
|
|
135
|
+
*/
|
|
136
|
+
export declare const reactive: IReactive;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 注册“跳过包装”的判定,可注册多个,任一命中即跳过;返回销毁函数。
|
|
140
|
+
* 命中的值不会生成外层 proxy,元素本身及其嵌套结构保持原始引用。
|
|
141
|
+
*/
|
|
142
|
+
export declare function registerSkip(matcher: SkipMatcher): () => void;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 创建纯 Vue 浅响应式对象,同时支持 field / accessor 装饰器。
|
|
146
|
+
* 不包 formvk 外层代理,不可被 observe,变更请用 autorun 监听。
|
|
147
|
+
*/
|
|
148
|
+
export declare const shallow: IShallow;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 浅响应式盒子(Vue shallowRef):只有整体替换 .value 才触发通知,
|
|
152
|
+
* 盒子内对象的深层变更不具响应性。不可被 observe,变更请用 autorun 监听。
|
|
153
|
+
*/
|
|
154
|
+
export declare const shallowRef: IShallowRef;
|
|
155
|
+
|
|
156
|
+
export declare type SkipMatcher = (value: object) => boolean;
|
|
157
|
+
|
|
158
|
+
/** 把可观察/响应式对象深拷贝为普通对象,其余值原样返回 */
|
|
159
|
+
export declare function toJS<T>(values: T): T;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 手动追踪器:track 时立即执行 view 并收集其中读取的响应式依赖,
|
|
163
|
+
* 依赖变化时只触发 scheduler 通知外部,不会自动重跑 view,
|
|
164
|
+
* 由外部(如 React 的 forceUpdate)决定何时再次调用 track。
|
|
165
|
+
* 主要供 UI 框架在渲染期手动接管追踪时机。
|
|
166
|
+
*/
|
|
167
|
+
export declare class Tracker {
|
|
168
|
+
private runner;
|
|
169
|
+
private view;
|
|
170
|
+
private result;
|
|
171
|
+
constructor(scheduler?: () => void);
|
|
172
|
+
/** 执行 view 并重新收集依赖,返回本次执行结果;可重复调用,依赖以最近一次为准 */
|
|
173
|
+
track<T>(view: () => T): T;
|
|
174
|
+
/** 停止追踪并释放全部响应式依赖 */
|
|
175
|
+
dispose: Dispose;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 在回调执行期间暂停依赖收集,也支持 untracked.bound 绑定上下文 */
|
|
179
|
+
export declare const untracked: Boundary;
|
|
180
|
+
|
|
181
|
+
/** computed 函数形态的可写返回值 */
|
|
182
|
+
export declare interface WritableComputedValue<T> {
|
|
183
|
+
value: T;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export { }
|