gdmb 1.3.4 → 1.3.5
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/dist/gdmb.es.js +1 -722
- package/dist/gdmb.umd.js +1 -1
- package/dist/src/command/Command.d.ts +1 -1
- package/dist/src/domain/Domain.d.ts +15 -0
- package/package.json +6 -4
package/dist/gdmb.es.js
CHANGED
|
@@ -1,722 +1 @@
|
|
|
1
|
-
import { container
|
|
2
|
-
class C {
|
|
3
|
-
/**
|
|
4
|
-
* 事件容器
|
|
5
|
-
*/
|
|
6
|
-
_mapper = {};
|
|
7
|
-
/**
|
|
8
|
-
* 为使用模块注册事件容器
|
|
9
|
-
* @param eventName 事件模块名称
|
|
10
|
-
*/
|
|
11
|
-
register(e) {
|
|
12
|
-
e in this._mapper || (this._mapper[e] = []);
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* 删除事件模块
|
|
16
|
-
*/
|
|
17
|
-
remove(e) {
|
|
18
|
-
e in this._mapper && delete this._mapper[e];
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* 事件监听注册
|
|
22
|
-
* @param eventName 注册事件名称
|
|
23
|
-
* @param handler 处理器
|
|
24
|
-
* @returns 监听句柄
|
|
25
|
-
*/
|
|
26
|
-
on(e, t) {
|
|
27
|
-
const s = this._mapper[e];
|
|
28
|
-
s ? s.push(t) : (this.register(e), this.on(e, t));
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* 移出指定监听函数监听
|
|
32
|
-
* @param eventName 事件名称
|
|
33
|
-
* @param handler 监听函数
|
|
34
|
-
*/
|
|
35
|
-
off(e, t) {
|
|
36
|
-
const s = this._mapper[e];
|
|
37
|
-
if (s) {
|
|
38
|
-
const i = s.indexOf(t);
|
|
39
|
-
i !== -1 && s.splice(i, 1);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* 触发事件
|
|
44
|
-
* @param eventName 事件名称
|
|
45
|
-
* @param args 时间回调参数
|
|
46
|
-
*/
|
|
47
|
-
trigger(e, ...t) {
|
|
48
|
-
const s = this._mapper[e];
|
|
49
|
-
s && setTimeout(() => {
|
|
50
|
-
for (const i of Object.values(s))
|
|
51
|
-
i(...t);
|
|
52
|
-
}, 0);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* 事件基类的构造函数
|
|
56
|
-
*/
|
|
57
|
-
constructor() {
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
class q {
|
|
61
|
-
}
|
|
62
|
-
class R {
|
|
63
|
-
/**
|
|
64
|
-
* 命令历史记录
|
|
65
|
-
*/
|
|
66
|
-
history = [];
|
|
67
|
-
/**
|
|
68
|
-
* 撤销历史记录
|
|
69
|
-
*/
|
|
70
|
-
undoHistory = [];
|
|
71
|
-
/**
|
|
72
|
-
* 命令历史最大深度
|
|
73
|
-
*/
|
|
74
|
-
maxHistoryDepth;
|
|
75
|
-
/**
|
|
76
|
-
* 撤销历史最大深度
|
|
77
|
-
*/
|
|
78
|
-
maxUndoHistoryDepth;
|
|
79
|
-
/**
|
|
80
|
-
* 执行命令
|
|
81
|
-
* @param command 要执行的命令
|
|
82
|
-
* @param pushToHistory 是否将命令推入历史记录,默认为 true
|
|
83
|
-
*/
|
|
84
|
-
execute(e, t = !0) {
|
|
85
|
-
e.execute(), t && (this.history.push({
|
|
86
|
-
command: e,
|
|
87
|
-
timestamp: Date.now()
|
|
88
|
-
}), this.undoHistory = [], this.history.length > this.maxHistoryDepth && this.history.shift());
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* 撤销上一个命令
|
|
92
|
-
* @returns 是否成功撤销
|
|
93
|
-
*/
|
|
94
|
-
undo() {
|
|
95
|
-
if (this.history.length === 0)
|
|
96
|
-
return !1;
|
|
97
|
-
const e = this.history.pop();
|
|
98
|
-
return e ? (e.command.undo(), this.undoHistory.push(e), this.undoHistory.length > this.maxUndoHistoryDepth && this.undoHistory.shift(), !0) : !1;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* 重做上一个撤销的命令
|
|
102
|
-
* @returns 是否成功重做
|
|
103
|
-
*/
|
|
104
|
-
redo() {
|
|
105
|
-
if (this.undoHistory.length === 0)
|
|
106
|
-
return !1;
|
|
107
|
-
const e = this.undoHistory.pop();
|
|
108
|
-
return e ? (e.command.execute(), this.history.push(e), this.history.length > this.maxHistoryDepth && this.history.shift(), !0) : !1;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* 批量执行命令
|
|
112
|
-
* @param commands 要执行的命令数组
|
|
113
|
-
* @param pushToHistory 是否将命令推入历史记录,默认为 true
|
|
114
|
-
*/
|
|
115
|
-
executeBatch(e, t = !0) {
|
|
116
|
-
e.forEach((s) => this.execute(s, t));
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* 清空命令历史
|
|
120
|
-
*/
|
|
121
|
-
clearHistory() {
|
|
122
|
-
this.history = [], this.undoHistory = [];
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* 获取命令历史记录
|
|
126
|
-
* @returns 命令历史记录数组
|
|
127
|
-
*/
|
|
128
|
-
getHistory() {
|
|
129
|
-
return [...this.history];
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* 获取撤销历史记录
|
|
133
|
-
* @returns 撤销历史记录数组
|
|
134
|
-
*/
|
|
135
|
-
getUndoHistory() {
|
|
136
|
-
return [...this.undoHistory];
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* 获取历史记录数量
|
|
140
|
-
* @returns 历史记录数量
|
|
141
|
-
*/
|
|
142
|
-
getHistoryCount() {
|
|
143
|
-
return this.history.length;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* 获取撤销历史记录数量
|
|
147
|
-
* @returns 撤销历史记录数量
|
|
148
|
-
*/
|
|
149
|
-
getUndoHistoryCount() {
|
|
150
|
-
return this.undoHistory.length;
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* 构造函数
|
|
154
|
-
* @param option 命令调用者构造配置参数
|
|
155
|
-
*/
|
|
156
|
-
constructor(e) {
|
|
157
|
-
this.maxHistoryDepth = e?.maxHistoryDepth || 30, this.maxUndoHistoryDepth = e?.maxUndoHistoryDepth || 30;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
class v {
|
|
161
|
-
/**
|
|
162
|
-
* 命令接收者
|
|
163
|
-
*/
|
|
164
|
-
receiver;
|
|
165
|
-
/**
|
|
166
|
-
* 执行操作的函数
|
|
167
|
-
*/
|
|
168
|
-
executeFn;
|
|
169
|
-
/**
|
|
170
|
-
* 撤销操作的函数
|
|
171
|
-
*/
|
|
172
|
-
undoFn;
|
|
173
|
-
/**
|
|
174
|
-
* 命令描述
|
|
175
|
-
*/
|
|
176
|
-
description;
|
|
177
|
-
/**
|
|
178
|
-
* 原始命令字符串
|
|
179
|
-
*/
|
|
180
|
-
commandString;
|
|
181
|
-
/**
|
|
182
|
-
* 构造函数
|
|
183
|
-
* @param receiver 命令接收者
|
|
184
|
-
* @param executeFn 执行操作的函数
|
|
185
|
-
* @param undoFn 撤销操作的函数
|
|
186
|
-
* @param description 命令描述
|
|
187
|
-
* @param commandString 原始命令字符串
|
|
188
|
-
*/
|
|
189
|
-
constructor(e, t, s, i, o = "") {
|
|
190
|
-
this.receiver = e, this.executeFn = t, this.undoFn = s, this.description = i, this.commandString = o;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* 执行命令
|
|
194
|
-
*/
|
|
195
|
-
execute() {
|
|
196
|
-
this.executeFn(this.receiver);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* 撤销命令
|
|
200
|
-
*/
|
|
201
|
-
undo() {
|
|
202
|
-
this.undoFn(this.receiver);
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* 获取命令描述
|
|
206
|
-
* @returns 命令描述
|
|
207
|
-
*/
|
|
208
|
-
getDescription() {
|
|
209
|
-
return this.description;
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* 获取原始命令字符串
|
|
213
|
-
* @returns 命令字符串
|
|
214
|
-
*/
|
|
215
|
-
getCommandString() {
|
|
216
|
-
return this.commandString;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
function b(n, e, t, s, i = "") {
|
|
220
|
-
return new v(n, e, t, s, i);
|
|
221
|
-
}
|
|
222
|
-
class H {
|
|
223
|
-
/** 命令注册映射 */
|
|
224
|
-
commands = /* @__PURE__ */ new Map();
|
|
225
|
-
/**
|
|
226
|
-
* 注册命令
|
|
227
|
-
* @param name 命令名
|
|
228
|
-
* @param receiver 命令接收者
|
|
229
|
-
* @param creator 命令创建函数
|
|
230
|
-
* @param description 命令描述
|
|
231
|
-
*/
|
|
232
|
-
register(e, t, s) {
|
|
233
|
-
this.commands.set(e, { receiver: t, description: s });
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* 获取命令注册信息
|
|
237
|
-
*/
|
|
238
|
-
get(e) {
|
|
239
|
-
return this.commands.get(e);
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* 列出所有已注册的命令
|
|
243
|
-
*/
|
|
244
|
-
listCommands() {
|
|
245
|
-
return [...this.commands.entries()].map(([e, t]) => ({
|
|
246
|
-
name: e,
|
|
247
|
-
description: t.description
|
|
248
|
-
}));
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
class D {
|
|
252
|
-
/**
|
|
253
|
-
* 解析命令字符串
|
|
254
|
-
* @param commandString 命令字符串,如 "add -a 10 -b 5"
|
|
255
|
-
* @returns ParsedCommand 对象
|
|
256
|
-
*/
|
|
257
|
-
parse(e) {
|
|
258
|
-
const t = e.trim().split(/\s+/g), s = t[0], i = /* @__PURE__ */ new Map();
|
|
259
|
-
for (let o = 1; o < t.length; o++) {
|
|
260
|
-
const r = t[o];
|
|
261
|
-
if (r.startsWith("-")) {
|
|
262
|
-
const a = r.slice(1);
|
|
263
|
-
if (o + 1 >= t.length || t[o + 1].startsWith("-"))
|
|
264
|
-
i.set(a, !0);
|
|
265
|
-
else {
|
|
266
|
-
const u = t[o + 1];
|
|
267
|
-
isNaN(Number(u)) ? i.set(a, u) : i.set(a, Number(u)), o++;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
return {
|
|
272
|
-
commandName: s,
|
|
273
|
-
options: i,
|
|
274
|
-
args: t.slice(1)
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
class f {
|
|
279
|
-
/**
|
|
280
|
-
* 命令调用者
|
|
281
|
-
*/
|
|
282
|
-
invoker;
|
|
283
|
-
/**
|
|
284
|
-
* 命令解析器
|
|
285
|
-
*/
|
|
286
|
-
parser = new D();
|
|
287
|
-
/**
|
|
288
|
-
* 命令注册器
|
|
289
|
-
*/
|
|
290
|
-
registry = new H();
|
|
291
|
-
/**
|
|
292
|
-
* 注册命令
|
|
293
|
-
* @param name 命令名称
|
|
294
|
-
* @param receiver 命令接收者
|
|
295
|
-
*/
|
|
296
|
-
register(e) {
|
|
297
|
-
this.registry.register(e.name, e, e.description);
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* 创建命令
|
|
301
|
-
* @param receiver 命令接收者
|
|
302
|
-
* @param executeFn 执行操作的函数
|
|
303
|
-
* @param undoFn 撤销操作的函数
|
|
304
|
-
* @param description 命令描述
|
|
305
|
-
* @param commandString 原始命令字符串
|
|
306
|
-
* @returns 命令对象
|
|
307
|
-
*/
|
|
308
|
-
create(e, t, s, i, o = "") {
|
|
309
|
-
return b(e, t, s, i, o);
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* 从命令字符串创建 Command
|
|
313
|
-
* @param commandString 命令字符串
|
|
314
|
-
* @param description 针对本次命令描述
|
|
315
|
-
* @returns Command 实例,失败返回 undefined
|
|
316
|
-
*/
|
|
317
|
-
createFromCommandString(e, t) {
|
|
318
|
-
const s = this.parser.parse(e), i = this.registry.get(s.commandName);
|
|
319
|
-
if (!i) {
|
|
320
|
-
console.warn(`命令 ${s.commandName} 未注册`);
|
|
321
|
-
return;
|
|
322
|
-
}
|
|
323
|
-
return i.receiver.creator?.(
|
|
324
|
-
s.options,
|
|
325
|
-
s.args,
|
|
326
|
-
t || i.description,
|
|
327
|
-
e
|
|
328
|
-
);
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* 执行命令
|
|
332
|
-
* @param command 要执行的命令
|
|
333
|
-
* @param description 针对本次命令描述
|
|
334
|
-
* @param pushToHistory 是否将命令推入历史记录,默认为 true
|
|
335
|
-
*/
|
|
336
|
-
execute(e, t, s = !0) {
|
|
337
|
-
if (typeof e == "string") {
|
|
338
|
-
const i = this.createFromCommandString(e, t);
|
|
339
|
-
i && this.invoker.execute(i, s);
|
|
340
|
-
} else
|
|
341
|
-
this.invoker.execute(e, s);
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* 撤销上一个命令
|
|
345
|
-
* @returns 是否成功撤销
|
|
346
|
-
*/
|
|
347
|
-
undo() {
|
|
348
|
-
return this.invoker.undo();
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* 重做上一个撤销的命令
|
|
352
|
-
* @returns 是否成功重做
|
|
353
|
-
*/
|
|
354
|
-
redo() {
|
|
355
|
-
return this.invoker.redo();
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* 批量执行命令
|
|
359
|
-
* @param commands 要执行的命令数组
|
|
360
|
-
* @param pushToHistory 是否将命令推入历史记录,默认为 true
|
|
361
|
-
*/
|
|
362
|
-
executeBatch(e, t = !0) {
|
|
363
|
-
this.invoker.executeBatch(e, t);
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* 清空命令历史
|
|
367
|
-
*/
|
|
368
|
-
clearHistory() {
|
|
369
|
-
this.invoker.clearHistory();
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
* 获取命令历史记录
|
|
373
|
-
* @returns 命令历史记录数组
|
|
374
|
-
*/
|
|
375
|
-
getHistory() {
|
|
376
|
-
return this.invoker.getHistory();
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* 获取撤销历史记录
|
|
380
|
-
* @returns 撤销历史记录数组
|
|
381
|
-
*/
|
|
382
|
-
getUndoHistory() {
|
|
383
|
-
return this.invoker.getUndoHistory();
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* 获取历史记录数量
|
|
387
|
-
* @returns 历史记录数量
|
|
388
|
-
*/
|
|
389
|
-
getHistoryCount() {
|
|
390
|
-
return this.invoker.getHistoryCount();
|
|
391
|
-
}
|
|
392
|
-
/**
|
|
393
|
-
* 获取撤销历史记录数量
|
|
394
|
-
* @returns 撤销历史记录数量
|
|
395
|
-
*/
|
|
396
|
-
getUndoHistoryCount() {
|
|
397
|
-
return this.invoker.getUndoHistoryCount();
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* 列出所有可用命令
|
|
401
|
-
*/
|
|
402
|
-
listCommands() {
|
|
403
|
-
return this.registry.listCommands();
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* 构造函数
|
|
407
|
-
* @param option 配置参数
|
|
408
|
-
*/
|
|
409
|
-
constructor(e) {
|
|
410
|
-
this.invoker = new R({
|
|
411
|
-
maxHistoryDepth: e?.maxHistoryDepth,
|
|
412
|
-
maxUndoHistoryDepth: e?.maxUndoHistoryDepth
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
class k {
|
|
417
|
-
/**
|
|
418
|
-
* @summary 领域内部依赖注入容器
|
|
419
|
-
* @description 领域内部依赖注入容器,用于注册和解析领域服务、实体、值对象等。
|
|
420
|
-
*/
|
|
421
|
-
dic = l.createChildContainer();
|
|
422
|
-
/**
|
|
423
|
-
* @description 领域事件句柄,用于触发、监听、取消监听领域事件
|
|
424
|
-
*/
|
|
425
|
-
e = new C();
|
|
426
|
-
/**
|
|
427
|
-
* @description 领域命令句柄,用于注册、执行、撤销、重做领域命令
|
|
428
|
-
*/
|
|
429
|
-
c;
|
|
430
|
-
/**
|
|
431
|
-
* @description 向领域依赖注入容器中注册一个或多个领域服务
|
|
432
|
-
* @param service 单个领域服务类或数组
|
|
433
|
-
* @param options 领域服务注册配置项,可传入领域服务构造参数以及配置依赖解决拦截器和频率等
|
|
434
|
-
* @example
|
|
435
|
-
* ```typescript
|
|
436
|
-
* class ServiceOne extends DomainService {
|
|
437
|
-
*
|
|
438
|
-
* // before resolve 拦截器
|
|
439
|
-
* public static beforeResolution(token: InjectionToken, resolutionType: string) {}
|
|
440
|
-
*
|
|
441
|
-
* // after resolve 拦截器
|
|
442
|
-
* public static afterResolution(token: InjectionToken, result: ServiceOne | ServiceOne[], resolutionType: string) {}
|
|
443
|
-
*
|
|
444
|
-
* constructor(public arg: string, public arg2: number) {
|
|
445
|
-
* super();
|
|
446
|
-
* }
|
|
447
|
-
* }
|
|
448
|
-
*
|
|
449
|
-
* // 注册领域服务
|
|
450
|
-
* domain.registerService(ServiceOne, { beforeResolution: ServiceOne.beforeResolution, afterResolution: ServiceOne.afterResolution })
|
|
451
|
-
* ```
|
|
452
|
-
*/
|
|
453
|
-
registerService(e, t) {
|
|
454
|
-
const s = Array.isArray(e) ? e : [e], i = t ? Array.isArray(t) ? t : [t] : [];
|
|
455
|
-
for (let o = 0; o < s.length; o += 1) {
|
|
456
|
-
const r = s[o], a = i[o];
|
|
457
|
-
if (this.dic.isRegistered(r.name) || this.dic.isRegistered(r)) {
|
|
458
|
-
console.warn(`服务 ${r.name} 已注册,跳过注册`);
|
|
459
|
-
continue;
|
|
460
|
-
}
|
|
461
|
-
this.dic.register(r.name, { useClass: r }), this.dic.register(r, { useClass: r }), a?.beforeResolution && (this.dic.beforeResolution(r.name, a.beforeResolution, {
|
|
462
|
-
frequency: a?.beforeResolutionFrequency || "Always"
|
|
463
|
-
}), this.dic.beforeResolution(r, a.beforeResolution, {
|
|
464
|
-
frequency: a?.beforeResolutionFrequency || "Always"
|
|
465
|
-
})), a?.afterResolution && (this.dic.afterResolution(r.name, a.afterResolution, {
|
|
466
|
-
frequency: a?.afterResolutionFrequency || "Always"
|
|
467
|
-
}), this.dic.afterResolution(r, a?.afterResolution, {
|
|
468
|
-
frequency: a?.afterResolutionFrequency || "Always"
|
|
469
|
-
}));
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
/**
|
|
473
|
-
* @description 向领域依赖注入容器中注册领域实体,指定缓存策略为'cache'时全局单例。
|
|
474
|
-
* @param entity 单个领域实体类或数组
|
|
475
|
-
* @param options 领域实体注册配置项
|
|
476
|
-
* @example
|
|
477
|
-
* ```typescript
|
|
478
|
-
* class EntityOne extends DomainEntity {
|
|
479
|
-
*
|
|
480
|
-
* // before resolve 拦截器
|
|
481
|
-
* public static beforeResolution(token: InjectionToken, resolutionType: string) {}
|
|
482
|
-
*
|
|
483
|
-
* // after resolve 拦截器
|
|
484
|
-
* public static afterResolution(token: InjectionToken, result: EntityOne | EntityOne[], resolutionType: string) {}
|
|
485
|
-
*
|
|
486
|
-
* constructor(public arg: string, public arg2: number) {
|
|
487
|
-
* super();
|
|
488
|
-
* }
|
|
489
|
-
* }
|
|
490
|
-
*
|
|
491
|
-
* // 注册领域实体
|
|
492
|
-
* domain.registerEntity(EntityOne, { cachePolicy: 'cache', beforeResolution: EntityOne.beforeResolution, afterResolution: EntityOne.afterResolution })
|
|
493
|
-
* ```
|
|
494
|
-
*/
|
|
495
|
-
registerEntity(e, t) {
|
|
496
|
-
const s = Array.isArray(e) ? e : [e], i = t ? Array.isArray(t) ? t : [t] : [];
|
|
497
|
-
for (let o = 0; o < s.length; o += 1) {
|
|
498
|
-
const r = s[o], a = i[o];
|
|
499
|
-
if (this.dic.isRegistered(r.name) || this.dic.isRegistered(r)) {
|
|
500
|
-
console.warn(`实体 ${r.name} 已注册,跳过注册`);
|
|
501
|
-
continue;
|
|
502
|
-
}
|
|
503
|
-
if (a?.cachePolicy)
|
|
504
|
-
switch (a.cachePolicy) {
|
|
505
|
-
case "cache":
|
|
506
|
-
l.register(r.name, { useClass: r }, { lifecycle: c.Singleton }), l.register(r, { useClass: r }, { lifecycle: c.Singleton });
|
|
507
|
-
break;
|
|
508
|
-
case "containerCache":
|
|
509
|
-
this.dic.register(r.name, { useClass: r }, { lifecycle: c.ContainerScoped }), this.dic.register(r, { useClass: r }, { lifecycle: c.ContainerScoped });
|
|
510
|
-
break;
|
|
511
|
-
case "resolution":
|
|
512
|
-
this.dic.register(r.name, { useClass: r }, { lifecycle: c.ResolutionScoped }), this.dic.register(r, { useClass: r }, { lifecycle: c.ResolutionScoped });
|
|
513
|
-
break;
|
|
514
|
-
}
|
|
515
|
-
else
|
|
516
|
-
this.dic.register(r.name, { useClass: r }, { lifecycle: c.Transient }), this.dic.register(r, { useClass: r }, { lifecycle: c.Transient });
|
|
517
|
-
a?.beforeResolution && (this.dic.beforeResolution(r.name, a.beforeResolution, {
|
|
518
|
-
frequency: a?.beforeResolutionFrequency || "Always"
|
|
519
|
-
}), this.dic.beforeResolution(r, a.beforeResolution, {
|
|
520
|
-
frequency: a?.beforeResolutionFrequency || "Always"
|
|
521
|
-
})), a?.afterResolution && (this.dic.afterResolution(r.name, a.afterResolution, {
|
|
522
|
-
frequency: a?.afterResolutionFrequency || "Always"
|
|
523
|
-
}), this.dic.afterResolution(r, a.afterResolution, {
|
|
524
|
-
frequency: a?.afterResolutionFrequency || "Always"
|
|
525
|
-
}));
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
/**
|
|
529
|
-
* @summary 项领域内依赖注入容器中注册值对象,当指定 factoryType 为'cache'时全局单例。
|
|
530
|
-
* @description 需要泛型类型 V,约束工厂函数返回类型,V 满足 VO 类型约束,不允许包含函数属性
|
|
531
|
-
* @param vos 值对象注册配置项
|
|
532
|
-
* @example
|
|
533
|
-
* ```typescript
|
|
534
|
-
* domain.registerVO({
|
|
535
|
-
* token: 'VO1',
|
|
536
|
-
* factoryType: 'cache',
|
|
537
|
-
* resolveFactory: () => ({ date: '2023-01-01' })
|
|
538
|
-
* })
|
|
539
|
-
* ```
|
|
540
|
-
*/
|
|
541
|
-
registerVO(e) {
|
|
542
|
-
const t = Array.isArray(e) ? e : [e];
|
|
543
|
-
for (let s = 0; s < t.length; s += 1) {
|
|
544
|
-
const i = t[s], o = Array.isArray(i.token) ? i.token : [i.token];
|
|
545
|
-
for (let r = 0; r < o.length; r += 1) {
|
|
546
|
-
const a = o[r];
|
|
547
|
-
switch (i.factoryType) {
|
|
548
|
-
case "cache":
|
|
549
|
-
l.register(a, { useFactory: p(i.resolveFactory) });
|
|
550
|
-
break;
|
|
551
|
-
case "containerCache":
|
|
552
|
-
this.dic.register(a, { useFactory: g(i.resolveFactory) });
|
|
553
|
-
break;
|
|
554
|
-
case "predicate":
|
|
555
|
-
this.dic.register(a, {
|
|
556
|
-
useFactory: m(
|
|
557
|
-
i.resolveFactory,
|
|
558
|
-
i.positiveTarget,
|
|
559
|
-
i.negativeTarget
|
|
560
|
-
)
|
|
561
|
-
});
|
|
562
|
-
break;
|
|
563
|
-
default:
|
|
564
|
-
this.dic.register(a, { useFactory: i.resolveFactory });
|
|
565
|
-
break;
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
/**
|
|
571
|
-
* @description 从依赖注入容器中获取领域服务实例对象
|
|
572
|
-
* @param token 领域服务token
|
|
573
|
-
* @returns 领域服务实例对象
|
|
574
|
-
*/
|
|
575
|
-
services(e) {
|
|
576
|
-
const t = this.dic.resolve(e);
|
|
577
|
-
return t && !t?.e && (t.e = this.e), t;
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* @description 从依赖注入容器中获取领域实体实例对象
|
|
581
|
-
* @param sourceToken 源领域实体token,存在适配器时是 DEF 泛型类型,否则为 DE 泛型类型
|
|
582
|
-
* @param adapter 领域实体适配器,将 DEF 类型转换为 DE 类型
|
|
583
|
-
* @returns 领域实体实例对象
|
|
584
|
-
*/
|
|
585
|
-
entities(e, t) {
|
|
586
|
-
let s;
|
|
587
|
-
if (t) {
|
|
588
|
-
const i = this.dic.resolve(e);
|
|
589
|
-
s = t.transform(i);
|
|
590
|
-
} else
|
|
591
|
-
s = this.dic.resolve(e);
|
|
592
|
-
return s && !s?.e && (s.e = this.e), s;
|
|
593
|
-
}
|
|
594
|
-
/**
|
|
595
|
-
* @description 从依赖注入容器中获取领域值对象实例对象
|
|
596
|
-
* @param sourceToken 源领域值对象token,存在适配器时是 VF 泛型类型,否则为 V 泛型类型
|
|
597
|
-
* @param adapter 领域值对象适配器,将 VF 类型转换为 V 类型
|
|
598
|
-
* @returns 领域值对象实例对象
|
|
599
|
-
*/
|
|
600
|
-
vos(e, t) {
|
|
601
|
-
if (t) {
|
|
602
|
-
const s = this.dic.resolve(e);
|
|
603
|
-
return t.transform(s);
|
|
604
|
-
} else
|
|
605
|
-
return this.dic.resolve(e);
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* 域基类构造函数
|
|
609
|
-
*/
|
|
610
|
-
constructor(e) {
|
|
611
|
-
this.c = new f(e);
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
const d = (n) => (n.prototype.$ins = null, class extends n {
|
|
615
|
-
constructor(...e) {
|
|
616
|
-
return super(...e), n.prototype.$ins || (n.prototype.$ins = this), n.prototype.$ins;
|
|
617
|
-
}
|
|
618
|
-
});
|
|
619
|
-
var x = Object.getOwnPropertyDescriptor, w = (n, e, t, s) => {
|
|
620
|
-
for (var i = s > 1 ? void 0 : s ? x(e, t) : e, o = n.length - 1, r; o >= 0; o--)
|
|
621
|
-
(r = n[o]) && (i = r(i) || i);
|
|
622
|
-
return i;
|
|
623
|
-
};
|
|
624
|
-
let h = class {
|
|
625
|
-
/** 事件句柄 */
|
|
626
|
-
e = void 0;
|
|
627
|
-
/**
|
|
628
|
-
* 域基类构造函数
|
|
629
|
-
*/
|
|
630
|
-
constructor() {
|
|
631
|
-
}
|
|
632
|
-
};
|
|
633
|
-
h = w([
|
|
634
|
-
d
|
|
635
|
-
], h);
|
|
636
|
-
class I {
|
|
637
|
-
/** 事件句柄 */
|
|
638
|
-
e = void 0;
|
|
639
|
-
/**
|
|
640
|
-
* 域基类构造函数
|
|
641
|
-
*/
|
|
642
|
-
constructor() {
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
var A = Object.getOwnPropertyDescriptor, F = (n, e, t, s) => {
|
|
646
|
-
for (var i = s > 1 ? void 0 : s ? A(e, t) : e, o = n.length - 1, r; o >= 0; o--)
|
|
647
|
-
(r = n[o]) && (i = r(i) || i);
|
|
648
|
-
return i;
|
|
649
|
-
};
|
|
650
|
-
let y = class {
|
|
651
|
-
/** 全局 DIC */
|
|
652
|
-
globalDIC = l;
|
|
653
|
-
/** 命令系统 */
|
|
654
|
-
c;
|
|
655
|
-
/**
|
|
656
|
-
* 注册领域对象
|
|
657
|
-
*/
|
|
658
|
-
registerDomain(n, e) {
|
|
659
|
-
const t = Array.isArray(n) ? n : [n], s = e ? Array.isArray(e) ? e : [e] : [];
|
|
660
|
-
for (let i = 0; i < t.length; i += 1) {
|
|
661
|
-
const o = t[i], r = s[i];
|
|
662
|
-
if (this.globalDIC.isRegistered(o.name)) {
|
|
663
|
-
console.warn(`领域 ${o.name} 已注册,无法重复注册`);
|
|
664
|
-
return;
|
|
665
|
-
}
|
|
666
|
-
const a = new o(...r?.args || []);
|
|
667
|
-
this.globalDIC.register(o.name, { useValue: a }), this.globalDIC.register(o, { useValue: a }), r?.beforeResolution && (this.globalDIC.beforeResolution(o.name, r.beforeResolution, {
|
|
668
|
-
frequency: r.beforeResolutionFrequency || "Always"
|
|
669
|
-
}), this.globalDIC.beforeResolution(o, r.beforeResolution, {
|
|
670
|
-
frequency: r.afterResolutionFrequency || "Always"
|
|
671
|
-
})), r?.afterResolution && (this.globalDIC.afterResolution(o.name, r.afterResolution, {
|
|
672
|
-
frequency: r.afterResolutionFrequency || "Always"
|
|
673
|
-
}), this.globalDIC.afterResolution(o, r.afterResolution, {
|
|
674
|
-
frequency: r.afterResolutionFrequency || "Always"
|
|
675
|
-
}));
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
/**
|
|
679
|
-
* 获取领域对象
|
|
680
|
-
*/
|
|
681
|
-
domains(n) {
|
|
682
|
-
return this.globalDIC.resolve(n);
|
|
683
|
-
}
|
|
684
|
-
/**
|
|
685
|
-
* 提供全局的转换方法
|
|
686
|
-
*/
|
|
687
|
-
transform(n, e) {
|
|
688
|
-
return e.transform(n);
|
|
689
|
-
}
|
|
690
|
-
/**
|
|
691
|
-
* 提供全局的转换方法,支持批量转换
|
|
692
|
-
*/
|
|
693
|
-
transformAll(n, e) {
|
|
694
|
-
const t = [];
|
|
695
|
-
for (let s = 0; s < n.length; s += 1) {
|
|
696
|
-
const i = e.transform(n[s]);
|
|
697
|
-
i != null && t.push(i);
|
|
698
|
-
}
|
|
699
|
-
return t;
|
|
700
|
-
}
|
|
701
|
-
/**
|
|
702
|
-
* 构造函数
|
|
703
|
-
* @param option 配置参数
|
|
704
|
-
*/
|
|
705
|
-
constructor(n) {
|
|
706
|
-
this.c = new f({
|
|
707
|
-
maxHistoryDepth: n?.maxHistoryDepth,
|
|
708
|
-
maxUndoHistoryDepth: n?.maxUndoHistoryDepth
|
|
709
|
-
});
|
|
710
|
-
}
|
|
711
|
-
};
|
|
712
|
-
y = F([
|
|
713
|
-
d
|
|
714
|
-
], y);
|
|
715
|
-
export {
|
|
716
|
-
q as Adapter,
|
|
717
|
-
f as CommandSystem,
|
|
718
|
-
k as Domain,
|
|
719
|
-
I as DomainEntity,
|
|
720
|
-
h as DomainService,
|
|
721
|
-
y as GDMB
|
|
722
|
-
};
|
|
1
|
+
import{container,Lifecycle,predicateAwareClassFactory,instancePerContainerCachingFactory,instanceCachingFactory}from"tsyringe";class BaseEvents{_mapper={};register(e){e in this._mapper||(this._mapper[e]=[])}remove(e){e in this._mapper&&delete this._mapper[e]}on(e,t){const r=this._mapper[e];r?r.push(t):(this.register(e),this.on(e,t))}off(e,t){const r=this._mapper[e];if(r){const e=r.indexOf(t);-1!==e&&r.splice(e,1)}}trigger(e,...t){const r=this._mapper[e];r&&setTimeout(()=>{for(const e of Object.values(r))e(...t)},0)}constructor(){}}class Adapter{}class CommandInvoker{history=[];undoHistory=[];maxHistoryDepth;maxUndoHistoryDepth;execute(e,t=!0){e.execute(),t&&(this.history.push({command:e,timestamp:Date.now()}),this.undoHistory=[],this.history.length>this.maxHistoryDepth&&this.history.shift())}undo(){if(0===this.history.length)return!1;const e=this.history.pop();return!!e&&(e.command.undo(),this.undoHistory.push(e),this.undoHistory.length>this.maxUndoHistoryDepth&&this.undoHistory.shift(),!0)}redo(){if(0===this.undoHistory.length)return!1;const e=this.undoHistory.pop();return!!e&&(e.command.execute(),this.history.push(e),this.history.length>this.maxHistoryDepth&&this.history.shift(),!0)}executeBatch(e,t=!0){e.forEach(e=>this.execute(e,t))}clearHistory(){this.history=[],this.undoHistory=[]}getHistory(){return[...this.history]}getUndoHistory(){return[...this.undoHistory]}getHistoryCount(){return this.history.length}getUndoHistoryCount(){return this.undoHistory.length}constructor(e){this.maxHistoryDepth=e?.maxHistoryDepth||30,this.maxUndoHistoryDepth=e?.maxUndoHistoryDepth||30}}class ConcreteCommand{receiver;executeFn;undoFn;description;commandString;constructor(e,t,r,s,i=""){this.receiver=e,this.executeFn=t,this.undoFn=r,this.description=s,this.commandString=i}execute(){this.executeFn(this.receiver)}undo(){this.undoFn(this.receiver)}getDescription(){return this.description}getCommandString(){return this.commandString}}function createCommand(e,t,r,s,i=""){return new ConcreteCommand(e,t,r,s,i)}class CommandRegistry{commands=new Map;register(e,t,r){this.commands.set(e,{receiver:t,description:r})}get(e){return this.commands.get(e)}listCommands(){return[...this.commands.entries()].map(([e,t])=>({name:e,description:t.description}))}}class CommandParser{parse(e){const t=e.trim().split(/\s+/g),r=t[0],s=new Map;for(let e=1;e<t.length;e++){const r=t[e];if(r.startsWith("-")){const i=r.slice(1);if(e+1>=t.length||t[e+1].startsWith("-"))s.set(i,!0);else{const r=t[e+1];isNaN(Number(r))?s.set(i,r):s.set(i,Number(r)),e++}}}return{commandName:r,options:s,args:t.slice(1)}}}class CommandSystem{invoker;parser=new CommandParser;registry=new CommandRegistry;register(e){this.registry.register(e.name,e,e.description)}create(e,t,r,s,i=""){return createCommand(e,t,r,s,i)}createFromCommandString(e,t){try{const r=this.parser.parse(e),s=this.registry.get(r.commandName);if(!s)return void console.warn(`命令 ${r.commandName} 未注册`);const i=s.receiver.creator?.(r.options,r.args,t||s.description,e);if(null==i)return;return i}catch(e){return void console.error(e)}}execute(e,t,r=!0){try{if("string"==typeof e){const s=this.createFromCommandString(e,t);s&&this.invoker.execute(s,r)}else this.invoker.execute(e,r)}catch(e){console.error(e)}}undo(){return this.invoker.undo()}redo(){return this.invoker.redo()}executeBatch(e,t=!0){this.invoker.executeBatch(e,t)}clearHistory(){this.invoker.clearHistory()}getHistory(){return this.invoker.getHistory()}getUndoHistory(){return this.invoker.getUndoHistory()}getHistoryCount(){return this.invoker.getHistoryCount()}getUndoHistoryCount(){return this.invoker.getUndoHistoryCount()}listCommands(){return this.registry.listCommands()}constructor(e){this.invoker=new CommandInvoker({maxHistoryDepth:e?.maxHistoryDepth,maxUndoHistoryDepth:e?.maxUndoHistoryDepth})}}class Domain{dic=container.createChildContainer();dicS=this.dic.createChildContainer();dicE=this.dic.createChildContainer();dicVO=this.dic.createChildContainer();e=new BaseEvents;c;registerService(e,t){const r=Array.isArray(e)?e:[e],s=t?Array.isArray(t)?t:[t]:[];for(let e=0;e<r.length;e+=1){const t=r[e],i=s[e];if(this.dicS.isRegistered(t.name)||this.dicS.isRegistered(t)){console.warn(`服务 ${t.name} 已注册,跳过注册`);continue}const o=new t;this.dicS.register(t.name,{useValue:o}),this.dicS.register(t,{useValue:o}),i?.beforeResolution&&(this.dicS.beforeResolution(t.name,i.beforeResolution,{frequency:i?.beforeResolutionFrequency||"Always"}),this.dicS.beforeResolution(t,i.beforeResolution,{frequency:i?.beforeResolutionFrequency||"Always"})),i?.afterResolution&&(this.dicS.afterResolution(t.name,i.afterResolution,{frequency:i?.afterResolutionFrequency||"Always"}),this.dicS.afterResolution(t,i?.afterResolution,{frequency:i?.afterResolutionFrequency||"Always"}))}}registerEntity(e,t){const r=Array.isArray(e)?e:[e],s=t?Array.isArray(t)?t:[t]:[];for(let e=0;e<r.length;e+=1){const t=r[e],i=s[e];if(this.dicE.isRegistered(t.name)||this.dicE.isRegistered(t))console.warn(`实体 ${t.name} 已注册,跳过注册`);else{if(i?.cachePolicy)switch(i.cachePolicy){case"cache":container.register(t.name,{useClass:t},{lifecycle:Lifecycle.Singleton}),container.register(t,{useClass:t},{lifecycle:Lifecycle.Singleton});break;case"containerCache":this.dicE.register(t.name,{useClass:t},{lifecycle:Lifecycle.ContainerScoped}),this.dicE.register(t,{useClass:t},{lifecycle:Lifecycle.ContainerScoped});break;case"resolution":this.dicE.register(t.name,{useClass:t},{lifecycle:Lifecycle.ResolutionScoped}),this.dicE.register(t,{useClass:t},{lifecycle:Lifecycle.ResolutionScoped})}else this.dicE.register(t.name,{useClass:t},{lifecycle:Lifecycle.Transient}),this.dicE.register(t,{useClass:t},{lifecycle:Lifecycle.Transient});i?.beforeResolution&&(this.dicE.beforeResolution(t.name,i.beforeResolution,{frequency:i?.beforeResolutionFrequency||"Always"}),this.dicE.beforeResolution(t,i.beforeResolution,{frequency:i?.beforeResolutionFrequency||"Always"})),i?.afterResolution&&(this.dicE.afterResolution(t.name,i.afterResolution,{frequency:i?.afterResolutionFrequency||"Always"}),this.dicE.afterResolution(t,i.afterResolution,{frequency:i?.afterResolutionFrequency||"Always"}))}}}registerVO(e){const t=Array.isArray(e)?e:[e];for(let e=0;e<t.length;e+=1){const r=t[e],s=Array.isArray(r.token)?r.token:[r.token];for(let e=0;e<s.length;e+=1){const t=s[e];switch(r.factoryType){case"cache":container.register(t,{useFactory:instanceCachingFactory(r.resolveFactory)});break;case"containerCache":this.dic.register(t,{useFactory:instancePerContainerCachingFactory(r.resolveFactory)}),this.dicVO.register(t,{useFactory:instancePerContainerCachingFactory(r.resolveFactory)});break;case"predicate":this.dic.register(t,{useFactory:predicateAwareClassFactory(r.resolveFactory,r.positiveTarget,r.negativeTarget)}),this.dicVO.register(t,{useFactory:predicateAwareClassFactory(r.resolveFactory,r.positiveTarget,r.negativeTarget)});break;default:this.dic.register(t,{useFactory:r.resolveFactory}),this.dicVO.register(t,{useFactory:r.resolveFactory})}}}}services(e){const t=this.dicS.resolve(e);return t&&!t?.e&&(t.e=this.e),t}entities(e,t){let r;if(t){const s=this.dicE.resolve(e);r=t.transform(s)}else r=this.dicE.resolve(e);return r&&!r?.e&&(r.e=this.e),r}vos(e,t){if(t){const r=this.dicVO.resolve(e);return t.transform(r)}return this.dicVO.resolve(e)}constructor(e){this.c=new CommandSystem(e)}}class DomainService{e=void 0;constructor(){}}class DomainEntity{e=void 0;constructor(){}}const SingletenDecorator=e=>(e.prototype.$ins=null,class extends e{constructor(...t){return super(...t),e.prototype.$ins||(e.prototype.$ins=this),e.prototype.$ins}});var __getOwnPropDesc=Object.getOwnPropertyDescriptor,__decorateClass=(e,t,r,s)=>{for(var i,o=s>1?void 0:s?__getOwnPropDesc(t,r):t,n=e.length-1;n>=0;n--)(i=e[n])&&(o=i(o)||o);return o};let GDMB=class{globalDIC=container;c;registerDomain(e,t){const r=Array.isArray(e)?e:[e],s=t?Array.isArray(t)?t:[t]:[];for(let e=0;e<r.length;e+=1){const t=r[e],i=s[e];if(this.globalDIC.isRegistered(t.name))return void console.warn(`领域 ${t.name} 已注册,无法重复注册`);const o=new t(...i?.args||[]);this.globalDIC.register(t.name,{useValue:o}),this.globalDIC.register(t,{useValue:o}),i?.beforeResolution&&(this.globalDIC.beforeResolution(t.name,i.beforeResolution,{frequency:i.beforeResolutionFrequency||"Always"}),this.globalDIC.beforeResolution(t,i.beforeResolution,{frequency:i.afterResolutionFrequency||"Always"})),i?.afterResolution&&(this.globalDIC.afterResolution(t.name,i.afterResolution,{frequency:i.afterResolutionFrequency||"Always"}),this.globalDIC.afterResolution(t,i.afterResolution,{frequency:i.afterResolutionFrequency||"Always"}))}}domains(e){return this.globalDIC.resolve(e)}transform(e,t){return t.transform(e)}transformAll(e,t){const r=[];for(let s=0;s<e.length;s+=1){const i=t.transform(e[s]);null!=i&&r.push(i)}return r}constructor(e){this.c=new CommandSystem({maxHistoryDepth:e?.maxHistoryDepth,maxUndoHistoryDepth:e?.maxUndoHistoryDepth})}};GDMB=__decorateClass([SingletenDecorator],GDMB);export{Adapter,CommandSystem,Domain,DomainEntity,DomainService,GDMB};
|
package/dist/gdmb.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("tsyringe")):"function"==typeof define&&define.amd?define(["exports","tsyringe"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).gdmb={},e.tsyringe)}(this,function(e,t){"use strict";class r{_mapper={};register(e){e in this._mapper||(this._mapper[e]=[])}remove(e){e in this._mapper&&delete this._mapper[e]}on(e,t){const r=this._mapper[e];r?r.push(t):(this.register(e),this.on(e,t))}off(e,t){const r=this._mapper[e];if(r){const e=r.indexOf(t);-1!==e&&r.splice(e,1)}}trigger(e,...t){const r=this._mapper[e];r&&setTimeout(()=>{for(const e of Object.values(r))e(...t)},0)}constructor(){}}class s{history=[];undoHistory=[];maxHistoryDepth;maxUndoHistoryDepth;execute(e,t=!0){e.execute(),t&&(this.history.push({command:e,timestamp:Date.now()}),this.undoHistory=[],this.history.length>this.maxHistoryDepth&&this.history.shift())}undo(){if(0===this.history.length)return!1;const e=this.history.pop();return!!e&&(e.command.undo(),this.undoHistory.push(e),this.undoHistory.length>this.maxUndoHistoryDepth&&this.undoHistory.shift(),!0)}redo(){if(0===this.undoHistory.length)return!1;const e=this.undoHistory.pop();return!!e&&(e.command.execute(),this.history.push(e),this.history.length>this.maxHistoryDepth&&this.history.shift(),!0)}executeBatch(e,t=!0){e.forEach(e=>this.execute(e,t))}clearHistory(){this.history=[],this.undoHistory=[]}getHistory(){return[...this.history]}getUndoHistory(){return[...this.undoHistory]}getHistoryCount(){return this.history.length}getUndoHistoryCount(){return this.undoHistory.length}constructor(e){this.maxHistoryDepth=e?.maxHistoryDepth||30,this.maxUndoHistoryDepth=e?.maxUndoHistoryDepth||30}}class i{receiver;executeFn;undoFn;description;commandString;constructor(e,t,r,s,i=""){this.receiver=e,this.executeFn=t,this.undoFn=r,this.description=s,this.commandString=i}execute(){this.executeFn(this.receiver)}undo(){this.undoFn(this.receiver)}getDescription(){return this.description}getCommandString(){return this.commandString}}class o{commands=new Map;register(e,t,r){this.commands.set(e,{receiver:t,description:r})}get(e){return this.commands.get(e)}listCommands(){return[...this.commands.entries()].map(([e,t])=>({name:e,description:t.description}))}}class n{parse(e){const t=e.trim().split(/\s+/g),r=t[0],s=new Map;for(let i=1;i<t.length;i++){const e=t[i];if(e.startsWith("-")){const r=e.slice(1);if(i+1>=t.length||t[i+1].startsWith("-"))s.set(r,!0);else{const e=t[i+1];isNaN(Number(e))?s.set(r,e):s.set(r,Number(e)),i++}}}return{commandName:r,options:s,args:t.slice(1)}}}class c{invoker;parser=new n;registry=new o;register(e){this.registry.register(e.name,e,e.description)}create(e,t,r,s,o=""){return function(e,t,r,s,o=""){return new i(e,t,r,s,o)}(e,t,r,s,o)}createFromCommandString(e,t){try{const r=this.parser.parse(e),s=this.registry.get(r.commandName);if(!s)return void console.warn(`命令 ${r.commandName} 未注册`);const i=s.receiver.creator?.(r.options,r.args,t||s.description,e);if(null==i)return;return i}catch(r){return void console.error(r)}}execute(e,t,r=!0){try{if("string"==typeof e){const s=this.createFromCommandString(e,t);s&&this.invoker.execute(s,r)}else this.invoker.execute(e,r)}catch(s){console.error(s)}}undo(){return this.invoker.undo()}redo(){return this.invoker.redo()}executeBatch(e,t=!0){this.invoker.executeBatch(e,t)}clearHistory(){this.invoker.clearHistory()}getHistory(){return this.invoker.getHistory()}getUndoHistory(){return this.invoker.getUndoHistory()}getHistoryCount(){return this.invoker.getHistoryCount()}getUndoHistoryCount(){return this.invoker.getUndoHistoryCount()}listCommands(){return this.registry.listCommands()}constructor(e){this.invoker=new s({maxHistoryDepth:e?.maxHistoryDepth,maxUndoHistoryDepth:e?.maxUndoHistoryDepth})}}var a=Object.getOwnPropertyDescriptor;e.GDMB=class{globalDIC=t.container;c;registerDomain(e,t){const r=Array.isArray(e)?e:[e],s=t?Array.isArray(t)?t:[t]:[];for(let i=0;i<r.length;i+=1){const e=r[i],t=s[i];if(this.globalDIC.isRegistered(e.name))return void console.warn(`领域 ${e.name} 已注册,无法重复注册`);const o=new e(...t?.args||[]);this.globalDIC.register(e.name,{useValue:o}),this.globalDIC.register(e,{useValue:o}),t?.beforeResolution&&(this.globalDIC.beforeResolution(e.name,t.beforeResolution,{frequency:t.beforeResolutionFrequency||"Always"}),this.globalDIC.beforeResolution(e,t.beforeResolution,{frequency:t.afterResolutionFrequency||"Always"})),t?.afterResolution&&(this.globalDIC.afterResolution(e.name,t.afterResolution,{frequency:t.afterResolutionFrequency||"Always"}),this.globalDIC.afterResolution(e,t.afterResolution,{frequency:t.afterResolutionFrequency||"Always"}))}}domains(e){return this.globalDIC.resolve(e)}transform(e,t){return t.transform(e)}transformAll(e,t){const r=[];for(let s=0;s<e.length;s+=1){const i=t.transform(e[s]);null!=i&&r.push(i)}return r}constructor(e){this.c=new c({maxHistoryDepth:e?.maxHistoryDepth,maxUndoHistoryDepth:e?.maxUndoHistoryDepth})}},e.GDMB=((e,t,r,s)=>{for(var i,o=s>1?void 0:s?a(t,r):t,n=e.length-1;n>=0;n--)(i=e[n])&&(o=i(o)||o);return o})([e=>(e.prototype.$ins=null,class extends e{constructor(...t){return super(...t),e.prototype.$ins||(e.prototype.$ins=this),e.prototype.$ins}})],e.GDMB),e.Adapter=class{},e.CommandSystem=c,e.Domain=class{dic=t.container.createChildContainer();dicS=this.dic.createChildContainer();dicE=this.dic.createChildContainer();dicVO=this.dic.createChildContainer();e=new r;c;registerService(e,t){const r=Array.isArray(e)?e:[e],s=t?Array.isArray(t)?t:[t]:[];for(let i=0;i<r.length;i+=1){const e=r[i],t=s[i];if(this.dicS.isRegistered(e.name)||this.dicS.isRegistered(e)){console.warn(`服务 ${e.name} 已注册,跳过注册`);continue}const o=new e;this.dicS.register(e.name,{useValue:o}),this.dicS.register(e,{useValue:o}),t?.beforeResolution&&(this.dicS.beforeResolution(e.name,t.beforeResolution,{frequency:t?.beforeResolutionFrequency||"Always"}),this.dicS.beforeResolution(e,t.beforeResolution,{frequency:t?.beforeResolutionFrequency||"Always"})),t?.afterResolution&&(this.dicS.afterResolution(e.name,t.afterResolution,{frequency:t?.afterResolutionFrequency||"Always"}),this.dicS.afterResolution(e,t?.afterResolution,{frequency:t?.afterResolutionFrequency||"Always"}))}}registerEntity(e,r){const s=Array.isArray(e)?e:[e],i=r?Array.isArray(r)?r:[r]:[];for(let o=0;o<s.length;o+=1){const e=s[o],r=i[o];if(this.dicE.isRegistered(e.name)||this.dicE.isRegistered(e))console.warn(`实体 ${e.name} 已注册,跳过注册`);else{if(r?.cachePolicy)switch(r.cachePolicy){case"cache":t.container.register(e.name,{useClass:e},{lifecycle:t.Lifecycle.Singleton}),t.container.register(e,{useClass:e},{lifecycle:t.Lifecycle.Singleton});break;case"containerCache":this.dicE.register(e.name,{useClass:e},{lifecycle:t.Lifecycle.ContainerScoped}),this.dicE.register(e,{useClass:e},{lifecycle:t.Lifecycle.ContainerScoped});break;case"resolution":this.dicE.register(e.name,{useClass:e},{lifecycle:t.Lifecycle.ResolutionScoped}),this.dicE.register(e,{useClass:e},{lifecycle:t.Lifecycle.ResolutionScoped})}else this.dicE.register(e.name,{useClass:e},{lifecycle:t.Lifecycle.Transient}),this.dicE.register(e,{useClass:e},{lifecycle:t.Lifecycle.Transient});r?.beforeResolution&&(this.dicE.beforeResolution(e.name,r.beforeResolution,{frequency:r?.beforeResolutionFrequency||"Always"}),this.dicE.beforeResolution(e,r.beforeResolution,{frequency:r?.beforeResolutionFrequency||"Always"})),r?.afterResolution&&(this.dicE.afterResolution(e.name,r.afterResolution,{frequency:r?.afterResolutionFrequency||"Always"}),this.dicE.afterResolution(e,r.afterResolution,{frequency:r?.afterResolutionFrequency||"Always"}))}}}registerVO(e){const r=Array.isArray(e)?e:[e];for(let s=0;s<r.length;s+=1){const e=r[s],i=Array.isArray(e.token)?e.token:[e.token];for(let r=0;r<i.length;r+=1){const s=i[r];switch(e.factoryType){case"cache":t.container.register(s,{useFactory:t.instanceCachingFactory(e.resolveFactory)});break;case"containerCache":this.dic.register(s,{useFactory:t.instancePerContainerCachingFactory(e.resolveFactory)}),this.dicVO.register(s,{useFactory:t.instancePerContainerCachingFactory(e.resolveFactory)});break;case"predicate":this.dic.register(s,{useFactory:t.predicateAwareClassFactory(e.resolveFactory,e.positiveTarget,e.negativeTarget)}),this.dicVO.register(s,{useFactory:t.predicateAwareClassFactory(e.resolveFactory,e.positiveTarget,e.negativeTarget)});break;default:this.dic.register(s,{useFactory:e.resolveFactory}),this.dicVO.register(s,{useFactory:e.resolveFactory})}}}}services(e){const t=this.dicS.resolve(e);return t&&!t?.e&&(t.e=this.e),t}entities(e,t){let r;if(t){const s=this.dicE.resolve(e);r=t.transform(s)}else r=this.dicE.resolve(e);return r&&!r?.e&&(r.e=this.e),r}vos(e,t){if(t){const r=this.dicVO.resolve(e);return t.transform(r)}return this.dicVO.resolve(e)}constructor(e){this.c=new c(e)}},e.DomainEntity=class{e=void 0;constructor(){}},e.DomainService=class{e=void 0;constructor(){}},Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -31,7 +31,7 @@ export interface ParsedCommand {
|
|
|
31
31
|
/**
|
|
32
32
|
* 命令创建函数类型
|
|
33
33
|
*/
|
|
34
|
-
export type CommandCreator = (options: Map<string, string | number | boolean>, args: string[], description: string, commandString: string) => Command;
|
|
34
|
+
export type CommandCreator = (options: Map<string, string | number | boolean>, args: string[], description: string, commandString: string) => Command | undefined | null;
|
|
35
35
|
/**
|
|
36
36
|
* 命令注册项
|
|
37
37
|
*/
|
|
@@ -15,6 +15,21 @@ export declare class Domain<E extends TEventEmits = TEventEmits> {
|
|
|
15
15
|
* @description 领域内部依赖注入容器,用于注册和解析领域服务、实体、值对象等。
|
|
16
16
|
*/
|
|
17
17
|
dic: import('tsyringe').DependencyContainer;
|
|
18
|
+
/**
|
|
19
|
+
* @summary 服务子容器
|
|
20
|
+
* @description 用于注册和解析领域服务
|
|
21
|
+
*/
|
|
22
|
+
dicS: import('tsyringe').DependencyContainer;
|
|
23
|
+
/**
|
|
24
|
+
* @summary 实体子容器
|
|
25
|
+
* @description 用于注册和解析领域实体
|
|
26
|
+
*/
|
|
27
|
+
dicE: import('tsyringe').DependencyContainer;
|
|
28
|
+
/**
|
|
29
|
+
* @summary 值对象子容器
|
|
30
|
+
* @description 用于注册和解析值对象
|
|
31
|
+
*/
|
|
32
|
+
dicVO: import('tsyringe').DependencyContainer;
|
|
18
33
|
/**
|
|
19
34
|
* @description 领域事件句柄,用于触发、监听、取消监听领域事件
|
|
20
35
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gdmb",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/gdmb.umd.js",
|
|
6
6
|
"module": "dist/gdmb.es.js",
|
|
@@ -9,10 +9,11 @@
|
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "tsc -b && vite build -c vitest.config.ts",
|
|
13
|
-
"build:watch": "tsc -b && vite build --watch -c vitest.config.ts",
|
|
12
|
+
"build": "tsc -b && vite build -c vitest.config.ts && npm run post-build",
|
|
13
|
+
"build:watch": "tsc -b && vite build --watch -c vitest.config.ts && npm run post-build",
|
|
14
14
|
"test": "vitest",
|
|
15
|
-
"test:coverage": "vitest --coverage"
|
|
15
|
+
"test:coverage": "vitest --coverage",
|
|
16
|
+
"post-build": "npx vite-node ./script/post-script.ts"
|
|
16
17
|
},
|
|
17
18
|
"keywords": [],
|
|
18
19
|
"author": "yzh",
|
|
@@ -23,6 +24,7 @@
|
|
|
23
24
|
"eslint": "^9.33.0",
|
|
24
25
|
"eslint-plugin-prettier": "^5.5.4",
|
|
25
26
|
"globals": "^16.3.0",
|
|
27
|
+
"terser": "^5.46.0",
|
|
26
28
|
"typescript": "^5.9.2",
|
|
27
29
|
"typescript-eslint": "^8.39.1",
|
|
28
30
|
"vite": "^7.1.2",
|