@virid/core 0.1.1 → 0.1.2
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/README.md +103 -67
- package/README.zh.md +37 -14
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
# @virid/core
|
|
2
2
|
|
|
3
|
-
`@virid/core`
|
|
3
|
+
`@virid/core` serves as the logical heartbeat of the entire Virid ecosystem. It provides a deterministic message distribution and scheduling mechanism, designed to completely decouple business logic from complex UI frameworks and runtime environments.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
⚠️ **Warning:** This framework heavily incorporates design philosophies from **Rust**, **Bevy**, and **NestJS**. It has a steep learning curve and requires the configuration of `reflect-metadata` with experimental metadata support enabled.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
### 🌟 Core Design Philosophy
|
|
8
|
+
|
|
9
|
+
- **Absolute Environment Independence:** This package does not rely on any browser-specific APIs, Node.js internals, or third-party libraries (with the sole exception of `reflect-metadata` for decorator functionality). This ensures the kernel can run seamlessly within Electron main processes, Worker threads, Web rendering layers, or even pure server-side environments.
|
|
10
|
+
- **Deterministic Scheduling:** By introducing a game-engine-inspired **Tick mechanism**, the framework utilizes double-buffered message pools to ensure the execution order of logic remains strictly predictable.
|
|
11
|
+
- **Strong Typing & Ownership:** All systems are built with robust type safety, enforcing the use of modern TypeScript types and classes as unique identifiers. It features a runtime "modification shield" to intercept any illegal write operations. Move beyond a reliance on hot-reloading; as the saying goes: *If it compiles, it works.*
|
|
9
12
|
|
|
10
13
|
## 🛠️ Core Functional Overview
|
|
11
14
|
|
|
12
15
|
### 1. Message-Driven & Dispatcher Mechanism
|
|
13
16
|
|
|
14
|
-
In Virid
|
|
17
|
+
In `Virid`, all state changes must be triggered by sending a `Message` command.
|
|
15
18
|
|
|
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).
|
|
19
|
+
- **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
20
|
- **Message Types**:
|
|
18
21
|
- **`SingleMessage`**: Messages of the same type within the same Tick are automatically merged, suitable for state synchronization.
|
|
19
22
|
- **`EventMessage`**: Sequentially appended, ensuring the integrity of action sequences.
|
|
@@ -23,13 +26,11 @@ In Virid, all state changes must be triggered by sending a `Message` command.
|
|
|
23
26
|
|
|
24
27
|
### 2. Dependency Injection System (DI)
|
|
25
28
|
|
|
26
|
-
Virid implements a lightweight, decorator-based DI system, allowing Systems to access data entities with minimal effort.
|
|
29
|
+
`Virid` implements a lightweight, decorator-based DI system, allowing Systems to access data entities with minimal effort.
|
|
27
30
|
|
|
28
31
|
- **Data Entities (Component)**: Classes marked with the `@Component()` decorator are defined as data containers.
|
|
29
32
|
- **Automatic Injection**: Once registered via `app.bindComponent()`, the Dispatcher automatically injects the corresponding instances based on the parameter types of the System function.
|
|
30
33
|
|
|
31
|
-
TypeScript
|
|
32
|
-
|
|
33
34
|
```ts
|
|
34
35
|
class IncrementMessage extends SingleMessage {
|
|
35
36
|
constructor(public amount: number) {
|
|
@@ -96,102 +97,137 @@ MyMessage.send(); // Parameters correspond to the constructor
|
|
|
96
97
|
- **Logic**: Sequential, no merging. Every `EventMessage` triggers a System execution strictly.
|
|
97
98
|
- **Example**: Same as `EventMessage`
|
|
98
99
|
|
|
100
|
+
## 3. Data & Logic Definitions (Decorators)
|
|
101
|
+
|
|
102
|
+
### `@Controller()`
|
|
103
|
+
|
|
104
|
+
- **Function:** Marks a class as a UI Controller. Instances of this class are tied to the lifecycle of Vue components—created when the component is mounted and destroyed when it is unmounted. For details, see `@virid/vue`.
|
|
105
|
+
|
|
106
|
+
- **Design:** Used in conjunction with `bindController`. Once registered, instances can be retrieved in `@virid/vue` using the `useController` hook.
|
|
107
|
+
|
|
108
|
+
- **Example:**
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
@Controller()
|
|
112
|
+
class PageController {}
|
|
113
|
+
|
|
114
|
+
// Registration is required before use
|
|
115
|
+
app.bindController(PageController);
|
|
116
|
+
```
|
|
117
|
+
|
|
99
118
|
------
|
|
100
119
|
|
|
101
|
-
###
|
|
120
|
+
### `@Component()`
|
|
102
121
|
|
|
103
|
-
|
|
122
|
+
- **Function:** Marks a class as a Data Entity. This class acts as a global singleton and persists throughout the application lifecycle.
|
|
104
123
|
|
|
105
|
-
- **
|
|
106
|
-
|
|
107
|
-
- **Example
|
|
124
|
+
- **Design:** Used with `bindComponent`. Once registered, it can be declared as a parameter type in a `@System` to enable automatic Dependency Injection (DI).
|
|
125
|
+
|
|
126
|
+
- **Example:**
|
|
108
127
|
|
|
109
128
|
```ts
|
|
110
129
|
@Component()
|
|
111
130
|
class CounterComponent {
|
|
112
|
-
|
|
131
|
+
public count = 0;
|
|
113
132
|
}
|
|
133
|
+
|
|
134
|
+
// Registration is required before use
|
|
135
|
+
app.bindComponent(CounterComponent);
|
|
114
136
|
```
|
|
115
137
|
|
|
116
|
-
|
|
138
|
+
------
|
|
139
|
+
|
|
140
|
+
### `@System(params?)`
|
|
141
|
+
|
|
142
|
+
- **Function:** Registers a static method as a business logic processor. It implements **"Automatic Dependency Wiring."** By simply specifying the `Component` type in the parameters, the engine automatically injects the corresponding instance during execution.
|
|
143
|
+
|
|
144
|
+
- **Parameters:**
|
|
145
|
+
|
|
146
|
+
- `priority`: Execution priority. Higher values execute earlier within the same **Tick**.
|
|
147
|
+
- `messageClass`: The specific message type that triggers this System. (Cannot be used simultaneously with the `@Message` decorator on parameters).
|
|
148
|
+
|
|
149
|
+
- **Example:**
|
|
117
150
|
|
|
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
151
|
|
|
125
152
|
```ts
|
|
126
|
-
import {
|
|
127
|
-
System,
|
|
128
|
-
Message,
|
|
129
|
-
} from "@virid/core";
|
|
153
|
+
import { System, Message } from "@virid/core";
|
|
130
154
|
|
|
131
155
|
class CounterSystem {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
156
|
+
// Setting priority; CounterComponent is automatically injected
|
|
157
|
+
@System({ priority: 0 })
|
|
158
|
+
static onIncrement(
|
|
159
|
+
@Message(IncrementMessage) message: IncrementMessage,
|
|
160
|
+
count: CounterComponent,
|
|
161
|
+
) {
|
|
162
|
+
count.count += message.amount;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Alternative: Define messageClass in the System decorator
|
|
166
|
+
@System({ messageClass: IncrementMessage })
|
|
167
|
+
static onQuickAdd(count: CounterComponent) {
|
|
168
|
+
count.count += 1;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
@System()
|
|
172
|
+
static onProcess(msg: SomeMessage) {
|
|
173
|
+
// Return a message (or an array of messages) to trigger a logic chain
|
|
174
|
+
// This removes the need to manually call a MessageWriter
|
|
175
|
+
return new NextStepMessage();
|
|
153
176
|
}
|
|
154
|
-
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Triggering logic via the .send() method
|
|
155
180
|
IncrementMessage.send(5);
|
|
156
181
|
```
|
|
157
182
|
|
|
158
|
-
|
|
183
|
+
------
|
|
184
|
+
|
|
185
|
+
### `@Message(Class, single?)`
|
|
186
|
+
|
|
187
|
+
- **Function:** A parameter-level decorator that explicitly defines which message type the System is listening to.
|
|
188
|
+
- **Batch Mode:** If `single: false` is set, the System receives an **array** of all messages of that type sent within the current Tick. This is ideal for high-performance batch processing (e.g., physics calculations or log aggregation).
|
|
189
|
+
- **Example:** See the `@System` section above.
|
|
159
190
|
|
|
160
|
-
|
|
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`
|
|
191
|
+
------
|
|
163
192
|
|
|
164
|
-
|
|
193
|
+
### `@Observer(callback)`
|
|
194
|
+
|
|
195
|
+
- **Function:** A property-level decorator for change detection, used to handle "non-command-driven" side effects.
|
|
196
|
+
|
|
197
|
+
- **Logic:** When a decorated property in a `Component` changes, the engine automatically triggers the specified callback function.
|
|
198
|
+
|
|
199
|
+
- **Example:**
|
|
165
200
|
|
|
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
201
|
|
|
170
202
|
```ts
|
|
171
203
|
@Component()
|
|
172
204
|
class PlayerComponent {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
205
|
+
// Automatically send a sync message or execute a callback when 'progress' changes
|
|
206
|
+
@Observer((old, val) => new SyncLyricMessage(val))
|
|
207
|
+
public progress = 0;
|
|
176
208
|
}
|
|
177
209
|
```
|
|
178
210
|
|
|
179
|
-
|
|
211
|
+
------
|
|
212
|
+
|
|
213
|
+
### `@Safe()`
|
|
214
|
+
|
|
215
|
+
- **Function:** A method access modifier. In Virid, external environments (UI layers) are strictly prohibited from directly modifying logic-layer data; all modifications and method calls are intercepted by default. However, "read-only" or "safe calculation" methods can be explicitly authorized via `@Safe()`, allowing the view layer to call them directly.
|
|
216
|
+
|
|
217
|
+
- **Design:** Primarily serves external projection layers like `@virid/vue`. See **Deep Shield** in the `@virid/vue` documentation for more details.
|
|
218
|
+
|
|
219
|
+
- **Example:**
|
|
180
220
|
|
|
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
221
|
|
|
184
222
|
```ts
|
|
185
223
|
@Component()
|
|
186
224
|
class PlayerComponent {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
225
|
+
// Without @Safe, Virid will block calls to this method from any Vue Controller
|
|
226
|
+
@Safe()
|
|
227
|
+
public someMethod() {}
|
|
190
228
|
}
|
|
191
229
|
```
|
|
192
230
|
|
|
193
|
-
------
|
|
194
|
-
|
|
195
231
|
### 4. Dispatcher & Hooks
|
|
196
232
|
|
|
197
233
|
- **Double-Buffered Flip**: Each execution locks current messages; new messages generated during execution enter the next cycle.
|
package/README.zh.md
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
`@virid/core` 是整个 Virid 生态系统的逻辑心脏。它提供了一套确定性的消息分发与调度机制,旨在将业务逻辑从复杂的 UI 框架与运行环境中彻底剥离。
|
|
4
4
|
|
|
5
|
+
⚠️本框架吸收了大量Rust、Bevy、Nestjs设计哲学,上手难度较高,且需要配置`reflect-metadata`并开启元数据支持。
|
|
6
|
+
|
|
5
7
|
## 🌟 核心设计理念
|
|
6
8
|
|
|
7
9
|
- **环境绝对独立**:本包不依赖任何浏览器、Node.js 的特殊 API 或第三方库(仅依赖 `reflect-metadata` 实现装饰器功能)。这确保了内核可以无缝运行在 Electron 主进程、Worker 线程、Web 渲染层甚至纯服务器环境中。
|
|
8
10
|
- **确定性调度**:引入了类似游戏引擎的 `Tick` 机制,通过双缓冲消息池确保逻辑执行顺序的可预测性。
|
|
11
|
+
- **强类型与所有权**:所有的系统使用强类型构建,强制使用现代TS类型和类本身作为标识,且拥有运行时修改护盾检查拦截任何非法写操作。**摆脱热更新依赖,If it compiles, it works**
|
|
9
12
|
|
|
10
13
|
## 🛠️ 核心功能详解
|
|
11
14
|
|
|
@@ -13,31 +16,34 @@
|
|
|
13
16
|
|
|
14
17
|
在 Virid 中,所有的状态变更都必须通过发送一个 `Message` 指令来触发。
|
|
15
18
|
|
|
16
|
-
- **自动调度**:通过定义特定类型的 `Message` 和对应的 `System`
|
|
19
|
+
- **自动调度**:通过定义特定类型的 `Message` 和对应的 `System` 处理函数,引擎会自动在下一个微任务周期(`Tick`)内调用已注册的逻辑。
|
|
17
20
|
- **消息类型**:
|
|
18
21
|
- `SingleMessage`:同一 Tick 内的同类消息会自动合并,适用于状态同步。
|
|
19
22
|
- `EventMessage`:顺序追加,确保动作序列的完整性。
|
|
20
|
-
- `ErrorMessage
|
|
21
|
-
- `WarnMessage
|
|
22
|
-
- `InfoMessage
|
|
23
|
+
- `ErrorMessage`:顺序追加,错误也是一种消息类型,拥有默认处理`System`。
|
|
24
|
+
- `WarnMessage`:顺序追加,警告也是一种消息类型,拥有默认处理`System`。
|
|
25
|
+
- `InfoMessage`:顺序追加,信息也是一种消息类型,拥有默认处理`System`。
|
|
23
26
|
|
|
24
27
|
### 2. 依赖注入系统 (Dependency Injection)
|
|
25
28
|
|
|
26
|
-
Virid 实现了基于装饰器的轻量依赖注入,使 System 能够以极简的方式访问数据实体。
|
|
29
|
+
`Virid` 实现了基于装饰器的轻量依赖注入,使 `System` 能够以极简的方式访问数据实体。
|
|
27
30
|
|
|
28
31
|
- **数据实体 (Component)**:通过 `@Component()` 装饰器标记 class,将其定义为数据容器。
|
|
29
32
|
- **自动注入**:使用 `app.bindComponent()` 注册后,Dispatcher 会根据 System 函数的参数类型自动注入对应的实例。
|
|
30
33
|
|
|
31
34
|
```ts
|
|
35
|
+
//定义消息
|
|
32
36
|
class IncrementMessage extends SingleMessage {
|
|
33
37
|
constructor(public amount: number) {
|
|
34
38
|
super();
|
|
35
39
|
}
|
|
36
40
|
}
|
|
37
41
|
|
|
42
|
+
//定义数据
|
|
38
43
|
@Component()
|
|
39
44
|
class CounterComponent { public count = 0; }
|
|
40
45
|
|
|
46
|
+
//纯静态static,当消息发送时引擎自动调用onIncrement
|
|
41
47
|
class CounterSystem {
|
|
42
48
|
@System()
|
|
43
49
|
static onIncrement(
|
|
@@ -51,14 +57,14 @@ class CounterSystem {
|
|
|
51
57
|
|
|
52
58
|
### 3. 系统调度与钩子 (Lifecycle Hooks)
|
|
53
59
|
|
|
54
|
-
Dispatcher 提供了完备的生命周期监控能力:
|
|
60
|
+
`Dispatcher` 提供了完备的生命周期监控能力:
|
|
55
61
|
|
|
56
62
|
- **执行钩子**:支持 `onBeforeExecute` 和 `onAfterExecute`,允许在逻辑执行前后插入全局审计或过滤逻辑。
|
|
57
63
|
- **周期钩子**:`onBeforeTick` 与 `onAfterTick` 用于监控每一轮逻辑帧的起止。
|
|
58
64
|
|
|
59
65
|
### 4. 工业级健壮性
|
|
60
66
|
|
|
61
|
-
-
|
|
67
|
+
- **死循环防御**:`Dispatcher` 内部设有 `internalDepth` 计数器。若逻辑链路产生超过 100 层的递归触发,系统将自动熔断并报错,防止环境假死。
|
|
62
68
|
- **执行优先级**:支持通过 `@System({ priority: number })` 明确多个系统处理同一消息时的先后顺序。
|
|
63
69
|
|
|
64
70
|
## 🛠️ @virid/core 核心 API 概览
|
|
@@ -73,7 +79,7 @@ Dispatcher 提供了完备的生命周期监控能力:
|
|
|
73
79
|
|
|
74
80
|
### 2. 指令与消息 (Messages)
|
|
75
81
|
|
|
76
|
-
|
|
82
|
+
消息是驱动系统的唯一原因。`Virid` 区分了两种不同的执行范式:
|
|
77
83
|
|
|
78
84
|
#### `SingleMessage`
|
|
79
85
|
|
|
@@ -85,11 +91,13 @@ Dispatcher 提供了完备的生命周期监控能力:
|
|
|
85
91
|
|
|
86
92
|
```ts
|
|
87
93
|
import {SingleMessage} form "@virid/core"
|
|
88
|
-
class MyMessage extends SingleMessage{
|
|
94
|
+
class MyMessage extends SingleMessage{
|
|
95
|
+
construter(public someData:SomeType)
|
|
96
|
+
}
|
|
89
97
|
|
|
90
98
|
//在任何的地方,只要发送
|
|
91
99
|
//send内的参数为MyMessage的构造函数可以接受的参数
|
|
92
|
-
MyMessage.send()
|
|
100
|
+
MyMessage.send(someData)
|
|
93
101
|
```
|
|
94
102
|
|
|
95
103
|
#### `EventMessage`
|
|
@@ -102,9 +110,22 @@ Dispatcher 提供了完备的生命周期监控能力:
|
|
|
102
110
|
|
|
103
111
|
### 3. 数据与逻辑定义 (Decorators)
|
|
104
112
|
|
|
113
|
+
#### `@Controller()`
|
|
114
|
+
|
|
115
|
+
- **功能**:标记一个类为**UI控制器**,该类将会随着每个`vue`组件的创建而创建,销毁而销毁。详情见`@virid/vue`。
|
|
116
|
+
- **设计**:配合 `bindController` 使用,注册后可以在`@virid/vue`中使用`useController`获得实例。
|
|
117
|
+
- **示例:**
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
@Controller()
|
|
121
|
+
class PageController {}
|
|
122
|
+
// 使用之前要注册
|
|
123
|
+
app.bindController(PageController)
|
|
124
|
+
```
|
|
125
|
+
|
|
105
126
|
#### `@Component()`
|
|
106
127
|
|
|
107
|
-
-
|
|
128
|
+
- **功能**:标记一个类为**数据实体**,该类将作为全局单例,并一直存在。
|
|
108
129
|
- **设计**:配合 `bindComponent` 使用,注册后可以在System中声明类型以获得依赖注入功能。
|
|
109
130
|
- **示例:**
|
|
110
131
|
|
|
@@ -113,11 +134,13 @@ Dispatcher 提供了完备的生命周期监控能力:
|
|
|
113
134
|
class CounterComponent {
|
|
114
135
|
public count = 0;
|
|
115
136
|
}
|
|
137
|
+
// 使用之前要注册
|
|
138
|
+
app.bindController(CounterComponent)
|
|
116
139
|
```
|
|
117
140
|
|
|
118
141
|
#### `@System(params?)`
|
|
119
142
|
|
|
120
|
-
-
|
|
143
|
+
- **功能**:将静态方法注册为业务逻辑处理器。它实现了“依赖自动装配”。只需要在参数里写上对应的 `Component` 类型,引擎就会在执行时自动注入实例。
|
|
121
144
|
- **参数**:
|
|
122
145
|
- `priority`: 执行优先级。数值越大,在同一个 `Tick` 中执行越早。
|
|
123
146
|
- `messageClass`: 触发该System需要的消息类型,不能与`@Message`共存
|
|
@@ -180,8 +203,8 @@ class PlayerComponent {
|
|
|
180
203
|
|
|
181
204
|
### `@Safe()`
|
|
182
205
|
|
|
183
|
-
-
|
|
184
|
-
- **设计**:主要服务于 `@virid/vue`
|
|
206
|
+
- **功能**:方法访问权限标记,在`Virid`中外部环境(UI 层)严禁直接修改逻辑层数据,所有的修改和方法调用都会被拦截。但对于某些“只读类”或“安全计算类”方法,可以通过 `@Safe()` 显式授权,允许视图层直接调用。
|
|
207
|
+
- **设计**:主要服务于 `@virid/vue` 等外部投影层,详情见`@virid/vue`中的`Deep Shield`
|
|
185
208
|
- **示例**:
|
|
186
209
|
|
|
187
210
|
```ts
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @virid/core v0.
|
|
2
|
+
* @virid/core v0.1.1
|
|
3
3
|
* A lightweight and powerful message core built using dependency injection and CCS concepts
|
|
4
4
|
*/
|
|
5
5
|
var C=Object.defineProperty;var ue=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var fe=Object.prototype.hasOwnProperty;var pe=(s,e,t)=>e in s?C(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var o=(s,e)=>C(s,"name",{value:e,configurable:!0});var ge=(s,e)=>{for(var t in e)C(s,t,{get:e[t],enumerable:!0})},me=(s,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of de(e))!fe.call(s,n)&&n!==t&&C(s,n,{get:()=>e[n],enumerable:!(r=ue(e,n))||r.enumerable});return s};var ye=s=>me(C({},"__esModule",{value:!0}),s);var i=(s,e,t)=>pe(s,typeof e!="symbol"?e+"":e,t);var He={};ge(He,{AtomicModifyMessage:()=>w,BaseMessage:()=>y,Component:()=>$e,Controller:()=>Ae,ErrorMessage:()=>v,EventMessage:()=>p,InfoMessage:()=>S,Message:()=>we,MessageInternal:()=>M,MessageRegistry:()=>E,MessageWriter:()=>a,Observer:()=>Me,Safe:()=>Ee,SingleMessage:()=>x,System:()=>ke,VIRID_METADATA:()=>g,WarnMessage:()=>b,activateInstance:()=>J,bindObservers:()=>R,createVirid:()=>Ce,handleResult:()=>A,publisher:()=>oe});module.exports=ye(He);var j=class j{static send(...e){a.write(this,...e)}};o(j,"BaseMessage");var y=j,L=class L extends y{constructor(){super();i(this,"__kind","SingleMessage")}};o(L,"SingleMessage");var x=L,W=class W extends y{constructor(){super();i(this,"__kind","EventMessage")}};o(W,"EventMessage");var p=W,F=class F extends p{constructor(t,r){super();i(this,"error");i(this,"context");this.error=t,this.context=r}};o(F,"ErrorMessage");var v=F,Q=class Q extends p{constructor(t){super();i(this,"context");this.context=t}};o(Q,"WarnMessage");var b=Q,z=class z extends p{constructor(t){super();i(this,"context");this.context=t}};o(z,"InfoMessage");var S=z,U=class U extends p{constructor(t,r,n){super();i(this,"ComponentClass");i(this,"recipe");i(this,"label");this.ComponentClass=t,this.recipe=r,this.label=n}};o(U,"AtomicModifyMessage");var w=U;var D=null;function J(s){D=s}o(J,"activateInstance");var oe=new Proxy({},{get(s,e){return e==="dispatch"?t=>{if(!D){console.error(`[Virid] Message dispatched before system init: ${t.constructor.name}`);return}return D.dispatch(t)}:Reflect.get(D||{},e)}}),Y=class Y{static write(e,...t){let r=typeof e=="function"?new e(...t):e;oe.dispatch(r)}static error(e,t=""){this.write(new v(e,t))}static warn(e){this.write(new b(e))}static info(e){this.write(new S(e))}};o(Y,"MessageWriter");var a=Y;var q=class q{constructor(e){i(this,"dirtySignalTypes",new Set);i(this,"eventQueue",[]);i(this,"isRunning",!1);i(this,"globalTick",0);i(this,"internalDepth",0);i(this,"eventHub");i(this,"tickPayload",{});i(this,"beforeExecuteHooks",[]);i(this,"afterExecuteHooks",[]);i(this,"beforeTickHooks",[]);i(this,"afterTickHooks",[]);this.eventHub=e}addBeforeExecute(e,t,r){r?this.beforeExecuteHooks.unshift({type:e,handler:t}):this.beforeExecuteHooks.push({type:e,handler:t})}addAfterExecute(e,t,r){r?this.afterExecuteHooks.unshift({type:e,handler:t}):this.afterExecuteHooks.push({type:e,handler:t})}addBeforeTick(e,t){t?this.beforeTickHooks.unshift(e):this.beforeTickHooks.push(e)}addAfterTick(e,t){t?this.afterTickHooks.unshift(e):this.afterTickHooks.push(e)}markDirty(e){e instanceof p?this.eventQueue.push(e):e instanceof x&&this.dirtySignalTypes.add(e.constructor)}tick(e){if(!(this.isRunning||this.dirtySignalTypes.size===0&&this.eventQueue.length===0)){if(this.internalDepth>100){this.internalDepth=0,this.dirtySignalTypes.clear(),this.eventQueue=[],this.eventHub.reset(),a.error(new Error("[Virid Dispatcher] Deadlock: Recursive loop detected \u{1F4A5}."));return}this.isRunning=!0,this.internalDepth++,queueMicrotask(()=>{let t,r;try{this.internalDepth==0&&(this.tickPayload={},this.executeTickHooks(this.beforeTickHooks));let n=this.prepareSnapshot();t=n.signalSnapshot,r=n.eventSnapshot;let u=this.collectTasks(r,t,e);this.executeTasks(u)}catch(n){a.error(n,"[Virid Dispatcher] Unhandled Error")}finally{t&&r&&this.clear(r,t),this.isRunning=!1,this.dirtySignalTypes.size>0||this.eventQueue.length>0?this.tick(e):(this.executeTickHooks(this.afterTickHooks),this.globalTick++,this.internalDepth=0)}})}}collectTasks(e,t,r){let n=[];for(let l of e)(r.get(l.constructor)||[]).forEach(f=>{n.push(new P(f.fn,f.priority,l,{context:f.fn.systemContext,tick:this.globalTick,payload:{}}))});let u=new Set;for(let l of t)(r.get(l)||[]).forEach(f=>{u.has(f.fn)||(n.push(new P(f.fn,f.priority,this.eventHub.peekSignal(l),{context:f.fn.systemContext,tick:this.globalTick,payload:{}})),u.add(f.fn))});return n}executeTasks(e){e.sort((t,r)=>r.priority-t.priority);for(let t of e)try{let r=t.execute(this.beforeExecuteHooks,this.afterExecuteHooks);r instanceof Promise&&r.catch(n=>a.error(n,`[Virid Dispatcher]: Async System Error.
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @virid/core v0.
|
|
2
|
+
* @virid/core v0.1.1
|
|
3
3
|
* A lightweight and powerful message core built using dependency injection and CCS concepts
|
|
4
4
|
*/
|
|
5
5
|
var se=Object.defineProperty;var he=(s,e,t)=>e in s?se(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var o=(s,e)=>se(s,"name",{value:e,configurable:!0});var i=(s,e,t)=>he(s,typeof e!="symbol"?e+"":e,t);var G=class G{static send(...e){a.write(this,...e)}};o(G,"BaseMessage");var y=G,j=class j extends y{constructor(){super();i(this,"__kind","SingleMessage")}};o(j,"SingleMessage");var x=j,L=class L extends y{constructor(){super();i(this,"__kind","EventMessage")}};o(L,"EventMessage");var g=L,W=class W extends g{constructor(t,r){super();i(this,"error");i(this,"context");this.error=t,this.context=r}};o(W,"ErrorMessage");var v=W,F=class F extends g{constructor(t){super();i(this,"context");this.context=t}};o(F,"WarnMessage");var b=F,Q=class Q extends g{constructor(t){super();i(this,"context");this.context=t}};o(Q,"InfoMessage");var S=Q,z=class z extends g{constructor(t,r,n){super();i(this,"ComponentClass");i(this,"recipe");i(this,"label");this.ComponentClass=t,this.recipe=r,this.label=n}};o(z,"AtomicModifyMessage");var M=z;var V=null;function ne(s){V=s}o(ne,"activateInstance");var ue=new Proxy({},{get(s,e){return e==="dispatch"?t=>{if(!V){console.error(`[Virid] Message dispatched before system init: ${t.constructor.name}`);return}return V.dispatch(t)}:Reflect.get(V||{},e)}}),U=class U{static write(e,...t){let r=typeof e=="function"?new e(...t):e;ue.dispatch(r)}static error(e,t=""){this.write(new v(e,t))}static warn(e){this.write(new b(e))}static info(e){this.write(new S(e))}};o(U,"MessageWriter");var a=U;var J=class J{constructor(e){i(this,"dirtySignalTypes",new Set);i(this,"eventQueue",[]);i(this,"isRunning",!1);i(this,"globalTick",0);i(this,"internalDepth",0);i(this,"eventHub");i(this,"tickPayload",{});i(this,"beforeExecuteHooks",[]);i(this,"afterExecuteHooks",[]);i(this,"beforeTickHooks",[]);i(this,"afterTickHooks",[]);this.eventHub=e}addBeforeExecute(e,t,r){r?this.beforeExecuteHooks.unshift({type:e,handler:t}):this.beforeExecuteHooks.push({type:e,handler:t})}addAfterExecute(e,t,r){r?this.afterExecuteHooks.unshift({type:e,handler:t}):this.afterExecuteHooks.push({type:e,handler:t})}addBeforeTick(e,t){t?this.beforeTickHooks.unshift(e):this.beforeTickHooks.push(e)}addAfterTick(e,t){t?this.afterTickHooks.unshift(e):this.afterTickHooks.push(e)}markDirty(e){e instanceof g?this.eventQueue.push(e):e instanceof x&&this.dirtySignalTypes.add(e.constructor)}tick(e){if(!(this.isRunning||this.dirtySignalTypes.size===0&&this.eventQueue.length===0)){if(this.internalDepth>100){this.internalDepth=0,this.dirtySignalTypes.clear(),this.eventQueue=[],this.eventHub.reset(),a.error(new Error("[Virid Dispatcher] Deadlock: Recursive loop detected \u{1F4A5}."));return}this.isRunning=!0,this.internalDepth++,queueMicrotask(()=>{let t,r;try{this.internalDepth==0&&(this.tickPayload={},this.executeTickHooks(this.beforeTickHooks));let n=this.prepareSnapshot();t=n.signalSnapshot,r=n.eventSnapshot;let u=this.collectTasks(r,t,e);this.executeTasks(u)}catch(n){a.error(n,"[Virid Dispatcher] Unhandled Error")}finally{t&&r&&this.clear(r,t),this.isRunning=!1,this.dirtySignalTypes.size>0||this.eventQueue.length>0?this.tick(e):(this.executeTickHooks(this.afterTickHooks),this.globalTick++,this.internalDepth=0)}})}}collectTasks(e,t,r){let n=[];for(let l of e)(r.get(l.constructor)||[]).forEach(f=>{n.push(new D(f.fn,f.priority,l,{context:f.fn.systemContext,tick:this.globalTick,payload:{}}))});let u=new Set;for(let l of t)(r.get(l)||[]).forEach(f=>{u.has(f.fn)||(n.push(new D(f.fn,f.priority,this.eventHub.peekSignal(l),{context:f.fn.systemContext,tick:this.globalTick,payload:{}})),u.add(f.fn))});return n}executeTasks(e){e.sort((t,r)=>r.priority-t.priority);for(let t of e)try{let r=t.execute(this.beforeExecuteHooks,this.afterExecuteHooks);r instanceof Promise&&r.catch(n=>a.error(n,`[Virid Dispatcher]: Async System Error.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@virid/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
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",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
],
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
|
-
"url": "git+https://github.com/
|
|
18
|
+
"url": "git+https://github.com/Ailrid/virid.git"
|
|
19
19
|
},
|
|
20
20
|
"main": "./dist/index.cjs",
|
|
21
21
|
"module": "./dist/index.js",
|
|
@@ -34,12 +34,12 @@
|
|
|
34
34
|
"access": "public",
|
|
35
35
|
"registry": "https://registry.npmjs.org/"
|
|
36
36
|
},
|
|
37
|
-
"scripts": {
|
|
38
|
-
"build": "tsup --config tsup.config.ts",
|
|
39
|
-
"example": "node --import @swc-node/register/esm-register examples/index.ts"
|
|
40
|
-
},
|
|
41
37
|
"dependencies": {},
|
|
42
38
|
"peerDependencies": {
|
|
43
39
|
"reflect-metadata": "^0.2.2"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup --config tsup.config.ts",
|
|
43
|
+
"example": "node --import @swc-node/register/esm-register examples/index.ts"
|
|
44
44
|
}
|
|
45
|
-
}
|
|
45
|
+
}
|