taro-bluetooth-print 2.9.0 → 2.9.2
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/CHANGELOG.md +16 -1
- package/README.md +53 -4
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/types/core/di/Container.d.ts +84 -0
- package/dist/types/core/di/Tokens.d.ts +29 -0
- package/dist/types/core/di/index.d.ts +3 -0
- package/dist/types/core/event/EventBus.d.ts +66 -0
- package/dist/types/core/event/index.d.ts +2 -0
- package/dist/types/core/index.d.ts +5 -4
- package/dist/types/core/plugin/PluginManager.d.ts +64 -0
- package/dist/types/core/plugin/index.d.ts +2 -0
- package/dist/types/device/MultiPrinterManager.d.ts +2 -0
- package/dist/types/factory/di-factory.d.ts +52 -0
- package/dist/types/index.d.ts +5 -1
- package/dist/types/providers/ServiceProvider.d.ts +56 -0
- package/dist/types/providers/index.d.ts +2 -0
- package/dist/types/template/TemplateEngine.d.ts +24 -68
- package/dist/types/template/engines/TemplateRenderer.d.ts +71 -0
- package/dist/types/template/parsers/TemplateParser.d.ts +23 -0
- package/dist/types/utils/index.d.ts +8 -0
- package/dist/types/utils/logger.d.ts +4 -3
- package/dist/types/utils/outputLimiter.d.ts +87 -0
- package/dist/types/utils/validation.d.ts +11 -309
- package/dist/types/utils/validators/array.d.ts +19 -0
- package/dist/types/utils/validators/buffer.d.ts +18 -0
- package/dist/types/utils/validators/chain.d.ts +31 -0
- package/dist/types/utils/validators/common.d.ts +22 -0
- package/dist/types/utils/validators/number.d.ts +20 -0
- package/dist/types/utils/validators/object.d.ts +24 -0
- package/dist/types/utils/validators/printer.d.ts +40 -0
- package/dist/types/utils/validators/types.d.ts +125 -0
- package/dist/types/utils/validators/uuid.d.ts +23 -0
- package/package.json +1 -1
- package/src/core/BluetoothPrinter.ts +2 -1
- package/src/core/di/Container.ts +332 -0
- package/src/core/di/Tokens.ts +45 -0
- package/src/core/di/index.ts +3 -0
- package/src/core/event/EventBus.ts +251 -0
- package/src/core/event/index.ts +2 -0
- package/src/core/index.ts +10 -4
- package/src/core/plugin/PluginManager.ts +161 -0
- package/src/core/plugin/index.ts +2 -0
- package/src/device/MultiPrinterManager.ts +15 -6
- package/src/factory/di-factory.ts +61 -0
- package/src/index.ts +50 -1
- package/src/providers/ServiceProvider.ts +213 -0
- package/src/providers/index.ts +2 -0
- package/src/template/TemplateEngine.ts +27 -792
- package/src/template/engines/TemplateRenderer.ts +762 -0
- package/src/template/parsers/TemplateParser.ts +94 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/logger.ts +17 -4
- package/src/utils/outputLimiter.ts +227 -0
- package/src/utils/validation.ts +21 -1138
- package/src/utils/validators/array.ts +95 -0
- package/src/utils/validators/buffer.ts +81 -0
- package/src/utils/validators/chain.ts +181 -0
- package/src/utils/validators/common.ts +216 -0
- package/src/utils/validators/number.ts +101 -0
- package/src/utils/validators/object.ts +63 -0
- package/src/utils/validators/printer.ts +294 -0
- package/src/utils/validators/types.ts +105 -0
- package/src/utils/validators/uuid.ts +49 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core validation types
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Validation error details
|
|
6
|
+
*/
|
|
7
|
+
export interface ValidationError {
|
|
8
|
+
/** Field that failed validation */
|
|
9
|
+
field: string;
|
|
10
|
+
/** Human-readable error message */
|
|
11
|
+
message: string;
|
|
12
|
+
/** Error code for programmatic handling */
|
|
13
|
+
code: string;
|
|
14
|
+
/** Actual value that failed (optional) */
|
|
15
|
+
value?: unknown;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validation result
|
|
19
|
+
*/
|
|
20
|
+
export interface ValidationResult {
|
|
21
|
+
/** Whether validation passed */
|
|
22
|
+
valid: boolean;
|
|
23
|
+
/** List of validation errors */
|
|
24
|
+
errors: ValidationError[];
|
|
25
|
+
/** Warnings (non-fatal issues) */
|
|
26
|
+
warnings?: ValidationError[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Validator function type
|
|
30
|
+
*/
|
|
31
|
+
export type ValidatorFn<T = unknown> = (value: T) => ValidationResult;
|
|
32
|
+
/**
|
|
33
|
+
* Validation rule definition
|
|
34
|
+
*/
|
|
35
|
+
export interface ValidationRule<T = unknown> {
|
|
36
|
+
/** Rule name */
|
|
37
|
+
name: string;
|
|
38
|
+
/** Validation function */
|
|
39
|
+
validate: (value: T) => boolean;
|
|
40
|
+
/** Error message if validation fails */
|
|
41
|
+
message: string;
|
|
42
|
+
/** Error code */
|
|
43
|
+
code: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Common error codes
|
|
47
|
+
*/
|
|
48
|
+
export declare const ValidationCodes: {
|
|
49
|
+
readonly REQUIRED: "REQUIRED";
|
|
50
|
+
readonly INVALID_TYPE: "INVALID_TYPE";
|
|
51
|
+
readonly INVALID_FORMAT: "INVALID_FORMAT";
|
|
52
|
+
readonly OUT_OF_RANGE: "OUT_OF_RANGE";
|
|
53
|
+
readonly TOO_SHORT: "TOO_SHORT";
|
|
54
|
+
readonly TOO_LONG: "TOO_LONG";
|
|
55
|
+
readonly INVALID_ENUM: "INVALID_ENUM";
|
|
56
|
+
readonly PATTERN_MISMATCH: "PATTERN_MISMATCH";
|
|
57
|
+
readonly INVALID_BUFFER: "INVALID_BUFFER";
|
|
58
|
+
readonly INVALID_ENCODING: "INVALID_ENCODING";
|
|
59
|
+
readonly EMPTY_ARRAY: "EMPTY_ARRAY";
|
|
60
|
+
readonly ARRAY_TOO_SHORT: "ARRAY_TOO_SHORT";
|
|
61
|
+
readonly ARRAY_TOO_LONG: "ARRAY_TOO_LONG";
|
|
62
|
+
readonly DUPLICATE_VALUE: "DUPLICATE_VALUE";
|
|
63
|
+
readonly NEGATIVE_VALUE: "NEGATIVE_VALUE";
|
|
64
|
+
readonly ZERO_VALUE: "ZERO_VALUE";
|
|
65
|
+
readonly NOT_A_NUMBER: "NOT_A_NUMBER";
|
|
66
|
+
readonly INFINITY_VALUE: "INFINITY_VALUE";
|
|
67
|
+
readonly INVALID_JSON: "INVALID_JSON";
|
|
68
|
+
readonly UNSUPPORTED_VALUE: "UNSUPPORTED_VALUE";
|
|
69
|
+
readonly INVALID_VERSION: "INVALID_VERSION";
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Printer data validation schema
|
|
73
|
+
*/
|
|
74
|
+
export interface PrinterDataSchema {
|
|
75
|
+
/** Device ID */
|
|
76
|
+
deviceId?: {
|
|
77
|
+
required?: boolean;
|
|
78
|
+
pattern?: RegExp;
|
|
79
|
+
};
|
|
80
|
+
/** Device name */
|
|
81
|
+
deviceName?: {
|
|
82
|
+
required?: boolean;
|
|
83
|
+
maxLength?: number;
|
|
84
|
+
};
|
|
85
|
+
/** Service UUID */
|
|
86
|
+
serviceUUID?: {
|
|
87
|
+
required?: boolean;
|
|
88
|
+
pattern?: RegExp;
|
|
89
|
+
};
|
|
90
|
+
/** Characteristic UUID */
|
|
91
|
+
characteristicUUID?: {
|
|
92
|
+
required?: boolean;
|
|
93
|
+
pattern?: RegExp;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Print job data validation schema
|
|
98
|
+
*/
|
|
99
|
+
export interface PrintJobSchema {
|
|
100
|
+
/** Job ID */
|
|
101
|
+
jobId?: {
|
|
102
|
+
required?: boolean;
|
|
103
|
+
maxLength?: number;
|
|
104
|
+
};
|
|
105
|
+
/** Data buffer */
|
|
106
|
+
data?: {
|
|
107
|
+
required?: boolean;
|
|
108
|
+
minSize?: number;
|
|
109
|
+
maxSize?: number;
|
|
110
|
+
};
|
|
111
|
+
/** Priority */
|
|
112
|
+
priority?: {
|
|
113
|
+
min?: number;
|
|
114
|
+
max?: number;
|
|
115
|
+
};
|
|
116
|
+
/** Retry count */
|
|
117
|
+
retryCount?: {
|
|
118
|
+
min?: number;
|
|
119
|
+
max?: number;
|
|
120
|
+
};
|
|
121
|
+
/** Metadata */
|
|
122
|
+
metadata?: {
|
|
123
|
+
required?: boolean;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UUID validation
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Validate a string is valid UUID
|
|
6
|
+
*
|
|
7
|
+
* @param value - Value to validate
|
|
8
|
+
* @param options - Validation options
|
|
9
|
+
* @returns Validation result
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const result = isValidUUID(value, { required: true });
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function isValidUUID(value: unknown, options?: {
|
|
17
|
+
required?: boolean;
|
|
18
|
+
versions?: number[];
|
|
19
|
+
}): {
|
|
20
|
+
valid: boolean;
|
|
21
|
+
version?: number;
|
|
22
|
+
error?: string;
|
|
23
|
+
};
|
package/package.json
CHANGED
|
@@ -118,7 +118,8 @@ export class BluetoothPrinter extends EventEmitter<PrinterEvents> {
|
|
|
118
118
|
this.commandBuilder = commandBuilder ?? new CommandBuilder();
|
|
119
119
|
} else {
|
|
120
120
|
// Modern DI
|
|
121
|
-
this.connectionManager =
|
|
121
|
+
this.connectionManager =
|
|
122
|
+
(connectionManagerOrAdapter as IConnectionManager) ?? new ConnectionManager();
|
|
122
123
|
this.printJobManager = printJobManager ?? new PrintJobManager(this.connectionManager);
|
|
123
124
|
this.commandBuilder = commandBuilder ?? new CommandBuilder();
|
|
124
125
|
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 依赖注入容器
|
|
3
|
+
* 提供现代化的依赖管理和服务定位功能
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
export type Constructor<T = object> = new (...args: any[]) => T;
|
|
8
|
+
export type Factory<T> = (container: Container) => T;
|
|
9
|
+
export type Provider<T> = Constructor<T> | Factory<T> | T;
|
|
10
|
+
|
|
11
|
+
export interface RegistrationOptions {
|
|
12
|
+
/** 是否单例 */
|
|
13
|
+
singleton?: boolean;
|
|
14
|
+
/** 生命周期 */
|
|
15
|
+
lifecycle?: 'transient' | 'singleton' | 'scoped';
|
|
16
|
+
/** 标签,用于区分同一接口的不同实现 */
|
|
17
|
+
tag?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Angular 风格的服务提供者配置
|
|
22
|
+
*/
|
|
23
|
+
export interface ServiceProviderConfig<T = unknown> {
|
|
24
|
+
/** 使用类 */
|
|
25
|
+
useClass?: Constructor<T>;
|
|
26
|
+
/** 使用工厂函数 */
|
|
27
|
+
useFactory?: Factory<T>;
|
|
28
|
+
/** 使用实例值 */
|
|
29
|
+
useValue?: T;
|
|
30
|
+
/** 生命周期 */
|
|
31
|
+
lifecycle?: 'transient' | 'singleton' | 'scoped';
|
|
32
|
+
/** 依赖项 */
|
|
33
|
+
dependencies?: (string | symbol | Constructor<unknown>)[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Registration<T> {
|
|
37
|
+
provider: Provider<T>;
|
|
38
|
+
options: RegistrationOptions;
|
|
39
|
+
instance?: T;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class Container {
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
44
|
+
private registrations = new Map<string | symbol, Registration<any>[]>();
|
|
45
|
+
private parent?: Container;
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
47
|
+
private scopedInstances = new Map<string | symbol, any>();
|
|
48
|
+
|
|
49
|
+
constructor(parent?: Container) {
|
|
50
|
+
this.parent = parent;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 创建子容器
|
|
55
|
+
*/
|
|
56
|
+
createChild(): Container {
|
|
57
|
+
return new Container(this);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 注册服务
|
|
62
|
+
*/
|
|
63
|
+
register<T>(
|
|
64
|
+
token: string | symbol | Constructor<T>,
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
66
|
+
provider: Provider<T> | ServiceProviderConfig<T>,
|
|
67
|
+
options: RegistrationOptions = {}
|
|
68
|
+
): this {
|
|
69
|
+
const key = this.getTokenKey(token);
|
|
70
|
+
|
|
71
|
+
// 支持 ServiceProviderConfig 格式
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
+
const config = provider as ServiceProviderConfig<T>;
|
|
74
|
+
|
|
75
|
+
// 如果是对象语法(Angular 风格)
|
|
76
|
+
if (
|
|
77
|
+
config &&
|
|
78
|
+
typeof config === 'object' &&
|
|
79
|
+
(config.useClass || config.useFactory || config.useValue !== undefined)
|
|
80
|
+
) {
|
|
81
|
+
let actualProvider: Provider<T>;
|
|
82
|
+
|
|
83
|
+
if (config.useClass) {
|
|
84
|
+
// 使用类 - 创建工厂函数来解析依赖
|
|
85
|
+
const Cls = config.useClass;
|
|
86
|
+
actualProvider = (container: Container) => {
|
|
87
|
+
return container.createInstance(Cls);
|
|
88
|
+
};
|
|
89
|
+
} else if (config.useFactory) {
|
|
90
|
+
actualProvider = config.useFactory;
|
|
91
|
+
} else {
|
|
92
|
+
actualProvider = config.useValue as T;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const registration: Registration<T> = {
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
97
|
+
provider: actualProvider,
|
|
98
|
+
options: {
|
|
99
|
+
lifecycle: config.lifecycle || 'transient',
|
|
100
|
+
...options,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
if (!this.registrations.has(key)) {
|
|
105
|
+
this.registrations.set(key, []);
|
|
106
|
+
}
|
|
107
|
+
this.registrations.get(key)!.push(registration);
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 原有逻辑:Provider<T> 格式
|
|
112
|
+
const registration: Registration<T> = {
|
|
113
|
+
provider: provider as Provider<T>,
|
|
114
|
+
options: {
|
|
115
|
+
lifecycle: 'transient',
|
|
116
|
+
...options,
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
if (!this.registrations.has(key)) {
|
|
121
|
+
this.registrations.set(key, []);
|
|
122
|
+
}
|
|
123
|
+
this.registrations.get(key)!.push(registration);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 注册单例服务
|
|
129
|
+
*/
|
|
130
|
+
registerSingleton<T>(token: string | symbol | Constructor<T>, provider: Provider<T>): this {
|
|
131
|
+
return this.register(token, provider, { lifecycle: 'singleton' });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 注册类型映射
|
|
136
|
+
*/
|
|
137
|
+
registerType<T>(
|
|
138
|
+
from: string | symbol | Constructor<T>,
|
|
139
|
+
to: Constructor<T>,
|
|
140
|
+
options: RegistrationOptions = {}
|
|
141
|
+
): this {
|
|
142
|
+
return this.register(from, c => c.resolve(to), options);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 注册实例
|
|
147
|
+
*/
|
|
148
|
+
registerInstance<T>(token: string | symbol | Constructor<T>, instance: T): this {
|
|
149
|
+
return this.register(token, instance, { lifecycle: 'singleton' });
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 解析服务
|
|
154
|
+
*/
|
|
155
|
+
resolve<T>(token: string | symbol | Constructor<T>, tag?: string): T {
|
|
156
|
+
const key = this.getTokenKey(token);
|
|
157
|
+
|
|
158
|
+
// 1. 检查当前容器的注册
|
|
159
|
+
const registrations = this.registrations.get(key);
|
|
160
|
+
if (registrations) {
|
|
161
|
+
const registration = tag
|
|
162
|
+
? registrations.find(r => r.options.tag === tag)
|
|
163
|
+
: registrations[registrations.length - 1];
|
|
164
|
+
|
|
165
|
+
if (registration) {
|
|
166
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
167
|
+
return this.getOrCreateInstance(registration, key);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 2. 检查父容器
|
|
172
|
+
if (this.parent) {
|
|
173
|
+
return this.parent.resolve(token, tag);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 3. 如果是构造函数,自动创建实例
|
|
177
|
+
if (typeof token === 'function' && this.isConstructor(token)) {
|
|
178
|
+
return this.createInstance(token);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
throw new Error(`No registration found for token: ${String(key)}`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* 解析所有注册的服务
|
|
186
|
+
*/
|
|
187
|
+
resolveAll<T>(token: string | symbol | Constructor<T>): T[] {
|
|
188
|
+
const key = this.getTokenKey(token);
|
|
189
|
+
const results: T[] = [];
|
|
190
|
+
|
|
191
|
+
// 从当前容器获取
|
|
192
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
193
|
+
const registrations = this.registrations.get(key);
|
|
194
|
+
if (registrations) {
|
|
195
|
+
for (const r of registrations) {
|
|
196
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
197
|
+
results.push(this.getOrCreateInstance(r, key));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 从父容器获取
|
|
202
|
+
if (this.parent) {
|
|
203
|
+
results.push(...this.parent.resolveAll(token));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return results;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* 检查是否已注册
|
|
211
|
+
*/
|
|
212
|
+
isRegistered<T>(token: string | symbol | Constructor<T>): boolean {
|
|
213
|
+
const key = this.getTokenKey(token);
|
|
214
|
+
return this.registrations.has(key) || (this.parent?.isRegistered(token) ?? false);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 清除所有注册
|
|
219
|
+
*/
|
|
220
|
+
clear(): void {
|
|
221
|
+
this.registrations.clear();
|
|
222
|
+
this.scopedInstances.clear();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 创建作用域
|
|
227
|
+
*/
|
|
228
|
+
createScope(): Container {
|
|
229
|
+
const scope = this.createChild();
|
|
230
|
+
return scope;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
234
|
+
private getTokenKey(token: string | symbol | Constructor<any>): string | symbol {
|
|
235
|
+
if (typeof token === 'function') {
|
|
236
|
+
return token.name || (token as unknown as symbol);
|
|
237
|
+
}
|
|
238
|
+
return token;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
242
|
+
private isConstructor(fn: Function): boolean {
|
|
243
|
+
return (
|
|
244
|
+
typeof fn.prototype === 'object' &&
|
|
245
|
+
fn.prototype !== null &&
|
|
246
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
247
|
+
fn.prototype.constructor === fn
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private getOrCreateInstance<T>(registration: Registration<T>, key: string | symbol): T {
|
|
252
|
+
const { provider, options } = registration;
|
|
253
|
+
|
|
254
|
+
// 单例模式
|
|
255
|
+
if (options.lifecycle === 'singleton') {
|
|
256
|
+
if (registration.instance === undefined) {
|
|
257
|
+
registration.instance = this.createProviderInstance(provider);
|
|
258
|
+
}
|
|
259
|
+
return registration.instance;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// 作用域模式
|
|
263
|
+
if (options.lifecycle === 'scoped') {
|
|
264
|
+
if (this.scopedInstances.has(key)) {
|
|
265
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
266
|
+
return this.scopedInstances.get(key) as T;
|
|
267
|
+
}
|
|
268
|
+
const instance = this.createProviderInstance(provider);
|
|
269
|
+
this.scopedInstances.set(key, instance);
|
|
270
|
+
return instance;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// 瞬态模式
|
|
274
|
+
return this.createProviderInstance(provider);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private createProviderInstance<T>(provider: Provider<T>): T {
|
|
278
|
+
if (typeof provider === 'function') {
|
|
279
|
+
// 工厂函数
|
|
280
|
+
if (provider.length === 1 && !this.isConstructor(provider)) {
|
|
281
|
+
return (provider as Factory<T>)(this);
|
|
282
|
+
}
|
|
283
|
+
// 构造函数
|
|
284
|
+
return this.createInstance(provider as Constructor<T>);
|
|
285
|
+
}
|
|
286
|
+
// 实例
|
|
287
|
+
return provider;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private createInstance<T>(constructor: Constructor<T>): T {
|
|
291
|
+
// 获取构造函数的参数类型(简化实现,实际可能需要反射元数据)
|
|
292
|
+
const paramCount = constructor.length;
|
|
293
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
294
|
+
const args: any[] = [];
|
|
295
|
+
|
|
296
|
+
for (let i = 0; i < paramCount; i++) {
|
|
297
|
+
// 尝试从容器解析参数
|
|
298
|
+
// 这里简化处理,实际应该使用参数装饰器
|
|
299
|
+
args.push(undefined);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
303
|
+
return new constructor(...args);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// 全局容器实例
|
|
308
|
+
export const rootContainer = new Container();
|
|
309
|
+
|
|
310
|
+
// 装饰器辅助函数
|
|
311
|
+
export function injectable<T extends Constructor<object>>(constructor: T): T {
|
|
312
|
+
// 标记类为可注入
|
|
313
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
314
|
+
(constructor as Constructor & { __injectable: boolean }).__injectable = true;
|
|
315
|
+
return constructor;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export function inject(token: string | symbol) {
|
|
319
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
320
|
+
return function (target: any, _propertyKey: string | symbol, parameterIndex: number) {
|
|
321
|
+
// 存储注入元数据
|
|
322
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
323
|
+
const existingInjections: (string | symbol)[] =
|
|
324
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
325
|
+
(target.__injections as (string | symbol)[]) || [];
|
|
326
|
+
existingInjections[parameterIndex] = token;
|
|
327
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
328
|
+
target.__injections = existingInjections;
|
|
329
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
330
|
+
return target;
|
|
331
|
+
};
|
|
332
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 服务标识符(Tokens)
|
|
3
|
+
* 用于依赖注入的常量定义
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// 适配器相关
|
|
7
|
+
export const ADAPTER_TOKEN = Symbol.for('Adapter');
|
|
8
|
+
export const ADAPTER_FACTORY_TOKEN = Symbol.for('AdapterFactory');
|
|
9
|
+
|
|
10
|
+
// 驱动相关
|
|
11
|
+
export const DRIVER_TOKEN = Symbol.for('Driver');
|
|
12
|
+
export const DRIVER_FACTORY_TOKEN = Symbol.for('DriverFactory');
|
|
13
|
+
|
|
14
|
+
// 服务相关
|
|
15
|
+
export const DEVICE_MANAGER_TOKEN = Symbol.for('DeviceManager');
|
|
16
|
+
export const CONNECTION_MANAGER_TOKEN = Symbol.for('ConnectionManager');
|
|
17
|
+
export const PRINT_JOB_MANAGER_TOKEN = Symbol.for('PrintJobManager');
|
|
18
|
+
export const PRINT_QUEUE_TOKEN = Symbol.for('PrintQueue');
|
|
19
|
+
export const OFFLINE_CACHE_TOKEN = Symbol.for('OfflineCache');
|
|
20
|
+
export const CONFIG_MANAGER_TOKEN = Symbol.for('ConfigManager');
|
|
21
|
+
|
|
22
|
+
// 新增服务令牌
|
|
23
|
+
export const COMMAND_BUILDER_TOKEN = Symbol.for('CommandBuilder');
|
|
24
|
+
export const PRINTER_STATUS_TOKEN = Symbol.for('PrinterStatus');
|
|
25
|
+
export const PRINT_HISTORY_TOKEN = Symbol.for('PrintHistory');
|
|
26
|
+
export const PRINT_STATISTICS_TOKEN = Symbol.for('PrintStatistics');
|
|
27
|
+
export const CLOUD_PRINT_MANAGER_TOKEN = Symbol.for('CloudPrintManager');
|
|
28
|
+
export const SCHEDULED_RETRY_MANAGER_TOKEN = Symbol.for('ScheduledRetryManager');
|
|
29
|
+
export const BATCH_PRINT_MANAGER_TOKEN = Symbol.for('BatchPrintManager');
|
|
30
|
+
|
|
31
|
+
// 工具服务
|
|
32
|
+
export const LOGGER_TOKEN = Symbol.for('Logger');
|
|
33
|
+
export const ENCODING_SERVICE_TOKEN = Symbol.for('EncodingService');
|
|
34
|
+
export const IMAGE_PROCESSING_TOKEN = Symbol.for('ImageProcessing');
|
|
35
|
+
export const BARCODE_GENERATOR_TOKEN = Symbol.for('BarcodeGenerator');
|
|
36
|
+
export const TEMPLATE_ENGINE_TOKEN = Symbol.for('TemplateEngine');
|
|
37
|
+
|
|
38
|
+
// 事件系统
|
|
39
|
+
export const EVENT_BUS_TOKEN = Symbol.for('EventBus');
|
|
40
|
+
|
|
41
|
+
// 插件系统
|
|
42
|
+
export const PLUGIN_MANAGER_TOKEN = Symbol.for('PluginManager');
|
|
43
|
+
|
|
44
|
+
// 性能监控
|
|
45
|
+
export const PERFORMANCE_MONITOR_TOKEN = Symbol.for('PerformanceMonitor');
|