Browse Source

chore: adds `getParent` and `hasParent` methods to service container

e22m4u 4 months ago
parent
commit
1643d04e75

+ 5 - 2
README.md

@@ -46,11 +46,14 @@ const {Service} = require('@e22m4u/js-service');
 Методы:
 
 - `get(ctor, ...args)` получить существующий или новый экземпляр
-- `has(ctor)` проверка существования конструктора в контейнере
+- `has(ctor)` проверить существование конструктора в контейнере
 - `add(ctor, ...args)` добавить конструктор в контейнер
 - `use(ctor, ...args)` добавить конструктор и создать экземпляр
 - `set(ctor, service)` добавить конструктор и его экземпляр
 
+- `getParent()` получить родительский сервис-контейнер
+- `hasParent()` проверить наличие родительского сервис-контейнера
+
 ### get
 
 Метод `get` класса `ServiceContainer` создает экземпляр
@@ -115,7 +118,7 @@ console.log(hasService); // true
 Методы:
 
 - `getService(ctor, ...args)` получить существующий или новый экземпляр
-- `hasService(ctor)` проверка существования конструктора в контейнере
+- `hasService(ctor)` проверить существование конструктора в контейнере
 - `addService(ctor, ...args)` добавить конструктор в контейнер
 - `useService(ctor, ...args)` добавить конструктор и создать экземпляр
 - `setService(ctor, service)` добавить конструктор и его экземпляр

+ 29 - 11
dist/cjs/index.cjs

@@ -69,12 +69,30 @@ var _ServiceContainer = class _ServiceContainer {
       this._parent = parent;
     }
   }
