@adaas/a-concept 0.1.35 → 0.1.37

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-concept",
3
- "version": "0.1.35",
3
+ "version": "0.1.37",
4
4
  "description": "A-Concept is a framework to build new Applications within or outside the ADAAS ecosystem. This framework is designed to be modular structure regardless environment and program goal.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.cjs",
@@ -248,9 +248,22 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
248
248
  this._name = params.name;
249
249
 
250
250
  // 2) get scope from where feature is called
251
- const componentScope = params.component
252
- ? A_Context.scope(params.component)
253
- : params.scope as A_Scope;
251
+ // 2) get scope from where feature is called
252
+ let componentScope: A_Scope | undefined;
253
+ // uses to extend a component scope if component is not registered in the context
254
+ let externalScope: A_Scope | undefined = params.scope;
255
+
256
+ try {
257
+ if (params.component)
258
+ componentScope = A_Context.scope(params.component);
259
+ } catch (error) {
260
+ if (!externalScope)
261
+ throw error;
262
+ }
263
+
264
+ if (componentScope && externalScope && !externalScope.isInheritedFrom(componentScope)) {
265
+ externalScope.inherit(componentScope);
266
+ }
254
267
 
255
268
  // 3) create caller wrapper for the simple injection of the caller component
256
269
  // - Just to prevent issues with undefined caller in features without component
@@ -261,7 +274,7 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
261
274
  const scope = A_Context.allocate(this);
262
275
 
263
276
  // 5) ensure that the scope of the caller component is inherited by the feature scope
264
- scope.inherit(componentScope);
277
+ scope.inherit(componentScope || externalScope!);
265
278
 
266
279
  // 6) create steps manager to organize steps into stages
267
280
  this._SM = new A_StepsManager(params.template);
@@ -291,7 +304,20 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
291
304
  this._name = params.name;
292
305
 
293
306
  // 2) get scope from where feature is called
294
- const componentScope = params.scope ? params.scope : A_Context.scope(params.component);
307
+ let componentScope: A_Scope | undefined;
308
+ // uses to extend a component scope if component is not registered in the context
309
+ let externalScope: A_Scope | undefined = params.scope;
310
+
311
+ try {
312
+ componentScope = A_Context.scope(params.component);
313
+ } catch (error) {
314
+ if (!externalScope)
315
+ throw error;
316
+ }
317
+
318
+ if (componentScope && externalScope && !externalScope.isInheritedFrom(componentScope)) {
319
+ externalScope.inherit(componentScope);
320
+ }
295
321
 
296
322
  // 3) create caller wrapper for the simple injection of the caller component
297
323
  this._caller = new A_Caller<T>(params.component);
@@ -300,7 +326,7 @@ export class A_Feature<T extends A_TYPES__FeatureAvailableComponents = A_TYPES__
300
326
  const scope = A_Context.allocate(this);
301
327
 
302
328
  // 5) ensure that the scope of the caller component is inherited by the feature scope
303
- scope.inherit(componentScope);
329
+ scope.inherit(componentScope || externalScope!);
304
330
 
305
331
  // 6) retrieve the template from the context
306
332
  const template = A_Context.featureTemplate(this._name, this._caller.component, scope);
@@ -153,7 +153,7 @@ export class A_Fragment<
153
153
  * }
154
154
  * ```
155
155
  */
156
- get<K extends keyof _MetaItems>(param: K): _MetaItems[K] | undefined {
156
+ get(param: keyof _MetaItems): _MetaItems[typeof param] | undefined {
157
157
  return this._meta.get(param);
158
158
  }
159
159
 
@@ -169,7 +169,7 @@ export class A_Fragment<
169
169
  * fragment.set('role', 'admin');
170
170
  * ```
171
171
  */
