From 9c0cf205d7116d8438d307aee8b5752c37588851 Mon Sep 17 00:00:00 2001 From: Iku-turso Date: Mon, 21 Feb 2022 14:35:36 +0200 Subject: [PATCH] feat: Show lifecycle names in dependency graphing --- ...Container.dependency-graphing.test.js.snap | 19 +++++++++++++++++++ ...reateContainer.dependency-graphing.test.js | 15 +-------------- ...-monitoring-for-injected-functions.test.js | 6 ++++++ ...error-monitoring-for-instantiation.test.js | 6 ++++++ .../createContainer.js | 12 ++++++++++-- .../dependency-graphing.js | 17 +++++++++++++---- .../lifecycleEnum.js | 4 +++- 7 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 packages/injectable/src/dependency-injection-container/__snapshots__/createContainer.dependency-graphing.test.js.snap diff --git a/packages/injectable/src/dependency-injection-container/__snapshots__/createContainer.dependency-graphing.test.js.snap b/packages/injectable/src/dependency-injection-container/__snapshots__/createContainer.dependency-graphing.test.js.snap new file mode 100644 index 00000000..2ae6bcfc --- /dev/null +++ b/packages/injectable/src/dependency-injection-container/__snapshots__/createContainer.dependency-graphing.test.js.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`createContainer.dependency-graph given dependency graphing, dependencies and injected, creates Plant-UML graph 1`] = ` +"@startuml +hide members +hide circle +\\"Setup(some-setuppable)\\" ..up* \\"some-child-injectable\\" : Setup +class \\"Setup(some-setuppable)\\" +class \\"some-child-injectable\\" +\\"some-child-injectable\\" ..up* \\"some-injection-token\\" : Setup +\\"some-injection-token\\" ..up* \\"some-token-injectable\\" : Setup +class \\"some-injection-token\\" #orange +class \\"some-token-injectable\\" +\\"Setup(some-setuppable)\\" ..up* \\"some-setuppable\\" : Setup +class \\"some-setuppable\\" +class \\"some-parent-injectable\\" +\\"some-parent-injectable\\" --up* \\"some-child-injectable\\" +@enduml" +`; diff --git a/packages/injectable/src/dependency-injection-container/createContainer.dependency-graphing.test.js b/packages/injectable/src/dependency-injection-container/createContainer.dependency-graphing.test.js index e92e4935..4ff03a05 100644 --- a/packages/injectable/src/dependency-injection-container/createContainer.dependency-graphing.test.js +++ b/packages/injectable/src/dependency-injection-container/createContainer.dependency-graphing.test.js @@ -50,19 +50,6 @@ describe('createContainer.dependency-graph', () => { const graph = di.inject(plantUmlDependencyGraphInjectable); - expect(graph).toBe( - [ - '@startuml', - 'hide members', - 'hide circle', - '"Setup(some-setuppable)" ..up* "some-child-injectable" : Setup', - '"some-child-injectable" ..up* "some-injection-token" : Setup', - 'class "some-injection-token" #orange', - '"some-injection-token" ..up* "some-token-injectable" : Setup', - '"Setup(some-setuppable)" ..up* "some-setuppable" : Setup', - '"some-parent-injectable" --up* "some-child-injectable"', - '@enduml', - ].join('\n'), - ); + expect(graph).toMatchSnapshot(); }); }); diff --git a/packages/injectable/src/dependency-injection-container/createContainer.error-monitoring-for-injected-functions.test.js b/packages/injectable/src/dependency-injection-container/createContainer.error-monitoring-for-injected-functions.test.js index 71cc8094..828672de 100644 --- a/packages/injectable/src/dependency-injection-container/createContainer.error-monitoring-for-injected-functions.test.js +++ b/packages/injectable/src/dependency-injection-container/createContainer.error-monitoring-for-injected-functions.test.js @@ -67,11 +67,13 @@ describe('createContainer.error-monitoring-for-injected-functions', () => { { id: 'some-parent-injectable', instantiationParameter: 'some-instantiation-parameter-for-parent', + lifecycleName: 'Transient', }, { id: 'some-child-injectable', instantiationParameter: 'some-instantiation-parameter-for-child', + lifecycleName: 'Transient', }, ], }); @@ -97,11 +99,13 @@ describe('createContainer.error-monitoring-for-injected-functions', () => { { id: 'some-parent-injectable', instantiationParameter: 'some-instantiation-parameter-for-parent', + lifecycleName: 'Transient', }, { id: 'some-child-injectable', instantiationParameter: 'some-instantiation-parameter-for-child', + lifecycleName: 'Transient', }, ], }); @@ -171,11 +175,13 @@ describe('createContainer.error-monitoring-for-injected-functions', () => { { id: 'some-parent-injectable', instantiationParameter: 'some-instantiation-parameter-for-parent', + lifecycleName: 'Transient', }, { id: 'some-child-injectable', instantiationParameter: 'some-instantiation-parameter-for-child', + lifecycleName: 'Transient', }, ], }); diff --git a/packages/injectable/src/dependency-injection-container/createContainer.error-monitoring-for-instantiation.test.js b/packages/injectable/src/dependency-injection-container/createContainer.error-monitoring-for-instantiation.test.js index 2c3dfeda..406e88ac 100644 --- a/packages/injectable/src/dependency-injection-container/createContainer.error-monitoring-for-instantiation.test.js +++ b/packages/injectable/src/dependency-injection-container/createContainer.error-monitoring-for-instantiation.test.js @@ -70,11 +70,13 @@ describe('createContainer.error-monitoring-for-instantiation', () => { { id: 'some-parent-injectable', instantiationParameter: 'some-instantiation-parameter-for-parent', + lifecycleName: 'Transient', }, { id: 'some-child-injectable', instantiationParameter: 'some-instantiation-parameter-for-child', + lifecycleName: 'Transient', }, ], }); @@ -148,11 +150,13 @@ describe('createContainer.error-monitoring-for-instantiation', () => { { id: 'some-parent-injectable', instantiationParameter: 'some-instantiation-parameter-for-parent', + lifecycleName: 'Transient', }, { id: 'some-child-injectable', instantiationParameter: 'some-instantiation-parameter-for-child', + lifecycleName: 'Transient', }, ], }); @@ -178,11 +182,13 @@ describe('createContainer.error-monitoring-for-instantiation', () => { { id: 'some-parent-injectable', instantiationParameter: 'some-instantiation-parameter-for-parent', + lifecycleName: 'Transient', }, { id: 'some-child-injectable', instantiationParameter: 'some-instantiation-parameter-for-child', + lifecycleName: 'Transient', }, ], }); diff --git a/packages/injectable/src/dependency-injection-container/createContainer.js b/packages/injectable/src/dependency-injection-container/createContainer.js index ca002101..d0a84eaf 100644 --- a/packages/injectable/src/dependency-injection-container/createContainer.js +++ b/packages/injectable/src/dependency-injection-container/createContainer.js @@ -199,7 +199,11 @@ export default (...listOfGetRequireContexts) => { } return privateDi.inject(alias, parameter, [ - { id: setuppable.id, isSetup: true }, + { + id: setuppable.id, + isSetup: true, + lifecycleName: setuppable.lifecycle.name, + }, ]); }, }); @@ -322,7 +326,11 @@ const getInstance = ({ const newContext = [ ...oldContext, - { id: injectable.id, instantiationParameter }, + { + id: injectable.id, + instantiationParameter, + lifecycleName: injectable.lifecycle.name, + }, ]; const injectableCausingCycle = pipeline( diff --git a/packages/injectable/src/dependency-injection-container/extensions/dependency-graphing/dependency-graphing.js b/packages/injectable/src/dependency-injection-container/extensions/dependency-graphing/dependency-graphing.js index 26cdbefb..45a74b2c 100644 --- a/packages/injectable/src/dependency-injection-container/extensions/dependency-graphing/dependency-graphing.js +++ b/packages/injectable/src/dependency-injection-container/extensions/dependency-graphing/dependency-graphing.js @@ -39,10 +39,6 @@ const plantUmlExtractorInjectable = getInjectable({ return ({ context }) => { context.reduce((parent, dependency) => { - if (parent.isInjectionToken) { - plantUmlState.add(`class "${parent.id}" #orange`); - } - if (parent.isChildOfSetup === true) { plantUmlState.add(`"${parent.id}" ..up* "${dependency.id}" : Setup`); @@ -53,12 +49,25 @@ const plantUmlExtractorInjectable = getInjectable({ plantUmlState.add( `"Setup(${parent.id})" ..up* "${dependency.id}" : Setup`, ); + return { ...dependency, isChildOfSetup: true }; } plantUmlState.add(`"${parent.id}" --up* "${dependency.id}"`); return dependency; }); + + context.forEach(contextItem => { + if (contextItem.isInjectionToken) { + plantUmlState.add(`class "${contextItem.id}" #orange`); + } else if (contextItem.isSetup === true) { + plantUmlState.add(`class "Setup(${contextItem.id})"`); + } else { + plantUmlState.add( + `class "${contextItem.id}"<${contextItem.lifecycleName}>`, + ); + } + }); }; }, diff --git a/packages/injectable/src/dependency-injection-container/lifecycleEnum.js b/packages/injectable/src/dependency-injection-container/lifecycleEnum.js index f71d863c..269ff001 100644 --- a/packages/injectable/src/dependency-injection-container/lifecycleEnum.js +++ b/packages/injectable/src/dependency-injection-container/lifecycleEnum.js @@ -2,12 +2,14 @@ export const nonStoredInstanceKey = Symbol('non-stored-instance-key'); export default { singleton: { + name: 'Singleton', getInstanceKey: () => 'singleton', }, - keyedSingleton: ({ getInstanceKey }) => ({ getInstanceKey }), + keyedSingleton: ({ getInstanceKey }) => ({ name: 'Keyed', getInstanceKey }), transient: { + name: 'Transient', getInstanceKey: () => nonStoredInstanceKey, }, };