Browse Source

chore: allows extended ServiceContainer set to Service

e22m4u 5 months ago
parent
commit
c05080abd8
5 changed files with 49 additions and 3 deletions
  1. 10 1
      dist/cjs/index.cjs
  2. 14 0
      src/service-container.js
  3. 7 0
      src/service-container.spec.js
  4. 6 1
      src/service.js
  5. 12 1
      src/service.spec.js

+ 10 - 1
dist/cjs/index.cjs

@@ -23,6 +23,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
 var index_exports = {};
 __export(index_exports, {
   SERVICE_CLASS_NAME: () => SERVICE_CLASS_NAME,
+  SERVICE_CONTAINER_CLASS_NAME: () => SERVICE_CONTAINER_CLASS_NAME,
   Service: () => Service,
   ServiceContainer: () => ServiceContainer
 });
@@ -36,6 +37,7 @@ __name(_InvalidArgumentError, "InvalidArgumentError");
 var InvalidArgumentError = _InvalidArgumentError;
 
 // src/service-container.js
+var SERVICE_CONTAINER_CLASS_NAME = "ServiceContainer";
 var _ServiceContainer = class _ServiceContainer {
   /**
    * Services map.
@@ -171,6 +173,12 @@ var _ServiceContainer = class _ServiceContainer {
   }
 };
 __name(_ServiceContainer, "ServiceContainer");
+/**
+ * Kinds.
+ *
+ * @type {string[]}
+ */
+__publicField(_ServiceContainer, "kinds", [SERVICE_CONTAINER_CLASS_NAME]);
 var ServiceContainer = _ServiceContainer;
 
 // src/service.js
@@ -188,7 +196,7 @@ var _Service = class _Service {
    * @param {ServiceContainer|undefined} container
    */
   constructor(container = void 0) {
-    this.container = container instanceof ServiceContainer ? container : new ServiceContainer();
+    this.container = container && typeof container === "object" && typeof container.constructor === "function" && Array.isArray(container.constructor.kinds) && container.constructor.kinds.includes(SERVICE_CONTAINER_CLASS_NAME) ? container : new ServiceContainer();
   }
   /**
    * Получить существующий или новый экземпляр.
@@ -254,6 +262,7 @@ var Service = _Service;
 // Annotate the CommonJS export names for ESM import in node:
 0 && (module.exports = {
   SERVICE_CLASS_NAME,
+  SERVICE_CONTAINER_CLASS_NAME,
   Service,
   ServiceContainer
 });

+ 14 - 0
src/service-container.js

@@ -1,10 +1,24 @@
 import {SERVICE_CLASS_NAME} from './service.js';
 import {InvalidArgumentError} from './errors/index.js';
 
+/**
+ * Service class name.
+ *
+ * @type {string}
+ */
+export const SERVICE_CONTAINER_CLASS_NAME = 'ServiceContainer';
+
 /**
  * Service container.
  */
 export class ServiceContainer {
+  /**
+   * Kinds.
+   *
+   * @type {string[]}
+   */
+  static kinds = [SERVICE_CONTAINER_CLASS_NAME];
+
   /**
    * Services map.
    *

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

@@ -2,8 +2,15 @@ import {expect} from 'chai';
 import {Service} from './service.js';
 import {format} from '@e22m4u/js-format';
 import {ServiceContainer} from './service-container.js';
+import {SERVICE_CONTAINER_CLASS_NAME} from './service-container.js';
 
 describe('ServiceContainer', function () {
+  it('exposes static property "kinds"', function () {
+    expect(ServiceContainer.kinds).to.be.eql([SERVICE_CONTAINER_CLASS_NAME]);
+    const MyContainer = class extends ServiceContainer {};
+    expect(MyContainer.kinds).to.be.eql([SERVICE_CONTAINER_CLASS_NAME]);
+  });
+
   describe('constructor', function () {
     it('does not require any arguments', function () {
       const res = new ServiceContainer();

+ 6 - 1
src/service.js

@@ -1,4 +1,5 @@
 import {ServiceContainer} from './service-container.js';
+import {SERVICE_CONTAINER_CLASS_NAME} from './service-container.js';
 
 /**
  * Service class name.
@@ -32,7 +33,11 @@ export class Service {
    */
   constructor(container = undefined) {
     this.container =
-      container instanceof ServiceContainer
+      container &&
+      typeof container === 'object' &&
+      typeof container.constructor === 'function' &&
+      Array.isArray(container.constructor.kinds) &&
+      container.constructor.kinds.includes(SERVICE_CONTAINER_CLASS_NAME)
         ? container
         : new ServiceContainer();
   }

+ 12 - 1
src/service.spec.js

@@ -1,6 +1,8 @@
 import {expect} from 'chai';
-import {Service, SERVICE_CLASS_NAME} from './service.js';
+import {Service} from './service.js';
+import {SERVICE_CLASS_NAME} from './service.js';
 import {ServiceContainer} from './service-container.js';
+import {SERVICE_CONTAINER_CLASS_NAME} from './service-container.js';
 
 describe('Service', function () {
   it('exposes static property "kinds"', function () {
@@ -20,6 +22,15 @@ describe('Service', function () {
       const service = new Service(container);
       expect(service.container).to.be.eq(container);
     });
+
+    it('sets a given object as service container by the kinds property', function () {
+      class MyServiceContainer {
+        static kinds = [SERVICE_CONTAINER_CLASS_NAME];
+      }
+      const container = new MyServiceContainer();
+      const service = new Service(container);
+      expect(service.container).to.be.eq(container);
+    });
   });
 
   describe('getService', function () {