index.cjs 7.3 KB

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