@xylabs/creatable 4.12.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/LICENSE +840 -0
- package/README.md +69 -0
- package/dist/neutral/index.mjs +70 -0
- package/dist/neutral/index.mjs.map +1 -0
- package/dist/types/AbstractCreatable.d.ts +30 -0
- package/dist/types/AbstractCreatable.d.ts.map +1 -0
- package/dist/types/Creatable.d.ts +29 -0
- package/dist/types/Creatable.d.ts.map +1 -0
- package/dist/types/getFunctionName.d.ts +2 -0
- package/dist/types/getFunctionName.d.ts.map +1 -0
- package/dist/types/getRootFunction.d.ts +2 -0
- package/dist/types/getRootFunction.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +49 -0
- package/src/AbstractCreatable.ts +71 -0
- package/src/Creatable.ts +67 -0
- package/src/getFunctionName.ts +18 -0
- package/src/getRootFunction.ts +8 -0
- package/src/index.ts +2 -0
- package/typedoc.json +5 -0
- package/xy.config.ts +10 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[![logo][]](https://xylabs.com)
|
|
2
|
+
|
|
3
|
+
# @xylabs/base
|
|
4
|
+
|
|
5
|
+
[![npm-badge][]][npm-link]
|
|
6
|
+
[![npm-downloads-badge][]][npm-link]
|
|
7
|
+
[![jsdelivr-badge][]][jsdelivr-link]
|
|
8
|
+
[![npm-license-badge][]](LICENSE)
|
|
9
|
+
[![socket-badge][]][socket-link]
|
|
10
|
+
|
|
11
|
+
> XY Labs generalized Javascript library
|
|
12
|
+
|
|
13
|
+
## Table of Contents
|
|
14
|
+
|
|
15
|
+
- [Description](#description)
|
|
16
|
+
- [Install](#install)
|
|
17
|
+
- [Maintainers](#maintainers)
|
|
18
|
+
- [License](#license)
|
|
19
|
+
- [Credits](#credits)
|
|
20
|
+
|
|
21
|
+
## Description
|
|
22
|
+
|
|
23
|
+
Common Javascript code that is used throughout XYO projects that use React.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
Using npm:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npm i --save @xylabs/base
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Using yarn:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
yarn add @xylabs/base
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Documentation
|
|
40
|
+
[Developer Reference](https://xylabs.github.io/sdk-js)
|
|
41
|
+
|
|
42
|
+
## Maintainers
|
|
43
|
+
|
|
44
|
+
- [Arie Trouw](https://github.com/arietrouw) ([arietrouw.com](https://arietrouw.com))
|
|
45
|
+
- [Joel Carter](https://github.com/JoelBCarter)
|
|
46
|
+
- [Matt Jones](https://github.com/jonesmac)
|
|
47
|
+
- [Jordan Trouw](https://github.com/jordantrouw)
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
See the [LICENSE](LICENSE) file for license details
|
|
52
|
+
|
|
53
|
+
## Credits
|
|
54
|
+
|
|
55
|
+
[Made with 🔥and ❄️ by XY Labs](https://xylabs.com)
|
|
56
|
+
|
|
57
|
+
[logo]: https://cdn.xy.company/img/brand/XYPersistentCompany_Logo_Icon_Colored.svg
|
|
58
|
+
|
|
59
|
+
[npm-badge]: https://img.shields.io/npm/v/@xylabs/base.svg
|
|
60
|
+
[npm-link]: https://www.npmjs.com/package/@xylabs/base
|
|
61
|
+
|
|
62
|
+
[npm-downloads-badge]: https://img.shields.io/npm/dw/@xylabs/base
|
|
63
|
+
[npm-license-badge]: https://img.shields.io/npm/l/@xylabs/base
|
|
64
|
+
|
|
65
|
+
[jsdelivr-badge]: https://data.jsdelivr.com/v1/package/npm/@xylabs/base/badge
|
|
66
|
+
[jsdelivr-link]: https://www.jsdelivr.com/package/npm/@xylabs/base
|
|
67
|
+
|
|
68
|
+
[socket-badge]: https://socket.dev/api/badge/npm/package/@xylabs/base
|
|
69
|
+
[socket-link]: https://socket.dev/npm/package/@xylabs/base
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// src/AbstractCreatable.ts
|
|
2
|
+
import { assertEx } from "@xylabs/assert";
|
|
3
|
+
import { BaseEmitter } from "@xylabs/events";
|
|
4
|
+
|
|
5
|
+
// src/getFunctionName.ts
|
|
6
|
+
import { isNumber } from "@xylabs/typeof";
|
|
7
|
+
function getFunctionName(depth = 2) {
|
|
8
|
+
const error = new Error("Stack");
|
|
9
|
+
let newIndex;
|
|
10
|
+
const stackParts = error.stack?.split("\n")[depth]?.split(" ");
|
|
11
|
+
const funcName = stackParts?.find((item, index) => {
|
|
12
|
+
if (item.length > 0 && item !== "at") {
|
|
13
|
+
if (item === "new") {
|
|
14
|
+
newIndex = index;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
}) ?? "<unknown>";
|
|
19
|
+
return isNumber(newIndex) ? `${funcName} ${stackParts?.[newIndex + 1]}` : funcName;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/getRootFunction.ts
|
|
23
|
+
function getRootFunction(obj, funcName) {
|
|
24
|
+
let anyObj = obj;
|
|
25
|
+
while (anyObj.__proto__?.[funcName]) {
|
|
26
|
+
anyObj = anyObj.__proto__;
|
|
27
|
+
}
|
|
28
|
+
return anyObj[funcName];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/AbstractCreatable.ts
|
|
32
|
+
var AbstractCreatable = class extends BaseEmitter {
|
|
33
|
+
defaultLogger;
|
|
34
|
+
name;
|
|
35
|
+
_populatedParams;
|
|
36
|
+
constructor(params) {
|
|
37
|
+
super(params);
|
|
38
|
+
this.name = params.name ?? this.constructor.name;
|
|
39
|
+
}
|
|
40
|
+
get params() {
|
|
41
|
+
this._noOverride();
|
|
42
|
+
this._populatedParams = this._populatedParams ?? this.paramsHandler(super.params);
|
|
43
|
+
return this._populatedParams;
|
|
44
|
+
}
|
|
45
|
+
static async create(params) {
|
|
46
|
+
const instance = new this(params);
|
|
47
|
+
await instance.createHandler();
|
|
48
|
+
return await Promise.resolve(instance);
|
|
49
|
+
}
|
|
50
|
+
createHandler() {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
_noOverride(functionName = getFunctionName(3)) {
|
|
54
|
+
const thisFunc = this[functionName];
|
|
55
|
+
const rootFunc = getRootFunction(this, functionName);
|
|
56
|
+
assertEx(thisFunc === rootFunc, () => `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/Creatable.ts
|
|
61
|
+
function creatable() {
|
|
62
|
+
return (constructor) => {
|
|
63
|
+
constructor;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
AbstractCreatable,
|
|
68
|
+
creatable
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/AbstractCreatable.ts","../../src/getFunctionName.ts","../../src/getRootFunction.ts","../../src/Creatable.ts"],"sourcesContent":["import type { MeterProvider, TracerProvider } from '@opentelemetry/api'\nimport { assertEx } from '@xylabs/assert'\nimport type { BaseClassName, BaseParams } from '@xylabs/base'\nimport { BaseEmitter } from '@xylabs/events'\nimport { type Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type { Promisable } from '@xylabs/promise'\n\nimport type {\n Creatable, CreatableInstance,\n CreatableName,\n} from './Creatable.ts'\nimport { getFunctionName } from './getFunctionName.ts'\nimport { getRootFunction } from './getRootFunction.ts'\n\nexport type CreatableStatus = 'creating' | 'created' | 'starting' | 'started' | 'stopping' | 'stopped'\n\nexport interface StatusReporter {\n report: (name: BaseClassName, status: CreatableStatus, progress?: number) => void\n}\n\nexport type CreatableParamsFields = {\n logger?: Logger\n meterProvider?: MeterProvider\n name?: BaseClassName\n traceProvider?: TracerProvider\n}\n\nexport type CreatableParams<TAdditionalParams extends EmptyObject | void = void> =\n BaseParams<TAdditionalParams extends EmptyObject ? CreatableParamsFields & TAdditionalParams : CreatableParamsFields>\n\nexport abstract class AbstractCreatable<TParams extends EmptyObject | void = void> extends BaseEmitter<Partial<CreatableParams<TParams>>> {\n defaultLogger?: Logger\n name: CreatableName\n\n private _populatedParams: CreatableParams<TParams> | undefined\n\n constructor(params: Partial<CreatableParams<TParams>>) {\n super(params)\n this.name = params.name ?? this.constructor.name as CreatableName\n }\n\n override get params(): CreatableParams<TParams> {\n this._noOverride()\n this._populatedParams = this._populatedParams ?? this.paramsHandler(super.params)\n return this._populatedParams\n }\n\n static async create<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void>(\n this: Creatable<T, TParams>,\n params: Partial<CreatableParams<TParams>>,\n ): Promise<CreatableInstance<T>> {\n const instance = new this(params)\n await instance.createHandler()\n return await Promise.resolve(instance as CreatableInstance<T>)\n }\n\n createHandler(): Promisable<boolean> {\n return true\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 abstract paramsHandler(params?: Partial<CreatableParams<TParams>>): CreatableParams<TParams>\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.__proto__?.[funcName]) {\n anyObj = anyObj.__proto__\n }\n return anyObj[funcName]\n}\n","import type { BaseClassName } from '@xylabs/base'\nimport type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\n\nimport type { AbstractCreatable, CreatableParams } from './AbstractCreatable.ts'\n\nexport type CreatableName = Exclude<string, 'creatable-name-reserved-32546239486'> & BaseClassName\n\nexport interface CreatableInstanceFields<TParams extends EmptyObject | void = void> {\n name: CreatableName\n params: CreatableParams<TParams>\n}\n\nexport type CreatableInstance<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void>\n = T extends EmptyObject ? T & CreatableInstanceFields<TParams> : CreatableInstanceFields<TParams>\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 EmptyObject | void = void, TParams extends EmptyObject | void = void> {\n\n defaultLogger?: Logger\n\n new (params: Partial<CreatableParams<TParams>>): CreatableInstance<T> & AbstractCreatable<TParams>\n\n create<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void>(\n this: Creatable<T, TParams>,\n params: Partial<CreatableParams<TParams>>): Promise<CreatableInstance<T>>\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<TInstance extends EmptyObject | void = void, TParams extends EmptyObject | void = void>() {\n return <U extends Creatable<TInstance, 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"],"mappings":";AACA,SAAS,gBAAgB;AAEzB,SAAS,mBAAmB;;;ACH5B,SAAS,gBAAgB;AAElB,SAAS,gBAAgB,QAAQ,GAAG;AACzC,QAAM,QAAQ,IAAI,MAAM,OAAO;AAC/B,MAAI;AACJ,QAAM,aAAa,MAAM,OAAO,MAAM,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG;AAC7D,QAAM,WACE,YAAY,KAAK,CAAC,MAAM,UAAU;AAClC,QAAI,KAAK,SAAS,KAAK,SAAS,MAAM;AAEpC,UAAI,SAAS,OAAO;AAClB,mBAAW;AAAA,MACb;AACA,aAAO;AAAA,IACT;AAAA,EACF,CAAC,KAAK;AACZ,SAAO,SAAS,QAAQ,IAAI,GAAG,QAAQ,IAAI,aAAa,WAAW,CAAC,CAAC,KAAK;AAC5E;;;ACjBO,SAAS,gBAAgB,KAAc,UAAkB;AAE9D,MAAI,SAAS;AACb,SAAO,OAAO,YAAY,QAAQ,GAAG;AACnC,aAAS,OAAO;AAAA,EAClB;AACA,SAAO,OAAO,QAAQ;AACxB;;;AFwBO,IAAe,oBAAf,cAAoF,YAA+C;AAAA,EACxI;AAAA,EACA;AAAA,EAEQ;AAAA,EAER,YAAY,QAA2C;AACrD,UAAM,MAAM;AACZ,SAAK,OAAO,OAAO,QAAQ,KAAK,YAAY;AAAA,EAC9C;AAAA,EAEA,IAAa,SAAmC;AAC9C,SAAK,YAAY;AACjB,SAAK,mBAAmB,KAAK,oBAAoB,KAAK,cAAc,MAAM,MAAM;AAChF,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,aAAa,OAEX,QAC+B;AAC/B,UAAM,WAAW,IAAI,KAAK,MAAM;AAChC,UAAM,SAAS,cAAc;AAC7B,WAAO,MAAM,QAAQ,QAAQ,QAAgC;AAAA,EAC/D;AAAA,EAEA,gBAAqC;AACnC,WAAO;AAAA,EACT;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;AAGF;;;AGzBO,SAAS,YAAoG;AAClH,SAAO,CAA0C,gBAAmB;AAElE;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { MeterProvider, TracerProvider } from '@opentelemetry/api';
|
|
2
|
+
import type { BaseClassName, BaseParams } from '@xylabs/base';
|
|
3
|
+
import { BaseEmitter } from '@xylabs/events';
|
|
4
|
+
import { type Logger } from '@xylabs/logger';
|
|
5
|
+
import type { EmptyObject } from '@xylabs/object';
|
|
6
|
+
import type { Promisable } from '@xylabs/promise';
|
|
7
|
+
import type { Creatable, CreatableInstance, CreatableName } from './Creatable.ts';
|
|
8
|
+
export type CreatableStatus = 'creating' | 'created' | 'starting' | 'started' | 'stopping' | 'stopped';
|
|
9
|
+
export interface StatusReporter {
|
|
10
|
+
report: (name: BaseClassName, status: CreatableStatus, progress?: number) => void;
|
|
11
|
+
}
|
|
12
|
+
export type CreatableParamsFields = {
|
|
13
|
+
logger?: Logger;
|
|
14
|
+
meterProvider?: MeterProvider;
|
|
15
|
+
name?: BaseClassName;
|
|
16
|
+
traceProvider?: TracerProvider;
|
|
17
|
+
};
|
|
18
|
+
export type CreatableParams<TAdditionalParams extends EmptyObject | void = void> = BaseParams<TAdditionalParams extends EmptyObject ? CreatableParamsFields & TAdditionalParams : CreatableParamsFields>;
|
|
19
|
+
export declare abstract class AbstractCreatable<TParams extends EmptyObject | void = void> extends BaseEmitter<Partial<CreatableParams<TParams>>> {
|
|
20
|
+
defaultLogger?: Logger;
|
|
21
|
+
name: CreatableName;
|
|
22
|
+
private _populatedParams;
|
|
23
|
+
constructor(params: Partial<CreatableParams<TParams>>);
|
|
24
|
+
get params(): CreatableParams<TParams>;
|
|
25
|
+
static create<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void>(this: Creatable<T, TParams>, params: Partial<CreatableParams<TParams>>): Promise<CreatableInstance<T>>;
|
|
26
|
+
createHandler(): Promisable<boolean>;
|
|
27
|
+
protected _noOverride(functionName?: string): void;
|
|
28
|
+
protected abstract paramsHandler(params?: Partial<CreatableParams<TParams>>): CreatableParams<TParams>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=AbstractCreatable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractCreatable.d.ts","sourceRoot":"","sources":["../../src/AbstractCreatable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAEvE,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,KAAK,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,EACV,SAAS,EAAE,iBAAiB,EAC5B,aAAa,EACd,MAAM,gBAAgB,CAAA;AAIvB,MAAM,MAAM,eAAe,GAAG,UAAU,GAAG,SAAS,GAAG,UAAU,GAAG,SAAS,GAAG,UAAU,GAAG,SAAS,CAAA;AAEtG,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,CAAC,IAAI,EAAE,aAAa,EAAE,MAAM,EAAE,eAAe,EAAE,QAAQ,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;CAClF;AAED,MAAM,MAAM,qBAAqB,GAAG;IAClC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf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import type { Logger } from '@xylabs/logger';
|
|
3
|
+
import type { EmptyObject } from '@xylabs/object';
|
|
4
|
+
import type { AbstractCreatable, CreatableParams } from './AbstractCreatable.ts';
|
|
5
|
+
export type CreatableName = Exclude<string, 'creatable-name-reserved-32546239486'> & BaseClassName;
|
|
6
|
+
export interface CreatableInstanceFields<TParams extends EmptyObject | void = void> {
|
|
7
|
+
name: CreatableName;
|
|
8
|
+
params: CreatableParams<TParams>;
|
|
9
|
+
}
|
|
10
|
+
export type CreatableInstance<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void> = T extends EmptyObject ? T & CreatableInstanceFields<TParams> : CreatableInstanceFields<TParams>;
|
|
11
|
+
export interface Creatable<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void> {
|
|
12
|
+
defaultLogger?: Logger;
|
|
13
|
+
new (params: Partial<CreatableParams<TParams>>): CreatableInstance<T> & AbstractCreatable<TParams>;
|
|
14
|
+
create<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void>(this: Creatable<T, TParams>, params: Partial<CreatableParams<TParams>>): Promise<CreatableInstance<T>>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Class annotation to be used to decorate Modules which support
|
|
18
|
+
* an asynchronous creation pattern
|
|
19
|
+
* @returns The decorated Module requiring it implement the members
|
|
20
|
+
* of the CreatableModule as statics properties/methods
|
|
21
|
+
*/
|
|
22
|
+
export declare function creatable<TInstance extends EmptyObject | void = void, TParams extends EmptyObject | void = void>(): <U extends Creatable<TInstance, TParams>>(constructor: U) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Class annotation to be used to decorate Modules which support
|
|
25
|
+
* an asynchronous creation factory pattern
|
|
26
|
+
* @returns The decorated Module requiring it implement the members
|
|
27
|
+
* of the CreatableModule as statics properties/methods
|
|
28
|
+
*/
|
|
29
|
+
//# sourceMappingURL=Creatable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Creatable.d.ts","sourceRoot":"","s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CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAA;CAM5E;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,SAAS,SAAS,WAAW,GAAG,IAAI,GAAG,IAAI,EAAE,OAAO,SAAS,WAAW,GAAG,IAAI,GAAG,IAAI,MACtG,CAAC,SAAS,SAAS,CAAC,SAAS,EAAE,OAAO,CAAC,EAAE,aAAa,CAAC,UAIhE;AAED;;;;;GAKG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getFunctionName.d.ts","sourceRoot":"","sources":["../../src/getFunctionName.ts"],"names":[],"mappings":"AAEA,wBAAgB,eAAe,CAAC,KAAK,SAAI,UAexC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getRootFunction.d.ts","sourceRoot":"","sources":["../../src/getRootFunction.ts"],"names":[],"mappings":"AAAA,wBAAgB,eAAe,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAO7D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA;AACtC,cAAc,gBAAgB,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xylabs/creatable",
|
|
3
|
+
"version": "4.12.3",
|
|
4
|
+
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
|
+
"homepage": "https://xyo.network",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues",
|
|
8
|
+
"email": "support@xyo.network"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
13
|
+
},
|
|
14
|
+
"license": "LGPL-3.0-only",
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "XYO Development Team",
|
|
17
|
+
"email": "support@xyo.network",
|
|
18
|
+
"url": "https://xyo.network"
|
|
19
|
+
},
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/types/index.d.ts",
|
|
25
|
+
"default": "./dist/neutral/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"module": "dist/neutral/index.mjs",
|
|
30
|
+
"types": "dist/types/index.d.ts",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@opentelemetry/api": "^1.9.0",
|
|
33
|
+
"@xylabs/assert": "^4.12.3",
|
|
34
|
+
"@xylabs/base": "^4.12.3",
|
|
35
|
+
"@xylabs/events": "^4.12.3",
|
|
36
|
+
"@xylabs/logger": "^4.12.3",
|
|
37
|
+
"@xylabs/object": "^4.12.3",
|
|
38
|
+
"@xylabs/typeof": "^4.12.3"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@xylabs/ts-scripts-yarn3": "^6.5.8",
|
|
42
|
+
"@xylabs/tsconfig": "^6.5.8",
|
|
43
|
+
"tslib": "^2.8.1",
|
|
44
|
+
"typescript": "^5.8.3"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { MeterProvider, TracerProvider } from '@opentelemetry/api'
|
|
2
|
+
import { assertEx } from '@xylabs/assert'
|
|
3
|
+
import type { BaseClassName, BaseParams } from '@xylabs/base'
|
|
4
|
+
import { BaseEmitter } from '@xylabs/events'
|
|
5
|
+
import { type Logger } from '@xylabs/logger'
|
|
6
|
+
import type { EmptyObject } from '@xylabs/object'
|
|
7
|
+
import type { Promisable } from '@xylabs/promise'
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
Creatable, CreatableInstance,
|
|
11
|
+
CreatableName,
|
|
12
|
+
} from './Creatable.ts'
|
|
13
|
+
import { getFunctionName } from './getFunctionName.ts'
|
|
14
|
+
import { getRootFunction } from './getRootFunction.ts'
|
|
15
|
+
|
|
16
|
+
export type CreatableStatus = 'creating' | 'created' | 'starting' | 'started' | 'stopping' | 'stopped'
|
|
17
|
+
|
|
18
|
+
export interface StatusReporter {
|
|
19
|
+
report: (name: BaseClassName, status: CreatableStatus, progress?: number) => void
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type CreatableParamsFields = {
|
|
23
|
+
logger?: Logger
|
|
24
|
+
meterProvider?: MeterProvider
|
|
25
|
+
name?: BaseClassName
|
|
26
|
+
traceProvider?: TracerProvider
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type CreatableParams<TAdditionalParams extends EmptyObject | void = void> =
|
|
30
|
+
BaseParams<TAdditionalParams extends EmptyObject ? CreatableParamsFields & TAdditionalParams : CreatableParamsFields>
|
|
31
|
+
|
|
32
|
+
export abstract class AbstractCreatable<TParams extends EmptyObject | void = void> extends BaseEmitter<Partial<CreatableParams<TParams>>> {
|
|
33
|
+
defaultLogger?: Logger
|
|
34
|
+
name: CreatableName
|
|
35
|
+
|
|
36
|
+
private _populatedParams: CreatableParams<TParams> | undefined
|
|
37
|
+
|
|
38
|
+
constructor(params: Partial<CreatableParams<TParams>>) {
|
|
39
|
+
super(params)
|
|
40
|
+
this.name = params.name ?? this.constructor.name as CreatableName
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
override get params(): CreatableParams<TParams> {
|
|
44
|
+
this._noOverride()
|
|
45
|
+
this._populatedParams = this._populatedParams ?? this.paramsHandler(super.params)
|
|
46
|
+
return this._populatedParams
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static async create<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void>(
|
|
50
|
+
this: Creatable<T, TParams>,
|
|
51
|
+
params: Partial<CreatableParams<TParams>>,
|
|
52
|
+
): Promise<CreatableInstance<T>> {
|
|
53
|
+
const instance = new this(params)
|
|
54
|
+
await instance.createHandler()
|
|
55
|
+
return await Promise.resolve(instance as CreatableInstance<T>)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
createHandler(): Promisable<boolean> {
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected _noOverride(functionName = getFunctionName(3)) {
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
|
+
const thisFunc = (this as any)[functionName]
|
|
65
|
+
|
|
66
|
+
const rootFunc = getRootFunction(this, functionName)
|
|
67
|
+
assertEx(thisFunc === rootFunc, () => `Override not allowed for [${functionName}] - override ${functionName}Handler instead`)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected abstract paramsHandler(params?: Partial<CreatableParams<TParams>>): CreatableParams<TParams>
|
|
71
|
+
}
|
package/src/Creatable.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { BaseClassName } from '@xylabs/base'
|
|
2
|
+
import type { Logger } from '@xylabs/logger'
|
|
3
|
+
import type { EmptyObject } from '@xylabs/object'
|
|
4
|
+
|
|
5
|
+
import type { AbstractCreatable, CreatableParams } from './AbstractCreatable.ts'
|
|
6
|
+
|
|
7
|
+
export type CreatableName = Exclude<string, 'creatable-name-reserved-32546239486'> & BaseClassName
|
|
8
|
+
|
|
9
|
+
export interface CreatableInstanceFields<TParams extends EmptyObject | void = void> {
|
|
10
|
+
name: CreatableName
|
|
11
|
+
params: CreatableParams<TParams>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type CreatableInstance<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void>
|
|
15
|
+
= T extends EmptyObject ? T & CreatableInstanceFields<TParams> : CreatableInstanceFields<TParams>
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
export interface CreatableFactory<T extends EmptyObject | void = void,
|
|
19
|
+
TParams extends EmptyObject | void = void> {
|
|
20
|
+
create(params?: CreatableParams<TParams>): Promise<CreatableInstance<T>>
|
|
21
|
+
}
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export interface Creatable<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void> {
|
|
25
|
+
|
|
26
|
+
defaultLogger?: Logger
|
|
27
|
+
|
|
28
|
+
new (params: Partial<CreatableParams<TParams>>): CreatableInstance<T> & AbstractCreatable<TParams>
|
|
29
|
+
|
|
30
|
+
create<T extends EmptyObject | void = void, TParams extends EmptyObject | void = void>(
|
|
31
|
+
this: Creatable<T, TParams>,
|
|
32
|
+
params: Partial<CreatableParams<TParams>>): Promise<CreatableInstance<T>>
|
|
33
|
+
|
|
34
|
+
/*
|
|
35
|
+
factory<T extends EmptyObject | void = void,
|
|
36
|
+
TParams extends EmptyObject | void = void> (this: Creatable<T, TParams>, params: Partial<CreatableParams<TParams>>): CreatableFactory<T, TParams>
|
|
37
|
+
*/
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Class annotation to be used to decorate Modules which support
|
|
42
|
+
* an asynchronous creation pattern
|
|
43
|
+
* @returns The decorated Module requiring it implement the members
|
|
44
|
+
* of the CreatableModule as statics properties/methods
|
|
45
|
+
*/
|
|
46
|
+
export function creatable<TInstance extends EmptyObject | void = void, TParams extends EmptyObject | void = void>() {
|
|
47
|
+
return <U extends Creatable<TInstance, TParams>>(constructor: U) => {
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
49
|
+
constructor
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Class annotation to be used to decorate Modules which support
|
|
55
|
+
* an asynchronous creation factory pattern
|
|
56
|
+
* @returns The decorated Module requiring it implement the members
|
|
57
|
+
* of the CreatableModule as statics properties/methods
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/*
|
|
61
|
+
export function creatableFactory() {
|
|
62
|
+
return <U extends CreatableFactory>(constructor: U) => {
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
64
|
+
constructor
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
*/
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { isNumber } from '@xylabs/typeof'
|
|
2
|
+
|
|
3
|
+
export function getFunctionName(depth = 2) {
|
|
4
|
+
const error = new Error('Stack')
|
|
5
|
+
let newIndex: number | undefined
|
|
6
|
+
const stackParts = error.stack?.split('\n')[depth]?.split(' ')
|
|
7
|
+
const funcName
|
|
8
|
+
= stackParts?.find((item, index) => {
|
|
9
|
+
if (item.length > 0 && item !== 'at') {
|
|
10
|
+
// check if constructor
|
|
11
|
+
if (item === 'new') {
|
|
12
|
+
newIndex = index
|
|
13
|
+
}
|
|
14
|
+
return true
|
|
15
|
+
}
|
|
16
|
+
}) ?? '<unknown>'
|
|
17
|
+
return isNumber(newIndex) ? `${funcName} ${stackParts?.[newIndex + 1]}` : funcName
|
|
18
|
+
}
|
package/src/index.ts
ADDED
package/typedoc.json
ADDED