[Typescript] Use Bitwise Flags
export enum EffectFlags {
/**
* ReactiveEffect only
*/
ACTIVE = 1 << 0,
RUNNING = 1 << 1,
TRACKING = 1 << 2,
NOTIFIED = 1 << 3,
DIRTY = 1 << 4,
ALLOW_RECURSE = 1 << 5,
PAUSED = 1 << 6,
}
What is 1 << N
?
1 << N
is a bitwise left shift operation, where the binary representation of 1
is shifted N
positions to the left. This is used to create flags that represent powers of 2 (1, 2, 4, 8, etc.). These values can be combined using bitwise operations (e.g., |
for combining flags or &
for checking flags).
For example:
1 << 0
is00000001
in binary (value1
).1 << 1
is00000010
in binary (value2
).1 << 2
is00000100
in binary (value4
).- And so on.
Explanation of the Enum
The EffectFlags
enum defines constants with specific meanings, as hinted by their names and comments. Here's what each flag might represent:
-
ACTIVE = 1 << 0
(value:1
)- Indicates that the effect is active or enabled.
-
RUNNING = 1 << 1
(value:2
)- Represents an effect that is currently running.
-
TRACKING = 1 << 2
(value:4
)- Denotes that the effect is tracking dependencies (e.g., observing reactive data).
-
NOTIFIED = 1 << 3
(value:8
)- Indicates the effect has been notified of a change.
-
DIRTY = 1 << 4
(value:16
)- Suggests that the effect is "dirty" and needs to be re-evaluated (often due to changes in dependencies).
-
ALLOW_RECURSE = 1 << 5
(value:32
)- Allows the effect to recurse (e.g., handle nested calls safely).
-
PAUSED = 1 << 6
(value:64
)- Represents that the effect is paused and will not execute until resumed.
Why Use Bitwise Flags?
-
Efficiency: Bitwise flags allow multiple states to be represented in a single number, making checks and combinations very fast.
-
Combination: You can combine states using the bitwise
|
operator. For example:
const combined = EffectFlags.ACTIVE | EffectFlags.DIRTY;
This creates a combined state with both ACTIVE
and DIRTY
.
-
Checking: You can check for specific flags using the bitwise
&
operator:
if (combined & EffectFlags.DIRTY) {
console.log('Effect is dirty');
}
Typical Usage
This kind of flag system is often used in libraries like Vue.js or Reactivity systems to manage and optimize dependency tracking, ensuring minimal recomputation by efficiently marking states.
For example, in Vue’s reactivity:
- An effect might transition from
ACTIVE
→TRACKING
→DIRTY
. - Flags like
NOTIFIED
orPAUSED
might be used during event handling or debugging.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2022-11-19 [Typescript] 111. Hard - String Join
2022-11-19 [Typescript] 110. Hard - Union to Tuple
2020-11-19 [React] Broadcaster + Operator + Listener pattern -- 20. useBroadcaster & useListener Example
2019-11-19 [Web] Adaptive loading
2015-11-19 [Angular + Webpack] ocLazyLoad compoment
2015-11-19 [Falcor] Retrieving Multiple Values
2014-11-19 [ES6] 05. The leg keyword -- 3. Block Scope