@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/dist/index.mjs
CHANGED
|
@@ -90,7 +90,11 @@ var A_TYPES__CommandMetaKey = /* @__PURE__ */ ((A_TYPES__CommandMetaKey2) => {
|
|
|
90
90
|
return A_TYPES__CommandMetaKey2;
|
|
91
91
|
})(A_TYPES__CommandMetaKey || {});
|
|
92
92
|
var A_CONSTANTS__A_Command_Status = /* @__PURE__ */ ((A_CONSTANTS__A_Command_Status2) => {
|
|
93
|
+
A_CONSTANTS__A_Command_Status2["CREATED"] = "CREATED";
|
|
94
|
+
A_CONSTANTS__A_Command_Status2["INITIALIZATION"] = "INITIALIZATION";
|
|
93
95
|
A_CONSTANTS__A_Command_Status2["INITIALIZED"] = "INITIALIZED";
|
|
96
|
+
A_CONSTANTS__A_Command_Status2["COMPILATION"] = "COMPILATION";
|
|
97
|
+
A_CONSTANTS__A_Command_Status2["COMPILED"] = "COMPILED";
|
|
94
98
|
A_CONSTANTS__A_Command_Status2["IN_PROGRESS"] = "IN_PROGRESS";
|
|
95
99
|
A_CONSTANTS__A_Command_Status2["COMPLETED"] = "COMPLETED";
|
|
96
100
|
A_CONSTANTS__A_Command_Status2["FAILED"] = "FAILED";
|
|
@@ -135,6 +139,15 @@ var A_Memory = class extends A_Fragment {
|
|
|
135
139
|
async error(error) {
|
|
136
140
|
this._errors.add(error);
|
|
137
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Retrieves a value from the context memory
|
|
144
|
+
*
|
|
145
|
+
* @param key
|
|
146
|
+
* @returns
|
|
147
|
+
*/
|
|
148
|
+
get(key) {
|
|
149
|
+
return this._memory.get(key);
|
|
150
|
+
}
|
|
138
151
|
/**
|
|
139
152
|
* Saves a value in the context memory
|
|
140
153
|
*
|
|
@@ -173,6 +186,9 @@ var A_Memory = class extends A_Fragment {
|
|
|
173
186
|
return obj;
|
|
174
187
|
}
|
|
175
188
|
};
|
|
189
|
+
var A_CommandError = class extends A_Error {
|
|
190
|
+
};
|
|
191
|
+
A_CommandError.CommandScopeBindingError = "A-Command Scope Binding Error";
|
|
176
192
|
|
|
177
193
|
// src/lib/A-Command/A-Command.entity.ts
|
|
178
194
|
var A_Command = class extends A_Entity {
|
|
@@ -273,28 +289,49 @@ var A_Command = class extends A_Entity {
|
|
|
273
289
|
// --------------------------------------------------------------------------
|
|
274
290
|
// should create a new Task in DB with basic records
|
|
275
291
|
async init() {
|
|
276
|
-
this._status
|
|
277
|
-
|
|
278
|
-
if (!this.scope.isInheritedFrom(A_Context.scope(this))) {
|
|
279
|
-
this.scope.inherit(A_Context.scope(this));
|
|
292
|
+
if (this._status !== "CREATED" /* CREATED */) {
|
|
293
|
+
return;
|
|
280
294
|
}
|
|
295
|
+
this._status = "INITIALIZATION" /* INITIALIZATION */;
|
|
296
|
+
this._startTime = /* @__PURE__ */ new Date();
|
|
297
|
+
this.checkScopeInheritance();
|
|
281
298
|
this.emit("init");
|
|
282
|
-
|
|
299
|
+
await this.call("init", this.scope);
|
|
300
|
+
this._status = "INITIALIZED" /* INITIALIZED */;
|
|
283
301
|
}
|
|
284
302
|
// Should compile everything before execution
|
|
285
303
|
async compile() {
|
|
304
|
+
if (this._status !== "INITIALIZED" /* INITIALIZED */) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
this.checkScopeInheritance();
|
|
308
|
+
this._status = "COMPILATION" /* COMPILATION */;
|
|
286
309
|
this.emit("compile");
|
|
287
|
-
|
|
310
|
+
await this.call("compile", this.scope);
|
|
311
|
+
this._status = "COMPILED" /* COMPILED */;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Processes the command execution
|
|
315
|
+
*
|
|
316
|
+
* @returns
|
|
317
|
+
*/
|
|
318
|
+
async process() {
|
|
319
|
+
if (this._status !== "COMPILED" /* COMPILED */)
|
|
320
|
+
return;
|
|
321
|
+
this._status = "IN_PROGRESS" /* IN_PROGRESS */;
|
|
322
|
+
this.checkScopeInheritance();
|
|
323
|
+
this.emit("execute");
|
|
324
|
+
await this.call("execute", this.scope);
|
|
288
325
|
}
|
|
289
326
|
/**
|
|
290
327
|
* Executes the command logic.
|
|
291
328
|
*/
|
|
292
329
|
async execute() {
|
|
330
|
+
this.checkScopeInheritance();
|
|
293
331
|
try {
|
|
294
332
|
await this.init();
|
|
295
333
|
await this.compile();
|
|
296
|
-
this.
|
|
297
|
-
await this.call("execute", this.scope);
|
|
334
|
+
await this.process();
|
|
298
335
|
await this.complete();
|
|
299
336
|
} catch (error) {
|
|
300
337
|
await this.fail();
|
|
@@ -304,6 +341,7 @@ var A_Command = class extends A_Entity {
|
|
|
304
341
|
* Marks the command as completed
|
|
305
342
|
*/
|
|
306
343
|
async complete() {
|
|
344
|
+
this.checkScopeInheritance();
|
|
307
345
|
this._status = "COMPLETED" /* COMPLETED */;
|
|
308
346
|
this._endTime = /* @__PURE__ */ new Date();
|
|
309
347
|
this._result = this.scope.resolve(A_Memory).toJSON();
|
|
@@ -314,6 +352,7 @@ var A_Command = class extends A_Entity {
|
|
|
314
352
|
* Marks the command as failed
|
|
315
353
|
*/
|
|
316
354
|
async fail() {
|
|
355
|
+
this.checkScopeInheritance();
|
|
317
356
|
this._status = "FAILED" /* FAILED */;
|
|
318
357
|
this._endTime = /* @__PURE__ */ new Date();
|
|
319
358
|
this._errors = this.scope.resolve(A_Memory).Errors;
|
|
@@ -369,7 +408,7 @@ var A_Command = class extends A_Entity {
|
|
|
369
408
|
this._executionScope = new A_Scope();
|
|
370
409
|
this._executionScope.register(new A_Memory());
|
|
371
410
|
this._params = newEntity;
|
|
372
|
-
this._status = "
|
|
411
|
+
this._status = "CREATED" /* CREATED */;
|
|
373
412
|
}
|
|
374
413
|
/**
|
|
375
414
|
* Allows to convert serialized data to Command instance
|
|
@@ -395,7 +434,8 @@ var A_Command = class extends A_Entity {
|
|
|
395
434
|
memory.error(new A_Error(err));
|
|
396
435
|
});
|
|
397
436
|
}
|
|
398
|
-
this.
|
|
437
|
+
this._params = serialized.params;
|
|
438
|
+
this._status = serialized.status || "CREATED" /* CREATED */;
|
|
399
439
|
}
|
|
400
440
|
/**
|
|
401
441
|
* Converts the Command instance to a plain object
|
|
@@ -407,6 +447,7 @@ var A_Command = class extends A_Entity {
|
|
|
407
447
|
...super.toJSON(),
|
|
408
448
|
code: this.code,
|
|
409
449
|
status: this._status,
|
|
450
|
+
params: this._params,
|
|
410
451
|
startedAt: this._startTime ? this._startTime.toISOString() : void 0,
|
|
411
452
|
endedAt: this._endTime ? this._endTime.toISOString() : void 0,
|
|
412
453
|
duration: this.duration,
|
|
@@ -414,8 +455,21 @@ var A_Command = class extends A_Entity {
|
|
|
414
455
|
errors: this.errors ? Array.from(this.errors).map((err) => err.toJSON()) : void 0
|
|
415
456
|
};
|
|
416
457
|
}
|
|
417
|
-
|
|
418
|
-
|
|
458
|
+
checkScopeInheritance() {
|
|
459
|
+
let attachedScope;
|
|
460
|
+
try {
|
|
461
|
+
attachedScope = A_Context.scope(this);
|
|
462
|
+
} catch (error) {
|
|
463
|
+
throw new A_CommandError({
|
|
464
|
+
title: A_CommandError.CommandScopeBindingError,
|
|
465
|
+
description: `Command ${this.code} is not bound to any context scope. Ensure the command is properly registered within a context before execution.`,
|
|
466
|
+
originalError: error
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
if (!this.scope.isInheritedFrom(A_Context.scope(this))) {
|
|
470
|
+
this.scope.inherit(A_Context.scope(this));
|
|
471
|
+
}
|
|
472
|
+
}
|
|
419
473
|
};
|
|
420
474
|
|
|
421
475
|
// src/lib/A-Config/A-Config.constants.ts
|