index.cjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 index_exports = {};
  23. __export(index_exports, {
  24. DebuggableService: () => DebuggableService,
  25. SERVICE_CLASS_NAME: () => SERVICE_CLASS_NAME,
  26. SERVICE_CONTAINER_CLASS_NAME: () => SERVICE_CONTAINER_CLASS_NAME,
  27. Service: () => Service,
  28. ServiceContainer: () => ServiceContainer,
  29. isServiceContainer: () => isServiceContainer
  30. });
  31. module.exports = __toCommonJS(index_exports);
  32. // src/errors/invalid-argument-error.js
  33. var import_js_format = require("@e22m4u/js-format");
  34. var _InvalidArgumentError = class _InvalidArgumentError extends import_js_format.Errorf {
  35. };
  36. __name(_InvalidArgumentError, "InvalidArgumentError");
  37. var InvalidArgumentError = _InvalidArgumentError;
  38. // src/service-container.js
  39. var SERVICE_CONTAINER_CLASS_NAME = "ServiceContainer";
  40. var _ServiceContainer = class _ServiceContainer {
  41. /**
  42. * Services map.
  43. *
  44. * @type {Map<any, any>}
  45. * @private
  46. */
  47. _services = /* @__PURE__ */ new Map();
  48. /**
  49. * Parent container.
  50. *
  51. * @type {ServiceContainer}
  52. * @private
  53. */
  54. _parent;
  55. /**
  56. * Constructor.
  57. *
  58. * @param {ServiceContainer|undefined} parent
  59. */
  60. constructor(parent = void 0) {
  61. if (parent != null) {
  62. if (!(parent instanceof _ServiceContainer))
  63. throw new InvalidArgumentError(
  64. 'The provided parameter "parent" of ServicesContainer.constructor must be an instance ServiceContainer, but %v given.',
  65. parent
  66. );
  67. this._parent = parent;
  68. }
  69. }
  70. /**
  71. * Получить родительский сервис-контейнер или выбросить ошибку.
  72. *
  73. * @returns {ServiceContainer}
  74. */
  75. getParent() {
  76. if (!this._parent)
  77. throw new InvalidArgumentError("The service container has no parent.");
  78. return this._parent;
  79. }
  80. /**
  81. * Проверить наличие родительского сервис-контейнера.
  82. *
  83. * @returns {boolean}
  84. */
  85. hasParent() {
  86. return Boolean(this._parent);
  87. }
  88. /**
  89. * Получить существующий или новый экземпляр.
  90. *
  91. * @param {*} ctor
  92. * @param {*} args
  93. * @returns {*}
  94. */
  95. get(ctor, ...args) {
  96. if (!ctor || typeof ctor !== "function")
  97. throw new InvalidArgumentError(
  98. "The first argument of ServicesContainer.get must be a class constructor, but %v given.",
  99. ctor
  100. );
  101. if (!this._services.has(ctor) && this._parent && this._parent.has(ctor)) {
  102. return this._parent.get(ctor);
  103. }
  104. let service = this._services.get(ctor);
  105. if (!service) {
  106. const ctors = this._services.keys();
  107. const inheritedCtor = ctors.find((v) => v.prototype instanceof ctor);
  108. if (inheritedCtor) {
  109. service = this._services.get(inheritedCtor);
  110. ctor = inheritedCtor;
  111. }
  112. }
  113. if (!service || args.length) {
  114. service = Array.isArray(ctor.kinds) && ctor.kinds.includes(SERVICE_CLASS_NAME) ? new ctor(this, ...args) : new ctor(...args);
  115. this._services.set(ctor, service);
  116. } else if (typeof service === "function") {
  117. service = service();
  118. this._services.set(ctor, service);
  119. }
  120. return service;
  121. }
  122. /**
  123. * Проверить существование конструктора в контейнере.
  124. *
  125. * @param {*} ctor
  126. * @returns {boolean}
  127. */
  128. has(ctor) {
  129. if (this._services.has(ctor)) return true;
  130. if (this._parent) return this._parent.has(ctor);
  131. const ctors = this._services.keys();
  132. const inheritedCtor = ctors.find((v) => v.prototype instanceof ctor);
  133. if (inheritedCtor) return true;
  134. return false;
  135. }
  136. /**
  137. * Добавить конструктор в контейнер.
  138. *
  139. * @param {*} ctor
  140. * @param {*} args
  141. * @returns {this}
  142. */
  143. add(ctor, ...args) {
  144. if (!ctor || typeof ctor !== "function")
  145. throw new InvalidArgumentError(
  146. "The first argument of ServicesContainer.add must be a class constructor, but %v given.",
  147. ctor
  148. );
  149. const factory = /* @__PURE__ */ __name(() => Array.isArray(ctor.kinds) && ctor.kinds.includes(SERVICE_CLASS_NAME) ? new ctor(this, ...args) : new ctor(...args), "factory");
  150. this._services.set(ctor, factory);
  151. return this;
  152. }
  153. /**
  154. * Добавить конструктор и создать экземпляр.
  155. *
  156. * @param {*} ctor
  157. * @param {*} args
  158. * @returns {this}
  159. */
  160. use(ctor, ...args) {
  161. if (!ctor || typeof ctor !== "function")
  162. throw new InvalidArgumentError(
  163. "The first argument of ServicesContainer.use must be a class constructor, but %v given.",
  164. ctor
  165. );
  166. const service = Array.isArray(ctor.kinds) && ctor.kinds.includes(SERVICE_CLASS_NAME) ? new ctor(this, ...args) : new ctor(...args);
  167. this._services.set(ctor, service);
  168. return this;
  169. }
  170. /**
  171. * Добавить конструктор и связанный экземпляр.
  172. *
  173. * @param {*} ctor
  174. * @param {*} service
  175. * @returns {this}
  176. */
  177. set(ctor, service) {
  178. if (!ctor || typeof ctor !== "function")
  179. throw new InvalidArgumentError(
  180. "The first argument of ServicesContainer.set must be a class constructor, but %v given.",
  181. ctor
  182. );
  183. if (!service || typeof service !== "object" || Array.isArray(service))
  184. throw new InvalidArgumentError(
  185. "The second argument of ServicesContainer.set must be an Object, but %v given.",
  186. service
  187. );
  188. this._services.set(ctor, service);
  189. return this;
  190. }
  191. };
  192. __name(_ServiceContainer, "ServiceContainer");
  193. /**
  194. * Kinds.
  195. *
  196. * @type {string[]}
  197. */
  198. __publicField(_ServiceContainer, "kinds", [SERVICE_CONTAINER_CLASS_NAME]);
  199. var ServiceContainer = _ServiceContainer;
  200. // src/utils/is-service-container.js
  201. function isServiceContainer(container) {
  202. return Boolean(
  203. container && typeof container === "object" && typeof container.constructor === "function" && Array.isArray(container.constructor.kinds) && container.constructor.kinds.includes(SERVICE_CONTAINER_CLASS_NAME)
  204. );
  205. }
  206. __name(isServiceContainer, "isServiceContainer");
  207. // src/service.js
  208. var SERVICE_CLASS_NAME = "Service";
  209. var _Service = class _Service {
  210. /**
  211. * Container.
  212. *
  213. * @type {ServiceContainer}
  214. */
  215. container;
  216. /**
  217. * Constructor.
  218. *
  219. * @param {ServiceContainer|undefined} container
  220. */
  221. constructor(container = void 0) {
  222. this.container = isServiceContainer(container) ? container : new ServiceContainer();
  223. }
  224. /**
  225. * Получить существующий или новый экземпляр.
  226. *
  227. * @param {*} ctor
  228. * @param {*} args
  229. * @returns {*}
  230. */
  231. getService(ctor, ...args) {
  232. return this.container.get(ctor, ...args);
  233. }
  234. /**
  235. * Проверка существования конструктора в контейнере.
  236. *
  237. * @param {*} ctor
  238. * @returns {boolean}
  239. */
  240. hasService(ctor) {
  241. return this.container.has(ctor);
  242. }
  243. /**
  244. * Добавить конструктор в контейнер.
  245. *
  246. * @param {*} ctor
  247. * @param {*} args
  248. * @returns {this}
  249. */
  250. addService(ctor, ...args) {
  251. this.container.add(ctor, ...args);
  252. return this;
  253. }
  254. /**
  255. * Добавить конструктор и создать экземпляр.
  256. *
  257. * @param {*} ctor
  258. * @param {*} args
  259. * @returns {this}
  260. */
  261. useService(ctor, ...args) {
  262. this.container.use(ctor, ...args);
  263. return this;
  264. }
  265. /**
  266. * Добавить конструктор и связанный экземпляр.
  267. *
  268. * @param {*} ctor
  269. * @param {*} service
  270. * @returns {this}
  271. */
  272. setService(ctor, service) {
  273. this.container.set(ctor, service);
  274. return this;
  275. }
  276. };
  277. __name(_Service, "Service");
  278. /**
  279. * Kinds.
  280. *
  281. * @type {string[]}
  282. */
  283. __publicField(_Service, "kinds", [SERVICE_CLASS_NAME]);
  284. var Service = _Service;
  285. // src/debuggable-service.js
  286. var import_js_debug = require("@e22m4u/js-debug");
  287. var _DebuggableService = class _DebuggableService extends import_js_debug.Debuggable {
  288. /**
  289. * Service.
  290. *
  291. * @type {Service}
  292. */
  293. _service;
  294. /**
  295. * Container.
  296. *
  297. * @type {ServiceContainer}
  298. */
  299. get container() {
  300. return this._service.container;
  301. }
  302. /**
  303. * Получить существующий или новый экземпляр.
  304. *
  305. * @type {Service['getService']}
  306. */
  307. get getService() {
  308. return this._service.getService;
  309. }
  310. /**
  311. * Проверка существования конструктора в контейнере.
  312. *
  313. * @type {Service['hasService']}
  314. */
  315. get hasService() {
  316. return this._service.hasService;
  317. }
  318. /**
  319. * Добавить конструктор в контейнер.
  320. *
  321. * @type {Service['addService']}
  322. */
  323. get addService() {
  324. return this._service.addService;
  325. }
  326. /**
  327. * Добавить конструктор и создать экземпляр.
  328. *
  329. * @type {Service['useService']}
  330. */
  331. get useService() {
  332. return this._service.useService;
  333. }
  334. /**
  335. * Добавить конструктор и связанный экземпляр.
  336. *
  337. * @type {Service['setService']}
  338. */
  339. get setService() {
  340. return this._service.setService;
  341. }
  342. /**
  343. * Constructor.
  344. *
  345. * @param {ServiceContainer|undefined} container
  346. * @param {import('@e22m4u/js-debug').DebuggableOptions|undefined} options
  347. */
  348. constructor(container = void 0, options = void 0) {
  349. super(options);
  350. this._service = new Service(container);
  351. }
  352. };
  353. __name(_DebuggableService, "DebuggableService");
  354. /**
  355. * Kinds.
  356. *
  357. * @type {string[]}
  358. */
  359. __publicField(_DebuggableService, "kinds", Service.kinds);
  360. var DebuggableService = _DebuggableService;
  361. // Annotate the CommonJS export names for ESM import in node:
  362. 0 && (module.exports = {
  363. DebuggableService,
  364. SERVICE_CLASS_NAME,
  365. SERVICE_CONTAINER_CLASS_NAME,
  366. Service,
  367. ServiceContainer,
  368. isServiceContainer
  369. });