@whitesev/utils 2.2.8 → 2.3.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/Vue.ts ADDED
@@ -0,0 +1,241 @@
1
+ const VueUtils = {
2
+ /** 标签 */
3
+ ReactiveFlags: {
4
+ IS_REACTIVE: Symbol("isReactive"),
5
+ },
6
+ /**
7
+ * 判断是否是对象
8
+ * @param value
9
+ */
10
+ isObject(value: any) {
11
+ return typeof value === "object" && value !== null;
12
+ },
13
+ /**
14
+ * 判断是否是函数
15
+ * @param val
16
+ */
17
+ isFunction(val: any) {
18
+ return typeof val === "function";
19
+ },
20
+ /**
21
+ * 处理对象再次代理,可以直接返回
22
+ * @param value
23
+ */
24
+ isReactive(value: any) {
25
+ return !!(value && value[VueUtils.ReactiveFlags.IS_REACTIVE]);
26
+ },
27
+ /**
28
+ * 判断是否是数组
29
+ * @param value
30
+ */
31
+ isArray(value: any): boolean {
32
+ return Array.isArray(value);
33
+ },
34
+ };
35
+
36
+ class ReactiveEffect {
37
+ deps: any[] = [];
38
+ private active = true;
39
+ private fn;
40
+ // @ts-ignore
41
+ private scheduler;
42
+ constructor(fn: Function, scheduler: any) {
43
+ this.fn = fn;
44
+ this.scheduler = scheduler;
45
+ }
46
+ run(cb: (activeEffect: any) => void) {
47
+ if (!this.active) {
48
+ this.fn();
49
+ }
50
+ try {
51
+ if (typeof cb === "function") {
52
+ cb(this);
53
+ }
54
+ return this.fn();
55
+ } finally {
56
+ if (typeof cb === "function") {
57
+ cb(undefined);
58
+ }
59
+ }
60
+ }
61
+ }
62
+ class RefImpl {
63
+ _value;
64
+ _isRef = true;
65
+ _rawValue;
66
+ _vue: Vue;
67
+ constructor(vueIns: Vue, rawValue: any) {
68
+ this._vue = vueIns;
69
+ this._rawValue = rawValue;
70
+ this._value = this._vue.toReactive(rawValue);
71
+ }
72
+ get value() {
73
+ return this._value;
74
+ }
75
+ set value(newValue) {
76
+ if (newValue !== this._rawValue) {
77
+ this._value = this._vue.toReactive(newValue);
78
+ this._rawValue = newValue;
79
+ }
80
+ }
81
+ }
82
+ class ObjectRefImpl {
83
+ object;
84
+ key;
85
+ constructor(object: any, key: any) {
86
+ this.object = object;
87
+ this.key = key;
88
+ }
89
+ get value() {
90
+ return this.object[this.key];
91
+ }
92
+ set value(newValue) {
93
+ this.object[this.key] = newValue;
94
+ }
95
+ }
96
+ export class Vue {
97
+ private reactMap = new WeakMap();
98
+ private targetMap = new WeakMap();
99
+ private activeEffect = undefined as any as ReactiveEffect;
100
+ constructor() {
101
+ // 将数据转化成响应式的数据,只能做对象的代理
102
+ }
103
+ /**
104
+ * 生成一个被代理的对象
105
+ * @param target 需要代理的对象
106
+ */
107
+ reactive<T extends object>(target: T): T {
108
+ const that = this;
109
+ if (!(typeof target === "object" && target !== null)) {
110
+ // @ts-ignore
111
+ return;
112
+ }
113
+ if (VueUtils.isReactive(target)) {
114
+ return target;
115
+ }
116
+ let exisProxy = this.reactMap.get(target);
117
+ if (exisProxy) {
118
+ return exisProxy;
119
+ }
120
+ const proxy = new Proxy(target, {
121
+ get(target, key, receiver) {
122
+ if (key === VueUtils.ReactiveFlags.IS_REACTIVE) {
123
+ return true;
124
+ }
125
+ that.track(target, "get", key);
126
+ return Reflect.get(target, key, receiver);
127
+ },
128
+ set(target, key, value, receiver) {
129
+ let oldValue = target[key as keyof T];
130
+ let result = Reflect.set(target, key, value, receiver);
131
+ if (oldValue !== value) {
132
+ that.trigger(target, "set", key, oldValue, value);
133
+ }
134
+ return result;
135
+ },
136
+ });
137
+ that.reactMap.set(target, proxy);
138
+ return proxy;
139
+ }
140
+ /**
141
+ * 观察被reactive的对象值改变
142
+ * @param source 被观察的对象,这里采用函数返回对象
143
+ * @param changeCallBack 值改变的回调
144
+ */
145
+ watch<T>(
146
+ source: () => T,
147
+ changeCallBack: (newValue: T | undefined, oldValue: T | undefined) => void
148
+ ) {
149
+ let getter;
150
+ if (VueUtils.isReactive(source)) {
151
+ getter = () => this.traversal(source);
152
+ } else if (VueUtils.isFunction(source)) {
153
+ getter = source;
154
+ } else {
155
+ return;
156
+ }
157
+ let oldValue: any;
158
+ const job = () => {
159
+ const newValue = effect.run((activeEffect) => {
160
+ this.activeEffect = activeEffect;
161
+ });
162
+ changeCallBack(newValue, oldValue);
163
+ oldValue = newValue;
164
+ };
165
+ const effect = new ReactiveEffect(getter, job);
166
+ oldValue = effect.run((activeEffect) => {
167
+ this.activeEffect = activeEffect;
168
+ });
169
+ }
170
+ toReactive(value: any) {
171
+ return VueUtils.isObject(value) ? this.reactive(value) : value;
172
+ }
173
+ ref(value: any) {
174
+ return new RefImpl(this, value);
175
+ }
176
+ toRef(object: any, key: any) {
177
+ return new ObjectRefImpl(object, key);
178
+ }
179
+ toRefs(object: any) {
180
+ const result = VueUtils.isArray(object) ? new Array(object.length) : {};
181
+ for (let key in object) {
182
+ // @ts-ignore
183
+ result[key] = this.toRef(object, key);
184
+ }
185
+ return result;
186
+ }
187
+ private trigger(
188
+ target: any,
189
+ type: string,
190
+ key: string | symbol,
191
+ oldValue: any,
192
+ value: any
193
+ ) {
194
+ const depsMap = this.targetMap.get(target);
195
+ if (!depsMap) return;
196
+ const effects = depsMap.get(key);
197
+ this.triggerEffect(effects, "effects");
198
+ }
199
+ private triggerEffect(effects: any[], name: string) {
200
+ effects &&
201
+ effects.forEach((effect) => {
202
+ if (effect.scheduler) {
203
+ effect.scheduler();
204
+ } else {
205
+ effect.run();
206
+ }
207
+ });
208
+ }
209
+ private track(target: WeakKey, type: string, key: string | symbol) {
210
+ if (!this.activeEffect) return;
211
+ let depsMap = this.targetMap.get(target);
212
+ if (!depsMap) {
213
+ this.targetMap.set(target, (depsMap = new Map()));
214
+ }
215
+ let dep = depsMap.get(key);
216
+ if (!dep) {
217
+ depsMap.set(key, (dep = new Set()));
218
+ }
219
+ this.trackEffect(dep);
220
+ }
221
+ private trackEffect(dep: any) {
222
+ if (this.activeEffect) {
223
+ let shouldTrack = !dep.has(this.activeEffect);
224
+ if (shouldTrack) {
225
+ dep.add(this.activeEffect);
226
+ this.activeEffect.deps.push(dep);
227
+ }
228
+ }
229
+ }
230
+ private traversal(value: any, set = new Set()) {
231
+ if (!VueUtils.isObject(value)) return value;
232
+ if (set.has(value)) {
233
+ return value;
234
+ }
235
+ set.add(value);
236
+ for (let key in value) {
237
+ this.traversal(value[key], set);
238
+ }
239
+ return value;
240
+ }
241
+ }
package/src/VueObject.ts CHANGED
@@ -1,52 +1,50 @@
1
- import type { AnyObject } from "./Utils";
2
-
3
- export declare interface Vue2Object extends AnyObject {
4
- $attrs: AnyObject;
1
+ export declare interface Vue2Object {
2
+ $attrs: any;
5
3
  $children: Vue2Object[];
6
4
  $createElement: (...args: any[]) => any;
7
5
  $el: HTMLElement;
8
- $listeners: AnyObject;
9
- $options: AnyObject;
6
+ $listeners: any;
7
+ $options: any;
10
8
  $parent: Vue2Object;
11
- $refs: AnyObject;
9
+ $refs: any;
12
10
  $root: Vue2Object;
13
- $scopedSlots: AnyObject;
14
- $slots: AnyObject;
15
- $store: AnyObject;
16
- $vnode: AnyObject;
11
+ $scopedSlots: any;
12
+ $slots: any;
13
+ $store: any;
14
+ $vnode: any;
17
15
 
18
- _data: AnyObject;
16
+ _data: any;
19
17
  _directInactive: boolean;
20
- _events: AnyObject;
18
+ _events: any;
21
19
  _hasHookEvent: boolean;
22
20
  _isBeingDestroyed: boolean;
23
21
  _isDestroyed: boolean;
24
22
  _isMounted: boolean;
25
23
  _isVue: boolean;
26
24
 
27
- $data: AnyObject;
25
+ $data: any;
28
26
  $isServer: boolean;
29
- $props: AnyObject;
30
- $route: AnyObject & {
27
+ $props: any;
28
+ $route: any & {
31
29
  fullPath: string;
32
30
  hash: string;
33
- matched: AnyObject[];
34
- meta: AnyObject;
31
+ matched: any[];
32
+ meta: any;
35
33
  name: string;
36
- params: AnyObject;
34
+ params: any;
37
35
  path: string;
38
- query: AnyObject;
36
+ query: any;
39
37
  };
40
- $router: AnyObject & {
38
+ $router: any & {
41
39
  afterHooks: Function[];
42
40
  app: Vue2Object;
43
41
  apps: Vue2Object[];
44
42
  beforeHooks: Function[];
45
43
  fallback: boolean;
46
- history: AnyObject & {
44
+ history: any & {
47
45
  base: string;
48
- current: AnyObject;
49
- listeners: AnyObject[];
46
+ current: any;
47
+ listeners: any[];
50
48
  router: Vue2Object["$router"];
51
49
  /**
52
50
  *
@@ -61,16 +59,16 @@ export declare interface Vue2Object extends AnyObject {
61
59
  * @param data 可选的 HistoryState 以关联该导航记录
62
60
  * @returns
63
61
  */
64
- push: (to: string, data?: AnyObject) => void;
62
+ push: (to: string, data?: any) => void;
65
63
  /**
66
64
  *
67
65
  * @param to 要设置的地址
68
66
  * @param data 可选的 HistoryState 以关联该导航记录
69
67
  * @returns
70
68
  */
71
- replace: (to: string, data?: AnyObject) => void;
69
+ replace: (to: string, data?: any) => void;
72
70
  };
73
- matcher: AnyObject & {
71
+ matcher: any & {
74
72
  addRoute: (...args: any[]) => any;
75
73
  addRoutes: (...args: any[]) => any;
76
74
  getRoutes: () => any;
@@ -78,7 +76,7 @@ export declare interface Vue2Object extends AnyObject {
78
76
  };
79
77
  mode: string;
80
78
  resolveHooks: ((...args: any[]) => any)[];
81
- currentRoute: AnyObject;
79
+ currentRoute: any;
82
80
  beforeEach: (
83
81
  callback:
84
82
  | ((
@@ -111,7 +109,7 @@ export declare interface Vue2Object extends AnyObject {
111
109
  | (() => void)
112
110
  ) => void;
113
111
  };
114
- $ssrContext: AnyObject;
112
+ $ssrContext: any;
115
113
 
116
114
  $watch: (
117
115
  key: string | string[] | (() => any),
@@ -121,6 +119,8 @@ export declare interface Vue2Object extends AnyObject {
121
119
  deep?: boolean;
122
120
  }
123
121
  ) => void;
122
+
123
+ [key: string]: any;
124
124
  }
125
125
 
126
126
  export declare interface HTMLVue2DivElement extends HTMLDivElement {