Skip to content

Commit

Permalink
Merge pull request #18 from AVykhrystyuk/feature/impl-legacy-factory-…
Browse files Browse the repository at this point in the history
…registry

Feature/implement legacy factory registry (es5)
  • Loading branch information
AVykhrystyuk committed Jul 10, 2021
2 parents 7f3a858 + 400a419 commit 6be9213
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Expand Up @@ -68,6 +68,9 @@ module.exports = {
// Allow properties in ctors, as it reduces code duplicates
'@typescript-eslint/no-parameter-properties': ['error', { allows: ['private readonly', 'protected readonly', 'public readonly'] }],

// You know what you're doing when you force-unwrap (dammit operator) an optional field
'@typescript-eslint/no-non-null-assertion': 'off',

'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }],
},
overrides: [
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Expand Up @@ -67,7 +67,7 @@ function buildOutput() {

/* esm */
{
file: `${ES5_BUNDLE_PATH}/index.esm${suffix}.js`,
file: `${ES5_BUNDLE_PATH}/index${suffix}.js`,
format: 'es',
sourcemap: true,
plugins: [terser()],
Expand Down
12 changes: 8 additions & 4 deletions src/impl/container-impl.spec.ts
Expand Up @@ -4,15 +4,19 @@ import * as assert from 'assert';
import { Container, DependencyInjectionError } from '../interfaces';
import { ContainerImpl } from './container-impl';
import { createModernFactoryRegistry } from './create-modern-factory-registry';
import { createLegacyFactoryRegistry } from './create-legacy-factory-registry';
import { Cat, Dog, Fish, Meat, Milk, stringToken, symbolToken } from './container-types.spec';

describe('ContainerImpl', () => {
// TODO: use legacyFactoryRegistry
describe('with ModernFactoryRegistry', () => {
const testBlocks = [
{ title: 'ContainerImpl with ModernFactoryRegistry', makeRegistry: createModernFactoryRegistry },
{ title: 'ContainerImpl with LegacyFactoryRegistry', makeRegistry: createLegacyFactoryRegistry },
];
testBlocks.forEach(({ title, makeRegistry }) => {
describe(title, () => {
let container: Container;

beforeEach(() => {
container = new ContainerImpl(createModernFactoryRegistry());
container = new ContainerImpl(makeRegistry());
});

it('error path - no registration', () => {
Expand Down
31 changes: 18 additions & 13 deletions src/impl/create-legacy-factory-registry.ts
@@ -1,23 +1,28 @@
import { FactoryRegistry } from '../interfaces';
import { FactoryRegistry, ResolveFactoryFunction, Token } from '../interfaces';

export function createLegacyFactoryRegistry(): FactoryRegistry {
const factoryByToken = Object.create(null);
// cannot use `string | symbol` type here, see https://github.com/Microsoft/TypeScript/issues/24587
const factoryByToken: Record<string, ResolveFactoryFunction<unknown>> = Object.create(null);
// cannot iterate through symbols as keys in ES5
const keys: Token<unknown>[] = [];

const toKey = (token: Token<unknown>) => token as unknown as string;

return {
getTokens: () => {
throw Error('not impl');
},
hasFactory: (token) => {
throw Error('not impl');
},
getFactory: (token) => {
throw Error('not impl');
},
getTokens: () => keys,
hasFactory: (token) => Object.prototype.hasOwnProperty.call(factoryByToken, toKey(token)),
getFactory: (token) => factoryByToken[toKey(token)],
setFactory(token, factory) {
throw Error('not impl');
if (!this.hasFactory(token)) {
keys.push(token);
}
factoryByToken[toKey(token)] = factory;
},
forEach(callback) {
throw Error('not impl');
keys.forEach((token) => {
const factory = this.getFactory(token)!;
callback(factory, token, this);
});
},
};
}
1 change: 0 additions & 1 deletion src/interfaces/di-error.spec.ts
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { describe, it } from 'mocha';
import * as assert from 'assert';

Expand Down

0 comments on commit 6be9213

Please sign in to comment.