@zwa73/utils 1.0.144 → 1.0.145

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.
@@ -0,0 +1,47 @@
1
+ import { Await, JToken, NeedInit } from "./UtilInterfaces";
2
+ export type ServiceInstance = {
3
+ /**停止实例释放内存
4
+ * @async
5
+ */
6
+ kill: () => Promise<void> | void;
7
+ };
8
+ export type ServiceConfig = {
9
+ name: string;
10
+ opt: JToken;
11
+ };
12
+ export type CtorTable = {
13
+ [Type in string]: (opt: any) => Promise<ServiceInstance> | ServiceInstance;
14
+ };
15
+ export type CtorTable2FullCfgTable<CT extends CtorTable, Cfg> = {
16
+ [P in keyof CT]: P extends string ? CT[P] extends (opt: infer Opt) => any ? Cfg & {
17
+ type: P;
18
+ opt: Opt;
19
+ } : never : never;
20
+ };
21
+ export declare class ServiceAdapter<CTT extends CtorTable, CFG extends ServiceConfig, FCT extends CtorTable2FullCfgTable<CTT, CFG> = CtorTable2FullCfgTable<CTT, CFG>> implements NeedInit {
22
+ private _cfgPath;
23
+ private _ctorTable;
24
+ inited: Promise<void>;
25
+ protected _cfgTable: Record<string, FCT[keyof FCT]>;
26
+ protected _insPool: Record<string, Await<ReturnType<CTT[keyof CTT]>>>;
27
+ protected constructor(_cfgPath: string, _ctorTable: CTT);
28
+ start<T extends keyof FCT>(type: T, opt: FCT[T]['opt'], name?: string): Promise<void>;
29
+ }
30
+ type ITest1 = {
31
+ test1(): void;
32
+ } & ServiceInstance;
33
+ type ITest2 = {
34
+ test2(): void;
35
+ } & ServiceInstance;
36
+ type TCT = {
37
+ 'Test1': (opt: {
38
+ t1: number;
39
+ }) => ITest1;
40
+ 'Test2': (opt: {
41
+ t2: string;
42
+ }) => ITest2;
43
+ };
44
+ export type CfgSchema = CtorTable2FullCfgTable<TCT, {
45
+ ssa: string;
46
+ }>;
47
+ export {};
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServiceAdapter = void 0;
4
+ const UtilFileTools_1 = require("./UtilFileTools");
5
+ const UtilFunctions_1 = require("./UtilFunctions");
6
+ class ServiceAdapter {
7
+ _cfgPath;
8
+ _ctorTable;
9
+ inited;
10
+ _cfgTable = {};
11
+ _insPool = {};
12
+ constructor(_cfgPath, _ctorTable) {
13
+ this._cfgPath = _cfgPath;
14
+ this._ctorTable = _ctorTable;
15
+ this.inited = UtilFunctions_1.UtilFunc.ivk(async () => {
16
+ this._cfgTable = await UtilFileTools_1.UtilFT.loadJSONFile(_cfgPath);
17
+ for (const name in this._cfgTable) {
18
+ const cfg = this._cfgTable[name];
19
+ //@ts-ignore
20
+ this._insPool[name] = await this._ctorTable[cfg.type](cfg.opt);
21
+ }
22
+ });
23
+ }
24
+ async start(type, opt, name = 'default') {
25
+ if (this._insPool[name] != undefined)
26
+ await this._insPool[name].kill();
27
+ //@ts-ignore
28
+ this._insPool[name] = await this._ctorTable[type](opt);
29
+ }
30
+ }
31
+ exports.ServiceAdapter = ServiceAdapter;
32
+ class TessClass extends ServiceAdapter {
33
+ }
34
+ let a = null;
35
+ a.start('Test1', { t1: 100 });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.144",
3
+ "version": "1.0.145",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -33,7 +33,7 @@
33
33
  "@types/fluent-ffmpeg": "^2.1.21",
34
34
  "@types/jest": "^29.5.12",
35
35
  "@types/node": "^18.16.3",
36
- "@zwa73/dev-utils": "^1.0.50",
36
+ "@zwa73/dev-utils": "^1.0.58",
37
37
  "jest": "^29.7.0",
38
38
  "ts-jest": "^29.1.2",
39
39
  "ts-morph": "^23.0.0",
@@ -0,0 +1,73 @@
1
+ import { UtilFT } from "./UtilFileTools";
2
+ import { UtilFunc } from "./UtilFunctions";
3
+ import { Await, JToken, NeedInit } from "./UtilInterfaces";
4
+
5
+
6
+
7
+
8
+
9
+
10
+ export type ServiceInstance = {
11
+ /**停止实例释放内存
12
+ * @async
13
+ */
14
+ kill:()=>Promise<void>|void;
15
+ }
16
+ export type ServiceConfig = {
17
+ name:string;
18
+ opt :JToken;
19
+ };
20
+ export type CtorTable = {
21
+ [Type in string]:(opt:any)=>Promise<ServiceInstance>|ServiceInstance
22
+ }
23
+ export type CtorTable2FullCfgTable<CT extends CtorTable,Cfg> = {
24
+ [P in keyof CT]: P extends string ?
25
+ CT[P] extends (opt:infer Opt)=>any
26
+ ? Cfg&{type:P,opt:Opt}
27
+ : never
28
+ : never
29
+ }
30
+ export class ServiceAdapter
31
+ <CTT extends CtorTable,
32
+ CFG extends ServiceConfig,
33
+ FCT extends CtorTable2FullCfgTable<CTT,CFG> = CtorTable2FullCfgTable<CTT,CFG>>
34
+ implements NeedInit{
35
+ inited:Promise<void>;
36
+ protected _cfgTable :Record<string,FCT[keyof FCT]>={};
37
+ protected _insPool :Record<string,Await<ReturnType<CTT[keyof CTT]>>>={};
38
+ protected constructor(private _cfgPath:string, private _ctorTable:CTT){
39
+ this.inited=UtilFunc.ivk(async ()=>{
40
+ this._cfgTable = await UtilFT.loadJSONFile(_cfgPath) as Record<string,FCT[keyof FCT]>;
41
+ for(const name in this._cfgTable){
42
+ const cfg = this._cfgTable[name];
43
+ //@ts-ignore
44
+ this._insPool[name] = await this._ctorTable[cfg.type](cfg.opt);
45
+ }
46
+ });
47
+ }
48
+ async start<T extends keyof FCT>(type:T,opt:FCT[T]['opt'],name:string='default'){
49
+ if(this._insPool[name]!=undefined)
50
+ await this._insPool[name].kill();
51
+ //@ts-ignore
52
+ this._insPool[name] = await this._ctorTable[type as keyof CTT](opt)
53
+ }
54
+ }
55
+
56
+
57
+ type ITest1 = {
58
+ test1():void;
59
+ }&ServiceInstance;
60
+ type ITest2 = {
61
+ test2():void;
62
+ }&ServiceInstance;
63
+
64
+ type TCT = {
65
+ 'Test1':(opt:{t1:number})=>ITest1;
66
+ 'Test2':(opt:{t2:string})=>ITest2;
67
+ }
68
+ class TessClass extends ServiceAdapter<TCT,{}&ServiceConfig>{
69
+ }
70
+ let a:TessClass = null as any;
71
+ a.start('Test1',{t1:100});
72
+
73
+ export type CfgSchema = CtorTable2FullCfgTable<TCT,{ssa:string}>;