@adaas/a-utils 0.2.1 → 0.2.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adaas/a-utils",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "A-Utils is a set of utilities that are used across the ADAAS ecosystem. This package is designed to be a collection of utilities that are used across the ADAAS ecosystem.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.cjs",
package/src/index.ts CHANGED
@@ -99,7 +99,7 @@ export { A_SignalBus } from './lib/A-Signal/components/A-SignalBus.component';
99
99
  export { A_SignalConfig } from './lib/A-Signal/context/A-SignalConfig.context';
100
100
  export { A_SignalState } from './lib/A-Signal/context/A-SignalState.context';
101
101
  export * from './lib/A-Signal/A-Signal.types';
102
- export * from './lib/A-Signal/A-Signal.constants';
102
+ // export * from './lib/A-Signal/A-Signal.constants';
103
103
 
104
104
  // ============================================================================
105
105
  // A-Schedule Components
@@ -1,10 +0,0 @@
1
-
2
- export enum A_SignalFeatures {
3
- Next = '_A_SignalFeatures_Next',
4
- }
5
-
6
-
7
-
8
- export enum A_SignalVectorFeatures {
9
- Next = '_A_SignalVectorFeatures_Next',
10
- }
@@ -1,11 +1,12 @@
1
- import { A_Caller, A_Component, A_Context, A_Feature, A_Inject, A_Scope } from "@adaas/a-concept";
2
- import { A_SignalFeatures } from "../A-Signal.constants";
1
+ import { A_Caller, A_Component, A_Context, A_Dependency, A_Error, A_Feature, A_Inject, A_Scope } from "@adaas/a-concept";
3
2
  import { A_SignalState } from "../context/A-SignalState.context";
4
3
  import { A_SignalConfig } from "../context/A-SignalConfig.context";
5
4
  import { A_Config } from "../../A-Config/A-Config.context";
6
5
  import { A_Logger } from "../../A-Logger/A-Logger.component";
7
6
  import { A_Signal } from "../entities/A-Signal.entity";
8
7
  import { A_Frame } from "@adaas/a-frame";
8
+ import { A_SignalBusFeatures } from "./A-SignalBus.constants";
9
+ import { A_SignalBusError } from "./A-SignalBus.error";
9
10
 
10
11
 
11
12
 
