@xylabs/creatable 5.0.66 → 5.0.69
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/AbstractCreatableV2.d.ts +1 -1
- package/dist/neutral/AbstractCreatableV2.d.ts.map +1 -1
- package/dist/neutral/index.d.ts +2 -0
- package/dist/neutral/index.d.ts.map +1 -1
- package/dist/neutral/index.mjs +227 -0
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/neutral/model/CreatableInstanceV2.d.ts +0 -1
- package/dist/neutral/model/CreatableInstanceV2.d.ts.map +1 -1
- package/package.json +8 -8
- package/src/AbstractCreatableV2.ts +3 -3
- package/src/index.ts +2 -0
- package/src/model/CreatableInstanceV2.ts +0 -1
|
@@ -5,7 +5,7 @@ import type { Promisable } from '@xylabs/promise';
|
|
|
5
5
|
import type { SpanConfig } from '@xylabs/telemetry';
|
|
6
6
|
import type { CreatableFactoryV2, CreatableV2 } from './CreatableV2.ts';
|
|
7
7
|
import type { CreatableInstanceV2, CreatableName, CreatableParamsV2, CreatableStatusV2, Labels } from './model/index.ts';
|
|
8
|
-
export declare abstract class AbstractCreatableV2<TParams extends CreatableParamsV2 = CreatableParamsV2, TEventData extends EventData = EventData> extends BaseEmitterV2<TParams, TEventData> {
|
|
8
|
+
export declare abstract class AbstractCreatableV2<TParams extends CreatableParamsV2 = CreatableParamsV2, TEventData extends EventData = EventData> extends BaseEmitterV2<TParams, TEventData> implements CreatableInstanceV2<TParams, TEventData> {
|
|
9
9
|
defaultLogger?: Logger;
|
|
10
10
|
protected _startPromise: Promisable<boolean> | undefined;
|
|
11
11
|
private _status;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractCreatableV2.d.ts","sourceRoot":"","sources":["../../src/AbstractCreatableV2.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AACpE,OAAO,EAAE,aAAa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,CAAC,SAAS,mBAAmB,EAC1C,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,EACpB,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,EAC7B,MAAM,CAAC,EAAE,MAAM,GACd,kBAAkB,CAAC,CAAC,CAAC;CAGzB"}
|
package/dist/neutral/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA;AACtC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA;AACtC,cAAc,0BAA0B,CAAA;AACxC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -286,6 +286,18 @@ AbstractCreatableWithFactory = __decorateClass([
|
|
|
286
286
|
creatable()
|
|
287
287
|
], AbstractCreatableWithFactory);
|
|
288
288
|
|
|
289
|
+
// src/AbstractCreatableV2.ts
|
|
290
|
+
import { assertEx as assertEx2 } from "@xylabs/assert";
|
|
291
|
+
import { BaseEmitterV2 } from "@xylabs/events";
|
|
292
|
+
import { spanRoot as spanRoot2, spanRootAsync as spanRootAsync2 } from "@xylabs/telemetry";
|
|
293
|
+
import {
|
|
294
|
+
isError as isError2,
|
|
295
|
+
isNumber as isNumber3,
|
|
296
|
+
isString as isString2,
|
|
297
|
+
isUndefined as isUndefined2
|
|
298
|
+
} from "@xylabs/typeof";
|
|
299
|
+
import { Mutex as Mutex2 } from "async-mutex";
|
|
300
|
+
|
|
289
301
|
// src/FactoryV2.ts
|
|
290
302
|
var FactoryV2 = class _FactoryV2 {
|
|
291
303
|
creatable;
|
|
@@ -308,6 +320,217 @@ var FactoryV2 = class _FactoryV2 {
|
|
|
308
320
|
}
|
|
309
321
|
};
|
|
310
322
|
|
|
323
|
+
// src/AbstractCreatableV2.ts
|
|
324
|
+
var AbstractCreatableConstructorKey2 = /* @__PURE__ */ Symbol.for("AbstractCreatableConstructor");
|
|
325
|
+
var CREATABLE_NOT_STARTED2 = "Creatable not Started";
|
|
326
|
+
var AbstractCreatableV2 = class extends BaseEmitterV2 {
|
|
327
|
+
defaultLogger;
|
|
328
|
+
_startPromise;
|
|
329
|
+
_status = null;
|
|
330
|
+
_statusMutex = new Mutex2();
|
|
331
|
+
_validatedParams;
|
|
332
|
+
constructor(key, params) {
|
|
333
|
+
assertEx2(key === AbstractCreatableConstructorKey2, () => "AbstractCreatable should not be instantiated directly, use the static create method instead");
|
|
334
|
+
super(params);
|
|
335
|
+
this.setStatus("creating");
|
|
336
|
+
}
|
|
337
|
+
get context() {
|
|
338
|
+
return this.params.context;
|
|
339
|
+
}
|
|
340
|
+
get name() {
|
|
341
|
+
return this.params.name;
|
|
342
|
+
}
|
|
343
|
+
get params() {
|
|
344
|
+
this._validatedParams = this._validatedParams ?? this.paramsValidator(super.params);
|
|
345
|
+
return assertEx2(this._validatedParams);
|
|
346
|
+
}
|
|
347
|
+
get startable() {
|
|
348
|
+
return this.status === "created" || this.status === "stopped";
|
|
349
|
+
}
|
|
350
|
+
get status() {
|
|
351
|
+
return this._status;
|
|
352
|
+
}
|
|
353
|
+
get statusReporter() {
|
|
354
|
+
return this.context.statusReporter;
|
|
355
|
+
}
|
|
356
|
+
static async create(inParams = {}) {
|
|
357
|
+
const params = await this.paramsHandler(inParams);
|
|
358
|
+
const name = params.name ?? this.name;
|
|
359
|
+
try {
|
|
360
|
+
const instance = new this(AbstractCreatableConstructorKey2, params);
|
|
361
|
+
const initializedInstance = await this.createHandler(instance);
|
|
362
|
+
await instance.createHandler();
|
|
363
|
+
return initializedInstance;
|
|
364
|
+
} catch (ex) {
|
|
365
|
+
params.context.statusReporter?.report(name, "error", isError2(ex) ? ex : new Error(`Error creating: ${name}`));
|
|
366
|
+
params.context.logger?.error(`Error creating creatable [${name}]: ${isError2(ex) ? `${ex.message} ${ex.stack}` : ex}`);
|
|
367
|
+
throw isError2(ex) ? ex : new Error(`Error creating: ${name}`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
static createHandler(instance) {
|
|
371
|
+
return instance;
|
|
372
|
+
}
|
|
373
|
+
static paramsHandler(params = {}) {
|
|
374
|
+
return { ...params };
|
|
375
|
+
}
|
|
376
|
+
createHandler() {
|
|
377
|
+
assertEx2(this._status === "creating", () => `createHandler can not be called [status = ${this.status}]`);
|
|
378
|
+
this.setStatus("created");
|
|
379
|
+
}
|
|
380
|
+
paramsValidator(params) {
|
|
381
|
+
return { ...params, name: params.name };
|
|
382
|
+
}
|
|
383
|
+
span(name, fn) {
|
|
384
|
+
return spanRoot2(name, fn, this.tracer);
|
|
385
|
+
}
|
|
386
|
+
async spanAsync(name, fn, config = {}) {
|
|
387
|
+
return await spanRootAsync2(name, fn, {
|
|
388
|
+
...config,
|
|
389
|
+
tracer: config.tracer ?? this.tracer,
|
|
390
|
+
logger: config.logger ?? this.defaultLogger
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
async start() {
|
|
394
|
+
this._noOverride("start");
|
|
395
|
+
if (this.status === "started") {
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
return await this._statusMutex.runExclusive(async () => {
|
|
399
|
+
try {
|
|
400
|
+
if (this.status === "started") {
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
assertEx2(this.startable, () => `Creatable [${this.name}] is not startable in status [${this.status}]`);
|
|
404
|
+
this.setStatus("starting");
|
|
405
|
+
await this.startHandler();
|
|
406
|
+
this.setStatus("started");
|
|
407
|
+
return true;
|
|
408
|
+
} catch (ex) {
|
|
409
|
+
this.setStatus("error", isError2(ex) ? ex : new Error(`Error starting: ${ex}`));
|
|
410
|
+
this.logger?.error(`Error starting creatable [${this.name}]: ${isError2(ex) ? `${ex.message} ${ex.stack}` : ex}`);
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
started(notStartedAction = "log") {
|
|
416
|
+
if (isString2(this.status) && this.status === "started") {
|
|
417
|
+
return true;
|
|
418
|
+
} else {
|
|
419
|
+
const message = () => `${CREATABLE_NOT_STARTED2} [${this.name}] current state: ${this.status}`;
|
|
420
|
+
switch (notStartedAction) {
|
|
421
|
+
case "error": {
|
|
422
|
+
throw new Error(message());
|
|
423
|
+
}
|
|
424
|
+
case "throw": {
|
|
425
|
+
throw new Error(message());
|
|
426
|
+
}
|
|
427
|
+
case "warn": {
|
|
428
|
+
this.logger?.warn(message());
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
case "log": {
|
|
432
|
+
this.logger?.log(message());
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
case "none": {
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
default: {
|
|
439
|
+
throw new Error(`Unknown notStartedAction: ${notStartedAction}`);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
async startedAsync(notStartedAction = "log", tryStart = true) {
|
|
446
|
+
if (isString2(this.status) && this.status === "started") {
|
|
447
|
+
return true;
|
|
448
|
+
} else if (this.status === "created" || this.status === "stopped" || this.status === "starting") {
|
|
449
|
+
this._startPromise = this._startPromise ?? (async () => {
|
|
450
|
+
if (tryStart) {
|
|
451
|
+
try {
|
|
452
|
+
return await this.start();
|
|
453
|
+
} finally {
|
|
454
|
+
this._startPromise = void 0;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return this.started(notStartedAction);
|
|
458
|
+
})();
|
|
459
|
+
} else {
|
|
460
|
+
const message = `${CREATABLE_NOT_STARTED2} [${this.name}] current state: ${this.status}`;
|
|
461
|
+
throw new Error(message);
|
|
462
|
+
}
|
|
463
|
+
if (isUndefined2(this._startPromise)) {
|
|
464
|
+
throw new Error(`Failed to create start promise: ${this.status}`);
|
|
465
|
+
}
|
|
466
|
+
return await this._startPromise;
|
|
467
|
+
}
|
|
468
|
+
async stop() {
|
|
469
|
+
this._noOverride("stop");
|
|
470
|
+
if (this.status === "stopped") {
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
473
|
+
return await this._statusMutex.runExclusive(async () => {
|
|
474
|
+
try {
|
|
475
|
+
if (this.status === "stopped") {
|
|
476
|
+
return true;
|
|
477
|
+
}
|
|
478
|
+
assertEx2(this.status === "started", () => `Creatable [${this.name}] is not stoppable in status [${this.status}]`);
|
|
479
|
+
this.setStatus("stopping");
|
|
480
|
+
await this.stopHandler();
|
|
481
|
+
this.setStatus("stopped");
|
|
482
|
+
return true;
|
|
483
|
+
} catch (ex) {
|
|
484
|
+
this.setStatus("error", isError2(ex) ? ex : new Error(`Error stopping: ${ex}`));
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
_noOverride(functionName = getFunctionName(3)) {
|
|
490
|
+
const thisFunc = this[functionName];
|
|
491
|
+
const rootFunc = getRootFunction(this, functionName);
|
|
492
|
+
assertEx2(thisFunc === rootFunc, () => `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
493
|
+
}
|
|
494
|
+
setStatus(value, progressOrError) {
|
|
495
|
+
this._status = value;
|
|
496
|
+
if (value !== null) {
|
|
497
|
+
if (value === "error") {
|
|
498
|
+
if (isError2(progressOrError)) {
|
|
499
|
+
this.statusReporter?.report(this.name, value, progressOrError);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
} else {
|
|
503
|
+
if (isNumber3(progressOrError)) {
|
|
504
|
+
this.statusReporter?.report(this.name, value, progressOrError);
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
this.statusReporter?.report(this.name, value);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
startHandler() {
|
|
512
|
+
}
|
|
513
|
+
stopHandler() {
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
var AbstractCreatableWithFactoryV2 = class extends AbstractCreatableV2 {
|
|
517
|
+
static factory(params, labels) {
|
|
518
|
+
return FactoryV2.withParams(this, params, labels);
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
// src/CreatableV2.ts
|
|
523
|
+
function creatableV2() {
|
|
524
|
+
return (constructor) => {
|
|
525
|
+
constructor;
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
function creatableFactoryV2() {
|
|
529
|
+
return (constructor) => {
|
|
530
|
+
constructor;
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
|
|
311
534
|
// src/model/CreatableParamsV2.ts
|
|
312
535
|
import { BaseContextZod } from "@xylabs/base";
|
|
313
536
|
import { BaseEmitterParamsV2Zod } from "@xylabs/events";
|
|
@@ -355,7 +578,9 @@ var hasAllLabels = (source, required) => {
|
|
|
355
578
|
};
|
|
356
579
|
export {
|
|
357
580
|
AbstractCreatable,
|
|
581
|
+
AbstractCreatableV2,
|
|
358
582
|
AbstractCreatableWithFactory,
|
|
583
|
+
AbstractCreatableWithFactoryV2,
|
|
359
584
|
CreatableContext,
|
|
360
585
|
CreatableParamsV2Zod,
|
|
361
586
|
CreatableStatusErrorZod,
|
|
@@ -366,6 +591,8 @@ export {
|
|
|
366
591
|
FactoryV2,
|
|
367
592
|
creatable,
|
|
368
593
|
creatableFactory,
|
|
594
|
+
creatableFactoryV2,
|
|
595
|
+
creatableV2,
|
|
369
596
|
hasAllLabels
|
|
370
597
|
};
|
|
371
598
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/AbstractCreatable.ts","../../src/Creatable.ts","../../src/Factory.ts","../../src/lib/getFunctionName.ts","../../src/lib/getRootFunction.ts","../../src/FactoryV2.ts","../../src/model/CreatableParamsV2.ts","../../src/model/CreatableStatusReporter.zod.ts","../../src/model/Labels.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 {\n SpanConfig, spanRoot, spanRootAsync,\n} from '@xylabs/telemetry'\nimport {\n isError, isNumber, isString,\n isUndefined,\n} from '@xylabs/typeof'\nimport { Mutex } from 'async-mutex'\n\nimport {\n type Creatable, creatable, CreatableFactory,\n} from './Creatable.ts'\nimport { Factory } from './Factory.ts'\nimport { getFunctionName, getRootFunction } from './lib/index.ts'\nimport { CreatableName } from './model/CreatableName.ts'\nimport type {\n CreatableInstance, CreatableParams,\n CreatableStatus,\n Labels,\n RequiredCreatableParams,\n} from './model/index.ts'\n\nconst AbstractCreatableConstructorKey = Symbol.for('AbstractCreatableConstructor')\nconst CREATABLE_NOT_STARTED = 'Creatable not Started' as const\n\n/** @deprecated use AbstractCreatableV2 instead */\n@creatable()\nexport class AbstractCreatable<TParams extends CreatableParams = CreatableParams,\n TEventData extends EventData = EventData> extends BaseEmitter<Partial<TParams & RequiredCreatableParams>, TEventData> {\n defaultLogger?: Logger\n\n protected _startPromise: Promisable<boolean> | undefined\n private _status: CreatableStatus | null = null\n private _statusMutex = new Mutex()\n private _validatedParams?: TParams & RequiredCreatableParams\n\n constructor(key: unknown, params: Partial<TParams & RequiredCreatableParams>) {\n assertEx(key === AbstractCreatableConstructorKey, () => 'AbstractCreatable should not be instantiated directly, use the static create method instead')\n super(params)\n }\n\n get name(): CreatableName {\n return this.params.name as CreatableName\n }\n\n override get params(): TParams & RequiredCreatableParams {\n this._validatedParams = this._validatedParams ?? this.paramsValidator(super.params)\n return this._validatedParams\n }\n\n get startable() {\n return this.status === 'created' || this.status === 'stopped'\n }\n\n get status(): CreatableStatus | null {\n return this._status\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 = await this.paramsHandler(inParams)\n const name: CreatableName = params.name ?? this.name as CreatableName\n try {\n const instance = new this(AbstractCreatableConstructorKey, params)\n instance.setStatus('creating')\n const initializedInstance = await this.createHandler(instance)\n await instance.createHandler()\n instance.setStatus('created')\n return initializedInstance\n } catch (ex) {\n params.statusReporter?.report(name, 'error', isError(ex) ? ex : new Error(`Error creating: ${name}`))\n params.logger?.error(`Error creating creatable [${name}]: ${(isError(ex) ? `${ex.message} ${ex.stack}` : ex)}`)\n throw isError(ex) ? ex : 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 paramsHandler<T extends CreatableInstance>(\n this: Creatable<T>,\n params: Partial<T['params']> = {},\n ): Promisable<T['params']> {\n return { ...params }\n }\n\n createHandler(): Promisable<void> {\n assertEx(this._status === 'creating', () => `createHandler can not be called [status = ${this.status}]`)\n }\n\n paramsValidator(params: Partial<TParams & RequiredCreatableParams>): TParams & RequiredCreatableParams {\n return { ...params, name: params.name } as TParams & RequiredCreatableParams\n }\n\n span<T>(name: string, fn: () => T): T {\n return spanRoot(name, fn, this.tracer)\n }\n\n async spanAsync<T>(name: string, fn: () => Promise<T>, config: SpanConfig = {}): Promise<T> {\n return await spanRootAsync(name, fn, {\n ...config, tracer: config.tracer ?? this.tracer, logger: config.logger ?? this.defaultLogger,\n })\n }\n\n async start(): Promise<boolean> {\n this._noOverride('start')\n if (this.status === 'started') {\n return true\n }\n return await this._statusMutex.runExclusive(async () => {\n try {\n // check again in case it changed\n if (this.status === 'started') {\n return true\n }\n assertEx(this.startable, () => `Creatable [${this.name}] is not startable in status [${this.status}]`)\n this.setStatus('starting')\n await this.startHandler()\n this.setStatus('started')\n return true\n } catch (ex) {\n this.setStatus('error', isError(ex) ? ex : new Error(`Error starting: ${ex}`))\n this.logger?.error(`Error starting creatable [${this.name}]: ${(isError(ex) ? `${ex.message} ${ex.stack}` : ex)}`)\n return false\n }\n })\n }\n\n started(notStartedAction: 'error' | 'throw' | 'warn' | 'log' | 'none' = 'log'): boolean {\n if (isString(this.status) && this.status === 'started') {\n return true\n } else {\n const message = () => `${CREATABLE_NOT_STARTED} [${this.name}] current state: ${this.status}`\n switch (notStartedAction) {\n case 'error': {\n throw new Error(message())\n }\n case 'throw': {\n throw new Error(message())\n }\n case 'warn': {\n this.logger?.warn(message())\n break\n }\n case 'log': {\n this.logger?.log(message())\n break\n }\n case 'none': {\n break\n }\n default: {\n throw new Error(`Unknown notStartedAction: ${notStartedAction}`)\n }\n }\n return false\n }\n }\n\n async startedAsync(notStartedAction: 'error' | 'throw' | 'warn' | 'log' | 'none' = 'log', tryStart = true): Promise<boolean> {\n if (isString(this.status) && this.status === 'started') {\n return true\n } else if (this.status === 'created' || this.status === 'stopped' || this.status === 'starting') {\n // using promise as mutex\n this._startPromise = this._startPromise ?? (async () => {\n if (tryStart) {\n try {\n return await this.start()\n } finally {\n this._startPromise = undefined\n }\n }\n return this.started(notStartedAction)\n })()\n } else {\n const message = `${CREATABLE_NOT_STARTED} [${this.name}] current state: ${this.status}`\n throw new Error(message)\n }\n\n if (isUndefined(this._startPromise)) {\n throw new Error(`Failed to create start promise: ${this.status}`)\n }\n return await this._startPromise\n }\n\n async stop(): Promise<boolean> {\n this._noOverride('stop')\n if (this.status === 'stopped') {\n return true\n }\n return await this._statusMutex.runExclusive(async () => {\n try {\n // check again in case it changed\n if (this.status === 'stopped') {\n return true\n }\n assertEx(this.status === 'started', () => `Creatable [${this.name}] is not stoppable in status [${this.status}]`)\n this.setStatus('stopping')\n await this.stopHandler()\n this.setStatus('stopped')\n return true\n } catch (ex) {\n this.setStatus('error', isError(ex) ? ex : new Error(`Error stopping: ${ex}`))\n return false\n }\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 setStatus(value: Exclude<CreatableStatus, 'error'>, progress?: number): void\n protected setStatus(value: Extract<CreatableStatus, 'error'>, error?: Error): void\n protected setStatus(value: CreatableStatus, progressOrError?: number | Error): void {\n this._status = value\n if (value !== null) {\n if (value === 'error') {\n if (isError(progressOrError)) {\n this.statusReporter?.report(this.name, value, progressOrError)\n return\n }\n } else {\n if (isNumber(progressOrError)) {\n this.statusReporter?.report(this.name, value, progressOrError)\n return\n }\n }\n this.statusReporter?.report(this.name, value)\n }\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\n@creatable()\nexport class AbstractCreatableWithFactory<TParams extends CreatableParams = CreatableParams,\n TEventData extends EventData = EventData> extends AbstractCreatable<TParams, TEventData> {\n static factory<T extends CreatableInstance>(\n this: Creatable<T>,\n params?: Partial<T['params']>,\n labels?: Labels,\n ): CreatableFactory<T> {\n return Factory.withParams<T>(this, params, labels)\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { Promisable } from '@xylabs/promise'\n\nimport type { AbstractCreatable } from './AbstractCreatable.ts'\nimport type {\n CreatableInstance, CreatableParams, Labels,\n RequiredCreatableParams,\n} from './model/index.ts'\n\n/** @deprecated use CreatableFactoryV2 instead */\nexport interface CreatableFactory<T extends CreatableInstance = CreatableInstance>\n extends Omit<Creatable<T>, 'create' | 'createHandler' | 'paramsHandler' | 'defaultLogger' | 'factory'> {\n\n create(\n this: CreatableFactory<T>,\n params?: Partial<T['params']>): Promise<T>\n}\n\n/** @deprecated use CreatableV2 instead */\nexport interface Creatable<T extends CreatableInstance = CreatableInstance> {\n\n defaultLogger?: Logger\n\n new(key: unknown, params: Partial<CreatableParams>): 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 paramsHandler<T extends CreatableInstance>(\n this: Creatable<T>, params?: Partial<T['params']>): Promisable<T['params'] & RequiredCreatableParams>\n}\n\n/** @deprecated use CreatableWithFactoryV2 instead */\nexport interface CreatableWithFactory<T extends CreatableInstance = CreatableInstance> extends Creatable<T> {\n factory<T extends CreatableInstance>(\n this: Creatable<T>,\n params?: Partial<T['params']>,\n labels?: Labels): CreatableFactory<T>\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 */\n/** @deprecated use creatableV2 instead */\nexport function creatable<T extends CreatableInstance>() {\n return <U extends Creatable<T>>(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/** @deprecated use creatableFactoryV2 instead */\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","import { type Creatable, type CreatableFactory } from './Creatable.ts'\nimport type {\n CreatableInstance, Labels, WithOptionalLabels,\n} from './model/index.ts'\n\nexport class Factory<T extends CreatableInstance = CreatableInstance> implements CreatableFactory<T> {\n creatable: Creatable<T>\n\n defaultParams?: Partial<T['params']>\n\n labels?: Labels\n\n constructor(\n creatable: Creatable<T>,\n params?: Partial<T['params']>,\n labels: Labels = {},\n ) {\n this.creatable = creatable\n this.defaultParams = params\n this.labels = Object.assign({}, (creatable as WithOptionalLabels).labels ?? {}, labels ?? {})\n }\n\n static withParams<T extends CreatableInstance>(\n creatableModule: Creatable<T>,\n params?: Partial<T['params']>,\n labels: Labels = {},\n ) {\n return new Factory<T>(creatableModule, params, labels)\n }\n\n create(params?: Partial<T['params']>): Promise<T> {\n const mergedParams: T['params'] = {\n ...this.defaultParams,\n ...params,\n } as T['params']\n return this.creatable.create<T>(mergedParams)\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.__proto__?.[funcName]) {\n anyObj = anyObj.__proto__\n }\n return anyObj[funcName]\n}\n","import type { CreatableFactoryV2, CreatableV2 } from './CreatableV2.ts'\nimport type {\n CreatableInstanceV2, Labels, WithOptionalLabels,\n} from './model/index.ts'\n\nexport class FactoryV2<T extends CreatableInstanceV2 = CreatableInstanceV2> implements CreatableFactoryV2<T> {\n creatable: CreatableV2<T>\n\n defaultParams?: Partial<T['params']>\n\n labels?: Labels\n\n constructor(\n creatable: CreatableV2<T>,\n params?: Partial<T['params']>,\n labels: Labels = {},\n ) {\n this.creatable = creatable\n this.defaultParams = params\n this.labels = Object.assign({}, (creatable as WithOptionalLabels).labels ?? {}, labels ?? {})\n }\n\n static withParams<T extends CreatableInstanceV2>(\n creatableModule: CreatableV2<T>,\n params?: Partial<T['params']>,\n labels: Labels = {},\n ) {\n return new FactoryV2<T>(creatableModule, params, labels)\n }\n\n create(params?: Partial<T['params']>): Promise<T> {\n const mergedParams: T['params'] = {\n ...this.defaultParams,\n ...params,\n } as T['params']\n return this.creatable.create<T>(mergedParams)\n }\n}\n","import { BaseContextZod } from '@xylabs/base'\nimport { BaseEmitterParamsV2Zod } from '@xylabs/events'\nimport z from 'zod'\n\nimport { CreatableStatusReporterV2Zod } from './CreatableStatusReporter.zod.ts'\n\nexport const CreatableContext = z.intersection(BaseContextZod, z.object({ name: z.string(), statusReporter: CreatableStatusReporterV2Zod.optional() }))\n\nexport const CreatableParamsV2Zod = BaseEmitterParamsV2Zod.extend(z.object({ context: CreatableContext, name: z.string() }).shape)\n\nexport type CreatableParamsV2 = z.infer<typeof CreatableParamsV2Zod>\n","import { z } from 'zod'\n\nexport const CreatableStatusSuccessZod = z.enum(['creating', 'created', 'starting', 'started', 'stopping', 'stopped'])\n\nexport const CreatableStatusErrorZod = z.enum(['error'])\n\nexport const CreatableStatusZod = z.union([CreatableStatusSuccessZod, CreatableStatusErrorZod])\n\nexport const CreatableStatusReporterV2Zod = z.object({\n report: z.union([\n z.function({\n input: z.union([\n z.tuple([\n z.string(),\n CreatableStatusSuccessZod,\n z.number().optional(),\n ]),\n z.tuple([\n z.string(),\n CreatableStatusErrorZod,\n z.instanceof(Error),\n ]),\n z.tuple([\n z.string(),\n CreatableStatusZod,\n ]),\n ]),\n output: z.void(),\n }),\n ]),\n})\n","/**\n * Object used to represent labels identifying a resource.\n */\nexport interface Labels {\n [key: string]: string | undefined\n}\n\n/**\n * Interface for objects that have labels.\n */\nexport interface WithLabels<T extends Labels = Labels> {\n labels: T\n}\n\n/**\n * Interface for objects that have labels.\n */\nexport interface WithOptionalLabels<T extends Labels = Labels> {\n labels?: T\n}\n\n/**\n * Returns true if the source object has all the labels from the required set\n * @param source Source object to check against\n * @param required Set of labels to check for in source\n * @returns True of the source object has all the labels from the required set\n */\nexport const hasAllLabels = (source?: Labels, required?: Labels): boolean => {\n if (!required) return true\n return Object.entries(required).every(([key, value]) => {\n return source?.hasOwnProperty(key as keyof typeof source)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","isNumber","creatable","z","z"]}
|
|
1
|
+
{"version":3,"sources":["../../src/AbstractCreatable.ts","../../src/Creatable.ts","../../src/Factory.ts","../../src/lib/getFunctionName.ts","../../src/lib/getRootFunction.ts","../../src/AbstractCreatableV2.ts","../../src/FactoryV2.ts","../../src/CreatableV2.ts","../../src/model/CreatableParamsV2.ts","../../src/model/CreatableStatusReporter.zod.ts","../../src/model/Labels.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 {\n SpanConfig, spanRoot, spanRootAsync,\n} from '@xylabs/telemetry'\nimport {\n isError, isNumber, isString,\n isUndefined,\n} from '@xylabs/typeof'\nimport { Mutex } from 'async-mutex'\n\nimport {\n type Creatable, creatable, CreatableFactory,\n} from './Creatable.ts'\nimport { Factory } from './Factory.ts'\nimport { getFunctionName, getRootFunction } from './lib/index.ts'\nimport { CreatableName } from './model/CreatableName.ts'\nimport type {\n CreatableInstance, CreatableParams,\n CreatableStatus,\n Labels,\n RequiredCreatableParams,\n} from './model/index.ts'\n\nconst AbstractCreatableConstructorKey = Symbol.for('AbstractCreatableConstructor')\nconst CREATABLE_NOT_STARTED = 'Creatable not Started' as const\n\n/** @deprecated use AbstractCreatableV2 instead */\n@creatable()\nexport class AbstractCreatable<TParams extends CreatableParams = CreatableParams,\n TEventData extends EventData = EventData> extends BaseEmitter<Partial<TParams & RequiredCreatableParams>, TEventData> {\n defaultLogger?: Logger\n\n protected _startPromise: Promisable<boolean> | undefined\n private _status: CreatableStatus | null = null\n private _statusMutex = new Mutex()\n private _validatedParams?: TParams & RequiredCreatableParams\n\n constructor(key: unknown, params: Partial<TParams & RequiredCreatableParams>) {\n assertEx(key === AbstractCreatableConstructorKey, () => 'AbstractCreatable should not be instantiated directly, use the static create method instead')\n super(params)\n }\n\n get name(): CreatableName {\n return this.params.name as CreatableName\n }\n\n override get params(): TParams & RequiredCreatableParams {\n this._validatedParams = this._validatedParams ?? this.paramsValidator(super.params)\n return this._validatedParams\n }\n\n get startable() {\n return this.status === 'created' || this.status === 'stopped'\n }\n\n get status(): CreatableStatus | null {\n return this._status\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 = await this.paramsHandler(inParams)\n const name: CreatableName = params.name ?? this.name as CreatableName\n try {\n const instance = new this(AbstractCreatableConstructorKey, params)\n instance.setStatus('creating')\n const initializedInstance = await this.createHandler(instance)\n await instance.createHandler()\n instance.setStatus('created')\n return initializedInstance\n } catch (ex) {\n params.statusReporter?.report(name, 'error', isError(ex) ? ex : new Error(`Error creating: ${name}`))\n params.logger?.error(`Error creating creatable [${name}]: ${(isError(ex) ? `${ex.message} ${ex.stack}` : ex)}`)\n throw isError(ex) ? ex : 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 paramsHandler<T extends CreatableInstance>(\n this: Creatable<T>,\n params: Partial<T['params']> = {},\n ): Promisable<T['params']> {\n return { ...params }\n }\n\n createHandler(): Promisable<void> {\n assertEx(this._status === 'creating', () => `createHandler can not be called [status = ${this.status}]`)\n }\n\n paramsValidator(params: Partial<TParams & RequiredCreatableParams>): TParams & RequiredCreatableParams {\n return { ...params, name: params.name } as TParams & RequiredCreatableParams\n }\n\n span<T>(name: string, fn: () => T): T {\n return spanRoot(name, fn, this.tracer)\n }\n\n async spanAsync<T>(name: string, fn: () => Promise<T>, config: SpanConfig = {}): Promise<T> {\n return await spanRootAsync(name, fn, {\n ...config, tracer: config.tracer ?? this.tracer, logger: config.logger ?? this.defaultLogger,\n })\n }\n\n async start(): Promise<boolean> {\n this._noOverride('start')\n if (this.status === 'started') {\n return true\n }\n return await this._statusMutex.runExclusive(async () => {\n try {\n // check again in case it changed\n if (this.status === 'started') {\n return true\n }\n assertEx(this.startable, () => `Creatable [${this.name}] is not startable in status [${this.status}]`)\n this.setStatus('starting')\n await this.startHandler()\n this.setStatus('started')\n return true\n } catch (ex) {\n this.setStatus('error', isError(ex) ? ex : new Error(`Error starting: ${ex}`))\n this.logger?.error(`Error starting creatable [${this.name}]: ${(isError(ex) ? `${ex.message} ${ex.stack}` : ex)}`)\n return false\n }\n })\n }\n\n started(notStartedAction: 'error' | 'throw' | 'warn' | 'log' | 'none' = 'log'): boolean {\n if (isString(this.status) && this.status === 'started') {\n return true\n } else {\n const message = () => `${CREATABLE_NOT_STARTED} [${this.name}] current state: ${this.status}`\n switch (notStartedAction) {\n case 'error': {\n throw new Error(message())\n }\n case 'throw': {\n throw new Error(message())\n }\n case 'warn': {\n this.logger?.warn(message())\n break\n }\n case 'log': {\n this.logger?.log(message())\n break\n }\n case 'none': {\n break\n }\n default: {\n throw new Error(`Unknown notStartedAction: ${notStartedAction}`)\n }\n }\n return false\n }\n }\n\n async startedAsync(notStartedAction: 'error' | 'throw' | 'warn' | 'log' | 'none' = 'log', tryStart = true): Promise<boolean> {\n if (isString(this.status) && this.status === 'started') {\n return true\n } else if (this.status === 'created' || this.status === 'stopped' || this.status === 'starting') {\n // using promise as mutex\n this._startPromise = this._startPromise ?? (async () => {\n if (tryStart) {\n try {\n return await this.start()\n } finally {\n this._startPromise = undefined\n }\n }\n return this.started(notStartedAction)\n })()\n } else {\n const message = `${CREATABLE_NOT_STARTED} [${this.name}] current state: ${this.status}`\n throw new Error(message)\n }\n\n if (isUndefined(this._startPromise)) {\n throw new Error(`Failed to create start promise: ${this.status}`)\n }\n return await this._startPromise\n }\n\n async stop(): Promise<boolean> {\n this._noOverride('stop')\n if (this.status === 'stopped') {\n return true\n }\n return await this._statusMutex.runExclusive(async () => {\n try {\n // check again in case it changed\n if (this.status === 'stopped') {\n return true\n }\n assertEx(this.status === 'started', () => `Creatable [${this.name}] is not stoppable in status [${this.status}]`)\n this.setStatus('stopping')\n await this.stopHandler()\n this.setStatus('stopped')\n return true\n } catch (ex) {\n this.setStatus('error', isError(ex) ? ex : new Error(`Error stopping: ${ex}`))\n return false\n }\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 setStatus(value: Exclude<CreatableStatus, 'error'>, progress?: number): void\n protected setStatus(value: Extract<CreatableStatus, 'error'>, error?: Error): void\n protected setStatus(value: CreatableStatus, progressOrError?: number | Error): void {\n this._status = value\n if (value !== null) {\n if (value === 'error') {\n if (isError(progressOrError)) {\n this.statusReporter?.report(this.name, value, progressOrError)\n return\n }\n } else {\n if (isNumber(progressOrError)) {\n this.statusReporter?.report(this.name, value, progressOrError)\n return\n }\n }\n this.statusReporter?.report(this.name, value)\n }\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\n@creatable()\nexport class AbstractCreatableWithFactory<TParams extends CreatableParams = CreatableParams,\n TEventData extends EventData = EventData> extends AbstractCreatable<TParams, TEventData> {\n static factory<T extends CreatableInstance>(\n this: Creatable<T>,\n params?: Partial<T['params']>,\n labels?: Labels,\n ): CreatableFactory<T> {\n return Factory.withParams<T>(this, params, labels)\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { Promisable } from '@xylabs/promise'\n\nimport type { AbstractCreatable } from './AbstractCreatable.ts'\nimport type {\n CreatableInstance, CreatableParams, Labels,\n RequiredCreatableParams,\n} from './model/index.ts'\n\n/** @deprecated use CreatableFactoryV2 instead */\nexport interface CreatableFactory<T extends CreatableInstance = CreatableInstance>\n extends Omit<Creatable<T>, 'create' | 'createHandler' | 'paramsHandler' | 'defaultLogger' | 'factory'> {\n\n create(\n this: CreatableFactory<T>,\n params?: Partial<T['params']>): Promise<T>\n}\n\n/** @deprecated use CreatableV2 instead */\nexport interface Creatable<T extends CreatableInstance = CreatableInstance> {\n\n defaultLogger?: Logger\n\n new(key: unknown, params: Partial<CreatableParams>): 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 paramsHandler<T extends CreatableInstance>(\n this: Creatable<T>, params?: Partial<T['params']>): Promisable<T['params'] & RequiredCreatableParams>\n}\n\n/** @deprecated use CreatableWithFactoryV2 instead */\nexport interface CreatableWithFactory<T extends CreatableInstance = CreatableInstance> extends Creatable<T> {\n factory<T extends CreatableInstance>(\n this: Creatable<T>,\n params?: Partial<T['params']>,\n labels?: Labels): CreatableFactory<T>\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 */\n/** @deprecated use creatableV2 instead */\nexport function creatable<T extends CreatableInstance>() {\n return <U extends Creatable<T>>(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/** @deprecated use creatableFactoryV2 instead */\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","import { type Creatable, type CreatableFactory } from './Creatable.ts'\nimport type {\n CreatableInstance, Labels, WithOptionalLabels,\n} from './model/index.ts'\n\nexport class Factory<T extends CreatableInstance = CreatableInstance> implements CreatableFactory<T> {\n creatable: Creatable<T>\n\n defaultParams?: Partial<T['params']>\n\n labels?: Labels\n\n constructor(\n creatable: Creatable<T>,\n params?: Partial<T['params']>,\n labels: Labels = {},\n ) {\n this.creatable = creatable\n this.defaultParams = params\n this.labels = Object.assign({}, (creatable as WithOptionalLabels).labels ?? {}, labels ?? {})\n }\n\n static withParams<T extends CreatableInstance>(\n creatableModule: Creatable<T>,\n params?: Partial<T['params']>,\n labels: Labels = {},\n ) {\n return new Factory<T>(creatableModule, params, labels)\n }\n\n create(params?: Partial<T['params']>): Promise<T> {\n const mergedParams: T['params'] = {\n ...this.defaultParams,\n ...params,\n } as T['params']\n return this.creatable.create<T>(mergedParams)\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.__proto__?.[funcName]) {\n anyObj = anyObj.__proto__\n }\n return anyObj[funcName]\n}\n","import { assertEx } from '@xylabs/assert'\nimport type { BaseEmitterParamsV2, EventData } from '@xylabs/events'\nimport { BaseEmitterV2 } from '@xylabs/events'\nimport { type Logger } from '@xylabs/logger'\nimport type { Promisable } from '@xylabs/promise'\nimport type { SpanConfig } from '@xylabs/telemetry'\nimport { spanRoot, spanRootAsync } from '@xylabs/telemetry'\nimport {\n isError, isNumber, isString,\n isUndefined,\n} from '@xylabs/typeof'\nimport { Mutex } from 'async-mutex'\n\nimport type { CreatableFactoryV2, CreatableV2 } from './CreatableV2.ts'\nimport { FactoryV2 } from './FactoryV2.ts'\nimport { getFunctionName, getRootFunction } from './lib/index.ts'\nimport type {\n CreatableInstanceV2,\n CreatableName, CreatableParamsV2,\n CreatableStatusV2,\n Labels,\n} from './model/index.ts'\n\nconst AbstractCreatableConstructorKey = Symbol.for('AbstractCreatableConstructor')\nconst CREATABLE_NOT_STARTED = 'Creatable not Started' as const\n\nexport abstract class AbstractCreatableV2<TParams extends CreatableParamsV2 = CreatableParamsV2,\n TEventData extends EventData = EventData> extends BaseEmitterV2<TParams, TEventData> implements CreatableInstanceV2<TParams, TEventData> {\n defaultLogger?: Logger\n\n protected _startPromise: Promisable<boolean> | undefined\n private _status: CreatableStatusV2 | null = null\n private _statusMutex = new Mutex()\n private _validatedParams?: TParams & BaseEmitterParamsV2\n\n constructor(key: unknown, params: TParams) {\n assertEx(key === AbstractCreatableConstructorKey, () => 'AbstractCreatable should not be instantiated directly, use the static create method instead')\n super(params)\n this.setStatus('creating')\n }\n\n override get context(): TParams['context'] & BaseEmitterParamsV2['context'] {\n return this.params.context\n }\n\n get name(): CreatableName {\n return this.params.name as CreatableName\n }\n\n override get params(): TParams & BaseEmitterParamsV2 {\n this._validatedParams = this._validatedParams ?? this.paramsValidator(super.params)\n return assertEx(this._validatedParams)\n }\n\n get startable() {\n return this.status === 'created' || this.status === 'stopped'\n }\n\n get status(): CreatableStatusV2 | null {\n return this._status\n }\n\n get statusReporter() {\n return this.context.statusReporter\n }\n\n abstract override get className(): CreatableName\n\n static async create<T extends CreatableInstanceV2>(\n this: CreatableV2<T>,\n inParams: Partial<T['params']> = {},\n ): Promise<T> {\n const params = await this.paramsHandler(inParams)\n const name = (params.name ?? this.name) as CreatableName\n try {\n const instance = new this(AbstractCreatableConstructorKey, params)\n const initializedInstance = await this.createHandler(instance)\n await instance.createHandler()\n return initializedInstance\n } catch (ex) {\n params.context.statusReporter?.report(name, 'error', isError(ex) ? ex : new Error(`Error creating: ${name}`))\n params.context.logger?.error(`Error creating creatable [${name}]: ${(isError(ex) ? `${ex.message} ${ex.stack}` : ex)}`)\n throw isError(ex) ? ex : new Error(`Error creating: ${name}`)\n }\n }\n\n static createHandler<T extends CreatableInstanceV2>(\n this: CreatableV2<T>,\n instance: T,\n ): Promisable<T> {\n return instance\n }\n\n static paramsHandler<T extends CreatableInstanceV2>(\n this: CreatableV2<T>,\n params: Partial<T['params']> = {},\n ): Promisable<T['params']> {\n return { ...params } as T['params']\n }\n\n createHandler(): Promisable<void> {\n assertEx(this._status === 'creating', () => `createHandler can not be called [status = ${this.status}]`)\n this.setStatus('created')\n }\n\n paramsValidator(params: Partial<TParams>): TParams {\n return { ...params, name: params.name } as TParams\n }\n\n span<T>(name: string, fn: () => T): T {\n return spanRoot(name, fn, this.tracer)\n }\n\n async spanAsync<T>(name: string, fn: () => Promise<T>, config: SpanConfig = {}): Promise<T> {\n return await spanRootAsync(name, fn, {\n ...config, tracer: config.tracer ?? this.tracer, logger: config.logger ?? this.defaultLogger,\n })\n }\n\n async start(): Promise<boolean> {\n this._noOverride('start')\n if (this.status === 'started') {\n return true\n }\n return await this._statusMutex.runExclusive(async () => {\n try {\n // check again in case it changed\n if (this.status === 'started') {\n return true\n }\n assertEx(this.startable, () => `Creatable [${this.name}] is not startable in status [${this.status}]`)\n this.setStatus('starting')\n await this.startHandler()\n this.setStatus('started')\n return true\n } catch (ex) {\n this.setStatus('error', isError(ex) ? ex : new Error(`Error starting: ${ex}`))\n this.logger?.error(`Error starting creatable [${this.name}]: ${(isError(ex) ? `${ex.message} ${ex.stack}` : ex)}`)\n return false\n }\n })\n }\n\n started(notStartedAction: 'error' | 'throw' | 'warn' | 'log' | 'none' = 'log'): boolean {\n if (isString(this.status) && this.status === 'started') {\n return true\n } else {\n const message = () => `${CREATABLE_NOT_STARTED} [${this.name}] current state: ${this.status}`\n switch (notStartedAction) {\n case 'error': {\n throw new Error(message())\n }\n case 'throw': {\n throw new Error(message())\n }\n case 'warn': {\n this.logger?.warn(message())\n break\n }\n case 'log': {\n this.logger?.log(message())\n break\n }\n case 'none': {\n break\n }\n default: {\n throw new Error(`Unknown notStartedAction: ${notStartedAction}`)\n }\n }\n return false\n }\n }\n\n async startedAsync(notStartedAction: 'error' | 'throw' | 'warn' | 'log' | 'none' = 'log', tryStart = true): Promise<boolean> {\n if (isString(this.status) && this.status === 'started') {\n return true\n } else if (this.status === 'created' || this.status === 'stopped' || this.status === 'starting') {\n // using promise as mutex\n this._startPromise = this._startPromise ?? (async () => {\n if (tryStart) {\n try {\n return await this.start()\n } finally {\n this._startPromise = undefined\n }\n }\n return this.started(notStartedAction)\n })()\n } else {\n const message = `${CREATABLE_NOT_STARTED} [${this.name}] current state: ${this.status}`\n throw new Error(message)\n }\n\n if (isUndefined(this._startPromise)) {\n throw new Error(`Failed to create start promise: ${this.status}`)\n }\n return await this._startPromise\n }\n\n async stop(): Promise<boolean> {\n this._noOverride('stop')\n if (this.status === 'stopped') {\n return true\n }\n return await this._statusMutex.runExclusive(async () => {\n try {\n // check again in case it changed\n if (this.status === 'stopped') {\n return true\n }\n assertEx(this.status === 'started', () => `Creatable [${this.name}] is not stoppable in status [${this.status}]`)\n this.setStatus('stopping')\n await this.stopHandler()\n this.setStatus('stopped')\n return true\n } catch (ex) {\n this.setStatus('error', isError(ex) ? ex : new Error(`Error stopping: ${ex}`))\n return false\n }\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 setStatus(value: Exclude<CreatableStatusV2, 'error'>, progress?: number): void\n protected setStatus(value: Extract<CreatableStatusV2, 'error'>, error?: Error): void\n protected setStatus(value: CreatableStatusV2, progressOrError?: number | Error): void {\n this._status = value\n if (value !== null) {\n if (value === 'error') {\n if (isError(progressOrError)) {\n this.statusReporter?.report(this.name, value, progressOrError)\n return\n }\n } else {\n if (isNumber(progressOrError)) {\n this.statusReporter?.report(this.name, value, progressOrError)\n return\n }\n }\n this.statusReporter?.report(this.name, value)\n }\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\nexport abstract class AbstractCreatableWithFactoryV2<TParams extends CreatableParamsV2 = CreatableParamsV2,\n TEventData extends EventData = EventData> extends AbstractCreatableV2<TParams, TEventData> {\n static factory<T extends CreatableInstanceV2>(\n this: CreatableV2<T>,\n params?: Partial<T['params']>,\n labels?: Labels,\n ): CreatableFactoryV2<T> {\n return FactoryV2.withParams<T>(this, params, labels)\n }\n}\n","import type { CreatableFactoryV2, CreatableV2 } from './CreatableV2.ts'\nimport type {\n CreatableInstanceV2, Labels, WithOptionalLabels,\n} from './model/index.ts'\n\nexport class FactoryV2<T extends CreatableInstanceV2 = CreatableInstanceV2> implements CreatableFactoryV2<T> {\n creatable: CreatableV2<T>\n\n defaultParams?: Partial<T['params']>\n\n labels?: Labels\n\n constructor(\n creatable: CreatableV2<T>,\n params?: Partial<T['params']>,\n labels: Labels = {},\n ) {\n this.creatable = creatable\n this.defaultParams = params\n this.labels = Object.assign({}, (creatable as WithOptionalLabels).labels ?? {}, labels ?? {})\n }\n\n static withParams<T extends CreatableInstanceV2>(\n creatableModule: CreatableV2<T>,\n params?: Partial<T['params']>,\n labels: Labels = {},\n ) {\n return new FactoryV2<T>(creatableModule, params, labels)\n }\n\n create(params?: Partial<T['params']>): Promise<T> {\n const mergedParams: T['params'] = {\n ...this.defaultParams,\n ...params,\n } as T['params']\n return this.creatable.create<T>(mergedParams)\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { Promisable } from '@xylabs/promise'\n\nimport type {\n CreatableInstanceV2, CreatableParamsV2, Labels,\n} from './model/index.ts'\n\nexport interface CreatableFactoryV2<T extends CreatableInstanceV2 = CreatableInstanceV2>\n extends Omit<CreatableV2<T>, 'create' | 'createHandler' | 'paramsHandler' | 'defaultLogger' | 'factory'> {\n\n create(\n this: CreatableFactoryV2<T>,\n params?: Partial<T['params']>): Promise<T>\n}\n\nexport interface CreatableV2<T extends CreatableInstanceV2 = CreatableInstanceV2> {\n\n defaultLogger?: Logger\n\n new(key: unknown, params: Partial<CreatableParamsV2>): T\n\n create<T extends CreatableInstanceV2>(\n this: CreatableV2<T>,\n params?: Partial<T['params']>): Promise<T>\n\n createHandler<T extends CreatableInstanceV2>(\n this: CreatableV2<T>,\n instance: T\n ): Promisable<T>\n\n paramsHandler<T extends CreatableInstanceV2>(\n this: CreatableV2<T>, params?: Partial<T['params']>): Promisable<T['params']>\n}\n\nexport interface CreatableWithFactoryV2<T extends CreatableInstanceV2 = CreatableInstanceV2> extends CreatableV2<T> {\n factory<T extends CreatableInstanceV2>(\n this: CreatableV2<T>,\n params?: Partial<T['params']>,\n labels?: Labels): CreatableFactoryV2<T>\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 creatableV2<T extends CreatableInstanceV2>() {\n return <U extends CreatableV2<T>>(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\nexport function creatableFactoryV2() {\n return <U extends CreatableFactoryV2>(constructor: U) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-expressions\n constructor\n }\n}\n","import { BaseContextZod } from '@xylabs/base'\nimport { BaseEmitterParamsV2Zod } from '@xylabs/events'\nimport z from 'zod'\n\nimport { CreatableStatusReporterV2Zod } from './CreatableStatusReporter.zod.ts'\n\nexport const CreatableContext = z.intersection(BaseContextZod, z.object({ name: z.string(), statusReporter: CreatableStatusReporterV2Zod.optional() }))\n\nexport const CreatableParamsV2Zod = BaseEmitterParamsV2Zod.extend(z.object({ context: CreatableContext, name: z.string() }).shape)\n\nexport type CreatableParamsV2 = z.infer<typeof CreatableParamsV2Zod>\n","import { z } from 'zod'\n\nexport const CreatableStatusSuccessZod = z.enum(['creating', 'created', 'starting', 'started', 'stopping', 'stopped'])\n\nexport const CreatableStatusErrorZod = z.enum(['error'])\n\nexport const CreatableStatusZod = z.union([CreatableStatusSuccessZod, CreatableStatusErrorZod])\n\nexport const CreatableStatusReporterV2Zod = z.object({\n report: z.union([\n z.function({\n input: z.union([\n z.tuple([\n z.string(),\n CreatableStatusSuccessZod,\n z.number().optional(),\n ]),\n z.tuple([\n z.string(),\n CreatableStatusErrorZod,\n z.instanceof(Error),\n ]),\n z.tuple([\n z.string(),\n CreatableStatusZod,\n ]),\n ]),\n output: z.void(),\n }),\n ]),\n})\n","/**\n * Object used to represent labels identifying a resource.\n */\nexport interface Labels {\n [key: string]: string | undefined\n}\n\n/**\n * Interface for objects that have labels.\n */\nexport interface WithLabels<T extends Labels = Labels> {\n labels: T\n}\n\n/**\n * Interface for objects that have labels.\n */\nexport interface WithOptionalLabels<T extends Labels = Labels> {\n labels?: T\n}\n\n/**\n * Returns true if the source object has all the labels from the required set\n * @param source Source object to check against\n * @param required Set of labels to check for in source\n * @returns True of the source object has all the labels from the required set\n */\nexport const hasAllLabels = (source?: Labels, required?: Labels): boolean => {\n if (!required) return true\n return Object.entries(required).every(([key, value]) => {\n return source?.hasOwnProperty(key as keyof typeof source) && source?.[key as keyof typeof source] === value\n })\n}\n"],"mappings":";;;;;;;;;;;;AAAA,SAAS,gBAAgB;AAEzB,SAAS,mBAAmB;AAG5B;AAAA,EACc;AAAA,EAAU;AAAA,OACjB;AACP;AAAA,EACE;AAAA,EAAS,YAAAA;AAAA,EAAU;AAAA,EACnB;AAAA,OACK;AACP,SAAS,aAAa;;;ACyCf,SAAS,YAAyC;AACvD,SAAO,CAAyB,gBAAmB;AAEjD;AAAA,EACF;AACF;AAUO,SAAS,mBAAmB;AACjC,SAAO,CAA6B,gBAAmB;AAErD;AAAA,EACF;AACF;;;ACpEO,IAAM,UAAN,MAAM,SAAwF;AAAA,EACnG;AAAA,E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AE,MAAMA,GAAE,OAAO,GAAG,gBAAgB,6BAA6B,SAAS,EAAE,CAAC,CAAC;AAE/I,IAAM,uBAAuB,uBAAuB,OAAOA,GAAE,OAAO,EAAE,SAAS,kBAAkB,MAAMA,GAAE,OAAO,EAAE,CAAC,EAAE,KAAK;;;AEmB1H,IAAM,eAAe,CAAC,QAAiB,aAA+B;AAC3E,MAAI,CAAC,SAAU,QAAO;AACtB,SAAO,OAAO,QAAQ,QAAQ,EAAE,MAAM,CAAC,CAAC,KAAK,KAAK,MAAM;AACtD,WAAO,QAAQ,eAAe,GAA0B,KAAK,SAAS,GAA0B,MAAM;AAAA,EACxG,CAAC;AACH;","names":["isNumber","creatable","isNumber","assertEx","spanRoot","spanRootAsync","isError","isNumber","isString","isUndefined","Mutex","creatable","AbstractCreatableConstructorKey","CREATABLE_NOT_STARTED","Mutex","assertEx","isError","spanRoot","spanRootAsync","isString","isUndefined","isNumber","z","z"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CreatableInstanceV2.d.ts","sourceRoot":"","sources":["../../../src/model/CreatableInstanceV2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AACvD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAE/D,MAAM,WAAW,mBAAmB,CAAC,OAAO,SAAS,iBAAiB,GAAG,iBAAiB,EACxF,UAAU,SAAS,SAAS,GAAG,SAAS,CAAE,SAAQ,YAAY,CAAC,UAAU,CAAC;IAC1E,aAAa,EAAE,MAAM,UAAU,CAAC,IAAI,CAAC,CAAA;IACrC,SAAS,EAAE,UAAU,CAAA;IACrB,IAAI,EAAE,aAAa,CAAA;IACnB,MAAM,EAAE,OAAO,CAAA;IACf,
|
|
1
|
+
{"version":3,"file":"CreatableInstanceV2.d.ts","sourceRoot":"","sources":["../../../src/model/CreatableInstanceV2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AACvD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAE/D,MAAM,WAAW,mBAAmB,CAAC,OAAO,SAAS,iBAAiB,GAAG,iBAAiB,EACxF,UAAU,SAAS,SAAS,GAAG,SAAS,CAAE,SAAQ,YAAY,CAAC,UAAU,CAAC;IAC1E,aAAa,EAAE,MAAM,UAAU,CAAC,IAAI,CAAC,CAAA;IACrC,SAAS,EAAE,UAAU,CAAA;IACrB,IAAI,EAAE,aAAa,CAAA;IACnB,MAAM,EAAE,OAAO,CAAA;IACf,KAAK,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAA;IAC7B,IAAI,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAA;CAC7B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/creatable",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.69",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -38,13 +38,13 @@
|
|
|
38
38
|
"!**/*.test.*"
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@xylabs/assert": "~5.0.
|
|
42
|
-
"@xylabs/base": "~5.0.
|
|
43
|
-
"@xylabs/events": "~5.0.
|
|
44
|
-
"@xylabs/logger": "~5.0.
|
|
45
|
-
"@xylabs/promise": "~5.0.
|
|
46
|
-
"@xylabs/telemetry": "~5.0.
|
|
47
|
-
"@xylabs/typeof": "~5.0.
|
|
41
|
+
"@xylabs/assert": "~5.0.69",
|
|
42
|
+
"@xylabs/base": "~5.0.69",
|
|
43
|
+
"@xylabs/events": "~5.0.69",
|
|
44
|
+
"@xylabs/logger": "~5.0.69",
|
|
45
|
+
"@xylabs/promise": "~5.0.69",
|
|
46
|
+
"@xylabs/telemetry": "~5.0.69",
|
|
47
|
+
"@xylabs/typeof": "~5.0.69",
|
|
48
48
|
"async-mutex": "~0.5.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
@@ -25,7 +25,7 @@ const AbstractCreatableConstructorKey = Symbol.for('AbstractCreatableConstructor
|
|
|
25
25
|
const CREATABLE_NOT_STARTED = 'Creatable not Started' as const
|
|
26
26
|
|
|
27
27
|
export abstract class AbstractCreatableV2<TParams extends CreatableParamsV2 = CreatableParamsV2,
|
|
28
|
-
TEventData extends EventData = EventData> extends BaseEmitterV2<TParams, TEventData> {
|
|
28
|
+
TEventData extends EventData = EventData> extends BaseEmitterV2<TParams, TEventData> implements CreatableInstanceV2<TParams, TEventData> {
|
|
29
29
|
defaultLogger?: Logger
|
|
30
30
|
|
|
31
31
|
protected _startPromise: Promisable<boolean> | undefined
|
|
@@ -36,6 +36,7 @@ export abstract class AbstractCreatableV2<TParams extends CreatableParamsV2 = Cr
|
|
|
36
36
|
constructor(key: unknown, params: TParams) {
|
|
37
37
|
assertEx(key === AbstractCreatableConstructorKey, () => 'AbstractCreatable should not be instantiated directly, use the static create method instead')
|
|
38
38
|
super(params)
|
|
39
|
+
this.setStatus('creating')
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
override get context(): TParams['context'] & BaseEmitterParamsV2['context'] {
|
|
@@ -73,10 +74,8 @@ export abstract class AbstractCreatableV2<TParams extends CreatableParamsV2 = Cr
|
|
|
73
74
|
const name = (params.name ?? this.name) as CreatableName
|
|
74
75
|
try {
|
|
75
76
|
const instance = new this(AbstractCreatableConstructorKey, params)
|
|
76
|
-
instance.setStatus('creating')
|
|
77
77
|
const initializedInstance = await this.createHandler(instance)
|
|
78
78
|
await instance.createHandler()
|
|
79
|
-
instance.setStatus('created')
|
|
80
79
|
return initializedInstance
|
|
81
80
|
} catch (ex) {
|
|
82
81
|
params.context.statusReporter?.report(name, 'error', isError(ex) ? ex : new Error(`Error creating: ${name}`))
|
|
@@ -101,6 +100,7 @@ export abstract class AbstractCreatableV2<TParams extends CreatableParamsV2 = Cr
|
|
|
101
100
|
|
|
102
101
|
createHandler(): Promisable<void> {
|
|
103
102
|
assertEx(this._status === 'creating', () => `createHandler can not be called [status = ${this.status}]`)
|
|
103
|
+
this.setStatus('created')
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
paramsValidator(params: Partial<TParams>): TParams {
|
package/src/index.ts
CHANGED