gdmb 1.3.2 → 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 c, predicateAwareClassFactory as
|
|
2
|
-
class
|
|
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
|
*/
|
|
@@ -24,8 +24,8 @@ class p {
|
|
|
24
24
|
* @returns 监听句柄
|
|
25
25
|
*/
|
|
26
26
|
on(e, t) {
|
|
27
|
-
const
|
|
28
|
-
|
|
27
|
+
const s = this._mapper[e];
|
|
28
|
+
s ? s.push(t) : (this.register(e), this.on(e, t));
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* 移出指定监听函数监听
|
|
@@ -33,10 +33,10 @@ class p {
|
|
|
33
33
|
* @param handler 监听函数
|
|
34
34
|
*/
|
|
35
35
|
off(e, t) {
|
|
36
|
-
const
|
|
37
|
-
if (
|
|
38
|
-
const i =
|
|
39
|
-
i !== -1 &&
|
|
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
|
/**
|
|
@@ -45,9 +45,9 @@ class p {
|
|
|
45
45
|
* @param args 时间回调参数
|
|
46
46
|
*/
|
|
47
47
|
trigger(e, ...t) {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
for (const i of Object.values(
|
|
48
|
+
const s = this._mapper[e];
|
|
49
|
+
s && setTimeout(() => {
|
|
50
|
+
for (const i of Object.values(s))
|
|
51
51
|
i(...t);
|
|
52
52
|
}, 0);
|
|
53
53
|
}
|
|
@@ -59,231 +59,7 @@ class p {
|
|
|
59
59
|
}
|
|
60
60
|
class q {
|
|
61
61
|
}
|
|
62
|
-
class
|
|
63
|
-
/**
|
|
64
|
-
* @summary 领域内部依赖注入容器
|
|
65
|
-
* @description 领域内部依赖注入容器,用于注册和解析领域服务、实体、值对象等。
|
|
66
|
-
*/
|
|
67
|
-
dic = l.createChildContainer();
|
|
68
|
-
/**
|
|
69
|
-
* @description 领域事件句柄,用于触发、监听、取消监听领域事件
|
|
70
|
-
*/
|
|
71
|
-
e = new p();
|
|
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, t) {
|
|
96
|
-
const r = Array.isArray(e) ? e : [e], i = t ? Array.isArray(t) ? t : [t] : [];
|
|
97
|
-
for (let o = 0; o < r.length; o += 1) {
|
|
98
|
-
const s = r[o], a = i[o];
|
|
99
|
-
if (this.dic.isRegistered(s.name) || this.dic.isRegistered(s)) {
|
|
100
|
-
console.warn(`服务 ${s.name} 已注册,跳过注册`);
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
this.dic.register(s.name, { useClass: s }), this.dic.register(s, { useClass: s }), a?.beforeResolution && (this.dic.beforeResolution(s.name, a.beforeResolution, {
|
|
104
|
-
frequency: a?.beforeResolutionFrequency || "Always"
|
|
105
|
-
}), this.dic.beforeResolution(s, a.beforeResolution, {
|
|
106
|
-
frequency: a?.beforeResolutionFrequency || "Always"
|
|
107
|
-
})), a?.afterResolution && (this.dic.afterResolution(s.name, a.afterResolution, {
|
|
108
|
-
frequency: a?.afterResolutionFrequency || "Always"
|
|
109
|
-
}), this.dic.afterResolution(s, a?.afterResolution, {
|
|
110
|
-
frequency: a?.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, t) {
|
|
138
|
-
const r = Array.isArray(e) ? e : [e], i = t ? Array.isArray(t) ? t : [t] : [];
|
|
139
|
-
for (let o = 0; o < r.length; o += 1) {
|
|
140
|
-
const s = r[o], a = i[o];
|
|
141
|
-
if (this.dic.isRegistered(s.name) || this.dic.isRegistered(s)) {
|
|
142
|
-
console.warn(`实体 ${s.name} 已注册,跳过注册`);
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
if (a?.cachePolicy)
|
|
146
|
-
switch (a.cachePolicy) {
|
|
147
|
-
case "cache":
|
|
148
|
-
l.register(s.name, { useClass: s }, { lifecycle: c.Singleton }), l.register(s, { useClass: s }, { lifecycle: c.Singleton });
|
|
149
|
-
break;
|
|
150
|
-
case "containerCache":
|
|
151
|
-
this.dic.register(s.name, { useClass: s }, { lifecycle: c.ContainerScoped }), this.dic.register(s, { useClass: s }, { lifecycle: c.ContainerScoped });
|
|
152
|
-
break;
|
|
153
|
-
case "resolution":
|
|
154
|
-
this.dic.register(s.name, { useClass: s }, { lifecycle: c.ResolutionScoped }), this.dic.register(s, { useClass: s }, { lifecycle: c.ResolutionScoped });
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
else
|
|
158
|
-
this.dic.register(s.name, { useClass: s }, { lifecycle: c.Transient }), this.dic.register(s, { useClass: s }, { lifecycle: c.Transient });
|
|
159
|
-
a?.beforeResolution && (this.dic.beforeResolution(s.name, a.beforeResolution, {
|
|
160
|
-
frequency: a?.beforeResolutionFrequency || "Always"
|
|
161
|
-
}), this.dic.beforeResolution(s, a.beforeResolution, {
|
|
162
|
-
frequency: a?.beforeResolutionFrequency || "Always"
|
|
163
|
-
})), a?.afterResolution && (this.dic.afterResolution(s.name, a.afterResolution, {
|
|
164
|
-
frequency: a?.afterResolutionFrequency || "Always"
|
|
165
|
-
}), this.dic.afterResolution(s, a.afterResolution, {
|
|
166
|
-
frequency: a?.afterResolutionFrequency || "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 t = Array.isArray(e) ? e : [e];
|
|
185
|
-
for (let r = 0; r < t.length; r += 1) {
|
|
186
|
-
const i = t[r], o = Array.isArray(i.token) ? i.token : [i.token];
|
|
187
|
-
for (let s = 0; s < o.length; s += 1) {
|
|
188
|
-
const a = o[s];
|
|
189
|
-
switch (i.factoryType) {
|
|
190
|
-
case "cache":
|
|
191
|
-
l.register(a, { useFactory: g(i.resolveFactory) });
|
|
192
|
-
break;
|
|
193
|
-
case "containerCache":
|
|
194
|
-
this.dic.register(a, { useFactory: m(i.resolveFactory) });
|
|
195
|
-
break;
|
|
196
|
-
case "predicate":
|
|
197
|
-
this.dic.register(a, {
|
|
198
|
-
useFactory: d(
|
|
199
|
-
i.resolveFactory,
|
|
200
|
-
i.positiveTarget,
|
|
201
|
-
i.negativeTarget
|
|
202
|
-
)
|
|
203
|
-
});
|
|
204
|
-
break;
|
|
205
|
-
default:
|
|
206
|
-
this.dic.register(a, { useFactory: i.resolveFactory });
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* @description 从依赖注入容器中获取领域服务实例对象
|
|
214
|
-
* @param token 领域服务token
|
|
215
|
-
* @returns 领域服务实例对象
|
|
216
|
-
*/
|
|
217
|
-
services(e) {
|
|
218
|
-
const t = this.dic.resolve(e);
|
|
219
|
-
return t && !t?.e && (t.e = this.e), t;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* @description 从依赖注入容器中获取领域实体实例对象
|
|
223
|
-
* @param sourceToken 源领域实体token,存在适配器时是 DEF 泛型类型,否则为 DE 泛型类型
|
|
224
|
-
* @param adapter 领域实体适配器,将 DEF 类型转换为 DE 类型
|
|
225
|
-
* @returns 领域实体实例对象
|
|
226
|
-
*/
|
|
227
|
-
entities(e, t) {
|
|
228
|
-
let r;
|
|
229
|
-
if (t) {
|
|
230
|
-
const i = this.dic.resolve(e);
|
|
231
|
-
r = t.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, t) {
|
|
243
|
-
if (t) {
|
|
244
|
-
const r = this.dic.resolve(e);
|
|
245
|
-
return t.transform(r);
|
|
246
|
-
} else
|
|
247
|
-
return this.dic.resolve(e);
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* 域基类构造函数
|
|
251
|
-
*/
|
|
252
|
-
constructor() {
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
const f = (n) => (n.prototype.$ins = null, class extends n {
|
|
256
|
-
constructor(...e) {
|
|
257
|
-
return super(...e), n.prototype.$ins || (n.prototype.$ins = this), n.prototype.$ins;
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
var C = Object.getOwnPropertyDescriptor, R = (n, e, t, r) => {
|
|
261
|
-
for (var i = r > 1 ? void 0 : r ? C(e, t) : e, o = n.length - 1, s; o >= 0; o--)
|
|
262
|
-
(s = n[o]) && (i = s(i) || i);
|
|
263
|
-
return i;
|
|
264
|
-
};
|
|
265
|
-
let h = class {
|
|
266
|
-
/** 事件句柄 */
|
|
267
|
-
e = void 0;
|
|
268
|
-
/**
|
|
269
|
-
* 域基类构造函数
|
|
270
|
-
*/
|
|
271
|
-
constructor() {
|
|
272
|
-
}
|
|
273
|
-
};
|
|
274
|
-
h = R([
|
|
275
|
-
f
|
|
276
|
-
], h);
|
|
277
|
-
class I {
|
|
278
|
-
/** 事件句柄 */
|
|
279
|
-
e = void 0;
|
|
280
|
-
/**
|
|
281
|
-
* 域基类构造函数
|
|
282
|
-
*/
|
|
283
|
-
constructor() {
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
class v {
|
|
62
|
+
class R {
|
|
287
63
|
/**
|
|
288
64
|
* 命令历史记录
|
|
289
65
|
*/
|
|
@@ -303,12 +79,13 @@ class v {
|
|
|
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 v {
|
|
|
333
110
|
/**
|
|
334
111
|
* 批量执行命令
|
|
335
112
|
* @param commands 要执行的命令数组
|
|
113
|
+
* @param pushToHistory 是否将命令推入历史记录,默认为 true
|
|
336
114
|
*/
|
|
337
|
-
executeBatch(e) {
|
|
338
|
-
e.forEach((
|
|
115
|
+
executeBatch(e, t = !0) {
|
|
116
|
+
e.forEach((s) => this.execute(s, t));
|
|
339
117
|
}
|
|
340
118
|
/**
|
|
341
119
|
* 清空命令历史
|
|
@@ -376,10 +154,10 @@ class v {
|
|
|
376
154
|
* @param option 命令调用者构造配置参数
|
|
377
155
|
*/
|
|
378
156
|
constructor(e) {
|
|
379
|
-
this.maxHistoryDepth = e?.maxHistoryDepth ||
|
|
157
|
+
this.maxHistoryDepth = e?.maxHistoryDepth || 30, this.maxUndoHistoryDepth = e?.maxUndoHistoryDepth || 30;
|
|
380
158
|
}
|
|
381
159
|
}
|
|
382
|
-
class
|
|
160
|
+
class v {
|
|
383
161
|
/**
|
|
384
162
|
* 命令接收者
|
|
385
163
|
*/
|
|
@@ -408,8 +186,8 @@ class H {
|
|
|
408
186
|
* @param description 命令描述
|
|
409
187
|
* @param commandString 原始命令字符串
|
|
410
188
|
*/
|
|
411
|
-
constructor(e, t,
|
|
412
|
-
this.receiver = e, this.executeFn = t, this.undoFn =
|
|
189
|
+
constructor(e, t, s, i, o = "") {
|
|
190
|
+
this.receiver = e, this.executeFn = t, this.undoFn = s, this.description = i, this.commandString = o;
|
|
413
191
|
}
|
|
414
192
|
/**
|
|
415
193
|
* 执行命令
|
|
@@ -438,10 +216,10 @@ class H {
|
|
|
438
216
|
return this.commandString;
|
|
439
217
|
}
|
|
440
218
|
}
|
|
441
|
-
function b(n, e, t,
|
|
442
|
-
return new
|
|
219
|
+
function b(n, e, t, s, i = "") {
|
|
220
|
+
return new v(n, e, t, s, i);
|
|
443
221
|
}
|
|
444
|
-
class
|
|
222
|
+
class H {
|
|
445
223
|
/** 命令注册映射 */
|
|
446
224
|
commands = /* @__PURE__ */ new Map();
|
|
447
225
|
/**
|
|
@@ -451,8 +229,8 @@ class D {
|
|
|
451
229
|
* @param creator 命令创建函数
|
|
452
230
|
* @param description 命令描述
|
|
453
231
|
*/
|
|
454
|
-
register(e, t,
|
|
455
|
-
this.commands.set(e, { receiver: t, description:
|
|
232
|
+
register(e, t, s) {
|
|
233
|
+
this.commands.set(e, { receiver: t, description: s });
|
|
456
234
|
}
|
|
457
235
|
/**
|
|
458
236
|
* 获取命令注册信息
|
|
@@ -470,18 +248,18 @@ class D {
|
|
|
470
248
|
}));
|
|
471
249
|
}
|
|
472
250
|
}
|
|
473
|
-
class
|
|
251
|
+
class D {
|
|
474
252
|
/**
|
|
475
253
|
* 解析命令字符串
|
|
476
254
|
* @param commandString 命令字符串,如 "add -a 10 -b 5"
|
|
477
255
|
* @returns ParsedCommand 对象
|
|
478
256
|
*/
|
|
479
257
|
parse(e) {
|
|
480
|
-
const t = e.trim().split(/\s+/g),
|
|
258
|
+
const t = e.trim().split(/\s+/g), s = t[0], i = /* @__PURE__ */ new Map();
|
|
481
259
|
for (let o = 1; o < t.length; o++) {
|
|
482
|
-
const
|
|
483
|
-
if (
|
|
484
|
-
const a =
|
|
260
|
+
const r = t[o];
|
|
261
|
+
if (r.startsWith("-")) {
|
|
262
|
+
const a = r.slice(1);
|
|
485
263
|
if (o + 1 >= t.length || t[o + 1].startsWith("-"))
|
|
486
264
|
i.set(a, !0);
|
|
487
265
|
else {
|
|
@@ -491,13 +269,13 @@ class x {
|
|
|
491
269
|
}
|
|
492
270
|
}
|
|
493
271
|
return {
|
|
494
|
-
commandName:
|
|
272
|
+
commandName: s,
|
|
495
273
|
options: i,
|
|
496
274
|
args: t.slice(1)
|
|
497
275
|
};
|
|
498
276
|
}
|
|
499
277
|
}
|
|
500
|
-
class
|
|
278
|
+
class f {
|
|
501
279
|
/**
|
|
502
280
|
* 命令调用者
|
|
503
281
|
*/
|
|
@@ -505,11 +283,11 @@ class w {
|
|
|
505
283
|
/**
|
|
506
284
|
* 命令解析器
|
|
507
285
|
*/
|
|
508
|
-
parser = new
|
|
286
|
+
parser = new D();
|
|
509
287
|
/**
|
|
510
288
|
* 命令注册器
|
|
511
289
|
*/
|
|
512
|
-
registry = new
|
|
290
|
+
registry = new H();
|
|
513
291
|
/**
|
|
514
292
|
* 注册命令
|
|
515
293
|
* @param name 命令名称
|
|
@@ -527,8 +305,8 @@ class w {
|
|
|
527
305
|
* @param commandString 原始命令字符串
|
|
528
306
|
* @returns 命令对象
|
|
529
307
|
*/
|
|
530
|
-
create(e, t,
|
|
531
|
-
return b(e, t,
|
|
308
|
+
create(e, t, s, i, o = "") {
|
|
309
|
+
return b(e, t, s, i, o);
|
|
532
310
|
}
|
|
533
311
|
/**
|
|
534
312
|
* 从命令字符串创建 Command
|
|
@@ -537,14 +315,14 @@ class w {
|
|
|
537
315
|
* @returns Command 实例,失败返回 undefined
|
|
538
316
|
*/
|
|
539
317
|
createFromCommandString(e, t) {
|
|
540
|
-
const
|
|
318
|
+
const s = this.parser.parse(e), i = this.registry.get(s.commandName);
|
|
541
319
|
if (!i) {
|
|
542
|
-
console.warn(`命令 ${
|
|
320
|
+
console.warn(`命令 ${s.commandName} 未注册`);
|
|
543
321
|
return;
|
|
544
322
|
}
|
|
545
323
|
return i.receiver.creator?.(
|
|
546
|
-
|
|
547
|
-
|
|
324
|
+
s.options,
|
|
325
|
+
s.args,
|
|
548
326
|
t || i.description,
|
|
549
327
|
e
|
|
550
328
|
);
|
|
@@ -553,13 +331,14 @@ class w {
|
|
|
553
331
|
* 执行命令
|
|
554
332
|
* @param command 要执行的命令
|
|
555
333
|
* @param description 针对本次命令描述
|
|
334
|
+
* @param pushToHistory 是否将命令推入历史记录,默认为 true
|
|
556
335
|
*/
|
|
557
|
-
execute(e, t) {
|
|
336
|
+
execute(e, t, s = !0) {
|
|
558
337
|
if (typeof e == "string") {
|
|
559
|
-
const
|
|
560
|
-
|
|
338
|
+
const i = this.createFromCommandString(e, t);
|
|
339
|
+
i && this.invoker.execute(i, s);
|
|
561
340
|
} else
|
|
562
|
-
this.invoker.execute(e);
|
|
341
|
+
this.invoker.execute(e, s);
|
|
563
342
|
}
|
|
564
343
|
/**
|
|
565
344
|
* 撤销上一个命令
|
|
@@ -578,9 +357,10 @@ class w {
|
|
|
578
357
|
/**
|
|
579
358
|
* 批量执行命令
|
|
580
359
|
* @param commands 要执行的命令数组
|
|
360
|
+
* @param pushToHistory 是否将命令推入历史记录,默认为 true
|
|
581
361
|
*/
|
|
582
|
-
executeBatch(e) {
|
|
583
|
-
this.invoker.executeBatch(e);
|
|
362
|
+
executeBatch(e, t = !0) {
|
|
363
|
+
this.invoker.executeBatch(e, t);
|
|
584
364
|
}
|
|
585
365
|
/**
|
|
586
366
|
* 清空命令历史
|
|
@@ -627,15 +407,244 @@ class w {
|
|
|
627
407
|
* @param option 配置参数
|
|
628
408
|
*/
|
|
629
409
|
constructor(e) {
|
|
630
|
-
this.invoker = new
|
|
410
|
+
this.invoker = new R({
|
|
631
411
|
maxHistoryDepth: e?.maxHistoryDepth,
|
|
632
412
|
maxUndoHistoryDepth: e?.maxUndoHistoryDepth
|
|
633
413
|
});
|
|
634
414
|
}
|
|
635
415
|
}
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
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);
|
|
639
648
|
return i;
|
|
640
649
|
};
|
|
641
650
|
let y = class {
|
|
@@ -647,22 +656,22 @@ let y = class {
|
|
|
647
656
|
* 注册领域对象
|
|
648
657
|
*/
|
|
649
658
|
registerDomain(n, e) {
|
|
650
|
-
const t = Array.isArray(n) ? n : [n],
|
|
659
|
+
const t = Array.isArray(n) ? n : [n], s = e ? Array.isArray(e) ? e : [e] : [];
|
|
651
660
|
for (let i = 0; i < t.length; i += 1) {
|
|
652
|
-
const o = t[i],
|
|
661
|
+
const o = t[i], r = s[i];
|
|
653
662
|
if (this.globalDIC.isRegistered(o.name)) {
|
|
654
663
|
console.warn(`领域 ${o.name} 已注册,无法重复注册`);
|
|
655
664
|
return;
|
|
656
665
|
}
|
|
657
|
-
const a = new o(...
|
|
658
|
-
this.globalDIC.register(o.name, { useValue: a }), this.globalDIC.register(o, { useValue: a }),
|
|
659
|
-
frequency:
|
|
660
|
-
}), this.globalDIC.beforeResolution(o,
|
|
661
|
-
frequency:
|
|
662
|
-
})),
|
|
663
|
-
frequency:
|
|
664
|
-
}), this.globalDIC.afterResolution(o,
|
|
665
|
-
frequency:
|
|
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"
|
|
666
675
|
}));
|
|
667
676
|
}
|
|
668
677
|
}
|
|
@@ -683,8 +692,8 @@ let y = class {
|
|
|
683
692
|
*/
|
|
684
693
|
transformAll(n, e) {
|
|
685
694
|
const t = [];
|
|
686
|
-
for (let
|
|
687
|
-
const i = e.transform(n[
|
|
695
|
+
for (let s = 0; s < n.length; s += 1) {
|
|
696
|
+
const i = e.transform(n[s]);
|
|
688
697
|
i != null && t.push(i);
|
|
689
698
|
}
|
|
690
699
|
return t;
|
|
@@ -694,23 +703,20 @@ let y = class {
|
|
|
694
703
|
* @param option 配置参数
|
|
695
704
|
*/
|
|
696
705
|
constructor(n) {
|
|
697
|
-
this.c = new
|
|
706
|
+
this.c = new f({
|
|
698
707
|
maxHistoryDepth: n?.maxHistoryDepth,
|
|
699
708
|
maxUndoHistoryDepth: n?.maxUndoHistoryDepth
|
|
700
709
|
});
|
|
701
710
|
}
|
|
702
711
|
};
|
|
703
712
|
y = F([
|
|
704
|
-
|
|
713
|
+
d
|
|
705
714
|
], y);
|
|
706
715
|
export {
|
|
707
716
|
q as Adapter,
|
|
708
|
-
|
|
709
|
-
w as CommandSystem,
|
|
710
|
-
H as ConcreteCommand,
|
|
717
|
+
f as CommandSystem,
|
|
711
718
|
k as Domain,
|
|
712
719
|
I as DomainEntity,
|
|
713
720
|
h as DomainService,
|
|
714
|
-
y as GDMB
|
|
715
|
-
b as createCommand
|
|
721
|
+
y as GDMB
|
|
716
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
|
|
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"})}));
|
|
@@ -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
|
*/
|
|
@@ -39,8 +39,9 @@ export declare class CommandSystem {
|
|
|
39
39
|
* 执行命令
|
|
40
40
|
* @param command 要执行的命令
|
|
41
41
|
* @param description 针对本次命令描述
|
|
42
|
+
* @param pushToHistory 是否将命令推入历史记录,默认为 true
|
|
42
43
|
*/
|
|
43
|
-
execute(command: Command | string, description?: string): void;
|
|
44
|
+
execute(command: Command | string, description?: string, pushToHistory?: boolean): void;
|
|
44
45
|
/**
|
|
45
46
|
* 撤销上一个命令
|
|
46
47
|
* @returns 是否成功撤销
|
|
@@ -54,8 +55,9 @@ export declare class CommandSystem {
|
|
|
54
55
|
/**
|
|
55
56
|
* 批量执行命令
|
|
56
57
|
* @param commands 要执行的命令数组
|
|
58
|
+
* @param pushToHistory 是否将命令推入历史记录,默认为 true
|
|
57
59
|
*/
|
|
58
|
-
executeBatch(commands: Command[]): void;
|
|
60
|
+
executeBatch(commands: Command[], pushToHistory?: boolean): void;
|
|
59
61
|
/**
|
|
60
62
|
* 清空命令历史
|
|
61
63
|
*/
|
|
@@ -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 {};
|