@virid/core 0.1.0 → 0.1.1

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.
Files changed (3) hide show
  1. package/README.md +273 -1
  2. package/README.zh.md +255 -1
  3. package/package.json +6 -6
package/README.md CHANGED
@@ -1 +1,273 @@
1
- todo
1
+ # @virid/core
2
+
3
+ `@virid/core` is the logical heart of the entire Virid ecosystem. It provides a deterministic message distribution and scheduling mechanism designed to decouple business logic from complex UI frameworks and runtime environments.
4
+
5
+ ## 🌟 Core Design Philosophy
6
+
7
+ - **Absolute Environment Independence**: This package does not depend on any browser, Node.js specific APIs, or third-party libraries (relying only on `reflect-metadata` for decorator functionality). This ensures the core can run seamlessly in Electron main processes, Worker threads, Web rendering layers, or even pure server environments.
8
+ - **Deterministic Scheduling**: It introduces a "Tick" mechanism similar to game engines, utilizing a double-buffered message pool to ensure the predictability of logic execution order.
9
+
10
+ ## 🛠️ Core Functional Overview
11
+
12
+ ### 1. Message-Driven & Dispatcher Mechanism
13
+
14
+ In Virid, all state changes must be triggered by sending a `Message` command.
15
+
16
+ - **Automatic Scheduling**: By defining specific types of `Message` and corresponding `System` handlers, the engine automatically invokes the registered logic in the next microtask cycle (Tick).
17
+ - **Message Types**:
18
+ - **`SingleMessage`**: Messages of the same type within the same Tick are automatically merged, suitable for state synchronization.
19
+ - **`EventMessage`**: Sequentially appended, ensuring the integrity of action sequences.
20
+ - **`ErrorMessage`**: Sequentially appended; errors are treated as a message type with a default handling System.
21
+ - **`WarnMessage`**: Sequentially appended; warnings are a message type with a default handling System.
22
+ - **`InfoMessage`**: Sequentially appended; information is a message type with a default handling System.
23
+
24
+ ### 2. Dependency Injection System (DI)
25
+
26
+ Virid implements a lightweight, decorator-based DI system, allowing Systems to access data entities with minimal effort.
27
+
28
+ - **Data Entities (Component)**: Classes marked with the `@Component()` decorator are defined as data containers.
29
+ - **Automatic Injection**: Once registered via `app.bindComponent()`, the Dispatcher automatically injects the corresponding instances based on the parameter types of the System function.
30
+
31
+ TypeScript
32
+
33
+ ```ts
34
+ class IncrementMessage extends SingleMessage {
35
+ constructor(public amount: number) {
36
+ super();
37
+ }
38
+ }
39
+
40
+ @Component()
41
+ class CounterComponent { public count = 0; }
42
+
43
+ class CounterSystem {
44
+ @System()
45
+ static onIncrement(
46
+ @Message(IncrementMessage) msg: IncrementMessage,
47
+ count: CounterComponent
48
+ ) {
49
+ count.count += msg.amount; // CounterComponent instance is automatically injected
50
+ }
51
+ }
52
+ ```
53
+
54
+ ### 3. Lifecycle Hooks
55
+
56
+ The Dispatcher provides comprehensive lifecycle monitoring:
57
+
58
+ - **Execution Hooks**: Supports `onBeforeExecute` and `onAfterExecute` for global auditing or filtering before and after logic execution.
59
+ - **Cycle Hooks**: `onBeforeTick` and `onAfterTick` monitor the start and end of each logic frame.
60
+
61
+ ### 4. Industrial-Grade Robustness
62
+
63
+ - **Deadlock Defense**: The Dispatcher includes an `internalDepth` counter. If a logic chain triggers more than 100 levels of recursion, the system automatically fuses and throws an error to prevent the environment from hanging.
64
+ - **Execution Priority**: Supports defining the order of multiple Systems handling the same message via `@System({ priority: number })`.
65
+
66
+ ------
67
+
68
+ ## 🛠️ @virid/core API Reference
69
+
70
+ ### 1. Engine Initialization
71
+
72
+ #### `createVirid()`
73
+
74
+ - **Function**: Initializes the logical kernel and creates the globally unique `Dispatcher` and container.
75
+
76
+ ------
77
+
78
+ ### 2. Instructions & Messages
79
+
80
+ #### `SingleMessage`
81
+
82
+ - **Feature**: State synchronization message.
83
+ - **Logic**: Multiple messages of the same type in one Tick are merged; the System typically receives only the latest one.
84
+ - **Example**:
85
+
86
+ ```ts
87
+ import { SingleMessage } from "@virid/core";
88
+ class MyMessage extends SingleMessage {}
89
+ //Send messages anywhere
90
+ MyMessage.send(); // Parameters correspond to the constructor
91
+ ```
92
+
93
+ #### `EventMessage`
94
+
95
+ - **Feature**: Action command message.
96
+ - **Logic**: Sequential, no merging. Every `EventMessage` triggers a System execution strictly.
97
+ - **Example**: Same as `EventMessage`
98
+
99
+ ------
100
+
101
+ ### 3. Data & Logic Definitions (Decorators)
102
+
103
+ #### `@Component()`
104
+
105
+ - **Function**: Marks a class as a **Data Entity**.
106
+ - **Usage**: Used with `bindComponent` to enable dependency injection in Systems.
107
+ - **Example**:
108
+
109
+ ```ts
110
+ @Component()
111
+ class CounterComponent {
112
+ public count = 0;
113
+ }
114
+ ```
115
+
116
+ #### `@System(params?)`
117
+
118
+ - **Function**: Registers a static method as a logic handler with automatic dependency assembly.
119
+ - **Parameters**:
120
+ - `priority`: Higher values execute earlier in a Tick.
121
+ - `messageClass`: The message type that triggers this System (cannot coexist with `@Message`).
122
+ - **Note**: Returning a Message (or array) from a System implements automatic logic chaining.
123
+ - **Examples**:
124
+
125
+ ```ts
126
+ import {
127
+ System,
128
+ Message,
129
+ } from "@virid/core";
130
+
131
+ class CounterSystem {
132
+ // Priority can be set for the system
133
+ @System({ priority: 0})
134
+ static onIncrement(
135
+ @Message(IncrementMessage) message: IncrementMessage,
136
+ count: CounterComponent,
137
+ ) {
138
+ count.count += message.amount;
139
+ }
140
+ //You don't need to use @ Message if you already used messageClass
141
+ @System({messageClass:IncrementMessage})
142
+ static onIncrement(
143
+ count: CounterComponent,
144
+ ) {
145
+ count.count += 1;
146
+ }
147
+ @System()
148
+ static onProcess(msg: SomeMessage) {
149
+ // Directly return the message to achieve logical chain triggering, without the need to //manually call the Message Writer
150
+ // You can also return a message array to trigger continuously
151
+ return new NextStepMessage();
152
+ }
153
+ }
154
+ //Send messages directly through. send
155
+ IncrementMessage.send(5);
156
+ ```
157
+
158
+ #### `@Message(Class, single?)`
159
+
160
+ - **Function**: Parameter decorator defining the message type for the System.
161
+ - **Batch Mode**: Set `single: false` to receive an array of all messages of that type in the current Tick (ideal for high-performance batch processing).
162
+ - **Example**: Same as `System`
163
+
164
+ #### @Observer(callback)
165
+
166
+ - **Function** : Attribute level change monitoring, used to handle side effects that are not instruction driven
167
+ - **Logic** : When the attribute marked in 'Component' changes, the engine will automatically trigger the specified callback function.
168
+ - **Example** :
169
+
170
+ ```ts
171
+ @Component()
172
+ class PlayerComponent {
173
+ //Automatically send a synchronization message or execute a callback when progress changes
174
+ @Observer((old, val) => new SyncLyricMessage(val))
175
+ public progress = 0;
176
+ }
177
+ ```
178
+
179
+ #### `@Safe()`
180
+
181
+ - **Function**: Method access marking. In Virid, The UI layer is restricted from directly modifying logic data. `@Safe()` explicitly authorizes the view layer to call specific "read-only" or "safe-calculation" methods.
182
+ - **Example** :
183
+
184
+ ```ts
185
+ @Component()
186
+ class PlayerComponent {
187
+ // 如果不加Safe,virid将会禁止在任何vue的controller中调用该方法
188
+ @Safe()
189
+ public someMethod(){}
190
+ }
191
+ ```
192
+
193
+ ------
194
+
195
+ ### 4. Dispatcher & Hooks
196
+
197
+ - **Double-Buffered Flip**: Each execution locks current messages; new messages generated during execution enter the next cycle.
198
+ - **Hooks Example**:
199
+
200
+ ```ts
201
+ const app = createVirid();
202
+ app.onBeforeExecute(MyMessage, (msg, context) => {
203
+ // Global performance tracking or permission validation
204
+ });
205
+ app.onAfterTick((context) => {
206
+ console.log("----------------onAfterTick------------------");
207
+ });
208
+ ```
209
+
210
+ ------
211
+
212
+ ### 4. Dispatcher & Hooks
213
+
214
+ #### **Dispatcher**
215
+
216
+ The core scheduling engine of Virid operates on the following logic:
217
+
218
+ - **Double-Buffered Flip**: The scheduler runs based on logical **Ticks**. A Tick starts at the beginning of a microtask and continues until no new messages are generated internally. During each execution, the scheduler locks the current pending messages; any new messages generated during this process are automatically moved to the next cycle.
219
+ - **Recursion Melt-down (Deadlock Defense)**: If the recursive execution depth (`internalDepth`) exceeds 100, the engine immediately halts and throws an error. This protects the main thread from being frozen by logical "black holes" or infinite loops.
220
+
221
+ #### **Life-Cycle Hooks**
222
+
223
+ - **onBeforeTick / onAfterTick**: Monitor the "pulse" of logical frames.
224
+ - **onBeforeExecute / onAfterExecute**: Audit the entire execution process for specific message types.
225
+ - **Example:**
226
+
227
+ ```ts
228
+ const app = createVirid();
229
+
230
+ app.onBeforeExecute(MyMessage, (msg, context) => {
231
+ // Implement global performance tracking or permission validation here
232
+ // This hook only triggers for MyMessage and its subclasses
233
+ });
234
+
235
+ app.onAfterTick((context) => {
236
+ console.log("----------------onAfterTick------------------");
237
+ });
238
+ ```
239
+
240
+ ### 5. System Communication (IO)
241
+
242
+ #### **MessageWriter**
243
+
244
+ - **Function**: A global static utility for system-wide communication.
245
+ - **API**:
246
+ - **`MessageWriter.write(MessageClass, ...args)`**: The underlying entry point for dispatching messages. It instantiates the message class with the provided arguments and sends it to the Dispatcher.
247
+ - **`MessageWriter.error(Error, context?)`**: Throws a system-level error, which automatically triggers an `ErrorMessage`.
248
+ - **`MessageWriter.warn(context)`**: Logs a warning, which automatically triggers a `WarnMessage`.
249
+ - **`MessageWriter.info(context)`**: Logs an informational message, which automatically triggers an `InfoMessage`.
250
+
251
+ ------
252
+
253
+ ### 🔬 Advanced: Atomic Operations
254
+
255
+ #### **AtomicModifyMessage**
256
+
257
+ - **Function**: Provides a "plug-and-play" solution for temporary logic modifications.
258
+ - **Scenario**: When you need to perform a one-time observation or tweak a `Component` without the overhead of defining a dedicated `System`, you can use this message to execute a closure logic in the next logical frame (Tick).
259
+ - **Example**:
260
+
261
+ ```ts
262
+ AtomicModifyMessage.send(
263
+ CounterComponent,
264
+ (comp) => {
265
+ // Perform one-time inspection or modification on the component instance
266
+ console.log("----------------AtomicModifyMessage------------------");
267
+ console.log("Current Component State:", comp);
268
+ },
269
+ "Check CounterComponent" // Optional: label for debugging/tracing
270
+ );
271
+ ```
272
+
273
+ ------
package/README.zh.md CHANGED
@@ -1 +1,255 @@
1
- todo
1
+ # @virid/core
2
+
3
+ `@virid/core` 是整个 Virid 生态系统的逻辑心脏。它提供了一套确定性的消息分发与调度机制,旨在将业务逻辑从复杂的 UI 框架与运行环境中彻底剥离。
4
+
5
+ ## 🌟 核心设计理念
6
+
7
+ - **环境绝对独立**:本包不依赖任何浏览器、Node.js 的特殊 API 或第三方库(仅依赖 `reflect-metadata` 实现装饰器功能)。这确保了内核可以无缝运行在 Electron 主进程、Worker 线程、Web 渲染层甚至纯服务器环境中。
8
+ - **确定性调度**:引入了类似游戏引擎的 `Tick` 机制,通过双缓冲消息池确保逻辑执行顺序的可预测性。
9
+
10
+ ## 🛠️ 核心功能详解
11
+
12
+ ### 1. 消息驱动与分发机制 (Message & Dispatcher)
13
+
14
+ 在 Virid 中,所有的状态变更都必须通过发送一个 `Message` 指令来触发。
15
+
16
+ - **自动调度**:通过定义特定类型的 `Message` 和对应的 `System` 处理函数,引擎会自动在下一个微任务周期(Tick)内调用已注册的逻辑。
17
+ - **消息类型**:
18
+ - `SingleMessage`:同一 Tick 内的同类消息会自动合并,适用于状态同步。
19
+ - `EventMessage`:顺序追加,确保动作序列的完整性。
20
+ - `ErrorMessage`:顺序追加,错误也是一种消息类型,拥有默认处理System。
21
+ - `WarnMessage`:顺序追加,警告也是一种消息类型,拥有默认处理System。
22
+ - `InfoMessage`:顺序追加,信息也是一种消息类型,拥有默认处理System。
23
+
24
+ ### 2. 依赖注入系统 (Dependency Injection)
25
+
26
+ Virid 实现了基于装饰器的轻量依赖注入,使 System 能够以极简的方式访问数据实体。
27
+
28
+ - **数据实体 (Component)**:通过 `@Component()` 装饰器标记 class,将其定义为数据容器。
29
+ - **自动注入**:使用 `app.bindComponent()` 注册后,Dispatcher 会根据 System 函数的参数类型自动注入对应的实例。
30
+
31
+ ```ts
32
+ class IncrementMessage extends SingleMessage {
33
+ constructor(public amount: number) {
34
+ super();
35
+ }
36
+ }
37
+
38
+ @Component()
39
+ class CounterComponent { public count = 0; }
40
+
41
+ class CounterSystem {
42
+ @System()
43
+ static onIncrement(
44
+ @Message(IncrementMessage) msg: IncrementMessage,
45
+ count: CounterComponent
46
+ ) {
47
+ count.count += msg.amount; // 自动注入的 CounterComponent 实例
48
+ }
49
+ }
50
+ ```
51
+
52
+ ### 3. 系统调度与钩子 (Lifecycle Hooks)
53
+
54
+ Dispatcher 提供了完备的生命周期监控能力:
55
+
56
+ - **执行钩子**:支持 `onBeforeExecute` 和 `onAfterExecute`,允许在逻辑执行前后插入全局审计或过滤逻辑。
57
+ - **周期钩子**:`onBeforeTick` 与 `onAfterTick` 用于监控每一轮逻辑帧的起止。
58
+
59
+ ### 4. 工业级健壮性
60
+
61
+ - **死循环防御**:Dispatcher 内部设有 `internalDepth` 计数器。若逻辑链路产生超过 100 层的递归触发,系统将自动熔断并报错,防止环境假死。
62
+ - **执行优先级**:支持通过 `@System({ priority: number })` 明确多个系统处理同一消息时的先后顺序。
63
+
64
+ ## 🛠️ @virid/core 核心 API 概览
65
+
66
+ ### 1. 引擎初始化 (Engine Entry)
67
+
68
+ #### `createVirid()`
69
+
70
+ - **功能**:初始化逻辑内核,创建全局唯一的 `Dispatcher` 与容器。
71
+
72
+ ------
73
+
74
+ ### 2. 指令与消息 (Messages)
75
+
76
+ 消息是驱动系统的唯一原因。Virid 区分了两种不同的执行范式:
77
+
78
+ #### `SingleMessage`
79
+
80
+ - **特性**:**状态同步型消息**。
81
+
82
+ - **逻辑**:在同一个 `Tick` 内发送的多次同类型 `SingleMessage` 会被自动合并。默认情况下,`System` 仅会收到最新的一条。
83
+
84
+ - **示例:**
85
+
86
+ ```ts
87
+ import {SingleMessage} form "@virid/core"
88
+ class MyMessage extends SingleMessage{}
89
+
90
+ //在任何的地方,只要发送
91
+ //send内的参数为MyMessage的构造函数可以接受的参数
92
+ MyMessage.send()
93
+ ```
94
+
95
+ #### `EventMessage`
96
+
97
+ - **特性**:**动作指令型消息**。
98
+ - **逻辑**:顺序追加,不合并。每一条 `EventMessage` 都会严格触发一次 `System` 执行。
99
+ - **示例:**同`SingleMessage`
100
+
101
+ ------
102
+
103
+ ### 3. 数据与逻辑定义 (Decorators)
104
+
105
+ #### `@Component()`
106
+
107
+ - **功能**:标记一个类为**数据实体**。
108
+ - **设计**:配合 `bindComponent` 使用,注册后可以在System中声明类型以获得依赖注入功能。
109
+ - **示例:**
110
+
111
+ ```ts
112
+ @Component()
113
+ class CounterComponent {
114
+ public count = 0;
115
+ }
116
+ ```
117
+
118
+ #### `@System(params?)`
119
+
120
+ - **功能**:将静态方法注册为业务逻辑处理器。它实现了“依赖自动装配”。你只需要在参数里写上对应的 `Component` 类型,引擎就会在执行时自动注入实例。
121
+ - **参数**:
122
+ - `priority`: 执行优先级。数值越大,在同一个 `Tick` 中执行越早。
123
+ - `messageClass`: 触发该System需要的消息类型,不能与`@Message`共存
124
+ - **示例**:
125
+
126
+ ```ts
127
+ import {
128
+ System,
129
+ Message,
130
+ } from "@virid/core";
131
+
132
+ class CounterSystem {
133
+ // 可以为System设置优先级
134
+ // count参数将被自动注入
135
+ @System({ priority: 0})
136
+ static onIncrement(
137
+ @Message(IncrementMessage) message: IncrementMessage,
138
+ count: CounterComponent,
139
+ ) {
140
+ count.count += message.amount;
141
+ }
142
+ //可以不使用@Message
143
+ @System({messageClass:IncrementMessage})
144
+ static onIncrement(
145
+ count: CounterComponent,
146
+ ) {
147
+ count.count += 1;
148
+ }
149
+ @System()
150
+ static onProcess(msg: SomeMessage) {
151
+ // 直接 return 消息,即可实现逻辑链式触发,无需手动调用 MessageWriter
152
+ // 也可以return一个message数组来连续触发
153
+ return new NextStepMessage();
154
+ }
155
+ }
156
+ //直接通过.send来发送消息
157
+ IncrementMessage.send(5);
158
+ ```
159
+
160
+ #### `@Message(Class, single?)`
161
+
162
+ - **功能**:参数级装饰器,明确当前 `System` 关注的消息类型。
163
+ - **批处理模式**:若设置 `single: false`,`System` 会收到一个包含本轮 `Tick` 内所有该消息的数组,适用于高性能的批量计算(如物理引擎或日志聚合)。
164
+ - **示例:**见System
165
+
166
+ ### `@Observer(callback)`
167
+
168
+ - **功能**:属性级变更监听,用于处理那些“非指令驱动”的副作用
169
+ - **逻辑**:当 `Component` 中被标记的属性发生变更时,引擎会自动触发指定的回调函数。
170
+ - **示例**:
171
+
172
+ ```ts
173
+ @Component()
174
+ class PlayerComponent {
175
+ // 当 progress 变更时,自动发送一个同步消息或执行回调
176
+ @Observer((old, val) => new SyncLyricMessage(val))
177
+ public progress = 0;
178
+ }
179
+ ```
180
+
181
+ ### `@Safe()`
182
+
183
+ - **功能**:方法访问权限标记,在virid中外部环境(UI 层)严禁直接修改逻辑层数据。但对于某些“只读类”或“安全计算类”方法,可以通过 `@Safe()` 显式授权,允许视图层直接调用。
184
+ - **设计**:主要服务于 `@virid/vue` 等外部投影层,详情请前往vue适配层文档。
185
+ - **示例**:
186
+
187
+ ```ts
188
+ @Component()
189
+ class PlayerComponent {
190
+ // 如果不加Safe,virid将会禁止在任何vue的controller中调用该方法
191
+ @Safe()
192
+ public someMethod(){}
193
+ }
194
+ ```
195
+
196
+ ------
197
+
198
+ ### 4. 调度与拦截 (Dispatcher & Hooks)
199
+
200
+ #### `Dispatcher`
201
+
202
+ - **核心逻辑**:
203
+ 1. **双缓冲交换 (Flip)**:调度器基于逻辑Tick运行,以微任务开始为起点,直到系统内部不在产生新的Message为止为一个逻辑Tick。每次执行调度器锁定当前待处理消息,新产生的消息自动进入下一轮。
204
+ 2. **死循环熔断**:若递归执行深度 `internalDepth > 100`,立即停止执行并报错,保护主线程不被逻辑黑洞卡死。
205
+
206
+ #### 生命周期钩子 (Life-Cycle Hooks)
207
+
208
+ - `onBeforeTick` / `onAfterTick`: 监控逻辑帧的脉搏。
209
+ - `onBeforeExecute` / `onAfterExecute`: 针对特定消息的执行全过程进行审计。
210
+ - **示例**:
211
+
212
+ ```ts
213
+ const app = createVirid();
214
+ app.onBeforeExecute(MyMessage, (msg, context) => {
215
+ // 可以在这里实现全局的性能追踪或权限校验
216
+ // 只会对MyMessage和子类消息生效
217
+ });
218
+ app.onAfterTick((context) => {
219
+ console.log("----------------onAfterTick------------------");
220
+ });
221
+ ```
222
+
223
+ ------
224
+
225
+ ### 5. 系统通讯 (IO)
226
+
227
+ #### `MessageWriter`
228
+
229
+ - **功能**:全局静态通讯工具。
230
+ - **API**:
231
+ - `MessageWriter.write(MessageClass, ...args)`: 发送消息的底层入口。
232
+ - `MessageWriter.error(Error, context?)`: 抛出一个系统级错误,它会自动触发 `ErrorMessage`。
233
+ - `MessageWriter.warn(context)`: 写入一个警告,他会自动触发`WarnMessage`。
234
+ - `MessageWriter.info(context)`: 写入一个信息,它会自动触发 `InfoMessage`。
235
+
236
+ ------
237
+
238
+ ## 🔬 进阶:原子化修改 (Atomic Operations)
239
+
240
+ #### `AtomicModifyMessage`
241
+
242
+ - **功能**:提供一种“即插即用”的临时逻辑修改方案。
243
+ - **场景**:当你需要对某个 `Component` 进行一次性的观察或微调,又不想专门写一个 `System` 时,可以使用此消息在下一帧执行一段闭包逻辑。
244
+ - **示例**:
245
+
246
+ ```ts
247
+ AtomicModifyMessage.send(
248
+ CounterComponent,
249
+ (comp) => {
250
+ console.log("----------------AtomicModifyMessage------------------");
251
+ },
252
+ "Check CounterComponent",
253
+ );
254
+ ```
255
+
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@virid/core",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "description": "A lightweight and powerful message core built using dependency injection and CCS concepts",
6
6
  "author": "Ailrid",
7
7
  "license": "Apache 2.0",
@@ -34,12 +34,12 @@
34
34
  "access": "public",
35
35
  "registry": "https://registry.npmjs.org/"
36
36
  },
37
- "dependencies": {},
38
- "peerDependencies": {
39
- "reflect-metadata": "^0.2.2"
40
- },
41
37
  "scripts": {
42
38
  "build": "tsup --config tsup.config.ts",
43
39
  "example": "node --import @swc-node/register/esm-register examples/index.ts"
40
+ },
41
+ "dependencies": {},
42
+ "peerDependencies": {
43
+ "reflect-metadata": "^0.2.2"
44
44
  }
45
- }
45
+ }