gdmb 1.3.1 → 1.3.3

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 CHANGED
@@ -1,5 +1,5 @@
1
- import { container as l, Lifecycle as a, predicateAwareClassFactory as f, instancePerContainerCachingFactory as d, instanceCachingFactory as g } from "tsyringe";
2
- class m {
1
+ import { container as l, Lifecycle as c, predicateAwareClassFactory as m, instancePerContainerCachingFactory as g, instanceCachingFactory as p } from "tsyringe";
2
+ class C {
3
3
  /**
4
4
  * 事件容器
5
5
  */
@@ -23,20 +23,20 @@ class m {
23
23
  * @param handler 处理器
24
24
  * @returns 监听句柄
25
25
  */
26
- on(e, s) {
27
- const r = this._mapper[e];
28
- r ? r.push(s) : (this.register(e), this.on(e, s));
26
+ on(e, t) {
27
+ const s = this._mapper[e];
28
+ s ? s.push(t) : (this.register(e), this.on(e, t));
29
29
  }
30
30
  /**
31
31
  * 移出指定监听函数监听
32
32
  * @param eventName 事件名称
33
33
  * @param handler 监听函数
34
34
  */
35
- off(e, s) {
36
- const r = this._mapper[e];
37
- if (r) {
38
- const i = r.indexOf(s);
39
- i !== -1 && r.splice(i, 1);
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
40
  }
41
41
  }
42
42
  /**
@@ -44,11 +44,11 @@ class m {
44
44
  * @param eventName 事件名称
45
45
  * @param args 时间回调参数
46
46
  */
47
- trigger(e, ...s) {
48
- const r = this._mapper[e];
49
- r && setTimeout(() => {
50
- for (const i of Object.values(r))
51
- i(...s);
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
52
  }, 0);
53
53
  }
54
54
  /**
@@ -57,231 +57,7 @@ class m {
57
57
  constructor() {
58
58
  }
59
59
  }
60
- class _ {
61
- }
62
- class w {
63
- /**
64
- * @summary 领域内部依赖注入容器
65
- * @description 领域内部依赖注入容器,用于注册和解析领域服务、实体、值对象等。
66
- */
67
- dic = l.createChildContainer();
68
- /**
69
- * @description 领域事件句柄,用于触发、监听、取消监听领域事件
70
- */
71
- e = new m();
72
- /**
73
- * @description 向领域依赖注入容器中注册一个或多个领域服务
74
- * @param service 单个领域服务类或数组
75
- * @param options 领域服务注册配置项,可传入领域服务构造参数以及配置依赖解决拦截器和频率等
76
- * @example
77
- * ```typescript
78
- * class ServiceOne extends DomainService {
79
- *
80
- * // before resolve 拦截器
81
- * public static beforeResolution(token: InjectionToken, resolutionType: string) {}
82
- *
83
- * // after resolve 拦截器
84
- * public static afterResolution(token: InjectionToken, result: ServiceOne | ServiceOne[], resolutionType: string) {}
85
- *
86
- * constructor(public arg: string, public arg2: number) {
87
- * super();
88
- * }
89
- * }
90
- *
91
- * // 注册领域服务
92
- * domain.registerService(ServiceOne, { beforeResolution: ServiceOne.beforeResolution, afterResolution: ServiceOne.afterResolution })
93
- * ```
94
- */
95
- registerService(e, s) {
96
- const r = Array.isArray(e) ? e : [e], i = s ? Array.isArray(s) ? s : [s] : [];
97
- for (let n = 0; n < r.length; n += 1) {
98
- const t = r[n], c = i[n];
99
- if (this.dic.isRegistered(t.name) || this.dic.isRegistered(t)) {
100
- console.warn(`服务 ${t.name} 已注册,跳过注册`);
101
- continue;
102
- }
103
- this.dic.register(t.name, { useClass: t }), this.dic.register(t, { useClass: t }), c?.beforeResolution && (this.dic.beforeResolution(t.name, c.beforeResolution, {
104
- frequency: c?.beforeResolutionFrequency || "Always"
105
- }), this.dic.beforeResolution(t, c.beforeResolution, {
106
- frequency: c?.beforeResolutionFrequency || "Always"
107
- })), c?.afterResolution && (this.dic.afterResolution(t.name, c.afterResolution, {
108
- frequency: c?.afterResolutionFrequency || "Always"
109
- }), this.dic.afterResolution(t, c?.afterResolution, {
110
- frequency: c?.afterResolutionFrequency || "Always"
111
- }));
112
- }
113
- }
114
- /**
115
- * @description 向领域依赖注入容器中注册领域实体,指定缓存策略为'cache'时全局单例。
116
- * @param entity 单个领域实体类或数组
117
- * @param options 领域实体注册配置项
118
- * @example
119
- * ```typescript
120
- * class EntityOne extends DomainEntity {
121
- *
122
- * // before resolve 拦截器
123
- * public static beforeResolution(token: InjectionToken, resolutionType: string) {}
124
- *
125
- * // after resolve 拦截器
126
- * public static afterResolution(token: InjectionToken, result: EntityOne | EntityOne[], resolutionType: string) {}
127
- *
128
- * constructor(public arg: string, public arg2: number) {
129
- * super();
130
- * }
131
- * }
132
- *
133
- * // 注册领域实体
134
- * domain.registerEntity(EntityOne, { cachePolicy: 'cache', beforeResolution: EntityOne.beforeResolution, afterResolution: EntityOne.afterResolution })
135
- * ```
136
- */
137
- registerEntity(e, s) {
138
- const r = Array.isArray(e) ? e : [e], i = s ? Array.isArray(s) ? s : [s] : [];
139
- for (let n = 0; n < r.length; n += 1) {
140
- const t = r[n], c = i[n];
141
- if (this.dic.isRegistered(t.name) || this.dic.isRegistered(t)) {
142
- console.warn(`实体 ${t.name} 已注册,跳过注册`);
143
- continue;
144
- }
145
- if (c?.cachePolicy)
146
- switch (c.cachePolicy) {
147
- case "cache":
148
- l.register(t.name, { useClass: t }, { lifecycle: a.Singleton }), l.register(t, { useClass: t }, { lifecycle: a.Singleton });
149
- break;
150
- case "containerCache":
151
- this.dic.register(t.name, { useClass: t }, { lifecycle: a.ContainerScoped }), this.dic.register(t, { useClass: t }, { lifecycle: a.ContainerScoped });
152
- break;
153
- case "resolution":
154
- this.dic.register(t.name, { useClass: t }, { lifecycle: a.ResolutionScoped }), this.dic.register(t, { useClass: t }, { lifecycle: a.ResolutionScoped });
155
- break;
156
- }
157
- else
158
- this.dic.register(t.name, { useClass: t }, { lifecycle: a.Transient }), this.dic.register(t, { useClass: t }, { lifecycle: a.Transient });
159
- c?.beforeResolution && (this.dic.beforeResolution(t.name, c.beforeResolution, {
160
- frequency: "Always"
161
- }), this.dic.beforeResolution(t, c.beforeResolution, {
162
- frequency: "Always"
163
- })), c?.afterResolution && (this.dic.afterResolution(t.name, c.afterResolution, {
164
- frequency: "Always"
165
- }), this.dic.afterResolution(t, c.afterResolution, {
166
- frequency: "Always"
167
- }));
168
- }
169
- }
170
- /**
171
- * @summary 项领域内依赖注入容器中注册值对象,当指定 factoryType 为'cache'时全局单例。
172
- * @description 需要泛型类型 V,约束工厂函数返回类型,V 满足 VO 类型约束,不允许包含函数属性
173
- * @param vos 值对象注册配置项
174
- * @example
175
- * ```typescript
176
- * domain.registerVO({
177
- * token: 'VO1',
178
- * factoryType: 'cache',
179
- * resolveFactory: () => ({ date: '2023-01-01' })
180
- * })
181
- * ```
182
- */
183
- registerVO(e) {
184
- const s = Array.isArray(e) ? e : [e];
185
- for (let r = 0; r < s.length; r += 1) {
186
- const i = s[r], n = Array.isArray(i.token) ? i.token : [i.token];
187
- for (let t = 0; t < n.length; t += 1) {
188
- const c = n[t];
189
- switch (i.factoryType) {
190
- case "cache":
191
- l.register(c, { useFactory: g(i.resolveFactory) });
192
- break;
193
- case "containerCache":
194
- this.dic.register(c, { useFactory: d(i.resolveFactory) });
195
- break;
196
- case "predicate":
197
- this.dic.register(c, {
198
- useFactory: f(
199
- i.resolveFactory,
200
- i.positiveTarget,
201
- i.negativeTarget
202
- )
203
- });
204
- break;
205
- default:
206
- this.dic.register(c, { useFactory: i.resolveFactory });
207
- break;
208
- }
209
- }
210
- }
211
- }
212
- /**
213
- * @description 从依赖注入容器中获取领域服务实例对象
214
- * @param token 领域服务token
215
- * @returns 领域服务实例对象
216
- */
217
- services(e) {
218
- const s = this.dic.resolve(e);
219
- return s && !s?.e && (s.e = this.e), s;
220
- }
221
- /**
222
- * @description 从依赖注入容器中获取领域实体实例对象
223
- * @param sourceToken 源领域实体token,存在适配器时是 DEF 泛型类型,否则为 DE 泛型类型
224
- * @param adapter 领域实体适配器,将 DEF 类型转换为 DE 类型
225
- * @returns 领域实体实例对象
226
- */
227
- entities(e, s) {
228
- let r;
229
- if (s) {
230
- const i = this.dic.resolve(e);
231
- r = s.transform(i);
232
- } else
233
- r = this.dic.resolve(e);
234
- return r && !r?.e && (r.e = this.e), r;
235
- }
236
- /**
237
- * @description 从依赖注入容器中获取领域值对象实例对象
238
- * @param sourceToken 源领域值对象token,存在适配器时是 VF 泛型类型,否则为 V 泛型类型
239
- * @param adapter 领域值对象适配器,将 VF 类型转换为 V 类型
240
- * @returns 领域值对象实例对象
241
- */
242
- vos(e, s) {
243
- if (s) {
244
- const r = this.dic.resolve(e);
245
- return s.transform(r);
246
- } else
247
- return this.dic.resolve(e);
248
- }
249
- /**
250
- * 域基类构造函数
251
- */
252
- constructor() {
253
- }
254
- }
255
- const y = (o) => (o.prototype.$ins = null, class extends o {
256
- constructor(...e) {
257
- return super(...e), o.prototype.$ins || (o.prototype.$ins = this), o.prototype.$ins;
258
- }
259
- });
260
- var p = Object.getOwnPropertyDescriptor, v = (o, e, s, r) => {
261
- for (var i = r > 1 ? void 0 : r ? p(e, s) : e, n = o.length - 1, t; n >= 0; n--)
262
- (t = o[n]) && (i = t(i) || i);
263
- return i;
264
- };
265
- let u = class {
266
- /** 事件句柄 */
267
- e = void 0;
268
- /**
269
- * 域基类构造函数
270
- */
271
- constructor() {
272
- }
273
- };
274
- u = v([
275
- y
276
- ], u);
277
- class F {
278
- /** 事件句柄 */
279
- e = void 0;
280
- /**
281
- * 域基类构造函数
282
- */
283
- constructor() {
284
- }
60
+ class q {
285
61
  }
286
62
  class R {
287
63
  /**
@@ -303,12 +79,13 @@ class R {
303
79
  /**
304
80
  * 执行命令
305
81
  * @param command 要执行的命令
82
+ * @param pushToHistory 是否将命令推入历史记录,默认为 true
306
83
  */
307
- execute(e) {
308
- e.execute(), this.history.push({
84
+ execute(e, t = !0) {
85
+ e.execute(), t && (this.history.push({
309
86
  command: e,
310
87
  timestamp: Date.now()
311
- }), this.undoHistory = [], this.history.length > this.maxHistoryDepth && this.history.shift();
88
+ }), this.undoHistory = [], this.history.length > this.maxHistoryDepth && this.history.shift());
312
89
  }
313
90
  /**
314
91
  * 撤销上一个命令
@@ -333,9 +110,10 @@ class R {
333
110
  /**
334
111
  * 批量执行命令
335
112
  * @param commands 要执行的命令数组
113
+ * @param pushToHistory 是否将命令推入历史记录,默认为 true
336
114
  */
337
- executeBatch(e) {
338
- e.forEach((s) => this.execute(s));
115
+ executeBatch(e, t = !0) {
116
+ e.forEach((s) => this.execute(s, t));
339
117
  }
340
118
  /**
341
119
  * 清空命令历史
@@ -376,10 +154,10 @@ class R {
376
154
  * @param option 命令调用者构造配置参数
377
155
  */
378
156
  constructor(e) {
379
- this.maxHistoryDepth = e?.maxHistoryDepth || 1 / 0, this.maxUndoHistoryDepth = e?.maxUndoHistoryDepth || 1 / 0;
157
+ this.maxHistoryDepth = e?.maxHistoryDepth || 30, this.maxUndoHistoryDepth = e?.maxUndoHistoryDepth || 30;
380
158
  }
381
159
  }
382
- class C {
160
+ class v {
383
161
  /**
384
162
  * 命令接收者
385
163
  */
@@ -396,15 +174,20 @@ class C {
396
174
  * 命令描述
397
175
  */
398
176
  description;
177
+ /**
178
+ * 原始命令字符串
179
+ */
180
+ commandString;
399
181
  /**
400
182
  * 构造函数
401
183
  * @param receiver 命令接收者
402
184
  * @param executeFn 执行操作的函数
403
185
  * @param undoFn 撤销操作的函数
404
186
  * @param description 命令描述
187
+ * @param commandString 原始命令字符串
405
188
  */
406
- constructor(e, s, r, i) {
407
- this.receiver = e, this.executeFn = s, this.undoFn = r, this.description = i;
189
+ constructor(e, t, s, i, o = "") {
190
+ this.receiver = e, this.executeFn = t, this.undoFn = s, this.description = i, this.commandString = o;
408
191
  }
409
192
  /**
410
193
  * 执行命令
@@ -425,32 +208,137 @@ class C {
425
208
  getDescription() {
426
209
  return this.description;
427
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);
428
221
  }
429
- function H(o, e, s, r) {
430
- return new C(o, e, s, r);
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
+ }
431
250
  }
432
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 {
433
279
  /**
434
280
  * 命令调用者
435
281
  */
436
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
+ }
437
299
  /**
438
300
  * 创建命令
439
301
  * @param receiver 命令接收者
440
302
  * @param executeFn 执行操作的函数
441
303
  * @param undoFn 撤销操作的函数
442
304
  * @param description 命令描述
305
+ * @param commandString 原始命令字符串
443
306
  * @returns 命令对象
444
307
  */
445
- create(e, s, r, i) {
446
- return H(e, s, r, i);
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
+ );
447
329
  }
448
330
  /**
449
331
  * 执行命令
450
332
  * @param command 要执行的命令
333
+ * @param description 针对本次命令描述
334
+ * @param pushToHistory 是否将命令推入历史记录,默认为 true
451
335
  */
452
- execute(e) {
453
- this.invoker.execute(e);
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);
454
342
  }
455
343
  /**
456
344
  * 撤销上一个命令
@@ -469,9 +357,10 @@ class D {
469
357
  /**
470
358
  * 批量执行命令
471
359
  * @param commands 要执行的命令数组
360
+ * @param pushToHistory 是否将命令推入历史记录,默认为 true
472
361
  */
473
- executeBatch(e) {
474
- this.invoker.executeBatch(e);
362
+ executeBatch(e, t = !0) {
363
+ this.invoker.executeBatch(e, t);
475
364
  }
476
365
  /**
477
366
  * 清空命令历史
@@ -507,6 +396,12 @@ class D {
507
396
  getUndoHistoryCount() {
508
397
  return this.invoker.getUndoHistoryCount();
509
398
  }
399
+ /**
400
+ * 列出所有可用命令
401
+ */
402
+ listCommands() {
403
+ return this.registry.listCommands();
404
+ }
510
405
  /**
511
406
  * 构造函数
512
407
  * @param option 配置参数
@@ -518,12 +413,241 @@ class D {
518
413
  });
519
414
  }
520
415
  }
521
- var b = Object.getOwnPropertyDescriptor, x = (o, e, s, r) => {
522
- for (var i = r > 1 ? void 0 : r ? b(e, s) : e, n = o.length - 1, t; n >= 0; n--)
523
- (t = o[n]) && (i = t(i) || i);
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);
524
622
  return i;
525
623
  };
526
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 {
527
651
  /** 全局 DIC */
528
652
  globalDIC = l;
529
653
  /** 命令系统 */
@@ -531,71 +655,68 @@ let h = class {
531
655
  /**
532
656
  * 注册领域对象
533
657
  */
534
- registerDomain(o, e) {
535
- const s = Array.isArray(o) ? o : [o], r = e ? Array.isArray(e) ? e : [e] : [];
536
- for (let i = 0; i < s.length; i += 1) {
537
- const n = s[i], t = r[i];
538
- if (this.globalDIC.isRegistered(n.name)) {
539
- console.warn(`领域 ${n.name} 已注册,无法重复注册`);
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} 已注册,无法重复注册`);
540
664
  return;
541
665
  }
542
- const c = new n(...t?.args || []);
543
- this.globalDIC.register(n.name, { useValue: c }), this.globalDIC.register(n, { useValue: c }), t?.beforeResolution && (this.globalDIC.beforeResolution(n.name, t.beforeResolution, {
544
- frequency: t.beforeResolutionFrequency || "Always"
545
- }), this.globalDIC.beforeResolution(n, t.beforeResolution, {
546
- frequency: t.afterResolutionFrequency || "Always"
547
- })), t?.afterResolution && (this.globalDIC.afterResolution(n.name, t.afterResolution, {
548
- frequency: t.afterResolutionFrequency || "Always"
549
- }), this.globalDIC.afterResolution(n, t.afterResolution, {
550
- frequency: t.afterResolutionFrequency || "Always"
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"
551
675
  }));
552
676
  }
553
677
  }
554
678
  /**
555
679
  * 获取领域对象
556
680
  */
557
- domains(o) {
558
- return this.globalDIC.resolve(o);
681
+ domains(n) {
682
+ return this.globalDIC.resolve(n);
559
683
  }
560
684
  /**
561
685
  * 提供全局的转换方法
562
686
  */
563
- transform(o, e) {
564
- return e.transform(o);
687
+ transform(n, e) {
688
+ return e.transform(n);
565
689
  }
566
690
  /**
567
691
  * 提供全局的转换方法,支持批量转换
568
692
  */
569
- transformAll(o, e) {
570
- const s = [];
571
- for (let r = 0; r < o.length; r += 1) {
572
- const i = e.transform(o[r]);
573
- i != null && s.push(i);
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);
574
698
  }
575
- return s;
699
+ return t;
576
700
  }
577
701
  /**
578
702
  * 构造函数
579
703
  * @param option 配置参数
580
704
  */
581
- constructor(o) {
582
- this.c = new D({
583
- maxHistoryDepth: o?.maxHistoryDepth,
584
- maxUndoHistoryDepth: o?.maxUndoHistoryDepth
705
+ constructor(n) {
706
+ this.c = new f({
707
+ maxHistoryDepth: n?.maxHistoryDepth,
708
+ maxUndoHistoryDepth: n?.maxUndoHistoryDepth
585
709
  });
586
710
  }
587
711
  };
588
- h = x([
589
- y
590
- ], h);
712
+ y = F([
713
+ d
714
+ ], y);
591
715
  export {
592
- _ as Adapter,
593
- R as CommandInvoker,
594
- D as CommandSystem,
595
- C as ConcreteCommand,
596
- w as Domain,
597
- F as DomainEntity,
598
- u as DomainService,
599
- h as GDMB,
600
- H as createCommand
716
+ q as Adapter,
717
+ f as CommandSystem,
718
+ k as Domain,
719
+ I as DomainEntity,
720
+ h as DomainService,
721
+ y as GDMB
601
722
  };
package/dist/gdmb.umd.js CHANGED
@@ -1 +1 @@
1
- (function(l,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("tsyringe")):typeof define=="function"&&define.amd?define(["exports","tsyringe"],a):(l=typeof globalThis<"u"?globalThis:l||self,a(l.gdmb={},l.tsyringe))})(this,(function(l,a){"use strict";class g{_mapper={};register(e){e in this._mapper||(this._mapper[e]=[])}remove(e){e in this._mapper&&delete this._mapper[e]}on(e,s){const i=this._mapper[e];i?i.push(s):(this.register(e),this.on(e,s))}off(e,s){const i=this._mapper[e];if(i){const r=i.indexOf(s);r!==-1&&i.splice(r,1)}}trigger(e,...s){const i=this._mapper[e];i&&setTimeout(()=>{for(const r of Object.values(i))r(...s)},0)}constructor(){}}class p{}class v{dic=a.container.createChildContainer();e=new g;registerService(e,s){const i=Array.isArray(e)?e:[e],r=s?Array.isArray(s)?s:[s]:[];for(let n=0;n<i.length;n+=1){const t=i[n],o=r[n];if(this.dic.isRegistered(t.name)||this.dic.isRegistered(t)){console.warn(`服务 ${t.name} 已注册,跳过注册`);continue}this.dic.register(t.name,{useClass:t}),this.dic.register(t,{useClass:t}),o?.beforeResolution&&(this.dic.beforeResolution(t.name,o.beforeResolution,{frequency:o?.beforeResolutionFrequency||"Always"}),this.dic.beforeResolution(t,o.beforeResolution,{frequency:o?.beforeResolutionFrequency||"Always"})),o?.afterResolution&&(this.dic.afterResolution(t.name,o.afterResolution,{frequency:o?.afterResolutionFrequency||"Always"}),this.dic.afterResolution(t,o?.afterResolution,{frequency:o?.afterResolutionFrequency||"Always"}))}}registerEntity(e,s){const i=Array.isArray(e)?e:[e],r=s?Array.isArray(s)?s:[s]:[];for(let n=0;n<i.length;n+=1){const t=i[n],o=r[n];if(this.dic.isRegistered(t.name)||this.dic.isRegistered(t)){console.warn(`实体 ${t.name} 已注册,跳过注册`);continue}if(o?.cachePolicy)switch(o.cachePolicy){case"cache":a.container.register(t.name,{useClass:t},{lifecycle:a.Lifecycle.Singleton}),a.container.register(t,{useClass:t},{lifecycle:a.Lifecycle.Singleton});break;case"containerCache":this.dic.register(t.name,{useClass:t},{lifecycle:a.Lifecycle.ContainerScoped}),this.dic.register(t,{useClass:t},{lifecycle:a.Lifecycle.ContainerScoped});break;case"resolution":this.dic.register(t.name,{useClass:t},{lifecycle:a.Lifecycle.ResolutionScoped}),this.dic.register(t,{useClass:t},{lifecycle:a.Lifecycle.ResolutionScoped});break}else this.dic.register(t.name,{useClass:t},{lifecycle:a.Lifecycle.Transient}),this.dic.register(t,{useClass:t},{lifecycle:a.Lifecycle.Transient});o?.beforeResolution&&(this.dic.beforeResolution(t.name,o.beforeResolution,{frequency:"Always"}),this.dic.beforeResolution(t,o.beforeResolution,{frequency:"Always"})),o?.afterResolution&&(this.dic.afterResolution(t.name,o.afterResolution,{frequency:"Always"}),this.dic.afterResolution(t,o.afterResolution,{frequency:"Always"}))}}registerVO(e){const s=Array.isArray(e)?e:[e];for(let i=0;i<s.length;i+=1){const r=s[i],n=Array.isArray(r.token)?r.token:[r.token];for(let t=0;t<n.length;t+=1){const o=n[t];switch(r.factoryType){case"cache":a.container.register(o,{useFactory:a.instanceCachingFactory(r.resolveFactory)});break;case"containerCache":this.dic.register(o,{useFactory:a.instancePerContainerCachingFactory(r.resolveFactory)});break;case"predicate":this.dic.register(o,{useFactory:a.predicateAwareClassFactory(r.resolveFactory,r.positiveTarget,r.negativeTarget)});break;default:this.dic.register(o,{useFactory:r.resolveFactory});break}}}}services(e){const s=this.dic.resolve(e);return s&&!s?.e&&(s.e=this.e),s}entities(e,s){let i;if(s){const r=this.dic.resolve(e);i=s.transform(r)}else i=this.dic.resolve(e);return i&&!i?.e&&(i.e=this.e),i}vos(e,s){if(s){const i=this.dic.resolve(e);return s.transform(i)}else return this.dic.resolve(e)}constructor(){}}const u=c=>(c.prototype.$ins=null,class extends c{constructor(...e){return super(...e),c.prototype.$ins||(c.prototype.$ins=this),c.prototype.$ins}});var C=Object.getOwnPropertyDescriptor,D=(c,e,s,i)=>{for(var r=i>1?void 0:i?C(e,s):e,n=c.length-1,t;n>=0;n--)(t=c[n])&&(r=t(r)||r);return r};l.DomainService=class{e=void 0;constructor(){}},l.DomainService=D([u],l.DomainService);class R{e=void 0;constructor(){}}class h{history=[];undoHistory=[];maxHistoryDepth;maxUndoHistoryDepth;execute(e){e.execute(),this.history.push({command:e,timestamp:Date.now()}),this.undoHistory=[],this.history.length>this.maxHistoryDepth&&this.history.shift()}undo(){if(this.history.length===0)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):!1}redo(){if(this.undoHistory.length===0)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):!1}executeBatch(e){e.forEach(s=>this.execute(s))}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||1/0,this.maxUndoHistoryDepth=e?.maxUndoHistoryDepth||1/0}}class f{receiver;executeFn;undoFn;description;constructor(e,s,i,r){this.receiver=e,this.executeFn=s,this.undoFn=i,this.description=r}execute(){this.executeFn(this.receiver)}undo(){this.undoFn(this.receiver)}getDescription(){return this.description}}function y(c,e,s,i){return new f(c,e,s,i)}class d{invoker;create(e,s,i,r){return y(e,s,i,r)}execute(e){this.invoker.execute(e)}undo(){return this.invoker.undo()}redo(){return this.invoker.redo()}executeBatch(e){this.invoker.executeBatch(e)}clearHistory(){this.invoker.clearHistory()}getHistory(){return this.invoker.getHistory()}getUndoHistory(){return this.invoker.getUndoHistory()}getHistoryCount(){return this.invoker.getHistoryCount()}getUndoHistoryCount(){return this.invoker.getUndoHistoryCount()}constructor(e){this.invoker=new h({maxHistoryDepth:e?.maxHistoryDepth,maxUndoHistoryDepth:e?.maxUndoHistoryDepth})}}var H=Object.getOwnPropertyDescriptor,b=(c,e,s,i)=>{for(var r=i>1?void 0:i?H(e,s):e,n=c.length-1,t;n>=0;n--)(t=c[n])&&(r=t(r)||r);return r};l.GDMB=class{globalDIC=a.container;c;registerDomain(e,s){const i=Array.isArray(e)?e:[e],r=s?Array.isArray(s)?s:[s]:[];for(let n=0;n<i.length;n+=1){const t=i[n],o=r[n];if(this.globalDIC.isRegistered(t.name)){console.warn(`领域 ${t.name} 已注册,无法重复注册`);return}const m=new t(...o?.args||[]);this.globalDIC.register(t.name,{useValue:m}),this.globalDIC.register(t,{useValue:m}),o?.beforeResolution&&(this.globalDIC.beforeResolution(t.name,o.beforeResolution,{frequency:o.beforeResolutionFrequency||"Always"}),this.globalDIC.beforeResolution(t,o.beforeResolution,{frequency:o.afterResolutionFrequency||"Always"})),o?.afterResolution&&(this.globalDIC.afterResolution(t.name,o.afterResolution,{frequency:o.afterResolutionFrequency||"Always"}),this.globalDIC.afterResolution(t,o.afterResolution,{frequency:o.afterResolutionFrequency||"Always"}))}}domains(e){return this.globalDIC.resolve(e)}transform(e,s){return s.transform(e)}transformAll(e,s){const i=[];for(let r=0;r<e.length;r+=1){const n=s.transform(e[r]);n!=null&&i.push(n)}return i}constructor(e){this.c=new d({maxHistoryDepth:e?.maxHistoryDepth,maxUndoHistoryDepth:e?.maxUndoHistoryDepth})}},l.GDMB=b([u],l.GDMB),l.Adapter=p,l.CommandInvoker=h,l.CommandSystem=d,l.ConcreteCommand=f,l.Domain=v,l.DomainEntity=R,l.createCommand=y,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(l,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("tsyringe")):typeof define=="function"&&define.amd?define(["exports","tsyringe"],a):(l=typeof globalThis<"u"?globalThis:l||self,a(l.gdmb={},l.tsyringe))})(this,(function(l,a){"use strict";class y{_mapper={};register(e){e in this._mapper||(this._mapper[e]=[])}remove(e){e in this._mapper&&delete this._mapper[e]}on(e,t){const s=this._mapper[e];s?s.push(t):(this.register(e),this.on(e,t))}off(e,t){const s=this._mapper[e];if(s){const i=s.indexOf(t);i!==-1&&s.splice(i,1)}}trigger(e,...t){const s=this._mapper[e];s&&setTimeout(()=>{for(const i of Object.values(s))i(...t)},0)}constructor(){}}class d{}class m{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(this.history.length===0)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):!1}redo(){if(this.undoHistory.length===0)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):!1}executeBatch(e,t=!0){e.forEach(s=>this.execute(s,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 g{receiver;executeFn;undoFn;description;commandString;constructor(e,t,s,i,n=""){this.receiver=e,this.executeFn=t,this.undoFn=s,this.description=i,this.commandString=n}execute(){this.executeFn(this.receiver)}undo(){this.undoFn(this.receiver)}getDescription(){return this.description}getCommandString(){return this.commandString}}function p(c,e,t,s,i=""){return new g(c,e,t,s,i)}class C{commands=new Map;register(e,t,s){this.commands.set(e,{receiver:t,description:s})}get(e){return this.commands.get(e)}listCommands(){return[...this.commands.entries()].map(([e,t])=>({name:e,description:t.description}))}}class v{parse(e){const t=e.trim().split(/\s+/g),s=t[0],i=new Map;for(let n=1;n<t.length;n++){const r=t[n];if(r.startsWith("-")){const o=r.slice(1);if(n+1>=t.length||t[n+1].startsWith("-"))i.set(o,!0);else{const u=t[n+1];isNaN(Number(u))?i.set(o,u):i.set(o,Number(u)),n++}}}return{commandName:s,options:i,args:t.slice(1)}}}class h{invoker;parser=new v;registry=new C;register(e){this.registry.register(e.name,e,e.description)}create(e,t,s,i,n=""){return p(e,t,s,i,n)}createFromCommandString(e,t){const s=this.parser.parse(e),i=this.registry.get(s.commandName);if(!i){console.warn(`命令 ${s.commandName} 未注册`);return}return i.receiver.creator?.(s.options,s.args,t||i.description,e)}execute(e,t,s=!0){if(typeof e=="string"){const i=this.createFromCommandString(e,t);i&&this.invoker.execute(i,s)}else this.invoker.execute(e,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 m({maxHistoryDepth:e?.maxHistoryDepth,maxUndoHistoryDepth:e?.maxUndoHistoryDepth})}}class R{dic=a.container.createChildContainer();e=new y;c;registerService(e,t){const s=Array.isArray(e)?e:[e],i=t?Array.isArray(t)?t:[t]:[];for(let n=0;n<s.length;n+=1){const r=s[n],o=i[n];if(this.dic.isRegistered(r.name)||this.dic.isRegistered(r)){console.warn(`服务 ${r.name} 已注册,跳过注册`);continue}this.dic.register(r.name,{useClass:r}),this.dic.register(r,{useClass:r}),o?.beforeResolution&&(this.dic.beforeResolution(r.name,o.beforeResolution,{frequency:o?.beforeResolutionFrequency||"Always"}),this.dic.beforeResolution(r,o.beforeResolution,{frequency:o?.beforeResolutionFrequency||"Always"})),o?.afterResolution&&(this.dic.afterResolution(r.name,o.afterResolution,{frequency:o?.afterResolutionFrequency||"Always"}),this.dic.afterResolution(r,o?.afterResolution,{frequency:o?.afterResolutionFrequency||"Always"}))}}registerEntity(e,t){const s=Array.isArray(e)?e:[e],i=t?Array.isArray(t)?t:[t]:[];for(let n=0;n<s.length;n+=1){const r=s[n],o=i[n];if(this.dic.isRegistered(r.name)||this.dic.isRegistered(r)){console.warn(`实体 ${r.name} 已注册,跳过注册`);continue}if(o?.cachePolicy)switch(o.cachePolicy){case"cache":a.container.register(r.name,{useClass:r},{lifecycle:a.Lifecycle.Singleton}),a.container.register(r,{useClass:r},{lifecycle:a.Lifecycle.Singleton});break;case"containerCache":this.dic.register(r.name,{useClass:r},{lifecycle:a.Lifecycle.ContainerScoped}),this.dic.register(r,{useClass:r},{lifecycle:a.Lifecycle.ContainerScoped});break;case"resolution":this.dic.register(r.name,{useClass:r},{lifecycle:a.Lifecycle.ResolutionScoped}),this.dic.register(r,{useClass:r},{lifecycle:a.Lifecycle.ResolutionScoped});break}else this.dic.register(r.name,{useClass:r},{lifecycle:a.Lifecycle.Transient}),this.dic.register(r,{useClass:r},{lifecycle:a.Lifecycle.Transient});o?.beforeResolution&&(this.dic.beforeResolution(r.name,o.beforeResolution,{frequency:o?.beforeResolutionFrequency||"Always"}),this.dic.beforeResolution(r,o.beforeResolution,{frequency:o?.beforeResolutionFrequency||"Always"})),o?.afterResolution&&(this.dic.afterResolution(r.name,o.afterResolution,{frequency:o?.afterResolutionFrequency||"Always"}),this.dic.afterResolution(r,o.afterResolution,{frequency:o?.afterResolutionFrequency||"Always"}))}}registerVO(e){const t=Array.isArray(e)?e:[e];for(let s=0;s<t.length;s+=1){const i=t[s],n=Array.isArray(i.token)?i.token:[i.token];for(let r=0;r<n.length;r+=1){const o=n[r];switch(i.factoryType){case"cache":a.container.register(o,{useFactory:a.instanceCachingFactory(i.resolveFactory)});break;case"containerCache":this.dic.register(o,{useFactory:a.instancePerContainerCachingFactory(i.resolveFactory)});break;case"predicate":this.dic.register(o,{useFactory:a.predicateAwareClassFactory(i.resolveFactory,i.positiveTarget,i.negativeTarget)});break;default:this.dic.register(o,{useFactory:i.resolveFactory});break}}}}services(e){const t=this.dic.resolve(e);return t&&!t?.e&&(t.e=this.e),t}entities(e,t){let s;if(t){const i=this.dic.resolve(e);s=t.transform(i)}else s=this.dic.resolve(e);return s&&!s?.e&&(s.e=this.e),s}vos(e,t){if(t){const s=this.dic.resolve(e);return t.transform(s)}else return this.dic.resolve(e)}constructor(e){this.c=new h(e)}}const f=c=>(c.prototype.$ins=null,class extends c{constructor(...e){return super(...e),c.prototype.$ins||(c.prototype.$ins=this),c.prototype.$ins}});var D=Object.getOwnPropertyDescriptor,b=(c,e,t,s)=>{for(var i=s>1?void 0:s?D(e,t):e,n=c.length-1,r;n>=0;n--)(r=c[n])&&(i=r(i)||i);return i};l.DomainService=class{e=void 0;constructor(){}},l.DomainService=b([f],l.DomainService);class H{e=void 0;constructor(){}}var w=Object.getOwnPropertyDescriptor,A=(c,e,t,s)=>{for(var i=s>1?void 0:s?w(e,t):e,n=c.length-1,r;n>=0;n--)(r=c[n])&&(i=r(i)||i);return i};l.GDMB=class{globalDIC=a.container;c;registerDomain(e,t){const s=Array.isArray(e)?e:[e],i=t?Array.isArray(t)?t:[t]:[];for(let n=0;n<s.length;n+=1){const r=s[n],o=i[n];if(this.globalDIC.isRegistered(r.name)){console.warn(`领域 ${r.name} 已注册,无法重复注册`);return}const u=new r(...o?.args||[]);this.globalDIC.register(r.name,{useValue:u}),this.globalDIC.register(r,{useValue:u}),o?.beforeResolution&&(this.globalDIC.beforeResolution(r.name,o.beforeResolution,{frequency:o.beforeResolutionFrequency||"Always"}),this.globalDIC.beforeResolution(r,o.beforeResolution,{frequency:o.afterResolutionFrequency||"Always"})),o?.afterResolution&&(this.globalDIC.afterResolution(r.name,o.afterResolution,{frequency:o.afterResolutionFrequency||"Always"}),this.globalDIC.afterResolution(r,o.afterResolution,{frequency:o.afterResolutionFrequency||"Always"}))}}domains(e){return this.globalDIC.resolve(e)}transform(e,t){return t.transform(e)}transformAll(e,t){const s=[];for(let i=0;i<e.length;i+=1){const n=t.transform(e[i]);n!=null&&s.push(n)}return s}constructor(e){this.c=new h({maxHistoryDepth:e?.maxHistoryDepth,maxUndoHistoryDepth:e?.maxUndoHistoryDepth})}},l.GDMB=A([f],l.GDMB),l.Adapter=d,l.CommandSystem=h,l.Domain=R,l.DomainEntity=H,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
@@ -155,11 +155,35 @@ const voOne = domainObject.vos<IVOOne>('IVOOne'); // { text: 'text', num: 1 }
155
155
  ### 命令系统(Command System)
156
156
  ```typescript
157
157
  import { GDMB } from 'gdmb';
158
- import type { Command, CommandReceiver } from 'gdmb';
158
+ import type { Command, CommandReceiver, CommandCreator } from 'gdmb';
159
159
  // 实例化全局对象
160
160
  const gdmb = new GDMB();
161
161
  // 定义命令接受者作为命令执行者
162
162
  class CounterReceiver implements CounterReceiver {
163
+ // 命令名称,用于命令注册和执行(必要)
164
+ name: 'counter';
165
+ // 命令描述,说明命令作用和支持的参数
166
+ description: `
167
+ 计算器命令,支持参数:
168
+ -type increment | decrement
169
+ -value number
170
+ `;
171
+ // 命令创建器,返回一个Command对象(必要)
172
+ creator: CommandCreator = (
173
+ options: Map<string, string | number | boolean>,
174
+ args: string[],
175
+ description: string,
176
+ commandString: string
177
+ ) => {
178
+ const value = (options.get('value') as number) || 0;
179
+ return gdmb.c.create(
180
+ this,
181
+ (r) => r.executeOperation(value),
182
+ (r) => r.undoOperation(),
183
+ description,
184
+ commandString
185
+ );
186
+ };
163
187
  // 执行函数(必要)
164
188
  executeOperation({ type, value = 1 }: { type: string; value?: number }): void {
165
189
  // 执行函数实现
@@ -178,6 +202,10 @@ const command = gdmb.c.create(
178
202
  );
179
203
  // 执行命令
180
204
  gdmb.c.execute(command);
205
+ // 撤销命令
206
+ gdmb.c.undo(command);
207
+ // 执行字符串命令
208
+ gdmb.c.execute('counter -type increment -value 5');
181
209
  ```
182
210
 
183
211
  ## 代码检查清单(Code Review Checklist)
@@ -15,6 +15,29 @@ export interface Command {
15
15
  * 获取命令描述
16
16
  */
17
17
  getDescription(): string;
18
+ /**
19
+ * 获取原始命令字符串
20
+ */
21
+ getCommandString(): string;
22
+ }
23
+ /**
24
+ * 解析后的命令参数
25
+ */
26
+ export interface ParsedCommand {
27
+ commandName: string;
28
+ options: Map<string, string | number | boolean>;
29
+ args: string[];
30
+ }
31
+ /**
32
+ * 命令创建函数类型
33
+ */
34
+ export type CommandCreator = (options: Map<string, string | number | boolean>, args: string[], description: string, commandString: string) => Command;
35
+ /**
36
+ * 命令注册项
37
+ */
38
+ export interface CommandRegistration<R extends CommandReceiver> {
39
+ receiver: R;
40
+ description: string;
18
41
  }
19
42
  /**
20
43
  * 命令历史记录项
@@ -28,6 +51,12 @@ export interface CommandHistoryItem {
28
51
  * 定义命令接收者的标准接口
29
52
  */
30
53
  export interface CommandReceiver {
54
+ /** 命名名称 */
55
+ name: string;
56
+ /** 命令描述 */
57
+ description: string;
58
+ /** 创建器 */
59
+ creator?: CommandCreator;
31
60
  /**
32
61
  * 执行命令操作
33
62
  * @param args 命令参数
@@ -19,8 +19,9 @@ export declare class CommandInvoker {
19
19
  /**
20
20
  * 执行命令
21
21
  * @param command 要执行的命令
22
+ * @param pushToHistory 是否将命令推入历史记录,默认为 true
22
23
  */
23
- execute(command: Command): void;
24
+ execute(command: Command, pushToHistory?: boolean): void;
24
25
  /**
25
26
  * 撤销上一个命令
26
27
  * @returns 是否成功撤销
@@ -34,8 +35,9 @@ export declare class CommandInvoker {
34
35
  /**
35
36
  * 批量执行命令
36
37
  * @param commands 要执行的命令数组
38
+ * @param pushToHistory 是否将命令推入历史记录,默认为 true
37
39
  */
38
- executeBatch(commands: Command[]): void;
40
+ executeBatch(commands: Command[], pushToHistory?: boolean): void;
39
41
  /**
40
42
  * 清空命令历史
41
43
  */
@@ -0,0 +1,12 @@
1
+ import { ParsedCommand } from './Command';
2
+ /**
3
+ * 命令解析器
4
+ */
5
+ export declare class CommandParser {
6
+ /**
7
+ * 解析命令字符串
8
+ * @param commandString 命令字符串,如 "add -a 10 -b 5"
9
+ * @returns ParsedCommand 对象
10
+ */
11
+ parse(commandString: string): ParsedCommand;
12
+ }
@@ -0,0 +1,27 @@
1
+ import { CommandRegistration, CommandReceiver } from './Command';
2
+ /**
3
+ * 命令注册器
4
+ */
5
+ export declare class CommandRegistry<R extends CommandReceiver> {
6
+ /** 命令注册映射 */
7
+ private commands;
8
+ /**
9
+ * 注册命令
10
+ * @param name 命令名
11
+ * @param receiver 命令接收者
12
+ * @param creator 命令创建函数
13
+ * @param description 命令描述
14
+ */
15
+ register(name: string, receiver: R, description: string): void;
16
+ /**
17
+ * 获取命令注册信息
18
+ */
19
+ get(name: string): CommandRegistration<R> | undefined;
20
+ /**
21
+ * 列出所有已注册的命令
22
+ */
23
+ listCommands(): Array<{
24
+ name: string;
25
+ description: string;
26
+ }>;
27
+ }
@@ -4,20 +4,44 @@ export declare class CommandSystem {
4
4
  * 命令调用者
5
5
  */
6
6
  private invoker;
7
+ /**
8
+ * 命令解析器
9
+ */
10
+ private parser;
11
+ /**
12
+ * 命令注册器
13
+ */
14
+ private registry;
15
+ /**
16
+ * 注册命令
17
+ * @param name 命令名称
18
+ * @param receiver 命令接收者
19
+ */
20
+ register<R extends CommandReceiver>(receiver: R): void;
7
21
  /**
8
22
  * 创建命令
9
23
  * @param receiver 命令接收者
10
24
  * @param executeFn 执行操作的函数
11
25
  * @param undoFn 撤销操作的函数
12
26
  * @param description 命令描述
27
+ * @param commandString 原始命令字符串
13
28
  * @returns 命令对象
14
29
  */
15
- create<R extends CommandReceiver>(receiver: R, executeFn: (receiver: R) => void, undoFn: (receiver: R) => void, description: string): Command;
30
+ create<R extends CommandReceiver>(receiver: R, executeFn: (receiver: R) => void, undoFn: (receiver: R) => void, description: string, commandString?: string): Command;
31
+ /**
32
+ * 从命令字符串创建 Command
33
+ * @param commandString 命令字符串
34
+ * @param description 针对本次命令描述
35
+ * @returns Command 实例,失败返回 undefined
36
+ */
37
+ createFromCommandString(commandString: string, description?: string): Command | undefined;
16
38
  /**
17
39
  * 执行命令
18
40
  * @param command 要执行的命令
41
+ * @param description 针对本次命令描述
42
+ * @param pushToHistory 是否将命令推入历史记录,默认为 true
19
43
  */
20
- execute(command: Command): void;
44
+ execute(command: Command | string, description?: string, pushToHistory?: boolean): void;
21
45
  /**
22
46
  * 撤销上一个命令
23
47
  * @returns 是否成功撤销
@@ -31,8 +55,9 @@ export declare class CommandSystem {
31
55
  /**
32
56
  * 批量执行命令
33
57
  * @param commands 要执行的命令数组
58
+ * @param pushToHistory 是否将命令推入历史记录,默认为 true
34
59
  */
35
- executeBatch(commands: Command[]): void;
60
+ executeBatch(commands: Command[], pushToHistory?: boolean): void;
36
61
  /**
37
62
  * 清空命令历史
38
63
  */
@@ -57,6 +82,13 @@ export declare class CommandSystem {
57
82
  * @returns 撤销历史记录数量
58
83
  */
59
84
  getUndoHistoryCount(): number;
85
+ /**
86
+ * 列出所有可用命令
87
+ */
88
+ listCommands(): Array<{
89
+ name: string;
90
+ description: string;
91
+ }>;
60
92
  /**
61
93
  * 构造函数
62
94
  * @param option 配置参数
@@ -16,14 +16,19 @@ export declare class ConcreteCommand<R extends CommandReceiver> implements Comma
16
16
  * 命令描述
17
17
  */
18
18
  private description;
19
+ /**
20
+ * 原始命令字符串
21
+ */
22
+ private commandString;
19
23
  /**
20
24
  * 构造函数
21
25
  * @param receiver 命令接收者
22
26
  * @param executeFn 执行操作的函数
23
27
  * @param undoFn 撤销操作的函数
24
28
  * @param description 命令描述
29
+ * @param commandString 原始命令字符串
25
30
  */
26
- constructor(receiver: R, executeFn: (receiver: R) => void, undoFn: (receiver: R) => void, description: string);
31
+ constructor(receiver: R, executeFn: (receiver: R) => void, undoFn: (receiver: R) => void, description: string, commandString?: string);
27
32
  /**
28
33
  * 执行命令
29
34
  */
@@ -37,6 +42,11 @@ export declare class ConcreteCommand<R extends CommandReceiver> implements Comma
37
42
  * @returns 命令描述
38
43
  */
39
44
  getDescription(): string;
45
+ /**
46
+ * 获取原始命令字符串
47
+ * @returns 命令字符串
48
+ */
49
+ getCommandString(): string;
40
50
  }
41
51
  /**
42
52
  * 创建命令的工厂函数
@@ -44,6 +54,7 @@ export declare class ConcreteCommand<R extends CommandReceiver> implements Comma
44
54
  * @param executeFn 执行操作的函数
45
55
  * @param undoFn 撤销操作的函数
46
56
  * @param description 命令描述
57
+ * @param commandString 原始命令字符串
47
58
  * @returns 命令对象
48
59
  */
49
- export declare function createCommand<R extends CommandReceiver>(receiver: R, executeFn: (receiver: R) => void, undoFn: (receiver: R) => void, description: string): Command;
60
+ export declare function createCommand<R extends CommandReceiver>(receiver: R, executeFn: (receiver: R) => void, undoFn: (receiver: R) => void, description: string, commandString?: string): Command;
@@ -1,7 +1,5 @@
1
1
  /**
2
2
  * 命令系统导出文件
3
3
  */
4
- export { CommandInvoker } from './CommandInvoker';
5
4
  export { CommandSystem } from './CommandSystem';
6
- export { ConcreteCommand, createCommand } from './ConcreteCommand';
7
- export type { Command, CommandReceiver, CommandHistoryItem, TCommandSystemOption, TCommandInvokerOption, TGDMBOption } from './Command';
5
+ export type * from './Command';
@@ -1,7 +1,9 @@
1
1
  import { BaseEvents, Adapter } from '../base-class';
2
+ import { CommandSystem } from '../command';
2
3
  import { TEventEmits, VO, IRegisterServiceOption, IRegisterEntityOption, IRegisterVoOptionCache, IRegisterVoOptionContainerCache, IRegisterVoOptionPredicate, IRegisterVoOptionNoCache, TCustomInjectionToken } from '../type';
3
4
  import { DomainService } from './DomainService';
4
5
  import { DomainEntity } from './DomainEntity';
6
+ import { TCommandSystemOption } from '../command/Command';
5
7
  /** 值对象注册配置类型 */
6
8
  type TRegisterVoOption<V extends VO<any>> = IRegisterVoOptionCache<V> | IRegisterVoOptionContainerCache<V> | IRegisterVoOptionPredicate<V> | IRegisterVoOptionNoCache<V>;
7
9
  /**
@@ -17,6 +19,10 @@ export declare class Domain<E extends TEventEmits = TEventEmits> {
17
19
  * @description 领域事件句柄,用于触发、监听、取消监听领域事件
18
20
  */
19
21
  e: BaseEvents<E>;
22
+ /**
23
+ * @description 领域命令句柄,用于注册、执行、撤销、重做领域命令
24
+ */
25
+ c: CommandSystem;
20
26
  /**
21
27
  * @description 向领域依赖注入容器中注册一个或多个领域服务
22
28
  * @param service 单个领域服务类或数组
@@ -102,6 +108,6 @@ export declare class Domain<E extends TEventEmits = TEventEmits> {
102
108
  /**
103
109
  * 域基类构造函数
104
110
  */
105
- constructor();
111
+ constructor(options?: TCommandSystemOption);
106
112
  }
107
113
  export {};
@@ -34,11 +34,11 @@ export interface IDomainRegisterOption<D extends Domain> {
34
34
  args?: any[];
35
35
  /** token 为领域名称的 before resolve拦截函数 */
36
36
  beforeResolution?: (token: InjectionToken, resolutionType: string) => void;
37
- /** token 为领域名称的 before resolve配置对象 */
37
+ /** before resolve 拦截器触发评率 */
38
38
  beforeResolutionFrequency?: 'Always' | 'Once';
39
39
  /** token 为领域名称的 after resolve拦截函数 */
40
40
  afterResolution?: (token: InjectionToken, result: D | D[], resolutionType: string) => void;
41
- /** token 为领域名称的 after resolve配置对象 */
41
+ /** after resolve 拦截器触发评率 */
42
42
  afterResolutionFrequency?: 'Always' | 'Once';
43
43
  }
44
44
  /**
@@ -49,8 +49,12 @@ export interface IRegisterEntityOption<E extends DomainEntity> {
49
49
  cachePolicy?: 'cache' | 'containerCache' | 'resolution';
50
50
  /** before 拦截器函数 */
51
51
  beforeResolution?: (token: InjectionToken, resolutionType: string) => void;
52
+ /** before 拦截器触发评率 */
53
+ beforeResolutionFrequency?: 'Always' | 'Once';
52
54
  /** after 拦截器函数 */
53
55
  afterResolution?: (token: InjectionToken, result: E | E[], resolutionType: string) => void;
56
+ /** after 拦截器触发评率 */
57
+ afterResolutionFrequency?: 'Always' | 'Once';
54
58
  }
55
59
  /** 值对象注册配置类型 - 全局缓存缓存 */
56
60
  export interface IRegisterVoOptionCache<V extends VO<any> = any> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gdmb",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "type": "module",
5
5
  "main": "dist/gdmb.umd.js",
6
6
  "module": "dist/gdmb.es.js",