+  /**
+   * Получить родительский сервис-контейнер или выбросить ошибку.
+   *
+   * @returns {ServiceContainer}
+   */
+  getParent() {
+    if (!this._parent)
+      throw new InvalidArgumentError("The service container has no parent.");
+    return this._parent;
+  }
+  /**
+   * Проверить наличие родительского сервис-контейнера.
+   *
+   * @returns {boolean}
+   */
+  hasParent() {
+    return Boolean(this._parent);
+  }
   /**
    * Получить существующий или новый экземпляр.
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {*}
+   * @returns {*}
    */
   get(ctor, ...args) {
     if (!ctor || typeof ctor !== "function")
@@ -104,10 +122,10 @@ var _ServiceContainer = class _ServiceContainer {
     return service;
   }
   /**
-   * Проверка существования конструктора в контейнере.
+   * Проверить существование конструктора в контейнере.
    *
    * @param {*} ctor
-   * @return {boolean}
+   * @returns {boolean}
    */
   has(ctor) {
     if (this._services.has(ctor)) return true;
@@ -122,7 +140,7 @@ var _ServiceContainer = class _ServiceContainer {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {this}
+   * @returns {this}
    */
   add(ctor, ...args) {
     if (!ctor || typeof ctor !== "function")
@@ -139,7 +157,7 @@ var _ServiceContainer = class _ServiceContainer {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {this}
+   * @returns {this}
    */
   use(ctor, ...args) {
     if (!ctor || typeof ctor !== "function")
@@ -156,7 +174,7 @@ var _ServiceContainer = class _ServiceContainer {
    *
    * @param {*} ctor
    * @param {*} service
-   * @return {this}
+   * @returns {this}
    */
   set(ctor, service) {
     if (!ctor || typeof ctor !== "function")
@@ -212,7 +230,7 @@ var _Service = class _Service {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {*}
+   * @returns {*}
    */
   getService(ctor, ...args) {
     return this.container.get(ctor, ...args);
@@ -221,7 +239,7 @@ var _Service = class _Service {
    * Проверка существования конструктора в контейнере.
    *
    * @param {*} ctor
-   * @return {boolean}
+   * @returns {boolean}
    */
   hasService(ctor) {
     return this.container.has(ctor);
@@ -231,7 +249,7 @@ var _Service = class _Service {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {this}
+   * @returns {this}
    */
   addService(ctor, ...args) {
     this.container.add(ctor, ...args);
@@ -242,7 +260,7 @@ var _Service = class _Service {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {this}
+   * @returns {this}
    */
   useService(ctor, ...args) {
     this.container.use(ctor, ...args);
@@ -253,7 +271,7 @@ var _Service = class _Service {
    *
    * @param {*} ctor
    * @param {*} service
-   * @return {this}
+   * @returns {this}
    */
   setService(ctor, service) {
     this.container.set(ctor, service);

+ 7 - 0
eslint.config.js

@@ -1,5 +1,6 @@
 import globals from 'globals';
 import eslintJs from '@eslint/js';
+import eslintJsdocPlugin from 'eslint-plugin-jsdoc';
 import eslintMochaPlugin from 'eslint-plugin-mocha';
 import eslintPrettierConfig from 'eslint-config-prettier';
 import eslintChaiExpectPlugin from 'eslint-plugin-chai-expect';
@@ -12,14 +13,20 @@ export default [{
     },
   },
   plugins: {
+    'jsdoc': eslintJsdocPlugin,
     'mocha': eslintMochaPlugin,
     'chai-expect': eslintChaiExpectPlugin,
   },
   rules: {
     ...eslintJs.configs.recommended.rules,
     ...eslintPrettierConfig.rules,
+    ...eslintJsdocPlugin.configs['flat/recommended-error'].rules,
     ...eslintMochaPlugin.configs.recommended.rules,
     ...eslintChaiExpectPlugin.configs['recommended-flat'].rules,
+    'jsdoc/require-param-description': 0,
+    'jsdoc/require-returns-description': 0,
+    'jsdoc/require-property-description': 0,
+    'jsdoc/tag-lines': ['error', 'any', {startLines: 1}],
   },
   files: ['src/**/*.js'],
 }];

+ 1 - 0
package.json

@@ -49,6 +49,7 @@
     "eslint": "~9.32.0",
     "eslint-config-prettier": "~10.1.8",
     "eslint-plugin-chai-expect": "~3.1.0",
+    "eslint-plugin-jsdoc": "~52.0.2",
     "eslint-plugin-mocha": "~11.1.0",
     "globals": "~16.3.0",
     "husky": "~9.1.7",

+ 11 - 1
src/service-container.d.ts

@@ -11,6 +11,16 @@ export declare class ServiceContainer {
    */
   constructor(parent?: ServiceContainer);
 
+  /**
+   * Получить родительский сервис-контейнер или выбросить ошибку.
+   */
+  getParent(): ServiceContainer;
+
+  /**
+   * Проверить наличие родительского сервис-контейнера.
+   */
+  hasParent(): boolean;
+
   /**
    * Получить существующий или новый экземпляр.
    *
@@ -23,7 +33,7 @@ export declare class ServiceContainer {
   ): T;
 
   /**
-   * Проверка существования конструктора в контейнере.
+   * Проверить существование конструктора в контейнере.
    *
    * @param ctor
    */

+ 26 - 6
src/service-container.js

@@ -52,12 +52,32 @@ export class ServiceContainer {
     }
   }
 
+  /**
+   * Получить родительский сервис-контейнер или выбросить ошибку.
+   *
+   * @returns {ServiceContainer}
+   */
+  getParent() {
+    if (!this._parent)
+      throw new InvalidArgumentError('The service container has no parent.');
+    return this._parent;
+  }
+
+  /**
+   * Проверить наличие родительского сервис-контейнера.
+   *
+   * @returns {boolean}
+   */
+  hasParent() {
+    return Boolean(this._parent);
+  }
+
   /**
    * Получить существующий или новый экземпляр.
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {*}
+   * @returns {*}
    */
   get(ctor, ...args) {
     if (!ctor || typeof ctor !== 'function')
@@ -104,10 +124,10 @@ export class ServiceContainer {
   }
 
   /**
-   * Проверка существования конструктора в контейнере.
+   * Проверить существование конструктора в контейнере.
    *
    * @param {*} ctor
-   * @return {boolean}
+   * @returns {boolean}
    */
   has(ctor) {
     if (this._services.has(ctor)) return true;
@@ -125,7 +145,7 @@ export class ServiceContainer {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {this}
+   * @returns {this}
    */
   add(ctor, ...args) {
     if (!ctor || typeof ctor !== 'function')
@@ -147,7 +167,7 @@ export class ServiceContainer {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {this}
+   * @returns {this}
    */
   use(ctor, ...args) {
     if (!ctor || typeof ctor !== 'function')
@@ -169,7 +189,7 @@ export class ServiceContainer {
    *
    * @param {*} ctor
    * @param {*} service
-   * @return {this}
+   * @returns {this}
    */
   set(ctor, service) {
     if (!ctor || typeof ctor !== 'function')

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

@@ -46,6 +46,31 @@ describe('ServiceContainer', function () {
     });
   });
 
+  describe('getParent', function () {
+    it('throws an error if no parent container', function () {
+      const container = new ServiceContainer();
+      const throwable = () => container.getParent();
+      expect(throwable).to.throw('The service container has no parent.');
+    });
+
+    it('returns parent container', function () {
+      const parent = new ServiceContainer();
+      const container = new ServiceContainer(parent);
+      const res = container.getParent();
+      expect(res).to.be.eq(parent);
+    });
+  });
+
+  describe('hasParent', function () {
+    it('returns true if a parent container exists and false otherwise', function () {
+      const container1 = new ServiceContainer();
+      const parent = new ServiceContainer();
+      const container2 = new ServiceContainer(parent);
+      expect(container1.hasParent()).to.be.false;
+      expect(container2.hasParent()).to.be.true;
+    });
+  });
+
   describe('get', function () {
     it('throws an error if no constructor given', function () {
       const container = new ServiceContainer();

+ 5 - 5
src/service.js

@@ -42,7 +42,7 @@ export class Service {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {*}
+   * @returns {*}
    */
   getService(ctor, ...args) {
     return this.container.get(ctor, ...args);
@@ -52,7 +52,7 @@ export class Service {
    * Проверка существования конструктора в контейнере.
    *
    * @param {*} ctor
-   * @return {boolean}
+   * @returns {boolean}
    */
   hasService(ctor) {
     return this.container.has(ctor);
@@ -63,7 +63,7 @@ export class Service {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {this}
+   * @returns {this}
    */
   addService(ctor, ...args) {
     this.container.add(ctor, ...args);
@@ -75,7 +75,7 @@ export class Service {
    *
    * @param {*} ctor
    * @param {*} args
-   * @return {this}
+   * @returns {this}
    */
   useService(ctor, ...args) {
     this.container.use(ctor, ...args);
@@ -87,7 +87,7 @@ export class Service {
    *
    * @param {*} ctor
    * @param {*} service
-   * @return {this}
+   * @returns {this}
    */
   setService(ctor, service) {
     this.container.set(ctor, service);

+ 2 - 1
src/utils/is-service-container.js

@@ -3,7 +3,8 @@ import {SERVICE_CONTAINER_CLASS_NAME} from '../service-container.js';
 /**
  * Определяет, является ли аргумент сервис-контейнером.
  *
- * @param container
+ * @param {*} container
+ *
  * @returns {false|*}
  */
 export function isServiceContainer(container) {