@adaas/a-utils 0.1.32 → 0.1.34

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.1.32",
3
+ "version": "0.1.34",
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",
@@ -1,10 +1,10 @@
1
1
 
2
2
  export enum A_SignalFeatures {
3
- Emit = '_A_SignalFeatures_Emit',
3
+ Next = '_A_SignalFeatures_Next',
4
4
  }
5
5
 
6
6
 
7
7
 
8
- export enum A_SignalBusFeatures {
9
- Emit = '_A_SignalBusFeatures_Emit',
8
+ export enum A_SignalVectorFeatures {
9
+ Next = '_A_SignalVectorFeatures_Next',
10
10
  }
@@ -16,6 +16,9 @@ export type A_SignalConfig_Init = {
16
16
  * OR "A_RouterWatcher,A_ScopeWatcher,A_LoggerWatcher"
17
17
  */
18
18
  stringStructure?: string
19
+
20
+
21
+ propagateSignals?: boolean
19
22
  }
20
23
 
21
24
 
@@ -1,5 +1,5 @@
1
1
  import { A_Caller, A_Component, A_Context, A_Feature, A_Inject, A_Scope } from "@adaas/a-concept";
2
- import { A_SignalBusFeatures, A_SignalFeatures } from "../A-Signal.constants";
2
+ import { A_SignalFeatures } from "../A-Signal.constants";
3
3
  import { A_SignalState } from "../context/A-SignalState.context";
4
4
  import { A_SignalConfig } from "../context/A-SignalConfig.context";
5
5
  import { A_Config } from "../../A-Config/A-Config.context";
@@ -36,7 +36,7 @@ export class A_SignalBus extends A_Component {
36
36
  @A_Feature.Extend({
37
37
  scope: [A_Signal]
38
38
  })
39
- async [A_SignalFeatures.Emit](
39
+ async [A_SignalFeatures.Next](
40
40
  @A_Inject(A_Caller) signal: A_Signal,
41
41
  @A_Inject(A_Scope) scope: A_Scope,
42
42
 
@@ -90,23 +90,6 @@ export class A_SignalBus extends A_Component {
90
90
 
91
91
  const vector = state.toVector();
92
92
 
93
- const nextScope = new A_Scope({
94
- name: `A_SignalBus_Next_Scope_of_${this.constructor.name}`,
95
- entities: [vector]
96
- })
97
- .inherit(scope);
98
-
99
- try {
100
-
101
- await this.call(A_SignalBusFeatures.Emit, nextScope);
102
-
103
- nextScope.destroy();
104
-
105
- } catch (error) {
106
-
107
- nextScope.destroy();
108
-
109
- throw error;
110
- }
93
+ await vector.next(scope);
111
94
  }
112
95
  }
@@ -54,8 +54,8 @@ export class A_Signal<
54
54
  *
55
55
  * @param scope
56
56
  */
57
- async emit(scope: A_Scope) {
58
- await this.call(A_SignalFeatures.Emit, scope);
57
+ async next(scope: A_Scope) {
58
+ await this.call(A_SignalFeatures.Next, scope);
59
59
  }
60
60
 
61
61
 
@@ -1,6 +1,7 @@
1
- import { A_Entity, A_TYPES__Component_Constructor, A_TYPES__Entity_Constructor } from "@adaas/a-concept";
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";
4
5
 
5
6
 
6
7
  /**
@@ -57,6 +58,46 @@ export class A_SignalVector<
57
58
  }
58
59
 
59
60
 
61
+ /**
62
+ * Enables iteration over the signals in the vector.
63
+ *
64
+ * @returns
65
+ */
66
+ [Symbol.iterator](): Iterator<TSignals[number]> {
67
+ let pointer = 0;
68
+ const signals = this.structure.map((signalConstructor) => {
69
+ const signalIndex = this._signals.findIndex(s => s.constructor === signalConstructor);
70
+ return signalIndex !== -1 ? this._signals[signalIndex] : undefined;
71
+ }) as TSignals[number][];
72
+
73
+ return {
74
+ next(): IteratorResult<TSignals[number]> {
75
+ if (pointer < signals.length) {
76
+ return {
77
+ done: false,
78
+ value: signals[pointer++]
79
+ };
80
+ } else {
81
+ return {
82
+ done: true,
83
+ value: undefined as any
84
+ };
85
+ }
86
+ }
87
+ };
88
+ }
89
+
90
+
91
+ /**
92
+ * Emits the signal vector to the specified scope.
93
+ *
94
+ * @param scope
95
+ */
96
+ async next(scope: A_Scope): Promise<void> {
97
+ return await this.call(A_SignalVectorFeatures.Next, scope);
98
+ }
99
+
100
+
60
101
  /**
61
102
  * Checks if the vector contains a signal of the specified type.
62
103
  *
@@ -74,22 +115,27 @@ export class A_SignalVector<
74
115
  return this.structure.includes(signalConstructor);
75
116
  }
76
117
 
77
- get(signal: A_Signal): Record<string, any> | undefined
78
- get(signalConstructor: A_TYPES__Component_Constructor<A_Signal>): Record<string, any> | undefined
79
- get(param1: A_Signal | A_TYPES__Component_Constructor<A_Signal>): Record<string, any> | undefined {
80
- let signalConstructor: A_TYPES__Component_Constructor<A_Signal>;
118
+ /**
119
+ * Retrieves the signal of the specified type from the vector.
120
+ *
121
+ * @param signal
122
+ */
123
+ get<T extends A_Signal>(signal: T): T | undefined
124
+ get<T extends A_Signal>(signalConstructor: A_TYPES__Entity_Constructor<T>): T | undefined
125
+ get<T extends A_Signal>(param1: T | A_TYPES__Entity_Constructor<T>): T | undefined {
126
+ let signalConstructor: A_TYPES__Entity_Constructor<A_Signal>;
81
127
 
82
128
  if (param1 instanceof A_Entity) {
83
- signalConstructor = param1.constructor as A_TYPES__Component_Constructor<A_Signal>;
129
+ signalConstructor = param1.constructor as A_TYPES__Entity_Constructor<A_Signal>;
84
130
  } else {
85
- signalConstructor = param1;
131
+ signalConstructor = param1 as A_TYPES__Entity_Constructor<A_Signal>;
86
132
  }
87
133
 
88
134
  const index = this._signals.findIndex(s => s.constructor === signalConstructor);
89
135
  if (index === -1) {
90
136
  return undefined;
91
137
  }
92
- return this._signals[index];
138
+ return this._signals[index] as T;
93
139
  }
94
140
 
95
141
 
@@ -195,4 +241,3 @@ export class A_SignalVector<
195
241
  };
196
242
  }
197
243
  }
198
-
@@ -1,5 +1,5 @@
1
- import { A_Component, A_Concept, A_Container, A_Context, A_Feature, A_Inject, A_Scope } from "@adaas/a-concept";
2
- import { A_SignalBusFeatures, A_SignalFeatures } from "@adaas/a-utils/lib/A-Signal/A-Signal.constants";
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";
3
3
  import { A_SignalBus } from "@adaas/a-utils/lib/A-Signal/components/A-SignalBus.component";
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";
@@ -10,6 +10,60 @@ import { A_SignalVector } from "@adaas/a-utils/lib/A-Signal/entities/A-SignalVec
10
10
  jest.retryTimes(0);
11
11
 
12
12
  describe('A-Signal tests', () => {
13
+ it('Should Allow to create a new Signal', async () => {
14
+ const signal = new A_Signal<{ message: string }>({
15
+ data: {
16
+ message: 'Hello, World!'
17
+ }
18
+ });
19
+
20
+ expect(signal).toBeDefined();
21
+ expect(signal).toBeInstanceOf(A_Signal);
22
+ expect(signal.data.message).toBe('Hello, World!');
23
+ });
24
+ it('Should Allow to create a new Signal Vector', async () => {
25
+ class MySignalA extends A_Signal<{ buttonId: string }> { }
26
+ class MySignalB extends A_Signal<{ pageId: string }> { }
27
+
28
+ const vector = new A_SignalVector({
29
+ structure: [MySignalA, MySignalB],
30
+ values: [
31
+ new MySignalA({ data: { buttonId: 'submit-order' } }),
32
+ new MySignalB({ data: { pageId: 'home-page' } })
33
+ ]
34
+ });
35
+
36
+ expect(vector).toBeDefined();
37
+ expect(vector).toBeInstanceOf(A_SignalVector);
38
+ expect(vector.length).toBe(2);
39
+ expect((await vector.toDataVector())[0]?.buttonId).toBe('submit-order');
40
+ expect((await vector.toDataVector())[1]?.pageId).toBe('home-page');
41
+ });
42
+ it('Should Allow to get signals fro Signal Vector', async () => {
43
+ class MySignalA extends A_Signal<{ buttonId: string }> { }
44
+ class MySignalB extends A_Signal<{ pageId: string }> { }
45
+ class MySignalC extends A_Signal<{ userId: string }> { }
46
+
47
+ const vector = new A_SignalVector({
48
+ structure: [MySignalA, MySignalB],
49
+ values: [
50
+ new MySignalA({ data: { buttonId: 'submit-order' } }),
51
+ new MySignalB({ data: { pageId: 'home-page' } })
52
+ ]
53
+ });
54
+
55
+ const signalA = vector.get(MySignalA);
56
+ const signalB = vector.get(MySignalB);
57
+ const signalC = vector.get(MySignalC);
58
+
59
+ expect(signalA).toBeDefined();
60
+ expect(signalA).toBeInstanceOf(MySignalA);
61
+ expect(signalA?.data.buttonId).toBe('submit-order');
62
+ expect(signalB).toBeDefined();
63
+ expect(signalB).toBeInstanceOf(MySignalB);
64
+ expect(signalB?.data.pageId).toBe('home-page');
65
+ expect(signalC).toBeUndefined();
66
+ });
13
67
  it('Should Allow to emit basic signal structure', async () => {
14
68
 
15
69
  let result: A_SignalVector | undefined = undefined;
@@ -27,13 +81,13 @@ describe('A-Signal tests', () => {
27
81
  buttonId: 'submit-order'
28
82
  }
29
83
  })
30
- await signal.emit(scope)
84
+ await signal.next(scope)
31
85
  }
32
86
 
33
87
 
34
88
  @A_Feature.Extend()
35
- async [A_SignalBusFeatures.Emit](
36
- @A_Inject(A_SignalVector) vector: A_SignalVector
89
+ async [A_SignalVectorFeatures.Next](
90
+ @A_Inject(A_Caller) vector: A_SignalVector
37
91
  ) {
38
92
  result = vector;
39
93
  }
@@ -91,13 +145,13 @@ describe('A-Signal tests', () => {
91
145
  }
92
146
  })
93
147
 
94
- await signal.emit(scope)
148
+ await signal.next(scope)
95
149
  }
96
150
 
97
151
 
98
152
  @A_Feature.Extend()
99
- async [A_SignalBusFeatures.Emit](
100
- @A_Inject(A_SignalVector) vector: A_SignalVector
153
+ async [A_SignalVectorFeatures.Next](
154
+ @A_Inject(A_Caller) vector: A_SignalVector
101
155
  ) {
102
156
  result = vector;
103
157
  }
@@ -130,4 +184,5 @@ describe('A-Signal tests', () => {
130
184
  expect((await result!.toDataVector())[2]).toBeUndefined();
131
185
 
132
186
  });
187
+
133
188
  })