index.cjs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  6. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  20. var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  21. // src/index.js
  22. var src_exports = {};
  23. __export(src_exports, {
  24. Service: () => Service,
  25. ServiceContainer: () => ServiceContainer
  26. });
  27. module.exports = __toCommonJS(src_exports);
  28. // src/errors/invalid-argument-error.js
  29. var import_js_format = require("@e22m4u/js-format");
  30. var _InvalidArgumentError = class _InvalidArgumentError extends import_js_format.Errorf {
  31. };
  32. __name(_InvalidArgumentError, "InvalidArgumentError");
  33. var InvalidArgumentError = _InvalidArgumentError;
  34. // src/service-container.js
  35. var _ServiceContainer = class _ServiceContainer {
  36. /**
  37. * Services map.
  38. *
  39. * @type {Map<any, any>}
  40. * @private
  41. */
  42. _services = /* @__PURE__ */ new Map();
  43. /**
  44. * Parent container.
  45. *
  46. * @type {ServiceContainer}
  47. * @private
  48. */
  49. _parent;
  50. /**
  51. * Constructor.
  52. *
  53. * @param {ServiceContainer|undefined} parent
  54. */
  55. constructor(parent = void 0) {
  56. if (parent != null) {
  57. if (!(parent instanceof _ServiceContainer))
  58. throw new InvalidArgumentError(
  59. 'The provided parameter "parent" of ServicesContainer.constructor must be an instance ServiceContainer, but %v given.',
  60. parent
  61. );
  62. this._parent = parent;
  63. }
  64. }
  65. /**
  66. * Получить существующий или новый экземпляр.
  67. *
  68. * @param {*} ctor
  69. * @param {*} args
  70. * @return {*}
  71. */
  72. get(ctor, ...args) {
  73. if (!ctor || typeof ctor !== "function")
  74. throw new InvalidArgumentError(
  75. "The first argument of ServicesContainer.get must be a class constructor, but %v given.",
  76. ctor
  77. );
  78. if (!this._services.has(ctor) && this._parent && this._parent.has(ctor)) {
  79. return this._parent.get(ctor);
  80. }
  81. let service = this._services.get(ctor);
  82. if (!service || args.length) {
  83. service = ctor.kind === Service.kind ? new ctor(this, ...args) : new ctor(...args);
  84. this._services.set(ctor, service);
  85. } else if (typeof service === "function") {
  86. service = service();
  87. this._services.set(ctor, service);
  88. }
  89. return service;
  90. }
  91. /**
  92. * Проверка существования конструктора в контейнере.
  93. *
  94. * @param {*} ctor
  95. * @return {boolean}
  96. */
  97. has(ctor) {
  98. if (this._services.has(ctor)) return true;
  99. if (this._parent) return this._parent.has(ctor);
  100. return false;
  101. }
  102. /**
  103. * Добавить конструктор в контейнер.
  104. *
  105. * @param {*} ctor
  106. * @param {*} args
  107. * @return {this}
  108. */
  109. add(ctor, ...args) {
  110. if (!ctor || typeof ctor !== "function")
  111. throw new InvalidArgumentError(
  112. "The first argument of ServicesContainer.add must be a class constructor, but %v given.",
  113. ctor
  114. );
  115. const factory = /* @__PURE__ */ __name(() => ctor.kind === Service.kind ? new ctor(this, ...args) : new ctor(...args), "factory");
  116. this._services.set(ctor, factory);
  117. return this;
  118. }
  119. /**
  120. * Добавить конструктор и создать экземпляр.
  121. *
  122. * @param {*} ctor
  123. * @param {*} args
  124. * @return {this}
  125. */
  126. use(ctor, ...args) {
  127. if (!ctor || typeof ctor !== "function")
  128. throw new InvalidArgumentError(
  129. "The first argument of ServicesContainer.use must be a class constructor, but %v given.",
  130. ctor
  131. );
  132. const service = ctor.kind === Service.kind ? new ctor(this, ...args) : new ctor(...args);
  133. this._services.set(ctor, service);
  134. return this;
  135. }
  136. /**
  137. * Добавить конструктор и связанный экземпляр.
  138. *
  139. * @param {*} ctor
  140. * @param {*} service
  141. * @return {this}
  142. */
  143. set(ctor, service) {
  144. if (!ctor || typeof ctor !== "function")
  145. throw new InvalidArgumentError(
  146. "The first argument of ServicesContainer.set must be a class constructor, but %v given.",
  147. ctor
  148. );
  149. if (!service || typeof service !== "object" || Array.isArray(service))
  150. throw new InvalidArgumentError(
  151. "The second argument of ServicesContainer.set must be an Object, but %v given.",
  152. service
  153. );
  154. this._services.set(ctor, service);
  155. return this;
  156. }
  157. };
  158. __name(_ServiceContainer, "ServiceContainer");
  159. var ServiceContainer = _ServiceContainer;
  160. // src/service.js
  161. var _Service = class _Service {
  162. /**
  163. * Container.
  164. *
  165. * @type {ServiceContainer}
  166. */
  167. container;
  168. /**
  169. * Constructor.
  170. *
  171. * @param {ServiceContainer|undefined} container
  172. */
  173. constructor(container = void 0) {
  174. this.container = container instanceof ServiceContainer ? container : new ServiceContainer();
  175. }
  176. /**
  177. * Получить существующий или новый экземпляр.
  178. *
  179. * @param {*} ctor
  180. * @param {*} args
  181. * @return {*}
  182. */
  183. getService(ctor, ...args) {
  184. return this.container.get(ctor, ...args);
  185. }
  186. /**
  187. * Проверка существования конструктора в контейнере.
  188. *
  189. * @param {*} ctor
  190. * @return {boolean}
  191. */
  192. hasService(ctor) {
  193. return this.container.has(ctor);
  194. }
  195. /**
  196. * Добавить конструктор в контейнер.
  197. *
  198. * @param {*} ctor
  199. * @param {*} args
  200. * @return {this}
  201. */
  202. addService(ctor, ...args) {
  203. this.container.add(ctor, ...args);
  204. return this;
  205. }
  206. /**
  207. * Добавить конструктор и создать экземпляр.
  208. *
  209. * @param {*} ctor
  210. * @param {*} args
  211. * @return {this}
  212. */
  213. useService(ctor, ...args) {
  214. this.container.use(ctor, ...args);
  215. return this;
  216. }
  217. /**
  218. * Добавить конструктор и связанный экземпляр.
  219. *
  220. * @param {*} ctor
  221. * @param {*} service
  222. * @return {this}
  223. */
  224. setService(ctor, service) {
  225. this.container.set(ctor, service);
  226. return this;
  227. }
  228. };
  229. __name(_Service, "Service");
  230. /**
  231. * Kind.
  232. *
  233. * @type {string}
  234. */
  235. __publicField(_Service, "kind", "Service");
  236. var Service = _Service;
  237. // Annotate the CommonJS export names for ESM import in node:
  238. 0 && (module.exports = {
  239. Service,
  240. ServiceContainer
  241. });