Browse Source

fix: container inheritance in cjs mode

e22m4u 1 year ago
parent
commit
505530a2c3
4 changed files with 14 additions and 20 deletions
  1. 4 6
      dist/cjs/index.cjs
  2. 3 7
      src/service-container.js
  3. 1 3
      src/service.js
  4. 6 4
      src/service.spec.js

+ 4 - 6
dist/cjs/index.cjs

@@ -169,7 +169,7 @@ var ServiceContainer = class _ServiceContainer {
     }
     }
     let service = this._services.get(ctor);
     let service = this._services.get(ctor);
     if (!service || args.length) {
     if (!service || args.length) {
-      service = "prototype" in ctor && ctor.prototype.kind === Service.name ? new ctor(this, ...args) : new ctor(...args);
+      service = ctor.kind === Service.name ? new ctor(this, ...args) : new ctor(...args);
       this._services.set(ctor, service);
       this._services.set(ctor, service);
     } else if (typeof service === "function") {
     } else if (typeof service === "function") {
       service = service();
       service = service();
@@ -201,7 +201,7 @@ var ServiceContainer = class _ServiceContainer {
         "The first argument of ServicesContainer.add must be a class constructor, but %v given.",
         "The first argument of ServicesContainer.add must be a class constructor, but %v given.",
         ctor
         ctor
       );
       );
-    const factory = () => ctor.prototype.kind === Service.name ? new ctor(this, ...args) : new ctor(...args);
+    const factory = () => ctor.kind === Service.name ? new ctor(this, ...args) : new ctor(...args);
     this._services.set(ctor, factory);
     this._services.set(ctor, factory);
     return this;
     return this;
   }
   }
@@ -218,7 +218,7 @@ var ServiceContainer = class _ServiceContainer {
         "The first argument of ServicesContainer.use must be a class constructor, but %v given.",
         "The first argument of ServicesContainer.use must be a class constructor, but %v given.",
         ctor
         ctor
       );
       );
-    const service = ctor.prototype.kind === Service.name ? new ctor(this, ...args) : new ctor(...args);
+    const service = ctor.kind === Service.name ? new ctor(this, ...args) : new ctor(...args);
     this._services.set(ctor, service);
     this._services.set(ctor, service);
     return this;
     return this;
   }
   }
@@ -252,9 +252,7 @@ var Service = class _Service {
    *
    *
    * @type {string}
    * @type {string}
    */
    */
-  get kind() {
-    return _Service.name;
-  }
+  static kind = _Service.name;
   /**
   /**
    * Container.
    * Container.
    *
    *

+ 3 - 7
src/service-container.js

@@ -64,7 +64,7 @@ export class ServiceContainer {
     // новый экземпляр
     // новый экземпляр
     if (!service || args.length) {
     if (!service || args.length) {
       service =
       service =
-        'prototype' in ctor && ctor.prototype.kind === Service.name
+        ctor.kind === Service.name
           ? new ctor(this, ...args)
           ? new ctor(this, ...args)
           : new ctor(...args);
           : new ctor(...args);
       this._services.set(ctor, service);
       this._services.set(ctor, service);
@@ -103,9 +103,7 @@ export class ServiceContainer {
         ctor,
         ctor,
       );
       );
     const factory = () =>
     const factory = () =>
-      ctor.prototype.kind === Service.name
-        ? new ctor(this, ...args)
-        : new ctor(...args);
+      ctor.kind === Service.name ? new ctor(this, ...args) : new ctor(...args);
     this._services.set(ctor, factory);
     this._services.set(ctor, factory);
     return this;
     return this;
   }
   }
@@ -125,9 +123,7 @@ export class ServiceContainer {
         ctor,
         ctor,
       );
       );
     const service =
     const service =
-      ctor.prototype.kind === Service.name
-        ? new ctor(this, ...args)
-        : new ctor(...args);
+      ctor.kind === Service.name ? new ctor(this, ...args) : new ctor(...args);
     this._services.set(ctor, service);
     this._services.set(ctor, service);
     return this;
     return this;
   }
   }

+ 1 - 3
src/service.js

@@ -9,9 +9,7 @@ export class Service {
    *
    *
    * @type {string}
    * @type {string}
    */
    */
-  get kind() {
-    return Service.name;
-  }
+  static kind = Service.name;
 
 
   /**
   /**
    * Container.
    * Container.

+ 6 - 4
src/service.spec.js

@@ -3,10 +3,12 @@ import {Service} from './service.js';
 import {ServiceContainer} from './service-container.js';
 import {ServiceContainer} from './service-container.js';
 
 
 describe('Service', function () {
 describe('Service', function () {
-  it('exposes kind getter', function () {
-    const service = new Service();
-    expect(service.kind).to.be.eq('Service');
-    expect(Service.prototype.kind).to.be.eq('Service');
+  it('exposes static property "kind"', function () {
+    expect(Service.kind).to.be.eq(Service.name);
+    const MyService1 = class extends Service {};
+    expect(MyService1.kind).to.be.eq(Service.name);
+    class MyService2 extends Service {}
+    expect(MyService2.kind).to.be.eq(Service.name);
   });
   });
 
 
   describe('constructor', function () {
   describe('constructor', function () {