Skip to content

Commit

Permalink
chore!: Extract dependency graphing from core as extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Iku-turso committed Mar 25, 2022
1 parent d255f06 commit 4ab3eeb
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Dependency graphing for Injectable in Ogre Tools

Todo

## Documentation

Check unit tests for documentation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],

'@babel/react',
],
};
19 changes: 19 additions & 0 deletions packages/injectable-extension-for-dependency-graphing/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DiContainer, InjectionToken } from '@ogre-tools/injectable';

declare module '@ogre-tools/injectable-extensions-for-dependency-graphing' {
interface GraphCustomizer {
shouldCustomize: (instance: any) => boolean;
// Todo: add proper typing
customizeLink: (link: any) => void;
customizeNode: (node: any) => void;
}

export const dependencyGraphCustomizerToken: InjectionToken<
GraphCustomizer,
void
>;

export function registerDependencyGraphing(di: DiContainer): void;

export const plantUmlDependencyGraphInjectable: InjectionToken<string, void>;
}
11 changes: 11 additions & 0 deletions packages/injectable-extension-for-dependency-graphing/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
dependencyGraphCustomizerToken,
plantUmlDependencyGraphInjectable,
registerDependencyGraphing,
} from './src/dependency-graphing';

export {
registerDependencyGraphing,
plantUmlDependencyGraphInjectable,
dependencyGraphCustomizerToken,
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions packages/injectable-extension-for-dependency-graphing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@ogre-tools/injectable-extensions-for-dependency-graphing",
"private": false,
"version": "5.2.0",
"description": "Dependency graphing for Injectable in Ogre Tools",
"repository": {
"type": "git",
"url": "https://github.com/ogre-works/ogre-tools"
},
"main": "build/index.js",
"types": "./index.d.ts",
"keywords": [
"js"
],
"author": "Ogre Works",
"license": "MIT",
"dependencies": {
"@ogre-tools/fp": "^5.2.0",
"@ogre-tools/injectable": "^5.2.0",
"lodash": "^4.17.21"
},
"peerDependencies": {},
"bugs": {
"url": "https://github.com/ogre-works/ogre-tools/issues"
},
"homepage": "https://github.com/ogre-works/ogre-tools#readme",
"publishConfig": {
"access": "public"
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import getInjectable from '../../../getInjectable/getInjectable';
import { injectionDecoratorToken } from '../../createContainer';
import lifecycleEnum from '../../lifecycleEnum';
import camelCase from 'lodash/fp/camelCase';
import getInjectionToken, {
injectionTokenSymbol,
} from '../../../getInjectionToken/getInjectionToken';
import last from 'lodash/fp/last';
import get from 'lodash/fp/get';
import some from 'lodash/fp/some';
import { isPromise, pipeline } from '@ogre-tools/fp';
import filter from 'lodash/fp/filter';
import tap from 'lodash/fp/tap';

import {
getInjectable,
getInjectionToken,
injectionDecoratorToken,
injectionTokenSymbol,
lifecycleEnum,
} from '@ogre-tools/injectable';

import { isPromise, pipeline } from '@ogre-tools/fp';
import lifecycleEnumForDependencyGraphing from './lifecycleEnumForDependencyGraphing';

export const registerDependencyGraphing = di => {
di.register(plantUmlDependencyGraphInjectable);
di.register(dependencyGraphStateInjectable);
Expand Down Expand Up @@ -78,10 +82,10 @@ const plantUmlExtractorInjectable = getInjectable({

if (alias.aliasType === injectionTokenSymbol) {
node.isInjectionToken = true;
node.lifecycle = tokenLifecycle;
node.lifecycle = lifecycleEnumForDependencyGraphing.injectionToken;
node.infos.add('Token');
} else {
node.lifecycle = alias.lifecycle;
node.lifecycle = lifecycleEnumForDependencyGraphing[alias.lifecycle.id];
}

const instanceIsAsync = isPromise(instance);
Expand Down Expand Up @@ -190,9 +194,3 @@ const toPlantUmlLink = ({

return `${parentId} --${lineStyle}up* ${dependencyId} #text:${textColor} ${infosString} `;
};

const tokenLifecycle = {
name: 'Transient',
shortName: 'X',
color: 'orange',
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import getDi from '../test-utils/getDiForUnitTesting';
import getInjectable from '../getInjectable/getInjectable';

import {
dependencyGraphCustomizerToken,
plantUmlDependencyGraphInjectable,
registerDependencyGraphing,
} from './extensions/dependency-graphing/dependency-graphing';
} from './dependency-graphing';

import isEqual from 'lodash/fp/isEqual';
import getInjectionToken from '../getInjectionToken/getInjectionToken';
import lifecycleEnum from './lifecycleEnum';
import {
createContainer,
getInjectable,
getInjectionToken,
lifecycleEnum,
} from '@ogre-tools/injectable';

const getDi = (...injectables) => {
const di = createContainer();

injectables.forEach(di.register);

return di;
};

describe('createContainer.dependency-graph', () => {
it('given dependency graphing, dependencies and injected, creates Plant-UML graph', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default {
singleton: {
name: 'Singleton',
shortName: 'S',
color: 'lightGreen',
},

keyedSingleton: {
name: 'Keyed',
shortName: 'K',
color: 'pink',
},

transient: {
name: 'Transient',
shortName: 'T',
color: 'orchid',
},

injectionToken: {
name: 'Transient',
shortName: 'X',
color: 'orange',
},
};
23 changes: 11 additions & 12 deletions packages/injectable/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import getInjectionToken from './src/getInjectionToken/getInjectionToken';
import getInjectionToken, {
injectionTokenSymbol,
} from './src/getInjectionToken/getInjectionToken';

import getInjectable from './src/getInjectable/getInjectable';
import lifecycleEnum from './src/dependency-injection-container/lifecycleEnum';
import createContainer from './src/dependency-injection-container/createContainer';

import {
registerDependencyGraphing,
plantUmlDependencyGraphInjectable,
dependencyGraphCustomizerToken,
} from './src/dependency-injection-container/extensions/dependency-graphing/dependency-graphing';
import createContainer, {
injectionDecoratorToken,
} from './src/dependency-injection-container/createContainer';

export {
getInjectionToken,
createContainer,
getInjectable,
getInjectionToken,
injectionDecoratorToken,
injectionTokenSymbol,
lifecycleEnum,
createContainer,
registerDependencyGraphing,
plantUmlDependencyGraphInjectable,
dependencyGraphCustomizerToken,
};
13 changes: 0 additions & 13 deletions packages/injectable/ogre-tools-injectable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,4 @@ declare module '@ogre-tools/injectable' {
export function createContainer(
...getRequireContexts: (() => RequireContext)[]
): DiContainer;

interface Customizer {
shouldCustomize: (instance: any) => boolean;
// Todo: add proper typing
customizeLink: (link: any) => void;
customizeNode: (node: any) => void;
}

export const dependencyGraphCustomizerToken: InjectionToken<Customizer, void>;

export function registerDependencyGraphing(di: DiContainer): void;

export const plantUmlDependencyGraphInjectable: InjectionToken<string, void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@ export const storedInstanceKey = Symbol('stored-instance-key');

export default {
singleton: {
name: 'Singleton',
shortName: 'S',
color: 'lightGreen',
id: 'singleton',
getInstanceKey: () => storedInstanceKey,
},

keyedSingleton: ({ getInstanceKey }) => ({
name: 'Keyed',
shortName: 'K',
color: 'pink',
id: 'keyedSingleton',
getInstanceKey,
}),

transient: {
name: 'Transient',
shortName: 'T',
color: 'orchid',
id: 'transient',
getInstanceKey: () => nonStoredInstanceKey,
},
};

0 comments on commit 4ab3eeb

Please sign in to comment.