@virid/core 0.0.1 → 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.
- package/LICENSE +190 -0
- package/README.md +187 -173
- package/README.zh.md +182 -130
- package/dist/index.cjs +32 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +240 -0
- package/dist/index.d.ts +139 -117
- package/dist/index.js +32 -712
- package/dist/index.js.map +1 -0
- package/package.json +15 -23
- package/dist/index.d.mts +0 -218
- package/dist/index.mjs +0 -674
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,54 @@
|
|
|
1
|
-
|
|
1
|
+
type Newable<TInstance = unknown, TArgs extends unknown[] = any[]> = new (...args: TArgs) => TInstance;
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
interface SystemParams {
|
|
4
|
+
priority?: number;
|
|
5
|
+
messageClass?: Newable<BaseMessage> | null;
|
|
6
|
+
}
|
|
7
|
+
interface MessageMetadata {
|
|
8
|
+
index: number;
|
|
9
|
+
messageClass: Newable<BaseMessage>;
|
|
10
|
+
single: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface ObserverItem {
|
|
13
|
+
key: string;
|
|
14
|
+
callback: (oldVal: any, newVal: any) => void | BaseMessage | BaseMessage;
|
|
15
|
+
}
|
|
16
|
+
type ObserverMetadata = ObserverItem[];
|
|
17
|
+
interface SafeMetadata extends Set<string> {
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type Middleware = (message: BaseMessage, next: () => void) => void;
|
|
21
|
+
type ExecuteHook<T extends BaseMessage> = (message: [BaseMessage] extends [T] ? SingleMessage[] | EventMessage : T extends SingleMessage ? T[] : T, context: ExecuteHookContext) => void | Promise<void>;
|
|
22
|
+
interface ExecuteHookContext {
|
|
23
|
+
context: SystemContext;
|
|
24
|
+
tick: number;
|
|
25
|
+
payload: {
|
|
26
|
+
[key: string]: any;
|
|
10
27
|
};
|
|
11
|
-
|
|
28
|
+
}
|
|
29
|
+
interface SystemContext {
|
|
30
|
+
params: any[];
|
|
31
|
+
targetClass: any;
|
|
32
|
+
methodName: string;
|
|
33
|
+
originalMethod: (...args: any[]) => any;
|
|
34
|
+
}
|
|
35
|
+
interface SystemTask {
|
|
36
|
+
fn: (...args: any[]) => any;
|
|
37
|
+
priority: number;
|
|
38
|
+
}
|
|
39
|
+
type MessagePayload<T> = T extends SingleMessage ? T[] : T extends EventMessage ? T : T | T[];
|
|
40
|
+
type MessageIdentifier<T> = (abstract new (...args: any[]) => T) | Newable<T>;
|
|
41
|
+
type TickHook = (context: TickHookContext) => void | Promise<void>;
|
|
42
|
+
interface TickHookContext {
|
|
43
|
+
tick: number;
|
|
44
|
+
timestamp: number;
|
|
45
|
+
payload: {
|
|
46
|
+
[key: string]: any;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
declare abstract class BaseMessage {
|
|
51
|
+
static send<T extends Newable<any>>(this: T, ...args: ConstructorParameters<T>): void;
|
|
12
52
|
}
|
|
13
53
|
/**
|
|
14
54
|
* 可合并的信号基类
|
|
@@ -39,6 +79,13 @@ declare class WarnMessage extends EventMessage {
|
|
|
39
79
|
readonly context: string;
|
|
40
80
|
constructor(context: string);
|
|
41
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* 基础信息消息:不可合并,必须被精准捕获
|
|
84
|
+
*/
|
|
85
|
+
declare class InfoMessage extends EventMessage {
|
|
86
|
+
readonly context: string;
|
|
87
|
+
constructor(context: string);
|
|
88
|
+
}
|
|
42
89
|
/**
|
|
43
90
|
* 原子修改消息:不可合并,带上组件类型、修改逻辑和语义标签
|
|
44
91
|
*/
|
|
@@ -50,138 +97,74 @@ declare class AtomicModifyMessage<T> extends EventMessage {
|
|
|
50
97
|
recipe: (comp: T) => void, // 你打算怎么改?
|
|
51
98
|
label: string);
|
|
52
99
|
}
|
|
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
100
|
|
|
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;
|
|
101
|
+
declare class MessageInternal {
|
|
116
102
|
private eventHub;
|
|
117
|
-
private
|
|
118
|
-
private
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
103
|
+
private dispatcher;
|
|
104
|
+
private registry;
|
|
105
|
+
private middlewares;
|
|
106
|
+
constructor();
|
|
107
|
+
useMiddleware(mw: Middleware, front: boolean): void;
|
|
108
|
+
onBeforeExecute<T extends BaseMessage>(type: MessageIdentifier<T>, hook: ExecuteHook<T>, front: boolean): void;
|
|
109
|
+
onAfterExecute<T extends BaseMessage>(type: MessageIdentifier<T>, hook: ExecuteHook<T>, front: boolean): void;
|
|
110
|
+
onBeforeTick(hook: TickHook, front: boolean): void;
|
|
111
|
+
onAfterTick(hook: TickHook, front: boolean): void;
|
|
124
112
|
/**
|
|
125
|
-
*
|
|
113
|
+
* 消息进入系统的唯一入口
|
|
126
114
|
*/
|
|
127
|
-
|
|
128
|
-
|
|
115
|
+
dispatch(message: BaseMessage): void;
|
|
116
|
+
private pipeline;
|
|
117
|
+
register(messageClass: any, systemFn: (...args: any[]) => any, priority?: number): () => void;
|
|
129
118
|
}
|
|
130
119
|
|
|
131
120
|
/**
|
|
132
121
|
* @description: 消息注册器 - 负责将系统函数或监听器与消息类型关联
|
|
133
122
|
*/
|
|
134
123
|
declare class MessageRegistry {
|
|
135
|
-
|
|
124
|
+
systemTaskMap: Map<any, SystemTask[]>;
|
|
136
125
|
/**
|
|
137
126
|
* 注册消息并返回一个卸载函数
|
|
138
127
|
* 这种模式能完美适配 Controller 的生命周期销毁
|
|
139
128
|
*/
|
|
140
|
-
register(
|
|
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;
|
|
129
|
+
register(messageClass: any, systemFn: (...args: any[]) => any, priority?: number): () => void;
|
|
157
130
|
}
|
|
158
131
|
|
|
159
132
|
interface IMessagePublisher {
|
|
160
133
|
dispatch(message: BaseMessage): void;
|
|
161
134
|
}
|
|
162
|
-
declare function
|
|
135
|
+
declare function activateInstance(instance: MessageInternal): void;
|
|
163
136
|
declare const publisher: IMessagePublisher;
|
|
164
137
|
declare class MessageWriter {
|
|
165
138
|
/**
|
|
166
139
|
* 核心入口:无论是类还是实例,统一交给 Internal 处理
|
|
167
140
|
*/
|
|
168
|
-
static write<T extends BaseMessage, K extends
|
|
141
|
+
static write<T extends BaseMessage, K extends Newable<T>>(target: K | T, ...args: ConstructorParameters<K>): void;
|
|
169
142
|
/**
|
|
170
143
|
* 快捷方式:系统内部常用
|
|
171
144
|
*/
|
|
172
145
|
static error(e: Error, context?: string): void;
|
|
173
146
|
static warn(context: string): void;
|
|
147
|
+
static info(context: string): void;
|
|
174
148
|
}
|
|
175
149
|
|
|
150
|
+
declare const handleResult: (res: any) => void;
|
|
176
151
|
/**
|
|
177
152
|
* @description: 系统装饰器
|
|
178
153
|
* @param priority 优先级,数值越大越早执行
|
|
179
154
|
*/
|
|
180
|
-
declare function System(
|
|
155
|
+
declare function System(params?: SystemParams): (target: any, key: string, descriptor: PropertyDescriptor) => void;
|
|
181
156
|
/**
|
|
182
157
|
* @description: 标记参数为 MessageReader 并锁定其消息类型
|
|
183
158
|
*/
|
|
184
|
-
declare function Message<T extends BaseMessage>(
|
|
159
|
+
declare function Message<T extends BaseMessage>(messageClass: Newable<T>, single?: boolean): (target: any, key: string, index: number) => void;
|
|
160
|
+
/**
|
|
161
|
+
* @description: 标识controller或者组件的方法是否是安全的,可被其他controller直接调用
|
|
162
|
+
*/
|
|
163
|
+
declare function Safe(): (target: any, key: string, _descriptor: PropertyDescriptor) => void;
|
|
164
|
+
/**
|
|
165
|
+
* @description: 标识Component里的这个属性被改了之后需要调用一个回调
|
|
166
|
+
*/
|
|
167
|
+
declare function Observer(callback: (oldVal: any, newVale: any) => void | BaseMessage | BaseMessage, []: Iterable<any, void, undefined>): (target: any, key: string) => void;
|
|
185
168
|
/**
|
|
186
169
|
* @description: 标记Controller身份
|
|
187
170
|
*/
|
|
@@ -191,28 +174,67 @@ declare function Controller(): (target: any) => void;
|
|
|
191
174
|
*/
|
|
192
175
|
declare function Component(): (target: any) => void;
|
|
193
176
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
177
|
+
declare function bindObservers(instance: any): any;
|
|
178
|
+
|
|
179
|
+
declare const VIRID_METADATA: {
|
|
180
|
+
readonly SYSTEM: "virid:core:system";
|
|
181
|
+
readonly MESSAGE: "virid:core:message";
|
|
182
|
+
readonly CONTROLLER: "virid:core:controller";
|
|
183
|
+
readonly COMPONENT: "virid:core:component";
|
|
184
|
+
readonly SAFE: "virid:core:safe";
|
|
185
|
+
readonly OBSERVER: "virid:core:observer";
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
declare class ViridContainer {
|
|
189
|
+
private bindings;
|
|
190
|
+
private singletonInstances;
|
|
191
|
+
bind<T>(identifier: Newable<T>): {
|
|
192
|
+
toSelf: () => {
|
|
193
|
+
inSingletonScope: () => {
|
|
194
|
+
onActivation: (fn: any) => void;
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
get<T>(identifier: Newable<T>, onActivate: (instance: T) => T): T;
|
|
197
199
|
}
|
|
198
200
|
|
|
201
|
+
interface ViridPlugin<T = any> {
|
|
202
|
+
name: string;
|
|
203
|
+
install: (app: ViridApp, options?: T) => void;
|
|
204
|
+
}
|
|
199
205
|
/**
|
|
200
206
|
* 创建 virid 核心实例
|
|
201
207
|
*/
|
|
202
208
|
declare class ViridApp {
|
|
203
|
-
container:
|
|
209
|
+
container: ViridContainer;
|
|
204
210
|
private messageInternal;
|
|
205
|
-
private
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
211
|
+
private activationHooks;
|
|
212
|
+
addActivationHook(hook: (instance: any) => any): void;
|
|
213
|
+
get<T>(identifier: Newable<T>): T;
|
|
214
|
+
private handleActivation;
|
|
215
|
+
/**
|
|
216
|
+
* 绑定多例 (Controller 通常是多例)
|
|
217
|
+
*/
|
|
218
|
+
bindController<T>(identifier: Newable<T>): {
|
|
219
|
+
inSingletonScope: () => {
|
|
220
|
+
onActivation: () => void;
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* 绑定单例 (Component 是单例)
|
|
225
|
+
*/
|
|
226
|
+
bindComponent<T>(identifier: Newable<T>): {
|
|
227
|
+
onActivation: () => void;
|
|
228
|
+
};
|
|
229
|
+
useMiddleware(mw: Middleware, front?: boolean): void;
|
|
230
|
+
onBeforeExecute<T extends BaseMessage>(type: MessageIdentifier<T>, hook: ExecuteHook<T>, front?: boolean): void;
|
|
231
|
+
onAfterExecute<T extends BaseMessage>(type: MessageIdentifier<T>, hook: ExecuteHook<T>, front?: boolean): void;
|
|
232
|
+
onBeforeTick(hook: TickHook, front?: boolean): void;
|
|
233
|
+
onAfterTick(hook: TickHook, front?: boolean): void;
|
|
234
|
+
register(messageClass: any, systemFn: (...args: any[]) => any, priority?: number): () => void;
|
|
235
|
+
use<T>(plugin: ViridPlugin<T>, options: T): this;
|
|
214
236
|
}
|
|
215
237
|
|
|
216
238
|
declare function createVirid(): ViridApp;
|
|
217
239
|
|
|
218
|
-
export { AtomicModifyMessage, BaseMessage,
|
|
240
|
+
export { AtomicModifyMessage, BaseMessage, Component, Controller, ErrorMessage, EventMessage, type ExecuteHook, type ExecuteHookContext, type IMessagePublisher, InfoMessage, Message, type MessageIdentifier, MessageInternal, type MessageMetadata, type MessagePayload, MessageRegistry, MessageWriter, type Middleware, type Newable, Observer, type ObserverItem, type ObserverMetadata, Safe, type SafeMetadata, SingleMessage, System, type SystemContext, type SystemParams, type SystemTask, type TickHook, type TickHookContext, VIRID_METADATA, ViridApp, type ViridPlugin, WarnMessage, activateInstance, bindObservers, createVirid, handleResult, publisher };
|