@virid/core 0.0.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.
- package/README.md +259 -0
- package/README.zh.md +203 -0
- package/dist/index.d.mts +218 -0
- package/dist/index.d.ts +218 -0
- package/dist/index.js +712 -0
- package/dist/index.mjs +674 -0
- package/package.json +53 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { Newable, Container, BindInWhenOnFluentSyntax, BindWhenOnFluentSyntax } from 'inversify';
|
|
2
|
+
|
|
3
|
+
type AnyConstructor = abstract new (...args: any[]) => any;
|
|
4
|
+
declare abstract class BaseMessage {
|
|
5
|
+
static send<T extends AnyConstructor>(this: T, ...args: ConstructorParameters<T>): void;
|
|
6
|
+
senderInfo?: {
|
|
7
|
+
fileName: string;
|
|
8
|
+
line: number;
|
|
9
|
+
timestamp: number;
|
|
10
|
+
};
|
|
11
|
+
constructor();
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 可合并的信号基类
|
|
15
|
+
*/
|
|
16
|
+
declare abstract class SingleMessage extends BaseMessage {
|
|
17
|
+
private readonly __kind;
|
|
18
|
+
constructor();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 不可合并的消息基类
|
|
22
|
+
*/
|
|
23
|
+
declare abstract class EventMessage extends BaseMessage {
|
|
24
|
+
private readonly __kind;
|
|
25
|
+
constructor();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 基础错误消息:不可合并,必须被精准捕获
|
|
29
|
+
*/
|
|
30
|
+
declare class ErrorMessage extends EventMessage {
|
|
31
|
+
readonly error: Error;
|
|
32
|
+
readonly context?: string;
|
|
33
|
+
constructor(error: Error, context?: string);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 基础警告消息:不可合并,必须被精准捕获
|
|
37
|
+
*/
|
|
38
|
+
declare class WarnMessage extends EventMessage {
|
|
39
|
+
readonly context: string;
|
|
40
|
+
constructor(context: string);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 原子修改消息:不可合并,带上组件类型、修改逻辑和语义标签
|
|
44
|
+
*/
|
|
45
|
+
declare class AtomicModifyMessage<T> extends EventMessage {
|
|
46
|
+
readonly ComponentClass: Newable<T>;
|
|
47
|
+
readonly recipe: (comp: T) => void;
|
|
48
|
+
readonly label: string;
|
|
49
|
+
constructor(ComponentClass: Newable<T>, // 你要改哪个组件?
|
|
50
|
+
recipe: (comp: T) => void, // 你打算怎么改?
|
|
51
|
+
label: string);
|
|
52
|
+
}
|
|
53
|
+
type Middleware = (message: BaseMessage, next: () => void) => void;
|
|
54
|
+
type Hook<T extends BaseMessage> = (message: [BaseMessage] extends [T] ? SingleMessage[] | EventMessage : T extends SingleMessage ? T[] : T, context: CCSSystemContext) => void | Promise<void>;
|
|
55
|
+
type MessageIdentifier<T> = (abstract new (...args: any[]) => T) | (new (...args: any[]) => T);
|
|
56
|
+
interface CCSSystemContext {
|
|
57
|
+
params: any[];
|
|
58
|
+
targetClass: any;
|
|
59
|
+
methodName: string;
|
|
60
|
+
originalMethod: (...args: any[]) => any;
|
|
61
|
+
}
|
|
62
|
+
interface SystemTask {
|
|
63
|
+
fn: (...args: any[]) => any;
|
|
64
|
+
priority: number;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 核心逻辑:根据 T 的类型决定 Hook 接收的是数组还是单体
|
|
68
|
+
*/
|
|
69
|
+
type MessagePayload<T> = T extends SingleMessage ? T[] : T extends EventMessage ? T : T | T[];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @description: 事件中心,存储和分发消息 - 物理隔离 SIGNAL 与 EVENT
|
|
73
|
+
*/
|
|
74
|
+
declare class EventHub {
|
|
75
|
+
private signalActive;
|
|
76
|
+
private signalStaging;
|
|
77
|
+
private eventActive;
|
|
78
|
+
private eventStaging;
|
|
79
|
+
/**
|
|
80
|
+
* 写入逻辑:根据消息策略分流
|
|
81
|
+
*/
|
|
82
|
+
push(event: any): void;
|
|
83
|
+
/**
|
|
84
|
+
* 翻转缓冲区:在 Dispatcher 的 Tick 开始时调用
|
|
85
|
+
*/
|
|
86
|
+
flip(): void;
|
|
87
|
+
/**
|
|
88
|
+
* [SIGNAL专用] 批量读取某种类型的信号
|
|
89
|
+
*/
|
|
90
|
+
peekSignal<T>(type: new (...args: any[]) => T): T[];
|
|
91
|
+
/**
|
|
92
|
+
* [EVENT专用] 获取当前所有待处理的事件流
|
|
93
|
+
*/
|
|
94
|
+
getEventStream(): any[];
|
|
95
|
+
/**
|
|
96
|
+
* [新增] 根据索引精准读取 EVENT 里的某个数据
|
|
97
|
+
* 配合 Dispatcher 逐条分发时使用
|
|
98
|
+
*/
|
|
99
|
+
peekEventAt(index: number): any;
|
|
100
|
+
/**
|
|
101
|
+
* 清理已处理的内容
|
|
102
|
+
*/
|
|
103
|
+
clearSignals(types: Set<any>): void;
|
|
104
|
+
clearEvents(): void;
|
|
105
|
+
/**
|
|
106
|
+
* 重置所有池子
|
|
107
|
+
*/
|
|
108
|
+
reset(): void;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
declare class Dispatcher {
|
|
112
|
+
private dirtySignalTypes;
|
|
113
|
+
private eventQueue;
|
|
114
|
+
private isRunning;
|
|
115
|
+
private tickCount;
|
|
116
|
+
private eventHub;
|
|
117
|
+
private beforeHooks;
|
|
118
|
+
private afterHooks;
|
|
119
|
+
constructor(eventHub: EventHub);
|
|
120
|
+
addBefore<T extends BaseMessage>(type: MessageIdentifier<T>, hook: Hook<T>): void;
|
|
121
|
+
addAfter<T extends BaseMessage>(type: MessageIdentifier<T>, hook: Hook<T>): void;
|
|
122
|
+
private cleanupHook;
|
|
123
|
+
setCleanupHook(hook: (types: Set<any>) => void): void;
|
|
124
|
+
/**
|
|
125
|
+
* 标记脏数据:根据基类判断进入哪个池子
|
|
126
|
+
*/
|
|
127
|
+
markDirty(message: any): void;
|
|
128
|
+
tick(interestMap: Map<any, SystemTask[]>): void;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @description: 消息注册器 - 负责将系统函数或监听器与消息类型关联
|
|
133
|
+
*/
|
|
134
|
+
declare class MessageRegistry {
|
|
135
|
+
interestMap: Map<any, SystemTask[]>;
|
|
136
|
+
/**
|
|
137
|
+
* 注册消息并返回一个卸载函数
|
|
138
|
+
* 这种模式能完美适配 Controller 的生命周期销毁
|
|
139
|
+
*/
|
|
140
|
+
register(eventClass: any, systemFn: (...args: any[]) => any, priority?: number): () => void;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
declare class MessageInternal {
|
|
144
|
+
readonly eventHub: EventHub;
|
|
145
|
+
readonly dispatcher: Dispatcher;
|
|
146
|
+
readonly registry: MessageRegistry;
|
|
147
|
+
readonly middlewares: Middleware[];
|
|
148
|
+
constructor();
|
|
149
|
+
useMiddleware(mw: Middleware): void;
|
|
150
|
+
onBeforeExecute<T extends BaseMessage>(type: MessageIdentifier<T>, hook: Hook<T>): void;
|
|
151
|
+
onAfterExecute<T extends BaseMessage>(type: MessageIdentifier<T>, hook: Hook<T>): void;
|
|
152
|
+
/**
|
|
153
|
+
* 消息进入系统的唯一入口
|
|
154
|
+
*/
|
|
155
|
+
dispatch(message: BaseMessage): void;
|
|
156
|
+
private pipeline;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
interface IMessagePublisher {
|
|
160
|
+
dispatch(message: BaseMessage): void;
|
|
161
|
+
}
|
|
162
|
+
declare function activatInstance(instance: MessageInternal): void;
|
|
163
|
+
declare const publisher: IMessagePublisher;
|
|
164
|
+
declare class MessageWriter {
|
|
165
|
+
/**
|
|
166
|
+
* 核心入口:无论是类还是实例,统一交给 Internal 处理
|
|
167
|
+
*/
|
|
168
|
+
static write<T extends BaseMessage, K extends new (...args: any[]) => T>(target: K | T, ...args: ConstructorParameters<K>): void;
|
|
169
|
+
/**
|
|
170
|
+
* 快捷方式:系统内部常用
|
|
171
|
+
*/
|
|
172
|
+
static error(e: Error, context?: string): void;
|
|
173
|
+
static warn(context: string): void;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @description: 系统装饰器
|
|
178
|
+
* @param priority 优先级,数值越大越早执行
|
|
179
|
+
*/
|
|
180
|
+
declare function System(priority?: number): (target: any, key: string, descriptor: PropertyDescriptor) => void;
|
|
181
|
+
/**
|
|
182
|
+
* @description: 标记参数为 MessageReader 并锁定其消息类型
|
|
183
|
+
*/
|
|
184
|
+
declare function Message<T extends BaseMessage>(eventClass: new (...args: any[]) => T, single?: boolean): (target: any, key: string, index: number) => void;
|
|
185
|
+
/**
|
|
186
|
+
* @description: 标记Controller身份
|
|
187
|
+
*/
|
|
188
|
+
declare function Controller(): (target: any) => void;
|
|
189
|
+
/**
|
|
190
|
+
* @description: 标记Component身份
|
|
191
|
+
*/
|
|
192
|
+
declare function Component(): (target: any) => void;
|
|
193
|
+
|
|
194
|
+
interface ViridPlugin {
|
|
195
|
+
name: string;
|
|
196
|
+
install: (app: ViridApp, options?: any) => void;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 创建 virid 核心实例
|
|
201
|
+
*/
|
|
202
|
+
declare class ViridApp {
|
|
203
|
+
container: Container;
|
|
204
|
+
private messageInternal;
|
|
205
|
+
private bindBase;
|
|
206
|
+
get(identifier: any): unknown;
|
|
207
|
+
bindController<T>(identifier: new (...args: any[]) => T): BindInWhenOnFluentSyntax<T>;
|
|
208
|
+
bindComponent<T>(identifier: new (...args: any[]) => T): BindWhenOnFluentSyntax<T>;
|
|
209
|
+
useMiddleware(mw: Middleware): void;
|
|
210
|
+
onBeforeExecute<T extends BaseMessage>(type: MessageIdentifier<T>, hook: Hook<T>): void;
|
|
211
|
+
onAfterExecute<T extends BaseMessage>(type: MessageIdentifier<T>, hook: Hook<T>): void;
|
|
212
|
+
register(eventClass: any, systemFn: (...args: any[]) => any, priority?: number): () => void;
|
|
213
|
+
use(plugin: ViridPlugin, options: any): this;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
declare function createVirid(): ViridApp;
|
|
217
|
+
|
|
218
|
+
export { AtomicModifyMessage, BaseMessage, type CCSSystemContext, Component, Controller, ErrorMessage, EventMessage, type Hook, type IMessagePublisher, Message, type MessageIdentifier, MessageInternal, type MessagePayload, MessageWriter, type Middleware, SingleMessage, System, type SystemTask, ViridApp, type ViridPlugin, WarnMessage, activatInstance, createVirid, publisher };
|