@feng3d/reactivity 1.0.8 → 1.0.9

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/effect.ts CHANGED
@@ -1,146 +1,146 @@
1
- import { batch, batchRun } from './batch';
2
- import { ComputedReactivity } from './computed';
3
- import { activeEffectScope } from './effectScope';
4
- import { Reactivity } from './Reactivity';
5
-
6
- /**
7
- * 创建效果反应式节点。
8
- *
9
- * 将会维持反应式效果,当被作用的函数所引用的响应式对象发生变化时,会立即执行 fn 函数。
10
- *
11
- * @param fn 要执行的函数
12
- * @returns 暂停和恢复副作用的函数
13
- *
14
- * 注:
15
- * 1. 与 `@vue/reactivity` 中的 effect 不同,此函数返回的是一个 Effect 对象,而不是一个函数。
16
- * 2. 不希望用户直接执行,而是通过反应式自动触发。
17
- * 3. 真有需求,可以使用 effect(func).run(true) 来代替 @vue/reactivity 中的 effect(func)() 。
18
- *
19
- */
20
- export function effect<T = any>(fn: () => T): Effect
21
- {
22
- return new EffectReactivity(fn);
23
- }
24
-
25
- /**
26
- * 效果反应式节点。
27
- */
28
- export class EffectReactivity<T = any> extends ComputedReactivity<T> implements Effect
29
- {
30
- /**
31
- * 是否为启用, 默认为 true。
32
- *
33
- * 启用时,会立即执行函数。
34
- */
35
- private _isEnable = true;
36
-
37
- constructor(func: (oldValue?: T) => T)
38
- {
39
- super(func);
40
- if (activeEffectScope && activeEffectScope.active)
41
- {
42
- activeEffectScope.effects.push(this);
43
- }
44
- this.runIfDirty();
45
- }
46
-
47
- /**
48
- * 暂停效果。
49
- *
50
- * 暂停后,当依赖发生变化时不会自动执行。
51
- */
52
- pause()
53
- {
54
- this._isEnable = false;
55
- }
56
-
57
- /**
58
- * 恢复效果。
59
- *
60
- * 恢复后,当依赖发生变化时会自动执行。
61
- */
62
- resume()
63
- {
64
- if (this._isEnable) return;
65
- this._isEnable = true;
66
- if (EffectReactivity.pausedQueueEffects.has(this))
67
- {
68
- EffectReactivity.pausedQueueEffects.delete(this);
69
- this.trigger();
70
- }
71
- }
72
-
73
- /**
74
- * 停止效果。
75
- *
76
- * 停止后,效果将不再响应依赖的变化。
77
- */
78
- stop()
79
- {
80
- this._isEnable = false;
81
- EffectReactivity.pausedQueueEffects.delete(this);
82
- }
83
-
84
- /**
85
- * 触发效果执行。
86
- *
87
- * 当依赖发生变化时,会调用此方法。
88
- */
89
- trigger()
90
- {
91
- batchRun(() =>
92
- {
93
- super.trigger();
94
-
95
- if (this._isEnable)
96
- {
97
- // 合批时需要判断是否已经运行的依赖。
98
- batch(this, Reactivity.activeReactivity === this);
99
- }
100
- else
101
- {
102
- EffectReactivity.pausedQueueEffects.add(this);
103
- }
104
- });
105
- }
106
-
107
- private static pausedQueueEffects = new WeakSet<EffectReactivity>();
108
-
109
- /**
110
- * 执行当前节点。
111
- *
112
- * 当暂停时将会直接执行被包装的函数。
113
- */
114
- run(): void
115
- {
116
- if (this._isEnable)
117
- {
118
- super.run();
119
- }
120
- else
121
- {
122
- this._func(this._value);
123
- }
124
- }
125
- }
126
-
127
- /**
128
- * 维持反应式效果。
129
- */
130
- export interface Effect
131
- {
132
- /**
133
- * 暂停。
134
- */
135
- pause: () => void;
136
-
137
- /**
138
- * 恢复。
139
- */
140
- resume: () => void;
141
-
142
- /**
143
- * 停止。
144
- */
145
- stop: () => void;
146
- }
1
+ import { batch, batchRun } from './batch';
2
+ import { ComputedReactivity } from './computed';
3
+ import { activeEffectScope } from './effectScope';
4
+ import { Reactivity } from './Reactivity';
5
+
6
+ /**
7
+ * 创建效果反应式节点。
8
+ *
9
+ * 将会维持反应式效果,当被作用的函数所引用的响应式对象发生变化时,会立即执行 fn 函数。
10
+ *
11
+ * @param fn 要执行的函数
12
+ * @returns 暂停和恢复副作用的函数
13
+ *
14
+ * 注:
15
+ * 1. 与 `@vue/reactivity` 中的 effect 不同,此函数返回的是一个 Effect 对象,而不是一个函数。
16
+ * 2. 不希望用户直接执行,而是通过反应式自动触发。
17
+ * 3. 真有需求,可以使用 effect(func).run(true) 来代替 @vue/reactivity 中的 effect(func)() 。
18
+ *
19
+ */
20
+ export function effect<T = any>(fn: () => T): Effect
21
+ {
22
+ return new EffectReactivity(fn);
23
+ }
24
+
25
+ /**
26
+ * 效果反应式节点。
27
+ */
28
+ export class EffectReactivity<T = any> extends ComputedReactivity<T> implements Effect
29
+ {
30
+ /**
31
+ * 是否为启用, 默认为 true。
32
+ *
33
+ * 启用时,会立即执行函数。
34
+ */
35
+ private _isEnable = true;
36
+
37
+ constructor(func: (oldValue?: T) => T)
38
+ {
39
+ super(func);
40
+ if (activeEffectScope && activeEffectScope.active)
41
+ {
42
+ activeEffectScope.effects.push(this);
43
+ }
44
+ this.runIfDirty();
45
+ }
46
+
47
+ /**
48
+ * 暂停效果。
49
+ *
50
+ * 暂停后,当依赖发生变化时不会自动执行。
51
+ */
52
+ pause()
53
+ {
54
+ this._isEnable = false;
55
+ }
56
+
57
+ /**
58
+ * 恢复效果。
59
+ *
60
+ * 恢复后,当依赖发生变化时会自动执行。
61
+ */
62
+ resume()
63
+ {
64
+ if (this._isEnable) return;
65
+ this._isEnable = true;
66
+ if (EffectReactivity.pausedQueueEffects.has(this))
67
+ {
68
+ EffectReactivity.pausedQueueEffects.delete(this);
69
+ this.trigger();
70
+ }
71
+ }
72
+
73
+ /**
74
+ * 停止效果。
75
+ *
76
+ * 停止后,效果将不再响应依赖的变化。
77
+ */
78
+ stop()
79
+ {
80
+ this._isEnable = false;
81
+ EffectReactivity.pausedQueueEffects.delete(this);
82
+ }
83
+
84
+ /**
85
+ * 触发效果执行。
86
+ *
87
+ * 当依赖发生变化时,会调用此方法。
88
+ */
89
+ trigger()
90
+ {
91
+ batchRun(() =>
92
+ {
93
+ super.trigger();
94
+
95
+ if (this._isEnable)
96
+ {
97
+ // 合批时需要判断是否已经运行的依赖。
98
+ batch(this, Reactivity.activeReactivity === this);
99
+ }
100
+ else
101
+ {
102
+ EffectReactivity.pausedQueueEffects.add(this);
103
+ }
104
+ });
105
+ }
106
+
107
+ private static pausedQueueEffects = new WeakSet<EffectReactivity>();
108
+
109
+ /**
110
+ * 执行当前节点。
111
+ *
112
+ * 当暂停时将会直接执行被包装的函数。
113
+ */
114
+ run(): void
115
+ {
116
+ if (this._isEnable)
117
+ {
118
+ super.run();
119
+ }
120
+ else
121
+ {
122
+ this._func(this._value);
123
+ }
124
+ }
125
+ }
126
+
127
+ /**
128
+ * 维持反应式效果。
129
+ */
130
+ export interface Effect
131
+ {
132
+ /**
133
+ * 暂停。
134
+ */
135
+ pause: () => void;
136
+
137
+ /**
138
+ * 恢复。
139
+ */
140
+ resume: () => void;
141
+
142
+ /**
143
+ * 停止。
144
+ */
145
+ stop: () => void;
146
+ }