@adaas/a-utils 0.1.11 → 0.1.13
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/README.md +87 -32
- package/dist/index.d.mts +33 -6
- package/dist/index.d.ts +33 -6
- package/dist/index.js +66 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +66 -12
- package/dist/index.mjs.map +1 -1
- package/examples/command-examples.ts +268 -0
- package/package.json +1 -1
- package/src/lib/A-Command/A-Command.constants.ts +4 -0
- package/src/lib/A-Command/A-Command.entity.ts +74 -15
- package/src/lib/A-Command/A-Command.error.ts +2 -0
- package/src/lib/A-Command/A-Command.types.ts +6 -1
- package/src/lib/A-Command/README.md +645 -0
- package/src/lib/A-Memory/A-Memory.context.ts +15 -0
- package/tests/A-Command.test.ts +447 -2
- package/tests/A-Memory.test.ts +189 -0
package/README.md
CHANGED
|
@@ -59,45 +59,73 @@ const channel = new A_Channel();
|
|
|
59
59
|
|
|
60
60
|
### A-Command
|
|
61
61
|
|
|
62
|
-
A powerful command execution system that provides structured command patterns with lifecycle management, status tracking, and serialization capabilities.
|
|
62
|
+
A powerful command execution system that provides structured command patterns with lifecycle management, event handling, status tracking, and serialization capabilities.
|
|
63
63
|
|
|
64
|
-
**
|
|
64
|
+
> 📚 **[Complete A-Command Documentation](./src/lib/A-Command/README.md)** - Comprehensive guide with examples, API reference, and advanced usage patterns.
|
|
65
|
+
|
|
66
|
+
**Quick Start:**
|
|
65
67
|
```typescript
|
|
66
68
|
import { A_Command } from '@adaas/a-utils';
|
|
69
|
+
import { A_Context } from '@adaas/a-concept';
|
|
67
70
|
|
|
68
|
-
// Create a command
|
|
69
|
-
const command = new A_Command({});
|
|
71
|
+
// Create and execute a simple command
|
|
72
|
+
const command = new A_Command({ action: 'greet', name: 'World' });
|
|
73
|
+
A_Context.root.register(command);
|
|
70
74
|
|
|
71
|
-
// Execute the command
|
|
72
75
|
await command.execute();
|
|
73
76
|
|
|
74
77
|
console.log(command.status); // 'COMPLETED'
|
|
75
78
|
console.log(command.duration); // Execution time in ms
|
|
76
79
|
```
|
|
77
80
|
|
|
78
|
-
**Advanced
|
|
81
|
+
**Advanced Example:**
|
|
79
82
|
```typescript
|
|
80
|
-
//
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
// Typed command with custom logic
|
|
84
|
+
interface UserCreateParams {
|
|
85
|
+
name: string;
|
|
86
|
+
email: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
interface UserCreateResult {
|
|
90
|
+
userId: string;
|
|
91
|
+
createdAt: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
class CreateUserCommand extends A_Command<UserCreateParams, UserCreateResult> {}
|
|
95
|
+
|
|
96
|
+
// Custom execution logic using components
|
|
97
|
+
class UserProcessor extends A_Component {
|
|
98
|
+
@A_Feature.Extend({ scope: [CreateUserCommand] })
|
|
99
|
+
async execute(@A_Inject(A_Memory) memory: A_Memory<UserCreateResult>) {
|
|
100
|
+
// Your business logic here
|
|
101
|
+
await memory.set('userId', 'user-123');
|
|
102
|
+
await memory.set('createdAt', new Date().toISOString());
|
|
85
103
|
}
|
|
86
104
|
}
|
|
87
105
|
|
|
88
|
-
//
|
|
89
|
-
const command = new
|
|
90
|
-
|
|
106
|
+
// Execute with event handling
|
|
107
|
+
const command = new CreateUserCommand({
|
|
108
|
+
name: 'John Doe',
|
|
109
|
+
email: 'john@example.com'
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
command.on('complete', (cmd) => {
|
|
113
|
+
console.log('User created:', cmd.result);
|
|
114
|
+
});
|
|
91
115
|
|
|
92
|
-
|
|
93
|
-
|
|
116
|
+
A_Context.root.register(UserProcessor);
|
|
117
|
+
A_Context.root.register(command);
|
|
118
|
+
await command.execute();
|
|
94
119
|
```
|
|
95
120
|
|
|
96
|
-
**Features:**
|
|
97
|
-
-
|
|
98
|
-
-
|
|
99
|
-
-
|
|
100
|
-
-
|
|
121
|
+
**Key Features:**
|
|
122
|
+
- ✅ **Complete Lifecycle Management** - Automatic progression through init → compile → execute → complete/fail
|
|
123
|
+
- ✅ **Event-Driven Architecture** - Subscribe to lifecycle events and custom events
|
|
124
|
+
- ✅ **State Persistence** - Full serialization/deserialization support
|
|
125
|
+
- ✅ **Type Safety** - Full TypeScript support with generic types
|
|
126
|
+
- ✅ **Error Handling** - Comprehensive error capture and management
|
|
127
|
+
- ✅ **Execution Tracking** - Built-in timing and duration tracking
|
|
128
|
+
- ✅ **Component Integration** - Extensible through A-Component architecture
|
|
101
129
|
- Scope integration with dependency injection
|
|
102
130
|
- Memory management integration
|
|
103
131
|
|
|
@@ -284,13 +312,15 @@ const manifest = new A_Manifest([
|
|
|
284
312
|
|
|
285
313
|
### A-Memory
|
|
286
314
|
|
|
287
|
-
A type-safe memory management system for storing intermediate values and tracking errors during complex operations.
|
|
315
|
+
A type-safe memory management system for storing intermediate values and tracking errors during complex operations. Fully integrated with A-Command for state management.
|
|
316
|
+
|
|
317
|
+
> 💡 **Note:** A-Memory is automatically used by A-Command for result and error storage. See [A-Command Documentation](./src/lib/A-Command/README.md) for integration examples.
|
|
288
318
|
|
|
289
319
|
**Basic Usage:**
|
|
290
320
|
```typescript
|
|
291
321
|
import { A_Memory } from '@adaas/a-utils';
|
|
292
322
|
|
|
293
|
-
// Create memory instance
|
|
323
|
+
// Create typed memory instance
|
|
294
324
|
const memory = new A_Memory<{
|
|
295
325
|
userId: string;
|
|
296
326
|
userData: any;
|
|
@@ -299,16 +329,19 @@ const memory = new A_Memory<{
|
|
|
299
329
|
userId: '12345'
|
|
300
330
|
});
|
|
301
331
|
|
|
302
|
-
// Store values
|
|
303
|
-
memory.set('userData', { name: 'John', email: 'john@example.com' });
|
|
304
|
-
memory.set('processedData', processUserData(memory.get('userData')));
|
|
332
|
+
// Store and retrieve values
|
|
333
|
+
await memory.set('userData', { name: 'John', email: 'john@example.com' });
|
|
334
|
+
await memory.set('processedData', processUserData(memory.get('userData')));
|
|
335
|
+
|
|
336
|
+
// Access values with type safety
|
|
337
|
+
const userId = memory.get('userId'); // string | undefined
|
|
338
|
+
const userData = memory.get('userData'); // any | undefined
|
|
305
339
|
|
|
306
340
|
// Check prerequisites
|
|
307
341
|
const hasRequired = await memory.verifyPrerequisites(['userId', 'userData']);
|
|
308
342
|
console.log(hasRequired); // true
|
|
309
343
|
|
|
310
|
-
//
|
|
311
|
-
const userId = memory.get('userId');
|
|
344
|
+
// Serialize all data
|
|
312
345
|
const allData = memory.toJSON();
|
|
313
346
|
```
|
|
314
347
|
|
|
@@ -318,20 +351,42 @@ import { A_Error } from '@adaas/a-concept';
|
|
|
318
351
|
|
|
319
352
|
const memory = new A_Memory();
|
|
320
353
|
|
|
321
|
-
//
|
|
354
|
+
// Add errors during processing
|
|
322
355
|
try {
|
|
323
356
|
// Some operation that might fail
|
|
324
|
-
throw new
|
|
357
|
+
throw new Error('Something went wrong');
|
|
325
358
|
} catch (error) {
|
|
326
|
-
memory.
|
|
359
|
+
await memory.error(new A_Error({
|
|
360
|
+
title: 'Operation Failed',
|
|
361
|
+
message: error.message
|
|
362
|
+
}));
|
|
327
363
|
}
|
|
328
364
|
|
|
329
365
|
// Check for errors
|
|
330
366
|
if (memory.Errors) {
|
|
331
|
-
console.log('Errors occurred:', memory.Errors);
|
|
367
|
+
console.log('Errors occurred:', Array.from(memory.Errors));
|
|
332
368
|
}
|
|
333
369
|
```
|
|
334
370
|
|
|
371
|
+
**Integration with A-Command:**
|
|
372
|
+
```typescript
|
|
373
|
+
// A-Memory is automatically used by A-Command
|
|
374
|
+
class DataProcessor extends A_Component {
|
|
375
|
+
@A_Feature.Extend({ scope: [MyCommand] })
|
|
376
|
+
async execute(@A_Inject(A_Memory) memory: A_Memory<ResultType>) {
|
|
377
|
+
// Store intermediate results
|
|
378
|
+
await memory.set('step1', 'completed');
|
|
379
|
+
await memory.set('step2', { data: 'processed' });
|
|
380
|
+
|
|
381
|
+
// Access stored values
|
|
382
|
+
const step1Result = memory.get('step1');
|
|
383
|
+
|
|
384
|
+
// Results automatically become command.result
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
```
|
|
388
|
+
```
|
|
389
|
+
|
|
335
390
|
**Features:**
|
|
336
391
|
- Type-safe value storage
|
|
337
392
|
- Prerequisite verification
|
package/dist/index.d.mts
CHANGED
|
@@ -33,7 +33,11 @@ declare enum A_TYPES__CommandMetaKey {
|
|
|
33
33
|
ABSTRACTIONS = "a-command-abstractions"
|
|
34
34
|
}
|
|
35
35
|
declare enum A_CONSTANTS__A_Command_Status {
|
|
36
|
+
CREATED = "CREATED",
|
|
37
|
+
INITIALIZATION = "INITIALIZATION",
|
|
36
38
|
INITIALIZED = "INITIALIZED",
|
|
39
|
+
COMPILATION = "COMPILATION",
|
|
40
|
+
COMPILED = "COMPILED",
|
|
37
41
|
IN_PROGRESS = "IN_PROGRESS",
|
|
38
42
|
COMPLETED = "COMPLETED",
|
|
39
43
|
FAILED = "FAILED"
|
|
@@ -58,11 +62,11 @@ type A_TYPES__Command_Constructor<T = A_Command> = new (...args: any[]) => T;
|
|
|
58
62
|
/**
|
|
59
63
|
* Command initialization type
|
|
60
64
|
*/
|
|
61
|
-
type A_TYPES__Command_Init = any
|
|
65
|
+
type A_TYPES__Command_Init = Record<string, any>;
|
|
62
66
|
/**
|
|
63
67
|
* Command serialized type
|
|
64
68
|
*/
|
|
65
|
-
type A_TYPES__Command_Serialized<ResultType extends Record<string, any> = Record<string, any>> = {
|
|
69
|
+
type A_TYPES__Command_Serialized<ParamsType extends Record<string, any> = Record<string, any>, ResultType extends Record<string, any> = Record<string, any>> = {
|
|
66
70
|
/**
|
|
67
71
|
* Unique code of the command
|
|
68
72
|
*/
|
|
@@ -71,6 +75,10 @@ type A_TYPES__Command_Serialized<ResultType extends Record<string, any> = Record
|
|
|
71
75
|
* Current status of the command
|
|
72
76
|
*/
|
|
73
77
|
status: A_CONSTANTS__A_Command_Status;
|
|
78
|
+
/**
|
|
79
|
+
* Parameters used to invoke the command
|
|
80
|
+
*/
|
|
81
|
+
params: ParamsType;
|
|
74
82
|
/**
|
|
75
83
|
* The time when the command was created
|
|
76
84
|
*/
|
|
@@ -121,7 +129,7 @@ type A_TYPES__CommandMeta = {
|
|
|
121
129
|
}>;
|
|
122
130
|
};
|
|
123
131
|
|
|
124
|
-
declare class A_Command<InvokeType extends A_TYPES__Command_Init = A_TYPES__Command_Init, ResultType extends Record<string, any> = Record<string, any>, LifecycleEvents extends string = A_CONSTANTS__A_Command_Event> extends A_Entity<InvokeType, A_TYPES__Command_Serialized<ResultType>> {
|
|
132
|
+
declare class A_Command<InvokeType extends A_TYPES__Command_Init = A_TYPES__Command_Init, ResultType extends Record<string, any> = Record<string, any>, LifecycleEvents extends string | A_CONSTANTS__A_Command_Event = A_CONSTANTS__A_Command_Event> extends A_Entity<InvokeType, A_TYPES__Command_Serialized<InvokeType, ResultType>> {
|
|
125
133
|
/**
|
|
126
134
|
* Command Identifier that corresponds to the class name
|
|
127
135
|
*/
|
|
@@ -195,9 +203,15 @@ declare class A_Command<InvokeType extends A_TYPES__Command_Init = A_TYPES__Comm
|
|
|
195
203
|
/**
|
|
196
204
|
* Command invocation parameters
|
|
197
205
|
*/
|
|
198
|
-
params: InvokeType | A_TYPES__Command_Serialized<ResultType> | string);
|
|
206
|
+
params: InvokeType | A_TYPES__Command_Serialized<InvokeType, ResultType> | string);
|
|
199
207
|
init(): Promise<void>;
|
|
200
208
|
compile(): Promise<void>;
|
|
209
|
+
/**
|
|
210
|
+
* Processes the command execution
|
|
211
|
+
*
|
|
212
|
+
* @returns
|
|
213
|
+
*/
|
|
214
|
+
process(): Promise<void>;
|
|
201
215
|
/**
|
|
202
216
|
* Executes the command logic.
|
|
203
217
|
*/
|
|
@@ -243,16 +257,18 @@ declare class A_Command<InvokeType extends A_TYPES__Command_Init = A_TYPES__Comm
|
|
|
243
257
|
*
|
|
244
258
|
* @param serialized
|
|
245
259
|
*/
|
|
246
|
-
fromJSON(serialized: A_TYPES__Command_Serialized<ResultType>): void;
|
|
260
|
+
fromJSON(serialized: A_TYPES__Command_Serialized<InvokeType, ResultType>): void;
|
|
247
261
|
/**
|
|
248
262
|
* Converts the Command instance to a plain object
|
|
249
263
|
*
|
|
250
264
|
* @returns
|
|
251
265
|
*/
|
|
252
|
-
toJSON(): A_TYPES__Command_Serialized<ResultType>;
|
|
266
|
+
toJSON(): A_TYPES__Command_Serialized<InvokeType, ResultType>;
|
|
267
|
+
protected checkScopeInheritance(): void;
|
|
253
268
|
}
|
|
254
269
|
|
|
255
270
|
declare class A_CommandError extends A_Error {
|
|
271
|
+
static readonly CommandScopeBindingError = "A-Command Scope Binding Error";
|
|
256
272
|
}
|
|
257
273
|
|
|
258
274
|
interface Ifspolyfill {
|
|
@@ -768,6 +784,17 @@ declare class A_Memory<_MemoryType extends Record<string, any> = Record<string,
|
|
|
768
784
|
* @param error
|
|
769
785
|
*/
|
|
770
786
|
error(error: A_Error): Promise<void>;
|
|
787
|
+
/**
|
|
788
|
+
* Retrieves a value from the context memory
|
|
789
|
+
*
|
|
790
|
+
* @param key
|
|
791
|
+
* @returns
|
|
792
|
+
*/
|
|
793
|
+
get<K extends keyof _MemoryType>(
|
|
794
|
+
/**
|
|
795
|
+
* Key to retrieve the value for
|
|
796
|
+
*/
|
|
797
|
+
key: K): _MemoryType[K] | undefined;
|
|
771
798
|
/**
|
|
772
799
|
* Saves a value in the context memory
|
|
773
800
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -33,7 +33,11 @@ declare enum A_TYPES__CommandMetaKey {
|
|
|
33
33
|
ABSTRACTIONS = "a-command-abstractions"
|
|
34
34
|
}
|
|
35
35
|
declare enum A_CONSTANTS__A_Command_Status {
|
|
36
|
+
CREATED = "CREATED",
|
|
37
|
+
INITIALIZATION = "INITIALIZATION",
|
|
36
38
|
INITIALIZED = "INITIALIZED",
|
|
39
|
+
COMPILATION = "COMPILATION",
|
|
40
|
+
COMPILED = "COMPILED",
|
|
37
41
|
IN_PROGRESS = "IN_PROGRESS",
|
|
38
42
|
COMPLETED = "COMPLETED",
|
|
39
43
|
FAILED = "FAILED"
|
|
@@ -58,11 +62,11 @@ type A_TYPES__Command_Constructor<T = A_Command> = new (...args: any[]) => T;
|
|
|
58
62
|
/**
|
|
59
63
|
* Command initialization type
|
|
60
64
|
*/
|
|
61
|
-
type A_TYPES__Command_Init = any
|
|
65
|
+
type A_TYPES__Command_Init = Record<string, any>;
|
|
62
66
|
/**
|
|
63
67
|
* Command serialized type
|
|
64
68
|
*/
|
|
65
|
-
type A_TYPES__Command_Serialized<ResultType extends Record<string, any> = Record<string, any>> = {
|
|
69
|
+
type A_TYPES__Command_Serialized<ParamsType extends Record<string, any> = Record<string, any>, ResultType extends Record<string, any> = Record<string, any>> = {
|
|
66
70
|
/**
|
|
67
71
|
* Unique code of the command
|
|
68
72
|
*/
|
|
@@ -71,6 +75,10 @@ type A_TYPES__Command_Serialized<ResultType extends Record<string, any> = Record
|
|
|
71
75
|
* Current status of the command
|
|
72
76
|
*/
|
|
73
77
|
status: A_CONSTANTS__A_Command_Status;
|
|
78
|
+
/**
|
|
79
|
+
* Parameters used to invoke the command
|
|
80
|
+
*/
|
|
81
|
+
params: ParamsType;
|
|
74
82
|
/**
|
|
75
83
|
* The time when the command was created
|
|
76
84
|
*/
|
|
@@ -121,7 +129,7 @@ type A_TYPES__CommandMeta = {
|
|
|
121
129
|
}>;
|
|
122
130
|
};
|
|
123
131
|
|
|
124
|
-
declare class A_Command<InvokeType extends A_TYPES__Command_Init = A_TYPES__Command_Init, ResultType extends Record<string, any> = Record<string, any>, LifecycleEvents extends string = A_CONSTANTS__A_Command_Event> extends A_Entity<InvokeType, A_TYPES__Command_Serialized<ResultType>> {
|
|
132
|
+
declare class A_Command<InvokeType extends A_TYPES__Command_Init = A_TYPES__Command_Init, ResultType extends Record<string, any> = Record<string, any>, LifecycleEvents extends string | A_CONSTANTS__A_Command_Event = A_CONSTANTS__A_Command_Event> extends A_Entity<InvokeType, A_TYPES__Command_Serialized<InvokeType, ResultType>> {
|
|
125
133
|
/**
|
|
126
134
|
* Command Identifier that corresponds to the class name
|
|
127
135
|
*/
|
|
@@ -195,9 +203,15 @@ declare class A_Command<InvokeType extends A_TYPES__Command_Init = A_TYPES__Comm
|
|
|
195
203
|
/**
|
|
196
204
|
* Command invocation parameters
|
|
197
205
|
*/
|
|
198
|
-
params: InvokeType | A_TYPES__Command_Serialized<ResultType> | string);
|
|
206
|
+
params: InvokeType | A_TYPES__Command_Serialized<InvokeType, ResultType> | string);
|
|
199
207
|
init(): Promise<void>;
|
|
200
208
|
compile(): Promise<void>;
|
|
209
|
+
/**
|
|
210
|
+
* Processes the command execution
|
|
211
|
+
*
|
|
212
|
+
* @returns
|
|
213
|
+
*/
|
|
214
|
+
process(): Promise<void>;
|
|
201
215
|
/**
|
|
202
216
|
* Executes the command logic.
|
|
203
217
|
*/
|
|
@@ -243,16 +257,18 @@ declare class A_Command<InvokeType extends A_TYPES__Command_Init = A_TYPES__Comm
|
|
|
243
257
|
*
|
|
244
258
|
* @param serialized
|
|
245
259
|
*/
|
|
246
|
-
fromJSON(serialized: A_TYPES__Command_Serialized<ResultType>): void;
|
|
260
|
+
fromJSON(serialized: A_TYPES__Command_Serialized<InvokeType, ResultType>): void;
|
|
247
261
|
/**
|
|
248
262
|
* Converts the Command instance to a plain object
|
|
249
263
|
*
|
|
250
264
|
* @returns
|
|
251
265
|
*/
|
|
252
|
-
toJSON(): A_TYPES__Command_Serialized<ResultType>;
|
|
266
|
+
toJSON(): A_TYPES__Command_Serialized<InvokeType, ResultType>;
|
|
267
|
+
protected checkScopeInheritance(): void;
|
|
253
268
|
}
|
|
254
269
|
|
|
255
270
|
declare class A_CommandError extends A_Error {
|
|
271
|
+
static readonly CommandScopeBindingError = "A-Command Scope Binding Error";
|
|
256
272
|
}
|
|
257
273
|
|
|
258
274
|
interface Ifspolyfill {
|
|
@@ -768,6 +784,17 @@ declare class A_Memory<_MemoryType extends Record<string, any> = Record<string,
|
|
|
768
784
|
* @param error
|
|
769
785
|
*/
|
|
770
786
|
error(error: A_Error): Promise<void>;
|
|
787
|
+
/**
|
|
788
|
+
* Retrieves a value from the context memory
|
|
789
|
+
*
|
|
790
|
+
* @param key
|
|
791
|
+
* @returns
|
|
792
|
+
*/
|
|
793
|
+
get<K extends keyof _MemoryType>(
|
|
794
|
+
/**
|
|
795
|
+
* Key to retrieve the value for
|
|
796
|
+
*/
|
|
797
|
+
key: K): _MemoryType[K] | undefined;
|
|
771
798
|
/**
|
|
772
799
|
* Saves a value in the context memory
|
|
773
800
|
*
|
package/dist/index.js
CHANGED
|
@@ -92,7 +92,11 @@ var A_TYPES__CommandMetaKey = /* @__PURE__ */ ((A_TYPES__CommandMetaKey2) => {
|
|
|
92
92
|
return A_TYPES__CommandMetaKey2;
|
|
93
93
|
})(A_TYPES__CommandMetaKey || {});
|
|
94
94
|
var A_CONSTANTS__A_Command_Status = /* @__PURE__ */ ((A_CONSTANTS__A_Command_Status2) => {
|
|
95
|
+
A_CONSTANTS__A_Command_Status2["CREATED"] = "CREATED";
|
|
96
|
+
A_CONSTANTS__A_Command_Status2["INITIALIZATION"] = "INITIALIZATION";
|
|
95
97
|
A_CONSTANTS__A_Command_Status2["INITIALIZED"] = "INITIALIZED";
|
|
98
|
+
A_CONSTANTS__A_Command_Status2["COMPILATION"] = "COMPILATION";
|
|
99
|
+
A_CONSTANTS__A_Command_Status2["COMPILED"] = "COMPILED";
|
|
96
100
|
A_CONSTANTS__A_Command_Status2["IN_PROGRESS"] = "IN_PROGRESS";
|
|
97
101
|
A_CONSTANTS__A_Command_Status2["COMPLETED"] = "COMPLETED";
|
|
98
102
|
A_CONSTANTS__A_Command_Status2["FAILED"] = "FAILED";
|
|
@@ -137,6 +141,15 @@ var A_Memory = class extends aConcept.A_Fragment {
|
|
|
137
141
|
async error(error) {
|
|
138
142
|
this._errors.add(error);
|
|
139
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Retrieves a value from the context memory
|
|
146
|
+
*
|
|
147
|
+
* @param key
|
|
148
|
+
* @returns
|
|
149
|
+
*/
|
|
150
|
+
get(key) {
|
|
151
|
+
return this._memory.get(key);
|
|
152
|
+
}
|
|
140
153
|
/**
|
|
141
154
|
* Saves a value in the context memory
|
|
142
155
|
*
|
|
@@ -175,6 +188,9 @@ var A_Memory = class extends aConcept.A_Fragment {
|
|
|
175
188
|
return obj;
|
|
176
189
|
}
|
|
177
190
|
};
|
|
191
|
+
var A_CommandError = class extends aConcept.A_Error {
|
|
192
|
+
};
|
|
193
|
+
A_CommandError.CommandScopeBindingError = "A-Command Scope Binding Error";
|
|
178
194
|
|
|
179
195
|
// src/lib/A-Command/A-Command.entity.ts
|
|
180
196
|
var A_Command = class extends aConcept.A_Entity {
|
|
@@ -275,28 +291,49 @@ var A_Command = class extends aConcept.A_Entity {
|
|
|
275
291
|
// --------------------------------------------------------------------------
|
|
276
292
|
// should create a new Task in DB with basic records
|
|
277
293
|
async init() {
|
|
278
|
-
this._status
|
|
279
|
-
|
|
280
|
-
if (!this.scope.isInheritedFrom(aConcept.A_Context.scope(this))) {
|
|
281
|
-
this.scope.inherit(aConcept.A_Context.scope(this));
|
|
294
|
+
if (this._status !== "CREATED" /* CREATED */) {
|
|
295
|
+
return;
|
|
282
296
|
}
|
|
297
|
+
this._status = "INITIALIZATION" /* INITIALIZATION */;
|
|
298
|
+
this._startTime = /* @__PURE__ */ new Date();
|
|
299
|
+
this.checkScopeInheritance();
|
|
283
300
|
this.emit("init");
|
|
284
|
-
|
|
301
|
+
await this.call("init", this.scope);
|
|
302
|
+
this._status = "INITIALIZED" /* INITIALIZED */;
|
|
285
303
|
}
|
|
286
304
|
// Should compile everything before execution
|
|
287
305
|
async compile() {
|
|
306
|
+
if (this._status !== "INITIALIZED" /* INITIALIZED */) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
this.checkScopeInheritance();
|
|
310
|
+
this._status = "COMPILATION" /* COMPILATION */;
|
|
288
311
|
this.emit("compile");
|
|
289
|
-
|
|
312
|
+
await this.call("compile", this.scope);
|
|
313
|
+
this._status = "COMPILED" /* COMPILED */;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Processes the command execution
|
|
317
|
+
*
|
|
318
|
+
* @returns
|
|
319
|
+
*/
|
|
320
|
+
async process() {
|
|
321
|
+
if (this._status !== "COMPILED" /* COMPILED */)
|
|
322
|
+
return;
|
|
323
|
+
this._status = "IN_PROGRESS" /* IN_PROGRESS */;
|
|
324
|
+
this.checkScopeInheritance();
|
|
325
|
+
this.emit("execute");
|
|
326
|
+
await this.call("execute", this.scope);
|
|
290
327
|
}
|
|
291
328
|
/**
|
|
292
329
|
* Executes the command logic.
|
|
293
330
|
*/
|
|
294
331
|
async execute() {
|
|
332
|
+
this.checkScopeInheritance();
|
|
295
333
|
try {
|
|
296
334
|
await this.init();
|
|
297
335
|
await this.compile();
|
|
298
|
-
this.
|
|
299
|
-
await this.call("execute", this.scope);
|
|
336
|
+
await this.process();
|
|
300
337
|
await this.complete();
|
|
301
338
|
} catch (error) {
|
|
302
339
|
await this.fail();
|
|
@@ -306,6 +343,7 @@ var A_Command = class extends aConcept.A_Entity {
|
|
|
306
343
|
* Marks the command as completed
|
|
307
344
|
*/
|
|
308
345
|
async complete() {
|
|
346
|
+
this.checkScopeInheritance();
|
|
309
347
|
this._status = "COMPLETED" /* COMPLETED */;
|
|
310
348
|
this._endTime = /* @__PURE__ */ new Date();
|
|
311
349
|
this._result = this.scope.resolve(A_Memory).toJSON();
|
|
@@ -316,6 +354,7 @@ var A_Command = class extends aConcept.A_Entity {
|
|
|
316
354
|
* Marks the command as failed
|
|
317
355
|
*/
|
|
318
356
|
async fail() {
|
|
357
|
+
this.checkScopeInheritance();
|
|
319
358
|
this._status = "FAILED" /* FAILED */;
|
|
320
359
|
this._endTime = /* @__PURE__ */ new Date();
|
|
321
360
|
this._errors = this.scope.resolve(A_Memory).Errors;
|
|
@@ -371,7 +410,7 @@ var A_Command = class extends aConcept.A_Entity {
|
|
|
371
410
|
this._executionScope = new aConcept.A_Scope();
|
|
372
411
|
this._executionScope.register(new A_Memory());
|
|
373
412
|
this._params = newEntity;
|
|
374
|
-
this._status = "
|
|
413
|
+
this._status = "CREATED" /* CREATED */;
|
|
375
414
|
}
|
|
376
415
|
/**
|
|
377
416
|
* Allows to convert serialized data to Command instance
|
|
@@ -397,7 +436,8 @@ var A_Command = class extends aConcept.A_Entity {
|
|
|
397
436
|
memory.error(new aConcept.A_Error(err));
|
|
398
437
|
});
|
|
399
438
|
}
|
|
400
|
-
this.
|
|
439
|
+
this._params = serialized.params;
|
|
440
|
+
this._status = serialized.status || "CREATED" /* CREATED */;
|
|
401
441
|
}
|
|
402
442
|
/**
|
|
403
443
|
* Converts the Command instance to a plain object
|
|
@@ -409,6 +449,7 @@ var A_Command = class extends aConcept.A_Entity {
|
|
|
409
449
|
...super.toJSON(),
|
|
410
450
|
code: this.code,
|
|
411
451
|
status: this._status,
|
|
452
|
+
params: this._params,
|
|
412
453
|
startedAt: this._startTime ? this._startTime.toISOString() : void 0,
|
|
413
454
|
endedAt: this._endTime ? this._endTime.toISOString() : void 0,
|
|
414
455
|
duration: this.duration,
|
|
@@ -416,8 +457,21 @@ var A_Command = class extends aConcept.A_Entity {
|
|
|
416
457
|
errors: this.errors ? Array.from(this.errors).map((err) => err.toJSON()) : void 0
|
|
417
458
|
};
|
|
418
459
|
}
|
|
419
|
-
|
|
420
|
-
|
|
460
|
+
checkScopeInheritance() {
|
|
461
|
+
let attachedScope;
|
|
462
|
+
try {
|
|
463
|
+
attachedScope = aConcept.A_Context.scope(this);
|
|
464
|
+
} catch (error) {
|
|
465
|
+
throw new A_CommandError({
|
|
466
|
+
title: A_CommandError.CommandScopeBindingError,
|
|
467
|
+
description: `Command ${this.code} is not bound to any context scope. Ensure the command is properly registered within a context before execution.`,
|
|
468
|
+
originalError: error
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
if (!this.scope.isInheritedFrom(aConcept.A_Context.scope(this))) {
|
|
472
|
+
this.scope.inherit(aConcept.A_Context.scope(this));
|
|
473
|
+
}
|
|
474
|
+
}
|
|
421
475
|
};
|
|
422
476
|
|
|
423
477
|
// src/lib/A-Config/A-Config.constants.ts
|