@adaas/a-concept 0.2.6 → 0.2.7
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/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/global/A-Feature/A-Feature.class.ts +56 -28
- package/tests/A-Feature.test.ts +182 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaas/a-concept",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "A-Concept is a framework of the new generation that is tailored to use AI, enabling developers to create AI-powered applications with ease. It provides a structured approach to building, managing, and deploying AI-driven solutions.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -375,11 +375,11 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
|
|
|
375
375
|
|
|
376
376
|
// Convert iterator to array to get all stages
|
|
377
377
|
const stages = Array.from(this);
|
|
378
|
-
|
|
378
|
+
|
|
379
379
|
return this.processStagesSequentially(stages, scope, 0);
|
|
380
380
|
|
|
381
381
|
} catch (error) {
|
|
382
|
-
this.failed(new A_FeatureError({
|
|
382
|
+
throw this.failed(new A_FeatureError({
|
|
383
383
|
title: A_FeatureError.FeatureProcessingError,
|
|
384
384
|
description: `An error occurred while processing the A-Feature: ${this.name}. Failed at stage: ${this.stage?.name || 'N/A'}.`,
|
|
385
385
|
stage: this.stage,
|
|
@@ -392,11 +392,16 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
|
|
|
392
392
|
* Process stages one by one, ensuring each stage completes before starting the next
|
|
393
393
|
*/
|
|
394
394
|
private processStagesSequentially(
|
|
395
|
-
stages: A_Stage[],
|
|
396
|
-
scope: A_Scope | undefined,
|
|
395
|
+
stages: A_Stage[],
|
|
396
|
+
scope: A_Scope | undefined,
|
|
397
397
|
index: number
|
|
398
398
|
): Promise<void> | void {
|
|
399
399
|
try {
|
|
400
|
+
// Check if feature has been interrupted before processing next stage
|
|
401
|
+
if (this.state === A_TYPES__FeatureState.INTERRUPTED) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
|
|
400
405
|
// If we've processed all stages, complete the feature
|
|
401
406
|
if (index >= stages.length) {
|
|
402
407
|
this.completed();
|
|
@@ -408,22 +413,28 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
|
|
|
408
413
|
|
|
409
414
|
if (A_TypeGuards.isPromiseInstance(result)) {
|
|
410
415
|
// Async stage - return promise that processes remaining stages
|
|
411
|
-
return result
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
416
|
+
return result
|
|
417
|
+
.then(() => {
|
|
418
|
+
// Check for interruption after async stage completes
|
|
419
|
+
if (this.state === A_TYPES__FeatureState.INTERRUPTED) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
return this.processStagesSequentially(stages, scope, index + 1);
|
|
423
|
+
})
|
|
424
|
+
.catch(error => {
|
|
425
|
+
throw this.failed(new A_FeatureError({
|
|
426
|
+
title: A_FeatureError.FeatureProcessingError,
|
|
427
|
+
description: `An error occurred while processing the A-Feature: ${this.name}. Failed at stage: ${stage.name}.`,
|
|
428
|
+
stage: stage,
|
|
429
|
+
originalError: error
|
|
430
|
+
}));
|
|
431
|
+
});
|
|
421
432
|
} else {
|
|
422
433
|
// Sync stage - continue to next stage immediately
|
|
423
434
|
return this.processStagesSequentially(stages, scope, index + 1);
|
|
424
435
|
}
|
|
425
436
|
} catch (error) {
|
|
426
|
-
this.failed(new A_FeatureError({
|
|
437
|
+
throw this.failed(new A_FeatureError({
|
|
427
438
|
title: A_FeatureError.FeatureProcessingError,
|
|
428
439
|
description: `An error occurred while processing the A-Feature: ${this.name}. Failed at stage: ${this.stage?.name || 'N/A'}.`,
|
|
429
440
|
stage: this.stage,
|
|
@@ -455,19 +466,24 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
|
|
|
455
466
|
completed(): void {
|
|
456
467
|
if (this.isProcessed) return;
|
|
457
468
|
|
|
469
|
+
// Don't complete if interrupted
|
|
470
|
+
if (this.state === A_TYPES__FeatureState.INTERRUPTED) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
458
473
|
|
|
459
474
|
this._state = A_TYPES__FeatureState.COMPLETED;
|
|
460
475
|
|
|
461
476
|
this.scope.destroy();
|
|
462
477
|
}
|
|
463
478
|
/**
|
|
464
|
-
* This method marks the feature as failed and
|
|
479
|
+
* This method marks the feature as failed and returns the error
|
|
465
480
|
* Uses to mark the feature as failed
|
|
466
481
|
*
|
|
467
482
|
* @param error
|
|
483
|
+
* @returns The error that caused the failure
|
|
468
484
|
*/
|
|
469
|
-
failed(error: A_FeatureError) {
|
|
470
|
-
if (this.isProcessed) return
|
|
485
|
+
failed(error: A_FeatureError): A_FeatureError {
|
|
486
|
+
if (this.isProcessed) return this._error!;
|
|
471
487
|
|
|
472
488
|
this._state = A_TYPES__FeatureState.FAILED;
|
|
473
489
|
|
|
@@ -475,45 +491,47 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
|
|
|
475
491
|
|
|
476
492
|
this.scope.destroy();
|
|
477
493
|
|
|
478
|
-
|
|
494
|
+
return this._error;
|
|
479
495
|
}
|
|
480
496
|
/**
|
|
481
|
-
* This method marks the feature as
|
|
497
|
+
* This method marks the feature as interrupted and throws an error
|
|
482
498
|
* Uses to interrupt or end the feature processing
|
|
483
499
|
*
|
|
484
500
|
* @param error
|
|
485
501
|
*/
|
|
486
|
-
|
|
502
|
+
interrupt(
|
|
487
503
|
/**
|
|
488
504
|
* The reason of feature interruption
|
|
489
505
|
*/
|
|
490
506
|
reason?: string | A_StageError | Error
|
|
491
|
-
) {
|
|
492
|
-
if (this.isProcessed) return
|
|
507
|
+
): A_FeatureError {
|
|
508
|
+
if (this.isProcessed) return this._error!;
|
|
493
509
|
|
|
494
510
|
this._state = A_TYPES__FeatureState.INTERRUPTED;
|
|
495
511
|
|
|
496
512
|
switch (true) {
|
|
497
513
|
case A_TypeGuards.isString(reason):
|
|
498
|
-
this._error = new A_FeatureError(A_FeatureError.Interruption, reason);
|
|
514
|
+
this._error = new A_FeatureError(A_FeatureError.Interruption, reason as string);
|
|
499
515
|
break;
|
|
500
516
|
|
|
501
517
|
case A_TypeGuards.isErrorInstance(reason):
|
|
502
518
|
this._error = new A_FeatureError({
|
|
503
519
|
code: A_FeatureError.Interruption,
|
|
504
|
-
title: reason.title,
|
|
505
|
-
description: reason.description,
|
|
520
|
+
title: (reason as any).title || 'Feature Interrupted',
|
|
521
|
+
description: (reason as any).description || (reason as Error).message,
|
|
506
522
|
stage: this.stage,
|
|
507
523
|
originalError: reason
|
|
508
524
|
});
|
|
509
525
|
break;
|
|
510
526
|
|
|
511
527
|
default:
|
|
528
|
+
this._error = new A_FeatureError(A_FeatureError.Interruption, 'Feature was interrupted');
|
|
512
529
|
break;
|
|
513
530
|
}
|
|
514
531
|
|
|
515
|
-
|
|
516
532
|
this.scope.destroy();
|
|
533
|
+
|
|
534
|
+
return this._error;
|
|
517
535
|
}
|
|
518
536
|
|
|
519
537
|
|
|
@@ -573,7 +591,17 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
|
|
|
573
591
|
// create new caller for the chained feature
|
|
574
592
|
feature._caller = this._caller;
|
|
575
593
|
|
|
576
|
-
|
|
594
|
+
const result = feature.process(featureScope);
|
|
595
|
+
|
|
596
|
+
// If the chained feature processing returns a promise, ensure errors are propagated
|
|
597
|
+
if (A_TypeGuards.isPromiseInstance(result)) {
|
|
598
|
+
return result.catch(error => {
|
|
599
|
+
// Re-throw to ensure chained feature errors propagate to caller
|
|
600
|
+
throw error;
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
return result;
|
|
577
605
|
}
|
|
578
606
|
|
|
579
607
|
|
package/tests/A-Feature.test.ts
CHANGED
|
@@ -591,22 +591,21 @@ describe('A-Feature tests', () => {
|
|
|
591
591
|
]
|
|
592
592
|
});
|
|
593
593
|
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
});
|
|
594
|
+
await Promise.all([
|
|
595
|
+
feature.process(),
|
|
596
|
+
new Promise<void>(async (resolve) => {
|
|
597
|
+
setTimeout(() => {
|
|
598
|
+
feature.interrupt();
|
|
599
|
+
resolve();
|
|
600
|
+
}, 800);
|
|
601
|
+
}),
|
|
602
|
+
new Promise<void>(async (resolve) => {
|
|
603
|
+
setTimeout(() => {
|
|
604
|
+
expect(feature.state).toBe(A_TYPES__FeatureState.INTERRUPTED);
|
|
605
|
+
resolve();
|
|
606
|
+
}, 1000);
|
|
607
|
+
})
|
|
608
|
+
]);
|
|
610
609
|
|
|
611
610
|
}, 5000);
|
|
612
611
|
it('Should allow to use extension if only parent class provided', async () => {
|
|
@@ -868,4 +867,171 @@ describe('A-Feature tests', () => {
|
|
|
868
867
|
]);
|
|
869
868
|
|
|
870
869
|
})
|
|
870
|
+
it('Should throw a Sync error when executed sync', async () => {
|
|
871
|
+
|
|
872
|
+
const resultChain: string[] = [];
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
class ChildComponent_A extends A_Component {
|
|
876
|
+
@A_Feature.Extend({
|
|
877
|
+
name: 'testFeature',
|
|
878
|
+
})
|
|
879
|
+
test1() {
|
|
880
|
+
resultChain.push('ChildComponent_A.test');
|
|
881
|
+
throw new A_Error('Deliberate Sync Error in test1');
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
class ChildComponent_B extends A_Component {
|
|
886
|
+
@A_Feature.Extend({
|
|
887
|
+
name: 'testFeature',
|
|
888
|
+
})
|
|
889
|
+
test2() {
|
|
890
|
+
resultChain.push('ChildComponent_B.test');
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
const testScope = new A_Scope({ name: 'TestScope', components: [ChildComponent_A, ChildComponent_B] });
|
|
896
|
+
|
|
897
|
+
try {
|
|
898
|
+
testScope.resolve(ChildComponent_A)!.call('testFeature');
|
|
899
|
+
} catch (error) {
|
|
900
|
+
expect(error).toBeInstanceOf(A_Error);
|
|
901
|
+
expect((error as A_Error).originalError).toBeInstanceOf(A_Error)
|
|
902
|
+
expect((error as A_Error).originalError.message).toBe('Deliberate Sync Error in test1');
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
expect(resultChain).toEqual([
|
|
906
|
+
'ChildComponent_A.test'
|
|
907
|
+
]);
|
|
908
|
+
const feature = new A_Feature({
|
|
909
|
+
name: 'testFeature',
|
|
910
|
+
component: testScope.resolve(ChildComponent_A)!,
|
|
911
|
+
})
|
|
912
|
+
|
|
913
|
+
try {
|
|
914
|
+
feature.process();
|
|
915
|
+
} catch (error) {
|
|
916
|
+
expect(error).toBeInstanceOf(A_Error);
|
|
917
|
+
expect((error as A_Error).originalError).toBeInstanceOf(A_Error)
|
|
918
|
+
expect((error as A_Error).originalError.message).toBe('Deliberate Sync Error in test1');
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
expect(feature.state).toBe(A_TYPES__FeatureState.FAILED);
|
|
922
|
+
|
|
923
|
+
expect(resultChain).toEqual([
|
|
924
|
+
'ChildComponent_A.test',
|
|
925
|
+
'ChildComponent_A.test'
|
|
926
|
+
]);
|
|
927
|
+
})
|
|
928
|
+
it('Should throw an Async error when executed async', async () => {
|
|
929
|
+
|
|
930
|
+
const resultChain: string[] = [];
|
|
931
|
+
|
|
932
|
+
class ChildComponent_A extends A_Component {
|
|
933
|
+
@A_Feature.Extend({
|
|
934
|
+
name: 'testFeature',
|
|
935
|
+
})
|
|
936
|
+
async test1() {
|
|
937
|
+
resultChain.push('ChildComponent_A.test');
|
|
938
|
+
|
|
939
|
+
await new Promise<void>(async (resolve, reject) => {
|
|
940
|
+
setTimeout(() => {
|
|
941
|
+
reject(new A_Error('Deliberate Async Error in test1'));
|
|
942
|
+
}, 2000);
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
class ChildComponent_B extends A_Component {
|
|
948
|
+
@A_Feature.Extend({
|
|
949
|
+
name: 'testFeature',
|
|
950
|
+
})
|
|
951
|
+
async test2() {
|
|
952
|
+
resultChain.push('ChildComponent_B.test');
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
const testScope = new A_Scope({ name: 'TestScope', components: [ChildComponent_A, ChildComponent_B] });
|
|
957
|
+
try {
|
|
958
|
+
await Promise.all([
|
|
959
|
+
new Promise<void>(async (resolve) => {
|
|
960
|
+
setTimeout(() => {
|
|
961
|
+
resultChain.push('feature3');
|
|
962
|
+
|
|
963
|
+
resolve();
|
|
964
|
+
}, 1000);
|
|
965
|
+
}),
|
|
966
|
+
testScope.resolve(ChildComponent_A)!.call('testFeature')
|
|
967
|
+
]);
|
|
968
|
+
|
|
969
|
+
} catch (error) {
|
|
970
|
+
expect(error).toBeInstanceOf(A_Error);
|
|
971
|
+
expect((error as A_Error).originalError).toBeInstanceOf(A_Error)
|
|
972
|
+
expect((error as A_Error).originalError.message).toBe('Deliberate Async Error in test1');
|
|
973
|
+
}
|
|
974
|
+
expect(resultChain).toEqual([
|
|
975
|
+
'ChildComponent_A.test',
|
|
976
|
+
'feature3'
|
|
977
|
+
]);
|
|
978
|
+
|
|
979
|
+
const feature = new A_Feature({
|
|
980
|
+
name: 'testFeature',
|
|
981
|
+
component: testScope.resolve(ChildComponent_A)!,
|
|
982
|
+
})
|
|
983
|
+
|
|
984
|
+
try {
|
|
985
|
+
await feature.process();
|
|
986
|
+
} catch (error) {
|
|
987
|
+
expect(error).toBeInstanceOf(A_Error);
|
|
988
|
+
expect((error as A_Error).originalError).toBeInstanceOf(A_Error)
|
|
989
|
+
expect((error as A_Error).originalError.message).toBe('Deliberate Async Error in test1');
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
expect(feature.state).toBe(A_TYPES__FeatureState.FAILED);
|
|
993
|
+
|
|
994
|
+
expect(resultChain).toEqual([
|
|
995
|
+
'ChildComponent_A.test',
|
|
996
|
+
'feature3',
|
|
997
|
+
'ChildComponent_A.test'
|
|
998
|
+
]);
|
|
999
|
+
})
|
|
1000
|
+
|
|
1001
|
+
it('Should throw an Async error when executed async and error in method', async () => {
|
|
1002
|
+
|
|
1003
|
+
const resultChain: string[] = [];
|
|
1004
|
+
|
|
1005
|
+
class ChildComponent_A extends A_Component {
|
|
1006
|
+
@A_Feature.Extend({
|
|
1007
|
+
name: 'testFeature',
|
|
1008
|
+
})
|
|
1009
|
+
async test1() {
|
|
1010
|
+
resultChain.push('ChildComponent_A.test');
|
|
1011
|
+
|
|
1012
|
+
throw new A_Error('Deliberate Async Error in test1');
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
const testScope = new A_Scope({ name: 'TestScope', components: [ChildComponent_A] });
|
|
1017
|
+
|
|
1018
|
+
const feature = new A_Feature({
|
|
1019
|
+
name: 'testFeature',
|
|
1020
|
+
component: testScope.resolve(ChildComponent_A)!,
|
|
1021
|
+
})
|
|
1022
|
+
|
|
1023
|
+
try {
|
|
1024
|
+
await feature.process();
|
|
1025
|
+
} catch (error) {
|
|
1026
|
+
expect(error).toBeInstanceOf(A_Error);
|
|
1027
|
+
expect((error as A_Error).originalError).toBeInstanceOf(A_Error)
|
|
1028
|
+
expect((error as A_Error).originalError.message).toBe('Deliberate Async Error in test1');
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
expect(feature.state).toBe(A_TYPES__FeatureState.FAILED);
|
|
1032
|
+
|
|
1033
|
+
expect(resultChain).toEqual([
|
|
1034
|
+
'ChildComponent_A.test'
|
|
1035
|
+
]);
|
|
1036
|
+
})
|
|
871
1037
|
});
|