@xylabs/creatable 4.12.18 → 4.12.19
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/neutral/index.mjs +35 -15
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/types/AbstractCreatable.d.ts +7 -6
- package/dist/types/AbstractCreatable.d.ts.map +1 -1
- package/dist/types/Creatable.d.ts +1 -0
- package/dist/types/Creatable.d.ts.map +1 -1
- package/package.json +8 -8
- package/src/AbstractCreatable.ts +22 -12
- package/src/Creatable.ts +4 -1
- package/src/TestCreatable.ts +10 -3
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,8 +1,26 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
|
+
if (decorator = decorators[i])
|
|
7
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
+
if (kind && result) __defProp(target, key, result);
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
|
|
1
12
|
// src/AbstractCreatable.ts
|
|
2
13
|
import { assertEx } from "@xylabs/assert";
|
|
3
14
|
import { BaseEmitter } from "@xylabs/events";
|
|
4
15
|
import { isError } from "@xylabs/typeof";
|
|
5
16
|
|
|
17
|
+
// src/Creatable.ts
|
|
18
|
+
function creatable() {
|
|
19
|
+
return (constructor) => {
|
|
20
|
+
constructor;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
6
24
|
// src/lib/getFunctionName.ts
|
|
7
25
|
import { isNumber } from "@xylabs/typeof";
|
|
8
26
|
function getFunctionName(depth = 2) {
|
|
@@ -32,21 +50,22 @@ function getRootFunction(obj, funcName) {
|
|
|
32
50
|
// src/AbstractCreatable.ts
|
|
33
51
|
var AbstractCreatable = class extends BaseEmitter {
|
|
34
52
|
defaultLogger;
|
|
35
|
-
|
|
36
|
-
_populatedParams;
|
|
53
|
+
_validatedParams;
|
|
37
54
|
constructor(params) {
|
|
38
55
|
super(params);
|
|
39
|
-
|
|
56
|
+
}
|
|
57
|
+
get name() {
|
|
58
|
+
return this.params.name;
|
|
40
59
|
}
|
|
41
60
|
get params() {
|
|
42
|
-
this.
|
|
43
|
-
|
|
44
|
-
return this._populatedParams;
|
|
61
|
+
this._validatedParams = this._validatedParams ?? this.paramsValidator(super.params);
|
|
62
|
+
return this._validatedParams;
|
|
45
63
|
}
|
|
46
64
|
get statusReporter() {
|
|
47
65
|
return this.params.statusReporter;
|
|
48
66
|
}
|
|
49
|
-
static async create(
|
|
67
|
+
static async create(inParams = {}) {
|
|
68
|
+
const params = { ...await this.defaultParams(), ...inParams };
|
|
50
69
|
const name = params.name ?? this.name;
|
|
51
70
|
params.statusReporter?.report(name, "creating");
|
|
52
71
|
try {
|
|
@@ -60,12 +79,17 @@ var AbstractCreatable = class extends BaseEmitter {
|
|
|
60
79
|
throw new Error(`Error creating: ${name}`);
|
|
61
80
|
}
|
|
62
81
|
}
|
|
63
|
-
/* override this for initialization of instance */
|
|
64
82
|
static createHandler(instance) {
|
|
65
83
|
return instance;
|
|
66
84
|
}
|
|
85
|
+
static defaultParams() {
|
|
86
|
+
return {};
|
|
87
|
+
}
|
|
67
88
|
createHandler() {
|
|
68
89
|
}
|
|
90
|
+
paramsValidator(params) {
|
|
91
|
+
return { ...params, name: params.name ?? this.constructor.name };
|
|
92
|
+
}
|
|
69
93
|
async start() {
|
|
70
94
|
this._noOverride("start");
|
|
71
95
|
try {
|
|
@@ -100,13 +124,9 @@ var AbstractCreatable = class extends BaseEmitter {
|
|
|
100
124
|
stopHandler() {
|
|
101
125
|
}
|
|
102
126
|
};
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return (constructor) => {
|
|
107
|
-
constructor;
|
|
108
|
-
};
|
|
109
|
-
}
|
|
127
|
+
AbstractCreatable = __decorateClass([
|
|
128
|
+
creatable()
|
|
129
|
+
], AbstractCreatable);
|
|
110
130
|
export {
|
|
111
131
|
AbstractCreatable,
|
|
112
132
|
creatable
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/AbstractCreatable.ts","../../src/
|
|
1
|
+
{"version":3,"sources":["../../src/AbstractCreatable.ts","../../src/Creatable.ts","../../src/lib/getFunctionName.ts","../../src/lib/getRootFunction.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport type { EventData } from '@xylabs/events'\nimport { BaseEmitter } from '@xylabs/events'\nimport { type Logger } from '@xylabs/logger'\nimport type { Promisable } from '@xylabs/promise'\nimport { isError } from '@xylabs/typeof'\n\nimport { type Creatable, creatable } from './Creatable.ts'\nimport { getFunctionName, getRootFunction } from './lib/index.ts'\nimport type {\n CreatableInstance, CreatableName, CreatableParams,\n} from './model/index.ts'\n\n@creatable()\nexport class AbstractCreatable<TParams extends CreatableParams = CreatableParams,\n TEventData extends EventData = EventData> extends BaseEmitter<Partial<TParams>, TEventData> {\n defaultLogger?: Logger\n\n private _validatedParams?: TParams\n\n constructor(params: Partial<TParams>) {\n super(params)\n }\n\n get name(): CreatableName {\n return this.params.name\n }\n\n override get params(): TParams {\n this._validatedParams = this._validatedParams ?? this.paramsValidator(super.params)\n return this._validatedParams\n }\n\n get statusReporter() {\n return this.params.statusReporter\n }\n\n static async create<T extends CreatableInstance>(\n this: Creatable<T>,\n inParams: Partial<T['params']> = {},\n ): Promise<T> {\n const params: Partial<T['params']> = { ...(await this.defaultParams()), ...inParams }\n const name: CreatableName = params.name ?? this.name\n params.statusReporter?.report(name, 'creating')\n try {\n const instance = new this(params)\n const initializedInstance = await this.createHandler(instance)\n await instance.createHandler()\n params.statusReporter?.report(name, 'created')\n return initializedInstance\n } catch (ex) {\n params.statusReporter?.report(name, 'error', isError(ex) ? ex : new Error(`Error creating: ${name}`))\n throw new Error(`Error creating: ${name}`)\n }\n }\n\n static createHandler<T extends CreatableInstance>(\n this: Creatable<T>,\n instance: T,\n ): Promisable<T> {\n return instance\n }\n\n static defaultParams<T extends CreatableInstance>(\n this: Creatable<T>,\n ): Promisable<Partial<T['params']>> {\n return {}\n }\n\n createHandler(): Promisable<void> {}\n\n paramsValidator(params: Partial<TParams>): TParams {\n return { ...params, name: params.name ?? this.constructor.name } as TParams\n }\n\n async start(): Promise<boolean> {\n this._noOverride('start')\n try {\n this.statusReporter?.report(this.name, 'starting')\n await this.startHandler()\n this.statusReporter?.report(this.name, 'started')\n return true\n } catch (ex) {\n this.statusReporter?.report(this.name, 'error', isError(ex) ? ex : new Error(`Error stopping: ${ex}`))\n return false\n }\n }\n\n async stop(): Promise<boolean> {\n this._noOverride('stop')\n try {\n this.statusReporter?.report(this.name, 'stopping')\n await this.stopHandler()\n this.statusReporter?.report(this.name, 'stopped')\n return true\n } catch (ex) {\n this.statusReporter?.report(this.name, 'error', isError(ex) ? ex : new Error(`Error stopping: ${ex}`))\n return false\n }\n }\n\n protected _noOverride(functionName = getFunctionName(3)) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const thisFunc = (this as any)[functionName]\n\n const rootFunc = getRootFunction(this, functionName)\n assertEx(thisFunc === rootFunc, () => `Override not allowed for [${functionName}] - override ${functionName}Handler instead`)\n }\n\n protected startHandler(): Promisable<void> {\n // when overriding this, throw an error on failure\n }\n\n protected stopHandler(): Promisable<void> {\n // when overriding this, throw an error on failure\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type { Promisable } from '@xylabs/promise'\n\nimport type { AbstractCreatable } from './AbstractCreatable.ts'\nimport type { CreatableInstance, CreatableParams } from './model/index.ts'\n\n/*\nexport interface CreatableFactory<T extends EmptyObject | void = void,\n TParams extends EmptyObject | void = void> {\n create(params?: CreatableParams<TParams>): Promise<CreatableInstance<T>>\n}\n*/\n\nexport interface Creatable<T extends CreatableInstance = CreatableInstance> {\n\n defaultLogger?: Logger\n\n new(params: Partial<T['params']>): T & AbstractCreatable<T['params']>\n\n create<T extends CreatableInstance>(\n this: Creatable<T>,\n params: Partial<T['params']>): Promise<T>\n\n createHandler<T extends CreatableInstance>(\n this: Creatable<T>,\n instance: T\n ): Promisable<T>\n\n defaultParams<T extends CreatableInstance>(\n this: Creatable<T>): Promisable<Partial<T['params']>>\n\n /*\n factory<T extends EmptyObject | void = void,\n TParams extends EmptyObject | void = void> (this: Creatable<T, TParams>, params: Partial<CreatableParams<TParams>>): CreatableFactory<T, TParams>\n */\n}\n\n/**\n * Class annotation to be used to decorate Modules which support\n * an asynchronous creation pattern\n * @returns The decorated Module requiring it implement the members\n * of the CreatableModule as statics properties/methods\n */\nexport function creatable<T extends EmptyObject | void = void, TParams extends CreatableParams = CreatableParams>() {\n return <U extends Creatable<CreatableInstance<T, TParams>>>(constructor: U) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-expressions\n constructor\n }\n}\n\n/**\n * Class annotation to be used to decorate Modules which support\n * an asynchronous creation factory pattern\n * @returns The decorated Module requiring it implement the members\n * of the CreatableModule as statics properties/methods\n */\n\n/*\nexport function creatableFactory() {\n return <U extends CreatableFactory>(constructor: U) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-expressions\n constructor\n }\n}\n*/\n","import { isNumber } from '@xylabs/typeof'\n\nexport function getFunctionName(depth = 2) {\n const error = new Error('Stack')\n let newIndex: number | undefined\n const stackParts = error.stack?.split('\\n')[depth]?.split(' ')\n const funcName\n = stackParts?.find((item, index) => {\n if (item.length > 0 && item !== 'at') {\n // check if constructor\n if (item === 'new') {\n newIndex = index\n }\n return true\n }\n }) ?? '<unknown>'\n return isNumber(newIndex) ? `${funcName} ${stackParts?.[newIndex + 1]}` : funcName\n}\n","export function getRootFunction(obj: unknown, funcName: string) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let anyObj = obj as any\n while (anyObj.__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WAAK,gBAAgB,OAAO,KAAK,MAAM,SAAS;AAChD,aAAO;AAAA,IACT,SAAS,IAAI;AACX,WAAK,gBAAgB,OAAO,KAAK,MAAM,SAAS,QAAQ,EAAE,IAAI,KAAK,IAAI,MAAM,mBAAmB,EAAE,EAAE,CAAC;AACrG,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAEU,YAAY,eAAe,gBAAgB,CAAC,GAAG;AAEvD,UAAM,WAAY,KAAa,YAAY;AAE3C,UAAM,WAAW,gBAAgB,MAAM,YAAY;AACnD,aAAS,aAAa,UAAU,MAAM,6BAA6B,YAAY,gBAAgB,YAAY,iBAAiB;AAAA,EAC9H;AAAA,EAEU,eAAiC;AAAA,EAE3C;AAAA,EAEU,cAAgC;AAAA,EAE1C;AACF;AAtGa,oBAAN;AAAA,EADN,UAAU;AAAA,GACE;","names":[]}
|
|
@@ -4,21 +4,22 @@ import { type Logger } from '@xylabs/logger';
|
|
|
4
4
|
import type { Promisable } from '@xylabs/promise';
|
|
5
5
|
import { type Creatable } from './Creatable.ts';
|
|
6
6
|
import type { CreatableInstance, CreatableName, CreatableParams } from './model/index.ts';
|
|
7
|
-
export declare
|
|
7
|
+
export declare class AbstractCreatable<TParams extends CreatableParams = CreatableParams, TEventData extends EventData = EventData> extends BaseEmitter<Partial<TParams>, TEventData> {
|
|
8
8
|
defaultLogger?: Logger;
|
|
9
|
-
|
|
10
|
-
private _populatedParams;
|
|
9
|
+
private _validatedParams?;
|
|
11
10
|
constructor(params: Partial<TParams>);
|
|
12
|
-
get
|
|
11
|
+
get name(): CreatableName;
|
|
12
|
+
get params(): TParams;
|
|
13
13
|
get statusReporter(): import("./model/CreatableStatusReporter.ts").CreatableStatusReporter | undefined;
|
|
14
|
-
static create<T extends CreatableInstance>(this: Creatable<T>,
|
|
14
|
+
static create<T extends CreatableInstance>(this: Creatable<T>, inParams?: Partial<T['params']>): Promise<T>;
|
|
15
15
|
static createHandler<T extends CreatableInstance>(this: Creatable<T>, instance: T): Promisable<T>;
|
|
16
|
+
static defaultParams<T extends CreatableInstance>(this: Creatable<T>): Promisable<Partial<T['params']>>;
|
|
16
17
|
createHandler(): Promisable<void>;
|
|
18
|
+
paramsValidator(params: Partial<TParams>): TParams;
|
|
17
19
|
start(): Promise<boolean>;
|
|
18
20
|
stop(): Promise<boolean>;
|
|
19
21
|
protected _noOverride(functionName?: string): void;
|
|
20
22
|
protected startHandler(): Promisable<void>;
|
|
21
23
|
protected stopHandler(): Promisable<void>;
|
|
22
|
-
protected abstract paramsHandler(params?: Partial<CreatableParams<TParams>>): CreatableParams<TParams>;
|
|
23
24
|
}
|
|
24
25
|
//# sourceMappingURL=AbstractCreatable.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractCreatable.d.ts","sourceRoot":"","sources":["../../src/AbstractCreatable.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAGjD,OAAO,EAAE,KAAK,SAAS,EAAa,MAAM,gBAAgB,CAAA;AAE1D,OAAO,KAAK,EACV,iBAAiB,EAAE,aAAa,EAAE,eAAe,EAClD,MAAM,kBAAkB,CAAA;AAEzB,
|
|
1
|
+
{"version":3,"file":"AbstractCreatable.d.ts","sourceRoot":"","sources":["../../src/AbstractCreatable.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAGjD,OA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SAAqB;IAQvD,SAAS,CAAC,YAAY,IAAI,UAAU,CAAC,IAAI,CAAC;IAI1C,SAAS,CAAC,WAAW,IAAI,UAAU,CAAC,IAAI,CAAC;CAG1C"}
|
|
@@ -8,6 +8,7 @@ export interface Creatable<T extends CreatableInstance = CreatableInstance> {
|
|
|
8
8
|
new (params: Partial<T['params']>): T & AbstractCreatable<T['params']>;
|
|
9
9
|
create<T extends CreatableInstance>(this: Creatable<T>, params: Partial<T['params']>): Promise<T>;
|
|
10
10
|
createHandler<T extends CreatableInstance>(this: Creatable<T>, instance: T): Promisable<T>;
|
|
11
|
+
defaultParams<T extends CreatableInstance>(this: Creatable<T>): Promisable<Partial<T['params']>>;
|
|
11
12
|
}
|
|
12
13
|
/**
|
|
13
14
|
* Class annotation to be used to decorate Modules which support
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Creatable.d.ts","sourceRoot":"","sources":["../../src/Creatable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAS1E,MAAM,WAAW,SAAS,CAAC,CAAC,SAAS,iBAAiB,GAAG,iBAAiB;IAExE,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB,
|
|
1
|
+
{"version":3,"file":"Creatable.d.ts","sourceRoot":"","sources":["../../src/Creatable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,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
|
|
3
|
+
"version": "4.12.19",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -29,13 +29,13 @@
|
|
|
29
29
|
"module": "dist/neutral/index.mjs",
|
|
30
30
|
"types": "dist/types/index.d.ts",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xylabs/assert": "^4.12.
|
|
33
|
-
"@xylabs/base": "^4.12.
|
|
34
|
-
"@xylabs/events": "^4.12.
|
|
35
|
-
"@xylabs/logger": "^4.12.
|
|
36
|
-
"@xylabs/object": "^4.12.
|
|
37
|
-
"@xylabs/promise": "^4.12.
|
|
38
|
-
"@xylabs/typeof": "^4.12.
|
|
32
|
+
"@xylabs/assert": "^4.12.19",
|
|
33
|
+
"@xylabs/base": "^4.12.19",
|
|
34
|
+
"@xylabs/events": "^4.12.19",
|
|
35
|
+
"@xylabs/logger": "^4.12.19",
|
|
36
|
+
"@xylabs/object": "^4.12.19",
|
|
37
|
+
"@xylabs/promise": "^4.12.19",
|
|
38
|
+
"@xylabs/typeof": "^4.12.19"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@xylabs/ts-scripts-yarn3": "^6.5.9",
|
package/src/AbstractCreatable.ts
CHANGED
|
@@ -11,22 +11,24 @@ import type {
|
|
|
11
11
|
CreatableInstance, CreatableName, CreatableParams,
|
|
12
12
|
} from './model/index.ts'
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
@creatable()
|
|
15
|
+
export class AbstractCreatable<TParams extends CreatableParams = CreatableParams,
|
|
15
16
|
TEventData extends EventData = EventData> extends BaseEmitter<Partial<TParams>, TEventData> {
|
|
16
17
|
defaultLogger?: Logger
|
|
17
|
-
name: CreatableName
|
|
18
18
|
|
|
19
|
-
private
|
|
19
|
+
private _validatedParams?: TParams
|
|
20
20
|
|
|
21
21
|
constructor(params: Partial<TParams>) {
|
|
22
22
|
super(params)
|
|
23
|
-
this.name = params.name ?? this.constructor.name as CreatableName
|
|
24
23
|
}
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
this.
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
get name(): CreatableName {
|
|
26
|
+
return this.params.name
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
override get params(): TParams {
|
|
30
|
+
this._validatedParams = this._validatedParams ?? this.paramsValidator(super.params)
|
|
31
|
+
return this._validatedParams
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
get statusReporter() {
|
|
@@ -35,8 +37,9 @@ export abstract class AbstractCreatable<TParams extends CreatableParams = Creata
|
|
|
35
37
|
|
|
36
38
|
static async create<T extends CreatableInstance>(
|
|
37
39
|
this: Creatable<T>,
|
|
38
|
-
|
|
40
|
+
inParams: Partial<T['params']> = {},
|
|
39
41
|
): Promise<T> {
|
|
42
|
+
const params: Partial<T['params']> = { ...(await this.defaultParams()), ...inParams }
|
|
40
43
|
const name: CreatableName = params.name ?? this.name
|
|
41
44
|
params.statusReporter?.report(name, 'creating')
|
|
42
45
|
try {
|
|
@@ -51,7 +54,6 @@ export abstract class AbstractCreatable<TParams extends CreatableParams = Creata
|
|
|
51
54
|
}
|
|
52
55
|
}
|
|
53
56
|
|
|
54
|
-
/* override this for initialization of instance */
|
|
55
57
|
static createHandler<T extends CreatableInstance>(
|
|
56
58
|
this: Creatable<T>,
|
|
57
59
|
instance: T,
|
|
@@ -59,8 +61,18 @@ export abstract class AbstractCreatable<TParams extends CreatableParams = Creata
|
|
|
59
61
|
return instance
|
|
60
62
|
}
|
|
61
63
|
|
|
64
|
+
static defaultParams<T extends CreatableInstance>(
|
|
65
|
+
this: Creatable<T>,
|
|
66
|
+
): Promisable<Partial<T['params']>> {
|
|
67
|
+
return {}
|
|
68
|
+
}
|
|
69
|
+
|
|
62
70
|
createHandler(): Promisable<void> {}
|
|
63
71
|
|
|
72
|
+
paramsValidator(params: Partial<TParams>): TParams {
|
|
73
|
+
return { ...params, name: params.name ?? this.constructor.name } as TParams
|
|
74
|
+
}
|
|
75
|
+
|
|
64
76
|
async start(): Promise<boolean> {
|
|
65
77
|
this._noOverride('start')
|
|
66
78
|
try {
|
|
@@ -102,6 +114,4 @@ export abstract class AbstractCreatable<TParams extends CreatableParams = Creata
|
|
|
102
114
|
protected stopHandler(): Promisable<void> {
|
|
103
115
|
// when overriding this, throw an error on failure
|
|
104
116
|
}
|
|
105
|
-
|
|
106
|
-
protected abstract paramsHandler(params?: Partial<CreatableParams<TParams>>): CreatableParams<TParams>
|
|
107
117
|
}
|
package/src/Creatable.ts
CHANGED
|
@@ -16,7 +16,7 @@ export interface Creatable<T extends CreatableInstance = CreatableInstance> {
|
|
|
16
16
|
|
|
17
17
|
defaultLogger?: Logger
|
|
18
18
|
|
|
19
|
-
new
|
|
19
|
+
new(params: Partial<T['params']>): T & AbstractCreatable<T['params']>
|
|
20
20
|
|
|
21
21
|
create<T extends CreatableInstance>(
|
|
22
22
|
this: Creatable<T>,
|
|
@@ -27,6 +27,9 @@ export interface Creatable<T extends CreatableInstance = CreatableInstance> {
|
|
|
27
27
|
instance: T
|
|
28
28
|
): Promisable<T>
|
|
29
29
|
|
|
30
|
+
defaultParams<T extends CreatableInstance>(
|
|
31
|
+
this: Creatable<T>): Promisable<Partial<T['params']>>
|
|
32
|
+
|
|
30
33
|
/*
|
|
31
34
|
factory<T extends EmptyObject | void = void,
|
|
32
35
|
TParams extends EmptyObject | void = void> (this: Creatable<T, TParams>, params: Partial<CreatableParams<TParams>>): CreatableFactory<T, TParams>
|
package/src/TestCreatable.ts
CHANGED
|
@@ -4,12 +4,19 @@ import { AbstractCreatable } from './AbstractCreatable.ts'
|
|
|
4
4
|
import { creatable } from './Creatable.ts'
|
|
5
5
|
import type { CreatableParams } from './model/index.ts'
|
|
6
6
|
|
|
7
|
+
interface TestCreatableParams extends CreatableParams {
|
|
8
|
+
testParam: string
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
// This is here to check types
|
|
8
12
|
@creatable()
|
|
9
13
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
10
|
-
class TestCreatable<TParams extends
|
|
14
|
+
class TestCreatable<TParams extends TestCreatableParams = TestCreatableParams, TEventData extends EventData = EventData>
|
|
11
15
|
extends AbstractCreatable<TParams, TEventData> {
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
override paramsValidator(params: Partial<TParams> = {}): TParams {
|
|
17
|
+
const result: TestCreatableParams = {
|
|
18
|
+
...params, name: params.name ?? this.constructor.name, testParam: 'yo',
|
|
19
|
+
}
|
|
20
|
+
return result as TParams
|
|
14
21
|
}
|
|
15
22
|
}
|