172
- set<K extends keyof _MetaItems>(param: K, value: _MetaItems[K]): void {
172
+ set(param: keyof _MetaItems, value: _MetaItems[typeof param]): void {
173
173
  this._meta.set(param, value);
174
174
  }
175
175
 
@@ -268,15 +268,15 @@ export class A_Fragment<
268
268
  * ```
269
269
  */
270
270
  clone(newName?: string): A_Fragment<_MetaItems, _SerializedType> {
271
- const cloned = new (this.constructor as any)({
272
- name: newName || `${this._name}_copy`
271
+ const cloned = new (this.constructor as any)({
272
+ name: newName || `${this._name}_copy`
273
273
  });
274
-
274
+
275
275
  // Copy all meta data
276
276
  this._meta.toArray().forEach(([key, value]) => {
277
277
  cloned.set(key, value);
278
278
  });
279
-
279
+
280
280
  return cloned;
281
281
  }
282
282
 
@@ -362,4 +362,61 @@ describe('A-Feature tests', () => {
362
362
 
363
363
  expect(executionOrder).toEqual(['stepOne', 'stepThree']);
364
364
  });
365
+
366
+ it('Should allow proceed with external scope', async () => {
367
+ const executionOrder: string[] = [];
368
+
369
+ class CustomComponent extends A_Component {
370
+
371
+ doSomething() {
372
+ executionOrder.push('customComponentAction');
373
+ }
374
+
375
+ }
376
+
377
+ // 1) create a base component with some feature
378
+ class ComponentA extends A_Component {
379
+
380
+ @A_Feature.Define({ invoke: false })
381
+ async feature1() {
382
+ const scope = new A_Scope({ name: 'ExternalScopeCaller', components: [CustomComponent] });
383
+
384
+ executionOrder.push('stepOne');
385
+
386
+ await this.call('feature1', scope);
387
+ }
388
+ }
389
+
390
+
391
+ class ComponentB extends A_Component {
392
+
393
+ @A_Feature.Extend({
394
+ name: 'feature1',
395
+ })
396
+ async feature1Extension(
397
+ @A_Inject(A_Scope) scope: A_Scope,
398
+ @A_Inject(CustomComponent) customComponent: CustomComponent
399
+ ) {
400
+ expect(customComponent).toBeInstanceOf(CustomComponent);
401
+ expect(customComponent.doSomething).toBeInstanceOf(Function);
402
+
403
+ executionOrder.push('stepThree');
404
+ }
405
+ }
406
+
407
+
408
+ // 2) create a running scope
409
+ const scope = new A_Scope({ name: 'TestScope' , components: [ComponentA, ComponentB] });
410
+
411
+
412
+ // 3) create an instance of the component from the scope
413
+ const myComponent = scope.resolve(ComponentA)!;
414
+ expect(myComponent).toBeInstanceOf(ComponentA);
415
+
416
+ await myComponent.feature1();
417
+
418
+ expect(executionOrder).toEqual(['stepOne', 'stepThree']);
419
+ });
420
+
421
+
365
422
  });
@@ -1,4 +1,5 @@
1
1
  import { A_Fragment } from "@adaas/a-concept/global/A-Fragment/A-Fragment.class";
2
+ import { A_Scope } from "../src";
2
3
 
3
4
  jest.retryTimes(0);
4
5
 
@@ -182,7 +183,7 @@ describe('A-Fragment Tests', () => {
182
183
  });
183
184
 
184
185
  it('It Should be possible to create an inherited A_Fragment instance', async () => {
185
-
186
+
186
187
  class CustomFragment extends A_Fragment<{ sessionId: string; timestamp: number }> {
187
188
  constructor() {
188
189
  super({ name: 'CustomFragment' });
@@ -209,7 +210,7 @@ describe('A-Fragment Tests', () => {
209
210
  });
210
211
 
211
212
  it('It Should be possible to create a fragment with custom serialization', async () => {
212
-
213
+
213
214
  class SessionFragment extends A_Fragment<
214
215
  { sessionId: string; timestamp: number },
215
216
  { name: string; sessionData: string }
@@ -286,5 +287,42 @@ describe('A-Fragment Tests', () => {
286
287
  expect(fragment.has('c')).toBe(true);
287
288
  expect(fragment.size()).toBe(2);
288
289
  });
290
+ it('It Should allow override get method correctly', async () => {
291
+
292
+ const scope = new A_Scope();
293
+
294
+ class CustomFragment<T extends string[]> extends A_Fragment<{
295
+ [key in T[number]]: any
296
+ }> {
297
+
298
+ constructor(properties: T) {
299
+ super({ name: 'CustomFragment' });
300
+ properties.forEach((prop) => {
301
+ this.set(prop, `value_of_${prop}`);
302
+ });
303
+ }
304
+ get<K extends T[number]>(param: K): { [key in T[number]]: any; }[K] | undefined {
305
+ const originalValue = super.get(param);
306
+ if (originalValue !== undefined) {
307
+ return `custom_${originalValue}`;
308
+ }
309
+ return undefined;
310
+ }
311
+
289
312
 
313
+ }
314
+
315
+
316
+ const fragment = new CustomFragment<['key1', 'key2']>(['key1', 'key2']);
317
+
318
+ scope.register(fragment);
319
+
320
+
321
+ expect(fragment.get('key1')).toBe('custom_value_of_key1');
322
+ expect(fragment.get('key2')).toBe('custom_value_of_key2');
323
+ expect(fragment.get('key3' as any)).toBeUndefined();
324
+
325
+ scope.destroy();
326
+
327
+ });
290
328
  });