@adaas/a-utils 0.2.1 → 0.2.3
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 +8 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +15 -24
- package/dist/index.d.ts +15 -24
- package/dist/index.mjs +8 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +5 -1
- package/src/lib/A-Signal/A-Signal.constants.ts +0 -10
- package/src/lib/A-Signal/components/A-SignalBus.component.ts +114 -36
- package/src/lib/A-Signal/components/A-SignalBus.constants.ts +7 -0
- package/src/lib/A-Signal/components/A-SignalBus.error.ts +9 -0
- package/src/lib/A-Signal/components/A-SignalBus.types.ts +0 -0
- package/src/lib/A-Signal/entities/A-Signal.entity.ts +0 -12
- package/src/lib/A-Signal/entities/A-SignalVector.entity.ts +0 -10
- package/tests/A-Signal.test.ts +9 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaas/a-utils",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
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
|
@@ -96,10 +96,14 @@ export { A_Route } from './lib/A-Route/A-Route.entity';
|
|
|
96
96
|
export { A_Signal } from './lib/A-Signal/entities/A-Signal.entity';
|
|
97
97
|
export { A_SignalVector } from './lib/A-Signal/entities/A-SignalVector.entity';
|
|
98
98
|
export { A_SignalBus } from './lib/A-Signal/components/A-SignalBus.component';
|
|
99
|
+
export { A_SignalBusError } from './lib/A-Signal/components/A-SignalBus.error';
|
|
100
|
+
|
|
101
|
+
export * from './lib/A-Signal/components/A-SignalBus.constants';
|
|
102
|
+
// export * from './lib/A-Signal/components/A-SignalBus.types';
|
|
99
103
|
export { A_SignalConfig } from './lib/A-Signal/context/A-SignalConfig.context';
|
|
100
104
|
export { A_SignalState } from './lib/A-Signal/context/A-SignalState.context';
|
|
101
105
|
export * from './lib/A-Signal/A-Signal.types';
|
|
102
|
-
export * from './lib/A-Signal/A-Signal.constants';
|
|
106
|
+
// export * from './lib/A-Signal/A-Signal.constants';
|
|
103
107
|
|
|
104
108
|
// ============================================================================
|
|
105
109
|
// A-Schedule Components
|
|
@@ -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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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: [
|
|
90
|
+
scope: [A_SignalBus],
|
|
91
|
+
before: /.*/
|
|
44
92
|
})
|
|
45
|
-
async [
|
|
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
|
-
|
|
86
|
-
|
|
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
|
-
|
|
171
|
+
state.set(signal);
|
|
94
172
|
|
|
95
|
-
|
|
173
|
+
const vector = state.toVector();
|
|
96
174
|
|
|
97
|
-
|
|
175
|
+
scope.register(vector);
|
|
98
176
|
|
|
99
|
-
|
|
177
|
+
}
|
|
100
178
|
}
|
|
101
179
|
}
|
|
File without changes
|
|
@@ -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.
|
package/tests/A-Signal.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
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 [
|
|
90
|
-
@A_Inject(
|
|
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
|
|
144
|
+
await bus.next(signal)
|
|
149
145
|
}
|
|
150
146
|
|
|
151
147
|
|
|
152
148
|
@A_Feature.Extend()
|
|
153
|
-
async [
|
|
154
|
-
@A_Inject(
|
|
149
|
+
async [A_SignalBusFeatures.onNext](
|
|
150
|
+
@A_Inject(A_SignalVector) vector: A_SignalVector
|
|
155
151
|
) {
|
|
156
152
|
result = vector;
|
|
157
153
|
}
|