Browse Source

chore: removes redundant "find" method

e22m4u 2 years ago
parent
commit
f1ee3849d4
4 changed files with 0 additions and 253 deletions
  1. 0 20
      src/service-container.js
  2. 0 211
      src/service-container.spec.js
  3. 0 10
      src/service.js
  4. 0 12
      src/service.spec.js

+ 0 - 20
src/service-container.js

@@ -74,24 +74,4 @@ export class ServiceContainer {
     this._services.set(ctor, factory);
     return this;
   }
-
-  /**
-   * Find.
-   *
-   * @param {Function} ctor
-   * @return {any[]}
-   */
-  find(ctor) {
-    if (!ctor || typeof ctor !== 'function')
-      throw new InvalidArgumentError(
-        'The first argument of ServicesContainer.find must be ' +
-          'a class constructor, but %v given.',
-        ctor,
-      );
-    const keys = Array.from(this._services.keys());
-    const ctors = keys.filter(
-      key => typeof key === 'function' && key.prototype instanceof ctor,
-    );
-    return ctors.map(c => this.get(c));
-  }
 }

+ 0 - 211
src/service-container.spec.js

@@ -399,215 +399,4 @@ describe('ServiceContainer', function () {
       });
     });
   });
-
-  describe('find', function () {
-    it('throws an error if no constructor given', function () {
-      const container = new ServiceContainer();
-      const throwable = v => () => container.find(v);
-      const error = v =>
-        format(
-          'The first argument of ServicesContainer.find must be ' +
-            'a class constructor, but %s given.',
-          v,
-        );
-      expect(throwable('str')).to.throw(error('"str"'));
-      expect(throwable(10)).to.throw(error('10'));
-      expect(throwable(true)).to.throw(error('true'));
-      expect(throwable(false)).to.throw(error('false'));
-      expect(throwable(undefined)).to.throw(error('undefined'));
-      expect(throwable(null)).to.throw(error('null'));
-      expect(throwable([])).to.throw(error('Array'));
-      expect(throwable({})).to.throw(error('Object'));
-    });
-
-    describe('Service', function () {
-      it('returns an empty array if no bound classes of the given super-class', function () {
-        const container = new ServiceContainer();
-        class MyService extends Service {}
-        const result1 = container.find(MyService);
-        expect(result1).to.be.instanceof(Array);
-        expect(result1).to.be.empty;
-      });
-
-      it('returns cached instances of the given super-class', function () {
-        class MyService extends Service {}
-        class FooService extends MyService {}
-        class BarService extends MyService {}
-        const container = new ServiceContainer();
-        const foo = container.get(FooService);
-        const bar = container.get(BarService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.have.lengthOf(2);
-        expect(result[0]).to.be.instanceof(FooService);
-        expect(result[0]).to.be.eq(foo);
-        expect(result[1]).to.be.instanceof(BarService);
-        expect(result[1]).to.be.eq(bar);
-      });
-
-      it('returns instances from factory functions of the given super-class', function () {
-        class MyService extends Service {}
-        class FooService extends MyService {}
-        class BarService extends MyService {}
-        const container = new ServiceContainer();
-        container.add(FooService);
-        container.add(BarService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.have.lengthOf(2);
-        expect(result[0]).to.be.instanceof(FooService);
-        expect(result[1]).to.be.instanceof(BarService);
-      });
-
-      it('does not return a cached instance of the given constructor', function () {
-        class MyService extends Service {}
-        const container = new ServiceContainer();
-        container.get(MyService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.be.empty;
-      });
-
-      it('does not return an instance from a factory function of the given constructor', function () {
-        class MyService extends Service {}
-        const container = new ServiceContainer();
-        container.add(MyService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.be.empty;
-      });
-
-      it('caches resolved instances from factory functions', function () {
-        class MyService extends Service {}
-        class FooService extends MyService {}
-        class BarService extends MyService {}
-        const container = new ServiceContainer();
-        container.add(FooService);
-        container.add(BarService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.have.lengthOf(2);
-        expect(result[0]).to.be.instanceof(FooService);
-        expect(result[1]).to.be.instanceof(BarService);
-        const foo = container.get(FooService);
-        const bar = container.get(BarService);
-        expect(foo).to.be.eq(result[0]);
-        expect(bar).to.be.eq(result[1]);
-      });
-
-      it('uses constructor arguments provided to the factory function', function () {
-        class MyService extends Service {}
-        let givenContainer;
-        let givenArgs;
-        class ChildService extends MyService {
-          constructor(container, ...args) {
-            super(container);
-            givenContainer = container;
-            givenArgs = args;
-          }
-        }
-        const container = new ServiceContainer();
-        container.add(ChildService, 'foo', 'bar');
-        const result = container.find(MyService);
-        expect(result[0]).to.be.instanceof(ChildService);
-        expect(givenContainer).to.be.eq(container);
-        expect(givenArgs).to.be.eql(['foo', 'bar']);
-      });
-    });
-
-    describe('non-Service', function () {
-      it('returns an empty array if no bound classes of the given super-class', function () {
-        const container = new ServiceContainer();
-        class MyService {}
-        const result1 = container.find(MyService);
-        expect(result1).to.be.instanceof(Array);
-        expect(result1).to.be.empty;
-      });
-
-      it('returns cached instances of the given super-class', function () {
-        class MyService {}
-        class FooService extends MyService {}
-        class BarService extends MyService {}
-        const container = new ServiceContainer();
-        const foo = container.get(FooService);
-        const bar = container.get(BarService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.have.lengthOf(2);
-        expect(result[0]).to.be.instanceof(FooService);
-        expect(result[0]).to.be.eq(foo);
-        expect(result[1]).to.be.instanceof(BarService);
-        expect(result[1]).to.be.eq(bar);
-      });
-
-      it('returns instances from factory functions of the given super-class', function () {
-        class MyService {}
-        class FooService extends MyService {}
-        class BarService extends MyService {}
-        const container = new ServiceContainer();
-        container.add(FooService);
-        container.add(BarService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.have.lengthOf(2);
-        expect(result[0]).to.be.instanceof(FooService);
-        expect(result[1]).to.be.instanceof(BarService);
-      });
-
-      it('does not return a cached instance of the given constructor', function () {
-        class MyService {}
-        const container = new ServiceContainer();
-        container.get(MyService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.be.empty;
-      });
-
-      it('does not return an instance from a factory function of the given constructor', function () {
-        class MyService {}
-        const container = new ServiceContainer();
-        container.add(MyService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.be.empty;
-      });
-
-      it('caches resolved instances from factory functions', function () {
-        class MyService {}
-        class FooService extends MyService {}
-        class BarService extends MyService {}
-        const container = new ServiceContainer();
-        container.add(FooService);
-        container.add(BarService);
-        const result = container.find(MyService);
-        expect(result).to.be.instanceof(Array);
-        expect(result).to.have.lengthOf(2);
-        expect(result[0]).to.be.instanceof(FooService);
-        expect(result[1]).to.be.instanceof(BarService);
-        const foo = container.get(FooService);
-        const bar = container.get(BarService);
-        expect(foo).to.be.eq(result[0]);
-        expect(bar).to.be.eq(result[1]);
-      });
-
-      it('uses constructor arguments provided to the factory function', function () {
-        class MyService {}
-        let givenContainer;
-        let givenArgs;
-        class ChildService extends MyService {
-          constructor(...args) {
-            super();
-            givenContainer = container;
-            givenArgs = args;
-          }
-        }
-        const container = new ServiceContainer();
-        container.add(ChildService, 'foo', 'bar');
-        const result = container.find(MyService);
-        expect(result[0]).to.be.instanceof(ChildService);
-        expect(givenContainer).to.be.eq(container);
-        expect(givenArgs).to.be.eql(['foo', 'bar']);
-      });
-    });
-  });
 });

+ 0 - 10
src/service.js

@@ -55,14 +55,4 @@ export class Service {
     this.container.add(ctor, ...args);
     return this;
   }
-
-  /**
-   * Find services.
-   *
-   * @param {any} ctor
-   * @return {any[]}
-   */
-  findServices(ctor) {
-    return this.container.find(ctor);
-  }
 }

+ 0 - 12
src/service.spec.js

@@ -54,16 +54,4 @@ describe('Service', function () {
       expect(res).to.be.eq(service);
     });
   });
-
-  describe('findServices', function () {
-    it('calls the container "find" method', function () {
-      const service = new Service();
-      spy.on(service.container, 'find', ctor => {
-        expect(ctor).to.be.eq(Date);
-        return 'OK';
-      });
-      const res = service.findServices(Date);
-      expect(res).to.be.eq('OK');
-    });
-  });
 });