@@ -27,44 +28,82 @@ import { A_Frame } from "@adaas/a-frame";
27
28
  export class A_SignalBus extends A_Component {
28
29
 
29
30
 
30
- /**
31
- * This methods extends A-Signal Emit feature to handle signal emission within the bus.
32
- *
33
- * It updates the signal state and emits the updated signal vector.
34
- *
35
- * @param signal
36
- * @param globalConfig
37
- * @param logger
38
- * @param state
39
- * @param config
40
- * @returns
41
- */
31
+ @A_Frame.Method({
32
+ description: 'Emit multiple signals through the signal bus.'
33
+ })
34
+ async next(...signals: A_Signal[]) {
35
+ const scope = new A_Scope({
36
+ name: `A_SignalBus-Next-Scope`,
37
+ entities: signals
38
+ })
39
+ .inherit(A_Context.scope(this));
40
+
41
+ try {
42
+ await this.call(A_SignalBusFeatures.onBeforeNext, scope);
43
+ await this.call(A_SignalBusFeatures.onNext, scope);
44
+
45
+ scope.destroy();
46
+
47
+ } catch (error) {
48
+
49
+ let wrappedError;
50
+
51
+ switch (true) {
52
+ case error instanceof A_SignalBusError:
53
+ wrappedError = error;
54
+ break;
55
+
56
+ case error instanceof A_Error && error.originalError instanceof A_SignalBusError:
57
+ wrappedError = error.originalError;
58
+ break;
59
+
60
+ default:
61
+ wrappedError = new A_SignalBusError({
62
+ title: A_SignalBusError.SignalProcessingError,
63
+ description: `An error occurred while processing the signal.`,
64
+ originalError: error
65
+ })
66
+ break;
67
+ }
68
+
69
+ scope.register(wrappedError);
70
+
71
+ await this.call(A_SignalBusFeatures.onError);
72
+
73
+ scope.destroy();
74
+ }
75
+ }
76
+
77
+
78
+ @A_Feature.Extend({
79
+ before: /.*/
80
+ })
81
+ protected async [A_SignalBusFeatures.onError](
82
+ @A_Inject(A_Error) error: A_Error,
83
+ @A_Inject(A_Logger) logger?: A_Logger,
84
+ ...args: any[]
85
+ ) {
86
+ logger?.error(error);
87
+ }
88
+
42
89
  @A_Feature.Extend({
43
- scope: [A_Signal]
90
+ scope: [A_SignalBus],
91
+ before: /.*/
44
92
  })
45
- async [A_SignalFeatures.Next](
46
- @A_Inject(A_Caller) signal: A_Signal,
93
+ async [A_SignalBusFeatures.onBeforeNext](
47
94
  @A_Inject(A_Scope) scope: A_Scope,
48
95
 
49
96
  @A_Inject(A_Config) globalConfig?: A_Config<['A_SIGNAL_VECTOR_STRUCTURE']>,
50
- @A_Inject(A_Logger) logger?: A_Logger,
51
97
  @A_Inject(A_SignalState) state?: A_SignalState,
98
+
99
+ @A_Inject(A_Logger) logger?: A_Logger,
52
100
  @A_Inject(A_SignalConfig) config?: A_SignalConfig,
53
101
  ) {
54
-
55
- /*
56
- 1) create a signal when it occurs via new A_Signal('somedata')
57
- 2) emit a signal when needed via signal.emit(scope)
58
- 3) the bus should listen for all emitted signals within the scope
59
- 4) when a signal is emitted, the bus should store a signal in some place (probably it's memory)
60
- */
61
-
62
102
  /**
63
103
  * We need a context where component is registered, to prevent any duplicate registrations
64
104
  */
65
105
  const componentContext = A_Context.scope(this);
66
106
 
67
-
68
107
  if (!config) {
69
108
  config = new A_SignalConfig({
70
109
  stringStructure: globalConfig?.get('A_SIGNAL_VECTOR_STRUCTURE') || undefined
@@ -80,22 +119,61 @@ export class A_SignalBus extends A_Component {
80
119
  state = new A_SignalState(config.structure);
81
120
  componentContext.register(state);
82
121
  }
122
+ }
123
+
124
+ /**
125
+ * This methods extends A-Signal Emit feature to handle signal emission within the bus.
126
+ *
127
+ * It updates the signal state and emits the updated signal vector.
128
+ *
129
+ * @param signal
130
+ * @param globalConfig
131
+ * @param logger
132
+ * @param state
133
+ * @param config
134
+ * @returns
135
+ */
136
+ @A_Feature.Extend({
137
+ scope: [A_SignalBus],
138
+ before: /.*/
139
+ })
140
+ async [A_SignalBusFeatures.onNext](
141
+ // @A_Dependency.All()
142
+ // @A_Inject(A_Signal) signal: A_Signal[],
143
+ @A_Inject(A_Scope) scope: A_Scope,
144
+
145
+ @A_Dependency.Required()
146
+ @A_Inject(A_SignalState) state: A_SignalState,
147
+
148
+ @A_Inject(A_Config) globalConfig?: A_Config<['A_SIGNAL_VECTOR_STRUCTURE']>,
149
+ @A_Inject(A_Logger) logger?: A_Logger,
150
+ @A_Inject(A_SignalConfig) config?: A_SignalConfig,
151
+ ) {
152
+ /*
153
+ 1) create a signal when it occurs via new A_Signal('somedata')
154
+ 2) emit a signal when needed via bus.next(signal)
155
+ 3) the bus should listen for all emitted signals within the scope
156
+ 4) when a signal is emitted, the bus should store a signal in some place (probably it's memory)
157
+ */
158
+ const signals = scope.resolveFlatAll<A_Signal>(A_Signal);
83
159
 
160
+ for (const signal of signals) {
84
161
 
85
- if (!state.has(signal))
86
- return;
162
+ if (!state.has(signal))
163
+ return;
87
164
 
165
+ // ------------------------------------------------------------------
166
+ // And finally if all checks are passed, we can update the state
167
+ // ------------------------------------------------------------------
88
168
 
89
- // ------------------------------------------------------------------
90
- // And finally if all checks are passed, we can update the state
91
- // ------------------------------------------------------------------
169
+ logger?.debug(`A_SignalBus: Updating state for signal '${signal.constructor.name}' with data:`, signal.data);
92
170
 
93
- logger?.debug(`A_SignalBus: Updating state for signal '${signal.constructor.name}' with data:`, signal.data);
171
+ state.set(signal);
94
172
 
95
- state.set(signal);
173
+ const vector = state.toVector();
96
174
 
97
- const vector = state.toVector();
175
+ scope.register(vector);
98
176
 
99
- await vector.next(scope);
177
+ }
100
178
  }
101
179
  }
@@ -0,0 +1,7 @@
1
+
2
+ export enum A_SignalBusFeatures {
3
+ onBeforeNext = '_A_SignalBusFeatures_onBeforeNext',
4
+ onNext = '_A_SignalBusFeatures_onNext',
5
+
6
+ onError = '_A_SignalBusFeatures_onError',
7
+ }
@@ -0,0 +1,9 @@
1
+ import { A_Error } from "@adaas/a-concept";
2
+
3
+
4
+
5
+ export class A_SignalBusError extends A_Error {
6
+
7
+
8
+ static readonly SignalProcessingError = 'Signal processing error';
9
+ }
@@ -1,6 +1,5 @@
1
1
  import { A_Entity, A_Scope } from "@adaas/a-concept";
2
2
  import { A_Signal_Init, A_Signal_Serialized } from "../A-Signal.types";
3
- import { A_SignalFeatures } from "../A-Signal.constants";
4
3
  import { A_Frame } from "@adaas/a-frame";
5
4
 
6
5
  /**
@@ -52,17 +51,6 @@ export class A_Signal<
52
51
  this.data = newEntity.data;
53
52
  }
54
53
 
55
- /**
56
- * Emits this signal within the provided scope.
57
- *
58
- * Scope is mandatory since signal itself should not be registered in the scope,
59
- * but should use particular scope context to use proper set of components
60
- *
61
- * @param scope
62
- */
63
- async next(scope: A_Scope) {
64
- await this.call(A_SignalFeatures.Next, scope);
65
- }
66
54
 
67
55
 
68
56
  toJSON(): A_Signal_Serialized<_TSignalDataType> {
@@ -1,7 +1,6 @@
1
1
  import { A_Entity, A_Scope, A_TYPES__Component_Constructor, A_TYPES__Entity_Constructor } from "@adaas/a-concept";
2
2
  import { A_SignalVector_Serialized, A_SignalVector_Init } from "../A-Signal.types";
3
3
  import { A_Signal } from "./A-Signal.entity";
4
- import { A_SignalVectorFeatures } from "../A-Signal.constants";
5
4
  import { A_Frame } from "@adaas/a-frame";
6
5
 
7
6
 
@@ -94,15 +93,6 @@ export class A_SignalVector<
94
93
  }
95
94
 
96
95
 
97
- /**
98
- * Emits the signal vector to the specified scope.
99
- *
100
- * @param scope
101
- */
102
- async next(scope: A_Scope): Promise<void> {
103
- return await this.call(A_SignalVectorFeatures.Next, scope);
104
- }
105
-
106
96
 
107
97
  /**
108
98
  * Checks if the vector contains a signal of the specified type.
@@ -1,6 +1,6 @@
1
- import { A_Caller, A_Component, A_Concept, A_Container, A_Context, A_Feature, A_Inject, A_Scope } from "@adaas/a-concept";
2
- import { A_SignalVectorFeatures } from "@adaas/a-utils/lib/A-Signal/A-Signal.constants";
1
+ import { A_Component, A_Concept, A_Container, A_Context, A_Feature, A_Inject, A_Scope } from "@adaas/a-concept";
3
2
  import { A_SignalBus } from "@adaas/a-utils/lib/A-Signal/components/A-SignalBus.component";
3
+ import { A_SignalBusFeatures } from "@adaas/a-utils/lib/A-Signal/components/A-SignalBus.constants";
4
4
  import { A_SignalConfig } from "@adaas/a-utils/lib/A-Signal/context/A-SignalConfig.context";
5
5
  import { A_Signal } from "@adaas/a-utils/lib/A-Signal/entities/A-Signal.entity";
6
6
  import { A_SignalVector } from "@adaas/a-utils/lib/A-Signal/entities/A-SignalVector.entity";
@@ -71,23 +71,19 @@ describe('A-Signal tests', () => {
71
71
  class UserIntentionListener extends A_Component {
72
72
  @A_Concept.Start()
73
73
  async start(
74
- @A_Inject(A_Scope) scope: A_Scope,
75
74
  @A_Inject(A_SignalBus) bus: A_SignalBus
76
75
  ) {
77
-
78
-
79
- const signal = new A_Signal({
76
+ await bus.next(new A_Signal({
80
77
  data: {
81
78
  buttonId: 'submit-order'
82
79
  }
83
- })
84
- await signal.next(scope)
80
+ }))
85
81
  }
86
82
 
87
83
 
88
84
  @A_Feature.Extend()
89
- async [A_SignalVectorFeatures.Next](
90
- @A_Inject(A_Caller) vector: A_SignalVector
85
+ async [A_SignalBusFeatures.onNext](
86
+ @A_Inject(A_SignalVector) vector: A_SignalVector
91
87
  ) {
92
88
  result = vector;
93
89
  }
@@ -145,13 +141,13 @@ describe('A-Signal tests', () => {
145
141
  }
146
142
  })
147
143
 
148
- await signal.next(scope)
144
+ await bus.next(signal)
149
145
  }
150
146
 
151
147
 
152
148
  @A_Feature.Extend()
153
- async [A_SignalVectorFeatures.Next](
154
- @A_Inject(A_Caller) vector: A_SignalVector
149
+ async [A_SignalBusFeatures.onNext](
150
+ @A_Inject(A_SignalVector) vector: A_SignalVector
155
151
  ) {
156
152
  result = vector;
157
153
  }