@feng3d/reactivity 1.0.8 → 1.0.11
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/LICENSE +15 -21
- package/README.md +158 -158
- package/dist/index.js +208 -46
- package/dist/index.js.map +1 -1
- package/dist/index.umd.cjs +209 -47
- package/dist/index.umd.cjs.map +1 -1
- package/lib/arrayInstrumentations.d.ts.map +1 -1
- package/lib/batch.d.ts.map +1 -1
- package/lib/effectScope.d.ts.map +1 -1
- package/package.json +76 -69
- package/src/ReactiveObject.ts +130 -130
- package/src/Reactivity.ts +168 -168
- package/src/arrayInstrumentations.ts +799 -801
- package/src/baseHandlers.ts +312 -312
- package/src/batch.ts +130 -134
- package/src/collectionHandlers.ts +486 -486
- package/src/computed.ts +253 -253
- package/src/effect.ts +146 -146
- package/src/effectScope.ts +293 -294
- package/src/index.ts +9 -10
- package/src/property.ts +231 -231
- package/src/reactive.ts +186 -186
- package/src/ref.ts +150 -150
- package/src/shared/constants.ts +41 -41
- package/src/shared/general.ts +109 -109
- package/tsconfig.json +21 -20
package/dist/index.umd.cjs
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
(function(global, factory) {
|
|
2
2
|
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.feng3d = {}));
|
|
3
3
|
})(this, (function(exports2) {
|
|
4
|
-
"use strict";
|
|
5
|
-
|
|
4
|
+
"use strict";var __defProp = Object.defineProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
|
|
8
|
+
var _a, _b;
|
|
9
|
+
const _Reactivity = class _Reactivity {
|
|
6
10
|
constructor() {
|
|
7
|
-
|
|
11
|
+
/**
|
|
12
|
+
* @private
|
|
13
|
+
*/
|
|
14
|
+
__publicField(this, "_value");
|
|
15
|
+
/**
|
|
16
|
+
* 父反应节点集合。
|
|
17
|
+
*
|
|
18
|
+
* 记录了哪些节点依赖了当前节点。
|
|
19
|
+
* 当当前节点值发生变化时,会通知所有父节点。
|
|
20
|
+
*
|
|
21
|
+
* Map 的 key 是父节点,value 是父节点的版本号。
|
|
22
|
+
* 版本号用于判断依赖关系是否过期。
|
|
23
|
+
*
|
|
24
|
+
* @private
|
|
25
|
+
*/
|
|
26
|
+
__publicField(this, "_parents", /* @__PURE__ */ new Map());
|
|
8
27
|
}
|
|
9
28
|
/**
|
|
10
29
|
* 获取当前节点值。
|
|
@@ -25,8 +44,8 @@
|
|
|
25
44
|
* 如果当前没有活动的响应式节点,或者不应该跟踪依赖,则不会建立依赖关系。
|
|
26
45
|
*/
|
|
27
46
|
track() {
|
|
28
|
-
if (!
|
|
29
|
-
const parent =
|
|
47
|
+
if (!_Reactivity.activeReactivity || !_shouldTrack) return;
|
|
48
|
+
const parent = _Reactivity.activeReactivity;
|
|
30
49
|
if (parent) {
|
|
31
50
|
this._parents.set(parent, parent._version);
|
|
32
51
|
}
|
|
@@ -51,7 +70,17 @@
|
|
|
51
70
|
});
|
|
52
71
|
this._parents.clear();
|
|
53
72
|
}
|
|
54
|
-
}
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* 当前正在执行的反应式节点。
|
|
76
|
+
*
|
|
77
|
+
* 用于在依赖收集过程中标识当前正在执行的节点。
|
|
78
|
+
* 当其他节点访问此节点的值时,会将其作为父节点。
|
|
79
|
+
*
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
__publicField(_Reactivity, "activeReactivity");
|
|
83
|
+
let Reactivity = _Reactivity;
|
|
55
84
|
function forceTrack(fn) {
|
|
56
85
|
const preShouldTrack = _shouldTrack;
|
|
57
86
|
_shouldTrack = true;
|
|
@@ -69,15 +98,13 @@
|
|
|
69
98
|
let _shouldTrack = true;
|
|
70
99
|
function batch(dep, isRunning) {
|
|
71
100
|
if (isRunning) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
console.warn("dep already in _isRunedDeps");
|
|
101
|
+
if (_isRunedDeps.indexOf(dep) !== -1) {
|
|
102
|
+
return;
|
|
75
103
|
}
|
|
76
104
|
_isRunedDeps.push(dep);
|
|
77
105
|
} else {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
console.warn("dep already in _needEffectDeps,存在多重依赖问题。可能是由于副作用中多次修改了同一个响应式对象。");
|
|
106
|
+
if (_needEffectDeps.indexOf(dep) !== -1) {
|
|
107
|
+
return;
|
|
81
108
|
}
|
|
82
109
|
_needEffectDeps.push(dep);
|
|
83
110
|
}
|
|
@@ -90,6 +117,7 @@
|
|
|
90
117
|
}
|
|
91
118
|
if (_isRunedDeps.length > 0) {
|
|
92
119
|
_isRunedDeps.forEach((dep) => {
|
|
120
|
+
__DEV__ && console.assert(dep._isDirty === false, "dep.dirty === false");
|
|
93
121
|
dep._children.forEach((version, node) => {
|
|
94
122
|
node._parents.set(dep, dep._version);
|
|
95
123
|
});
|
|
@@ -122,10 +150,48 @@
|
|
|
122
150
|
*/
|
|
123
151
|
constructor(func) {
|
|
124
152
|
super();
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
153
|
+
/**
|
|
154
|
+
* 标识这是一个 ref 对象。
|
|
155
|
+
*
|
|
156
|
+
* @internal
|
|
157
|
+
*/
|
|
158
|
+
__publicField(this, "__v_isRef", true);
|
|
159
|
+
/**
|
|
160
|
+
* 计算函数。
|
|
161
|
+
*
|
|
162
|
+
* 用于计算属性值的函数,可以访问其他响应式数据。
|
|
163
|
+
* 当依赖发生变化时,会重新执行此函数。
|
|
164
|
+
*/
|
|
165
|
+
__publicField(this, "_func");
|
|
166
|
+
/**
|
|
167
|
+
* 失效子节点集合。
|
|
168
|
+
*
|
|
169
|
+
* 记录所有依赖此计算属性的子节点。
|
|
170
|
+
* 当计算属性重新计算时,会通知这些子节点。
|
|
171
|
+
*
|
|
172
|
+
* @private
|
|
173
|
+
*/
|
|
174
|
+
__publicField(this, "_children", /* @__PURE__ */ new Map());
|
|
175
|
+
/**
|
|
176
|
+
* 脏标记。
|
|
177
|
+
*
|
|
178
|
+
* 表示计算属性是否需要重新计算。
|
|
179
|
+
* 当依赖发生变化时,会设置此标记。
|
|
180
|
+
* 重新计算后会清除此标记。
|
|
181
|
+
*
|
|
182
|
+
* @private
|
|
183
|
+
*/
|
|
184
|
+
__publicField(this, "_isDirty", true);
|
|
185
|
+
/**
|
|
186
|
+
* 版本号。
|
|
187
|
+
*
|
|
188
|
+
* 每次重新计算后自动递增。
|
|
189
|
+
* 用于判断子节点中的父节点引用是否过期。
|
|
190
|
+
* 当子节点发现父节点的版本号不匹配时,会重新建立依赖关系。
|
|
191
|
+
*
|
|
192
|
+
* @private
|
|
193
|
+
*/
|
|
194
|
+
__publicField(this, "_version", -1);
|
|
129
195
|
this._func = func;
|
|
130
196
|
}
|
|
131
197
|
/**
|
|
@@ -250,9 +316,9 @@
|
|
|
250
316
|
TriggerOpTypes2["CLEAR"] = "clear";
|
|
251
317
|
return TriggerOpTypes2;
|
|
252
318
|
})(TriggerOpTypes || {});
|
|
253
|
-
const ITERATE_KEY = Symbol("");
|
|
254
|
-
const MAP_KEY_ITERATE_KEY = Symbol("");
|
|
255
|
-
const ARRAY_ITERATE_KEY = Symbol("");
|
|
319
|
+
const ITERATE_KEY = Symbol(__DEV__ ? "Object iterate" : "");
|
|
320
|
+
const MAP_KEY_ITERATE_KEY = Symbol(__DEV__ ? "Map keys iterate" : "");
|
|
321
|
+
const ARRAY_ITERATE_KEY = Symbol(__DEV__ ? "Array iterate" : "");
|
|
256
322
|
globalThis.__DEV__ ?? (globalThis.__DEV__ = true);
|
|
257
323
|
const isObject = (val) => val !== null && typeof val === "object";
|
|
258
324
|
const isArray = Array.isArray;
|
|
@@ -289,6 +355,9 @@
|
|
|
289
355
|
const raw = observed && observed[ReactiveFlags.RAW];
|
|
290
356
|
return raw ? toRaw(raw) : observed;
|
|
291
357
|
}
|
|
358
|
+
function warn(msg, ...args) {
|
|
359
|
+
console.warn(`[警告] ${msg}`, ...args);
|
|
360
|
+
}
|
|
292
361
|
// @__NO_SIDE_EFFECTS__
|
|
293
362
|
function makeMap(str) {
|
|
294
363
|
const map = /* @__PURE__ */ Object.create(null);
|
|
@@ -302,12 +371,50 @@
|
|
|
302
371
|
* @param detached 是否创建分离的作用域
|
|
303
372
|
*/
|
|
304
373
|
constructor(detached = false) {
|
|
374
|
+
/**
|
|
375
|
+
* 作用域是否处于活动状态
|
|
376
|
+
* @internal
|
|
377
|
+
*/
|
|
378
|
+
__publicField(this, "_active", true);
|
|
379
|
+
/**
|
|
380
|
+
* 跟踪 on 方法的调用次数,允许多次调用 on 方法
|
|
381
|
+
* @internal
|
|
382
|
+
*/
|
|
383
|
+
__publicField(this, "_on", 0);
|
|
384
|
+
/**
|
|
385
|
+
* 存储当前作用域中的所有效果
|
|
386
|
+
* @internal
|
|
387
|
+
*/
|
|
388
|
+
__publicField(this, "effects", []);
|
|
389
|
+
/**
|
|
390
|
+
* 存储清理函数
|
|
391
|
+
* @internal
|
|
392
|
+
*/
|
|
393
|
+
__publicField(this, "cleanups", []);
|
|
394
|
+
/**
|
|
395
|
+
* 作用域是否被暂停
|
|
396
|
+
*/
|
|
397
|
+
__publicField(this, "_isPaused", false);
|
|
398
|
+
/**
|
|
399
|
+
* 父作用域,仅由非分离的作用域分配
|
|
400
|
+
* @internal
|
|
401
|
+
*/
|
|
402
|
+
__publicField(this, "parent");
|
|
403
|
+
/**
|
|
404
|
+
* 记录未分离的子作用域
|
|
405
|
+
* @internal
|
|
406
|
+
*/
|
|
407
|
+
__publicField(this, "scopes");
|
|
408
|
+
/**
|
|
409
|
+
* 在父作用域的 scopes 数组中记录子作用域的索引,用于优化移除操作
|
|
410
|
+
* @internal
|
|
411
|
+
*/
|
|
412
|
+
__publicField(this, "index");
|
|
413
|
+
/**
|
|
414
|
+
* 前一个作用域
|
|
415
|
+
*/
|
|
416
|
+
__publicField(this, "prevScope");
|
|
305
417
|
this.detached = detached;
|
|
306
|
-
this._active = true;
|
|
307
|
-
this._on = 0;
|
|
308
|
-
this.effects = [];
|
|
309
|
-
this.cleanups = [];
|
|
310
|
-
this._isPaused = false;
|
|
311
418
|
this.parent = activeEffectScope;
|
|
312
419
|
if (!detached && activeEffectScope) {
|
|
313
420
|
this.index = (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
|
|
@@ -375,6 +482,8 @@
|
|
|
375
482
|
} finally {
|
|
376
483
|
activeEffectScope = currentEffectScope;
|
|
377
484
|
}
|
|
485
|
+
} else if (__DEV__) {
|
|
486
|
+
warn(`无法运行已停用的 effect 作用域。`);
|
|
378
487
|
}
|
|
379
488
|
}
|
|
380
489
|
/**
|
|
@@ -443,6 +552,10 @@
|
|
|
443
552
|
function onScopeDispose(fn, failSilently = false) {
|
|
444
553
|
if (activeEffectScope) {
|
|
445
554
|
activeEffectScope.cleanups.push(fn);
|
|
555
|
+
} else if (__DEV__ && !failSilently) {
|
|
556
|
+
warn(
|
|
557
|
+
`onScopeDispose() 在没有活动的 effect 作用域时被调用,无法关联。`
|
|
558
|
+
);
|
|
446
559
|
}
|
|
447
560
|
}
|
|
448
561
|
function effect(fn) {
|
|
@@ -451,7 +564,12 @@
|
|
|
451
564
|
const _EffectReactivity = class _EffectReactivity extends ComputedReactivity {
|
|
452
565
|
constructor(func) {
|
|
453
566
|
super(func);
|
|
454
|
-
|
|
567
|
+
/**
|
|
568
|
+
* 是否为启用, 默认为 true。
|
|
569
|
+
*
|
|
570
|
+
* 启用时,会立即执行函数。
|
|
571
|
+
*/
|
|
572
|
+
__publicField(this, "_isEnable", true);
|
|
455
573
|
if (activeEffectScope && activeEffectScope.active) {
|
|
456
574
|
activeEffectScope.effects.push(this);
|
|
457
575
|
}
|
|
@@ -515,7 +633,7 @@
|
|
|
515
633
|
}
|
|
516
634
|
}
|
|
517
635
|
};
|
|
518
|
-
_EffectReactivity
|
|
636
|
+
__publicField(_EffectReactivity, "pausedQueueEffects", /* @__PURE__ */ new WeakSet());
|
|
519
637
|
let EffectReactivity = _EffectReactivity;
|
|
520
638
|
function property(target, key) {
|
|
521
639
|
let depsMap = PropertyReactivity._targetMap.get(target);
|
|
@@ -530,7 +648,21 @@
|
|
|
530
648
|
}
|
|
531
649
|
return dep;
|
|
532
650
|
}
|
|
533
|
-
|
|
651
|
+
class PropertyReactivity extends Reactivity {
|
|
652
|
+
constructor(target, key) {
|
|
653
|
+
super();
|
|
654
|
+
__publicField(this, "_target");
|
|
655
|
+
__publicField(this, "_key");
|
|
656
|
+
this._target = target;
|
|
657
|
+
this._key = key;
|
|
658
|
+
if (target instanceof Map || target instanceof WeakMap) {
|
|
659
|
+
this._value = target.get(key);
|
|
660
|
+
} else if (target instanceof Set || target instanceof WeakSet) {
|
|
661
|
+
this._value = target.has(key);
|
|
662
|
+
} else {
|
|
663
|
+
this._value = target[key];
|
|
664
|
+
}
|
|
665
|
+
}
|
|
534
666
|
/**
|
|
535
667
|
* 获取当前节点值。
|
|
536
668
|
*
|
|
@@ -550,18 +682,6 @@
|
|
|
550
682
|
this.trigger();
|
|
551
683
|
this._value = v;
|
|
552
684
|
}
|
|
553
|
-
constructor(target, key) {
|
|
554
|
-
super();
|
|
555
|
-
this._target = target;
|
|
556
|
-
this._key = key;
|
|
557
|
-
if (target instanceof Map || target instanceof WeakMap) {
|
|
558
|
-
this._value = target.get(key);
|
|
559
|
-
} else if (target instanceof Set || target instanceof WeakSet) {
|
|
560
|
-
this._value = target.has(key);
|
|
561
|
-
} else {
|
|
562
|
-
this._value = target[key];
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
685
|
triggerIfChanged() {
|
|
566
686
|
}
|
|
567
687
|
/**
|
|
@@ -645,9 +765,11 @@
|
|
|
645
765
|
}
|
|
646
766
|
});
|
|
647
767
|
}
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* @private
|
|
771
|
+
*/
|
|
772
|
+
__publicField(PropertyReactivity, "_targetMap", /* @__PURE__ */ new WeakMap());
|
|
651
773
|
const arrayInstrumentations = {
|
|
652
774
|
__proto__: null,
|
|
653
775
|
/**
|
|
@@ -1128,7 +1250,6 @@
|
|
|
1128
1250
|
);
|
|
1129
1251
|
return res;
|
|
1130
1252
|
}
|
|
1131
|
-
var _a, _b;
|
|
1132
1253
|
function ref(value) {
|
|
1133
1254
|
if (isRef(value)) {
|
|
1134
1255
|
return value;
|
|
@@ -1146,7 +1267,21 @@
|
|
|
1146
1267
|
*/
|
|
1147
1268
|
constructor(value) {
|
|
1148
1269
|
super();
|
|
1149
|
-
|
|
1270
|
+
/**
|
|
1271
|
+
* 标识这是一个 ref 对象。
|
|
1272
|
+
*
|
|
1273
|
+
* 用于 isRef 函数判断对象是否为引用。
|
|
1274
|
+
*/
|
|
1275
|
+
__publicField(this, _a, true);
|
|
1276
|
+
/**
|
|
1277
|
+
* 原始值。
|
|
1278
|
+
*
|
|
1279
|
+
* 存储未经响应式处理的原始值。
|
|
1280
|
+
* 用于比较值是否发生变化。
|
|
1281
|
+
*
|
|
1282
|
+
* @private
|
|
1283
|
+
*/
|
|
1284
|
+
__publicField(this, "_rawValue");
|
|
1150
1285
|
this._rawValue = toRaw(value);
|
|
1151
1286
|
this._value = toReactive(value);
|
|
1152
1287
|
}
|
|
@@ -1267,6 +1402,7 @@
|
|
|
1267
1402
|
value,
|
|
1268
1403
|
isRef(target) ? target : receiver
|
|
1269
1404
|
);
|
|
1405
|
+
__DEV__ && console.assert(target === toRaw(receiver));
|
|
1270
1406
|
if (target === toRaw(receiver)) {
|
|
1271
1407
|
if (!hadKey) {
|
|
1272
1408
|
PropertyReactivity.trigger(target, TriggerOpTypes.ADD, key, value);
|
|
@@ -1493,6 +1629,8 @@
|
|
|
1493
1629
|
if (!hadKey) {
|
|
1494
1630
|
key = toRaw(key);
|
|
1495
1631
|
hadKey = has.call(target, key);
|
|
1632
|
+
} else if (__DEV__) {
|
|
1633
|
+
checkIdentityKeys(target, has, key);
|
|
1496
1634
|
}
|
|
1497
1635
|
const oldValue = get.call(target, key);
|
|
1498
1636
|
target.set(key, value);
|
|
@@ -1520,6 +1658,8 @@
|
|
|
1520
1658
|
if (!hadKey) {
|
|
1521
1659
|
key = toRaw(key);
|
|
1522
1660
|
hadKey = has.call(target, key);
|
|
1661
|
+
} else if (__DEV__) {
|
|
1662
|
+
checkIdentityKeys(target, has, key);
|
|
1523
1663
|
}
|
|
1524
1664
|
const oldValue = get ? get.call(target, key) : void 0;
|
|
1525
1665
|
const result = target.delete(key);
|
|
@@ -1541,7 +1681,7 @@
|
|
|
1541
1681
|
clear() {
|
|
1542
1682
|
const target = toRaw(this);
|
|
1543
1683
|
const hadItems = target.size !== 0;
|
|
1544
|
-
const oldTarget = void 0;
|
|
1684
|
+
const oldTarget = __DEV__ ? isMap(target) ? new Map(target) : new Set(target) : void 0;
|
|
1545
1685
|
const result = target.clear();
|
|
1546
1686
|
if (hadItems) {
|
|
1547
1687
|
PropertyReactivity.trigger(
|
|
@@ -1596,6 +1736,15 @@
|
|
|
1596
1736
|
};
|
|
1597
1737
|
};
|
|
1598
1738
|
}
|
|
1739
|
+
function checkIdentityKeys(target, has, key) {
|
|
1740
|
+
const rawKey = toRaw(key);
|
|
1741
|
+
if (rawKey !== key && has.call(target, rawKey)) {
|
|
1742
|
+
const type = toRawType(target);
|
|
1743
|
+
warn(
|
|
1744
|
+
`响应式 ${type} 同时包含同一对象的原始版本和响应式版本${type === `Map` ? `作为键` : ``},这可能导致不一致。建议仅使用响应式版本。`
|
|
1745
|
+
);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1599
1748
|
const getProto = (v) => Reflect.getPrototypeOf(v);
|
|
1600
1749
|
function reactive(target) {
|
|
1601
1750
|
if (!isObject(target)) {
|
|
@@ -1634,8 +1783,21 @@
|
|
|
1634
1783
|
}
|
|
1635
1784
|
class ReactiveObject {
|
|
1636
1785
|
constructor() {
|
|
1637
|
-
|
|
1638
|
-
|
|
1786
|
+
/**
|
|
1787
|
+
* 副作用作用域
|
|
1788
|
+
*
|
|
1789
|
+
* 用于管理所有副作用的生命周期:
|
|
1790
|
+
* - 收集所有通过 effect() 方法创建的副作用
|
|
1791
|
+
* - 在类销毁时自动停止所有副作用
|
|
1792
|
+
* - 防止副作用在类销毁后继续执行,避免内存泄漏
|
|
1793
|
+
*
|
|
1794
|
+
* 私有属性,外部无法直接访问,只能通过 effect() 方法使用
|
|
1795
|
+
*/
|
|
1796
|
+
__publicField(this, "_effectScope", new EffectScope());
|
|
1797
|
+
/**
|
|
1798
|
+
* 销毁时需要执行的函数
|
|
1799
|
+
*/
|
|
1800
|
+
__publicField(this, "_destroyCallbacks", []);
|
|
1639
1801
|
}
|
|
1640
1802
|
/**
|
|
1641
1803
|
* 创建并运行副作用
|