ditox 2.4.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,16 +12,12 @@ export class ResolverError extends Error {
12
12
  /** @internal */
13
13
  export const CONTAINER = token('ditox.Container');
14
14
  /** @internal */
15
- export const PARENT_CONTAINER = token('ditox.ParentContainer');
15
+ export const PARENT_CONTAINERS = token('ditox.ParentContainer');
16
16
  /** @internal */
17
17
  export const RESOLVER = token('ditox.Resolver');
18
18
  /** @internal */
19
19
  const NOT_FOUND = Symbol();
20
20
  /** @internal */
21
- export const FAKE_FACTORY = () => {
22
- throw new Error('FAKE_FACTORY');
23
- };
24
- /** @internal */
25
21
  const DEFAULT_SCOPE = 'singleton';
26
22
  /** @internal */
27
23
  export const FACTORIES_MAP = token('ditox.FactoriesMap');
@@ -41,7 +37,7 @@ function getOnRemoved(options) {
41
37
  /** @internal */
42
38
  function isInternalToken(token) {
43
39
  return (token.symbol === CONTAINER.symbol ||
44
- token.symbol === PARENT_CONTAINER.symbol ||
40
+ token.symbol === PARENT_CONTAINERS.symbol ||
45
41
  token.symbol === RESOLVER.symbol);
46
42
  }
47
43
  /**
@@ -49,9 +45,14 @@ function isInternalToken(token) {
49
45
  *
50
46
  * Container can have an optional parent to chain token resolution. The parent is used in case the current container does not have a registered token.
51
47
  *
52
- * @param parentContainer - Optional parent container.
48
+ * @param parentArg - Optional parent container or an array of containers.
53
49
  */
54
- export function createContainer(parentContainer) {
50
+ export function createContainer(parentArg) {
51
+ const parents = parentArg
52
+ ? Array.isArray(parentArg)
53
+ ? [...parentArg]
54
+ : [parentArg]
55
+ : undefined;
55
56
  const values = new Map();
56
57
  const factories = new Map();
57
58
  const container = {
@@ -90,10 +91,9 @@ export function createContainer(parentContainer) {
90
91
  bindInternalTokens();
91
92
  },
92
93
  hasToken(token) {
93
- var _a;
94
94
  return (values.has(token.symbol) ||
95
95
  factories.has(token.symbol) ||
96
- ((_a = parentContainer === null || parentContainer === void 0 ? void 0 : parentContainer.hasToken(token)) !== null && _a !== void 0 ? _a : false));
96
+ parentContainersHaveToken(parents, token));
97
97
  },
98
98
  get(token) {
99
99
  const value = resolver(token, container);
@@ -124,14 +124,14 @@ export function createContainer(parentContainer) {
124
124
  return value;
125
125
  }
126
126
  const factoryContext = factories.get(token.symbol);
127
- if (factoryContext && factoryContext.factory !== FAKE_FACTORY) {
127
+ if (factoryContext) {
128
128
  const scope = getScope(factoryContext.options);
129
129
  switch (scope) {
130
130
  case 'singleton': {
131
131
  if (hasValue) {
132
132
  return value;
133
133
  }
134
- else if (parentContainer === null || parentContainer === void 0 ? void 0 : parentContainer.hasToken(token)) {
134
+ else if (parentContainersHaveToken(parents, token)) {
135
135
  break;
136
136
  }
137
137
  else {
@@ -142,14 +142,15 @@ export function createContainer(parentContainer) {
142
142
  }
143
143
  }
144
144
  case 'scoped': {
145
- // Create a value within the origin container and cache it.
146
- const value = factoryContext.factory(origin);
147
- origin.bindValue(token, value);
148
- if (origin !== container) {
149
- // Bind a fake factory with actual options to make onRemoved() works.
150
- origin.bindFactory(token, FAKE_FACTORY, factoryContext.options);
145
+ if (hasValue) {
146
+ return value;
147
+ }
148
+ else {
149
+ // Create a value within the factory's container and cache it.
150
+ const value = factoryContext.factory(container);
151
+ container.bindValue(token, value);
152
+ return value;
151
153
  }
152
- return value;
153
154
  }
154
155
  case 'transient': {
155
156
  // Create a value within the origin container and don't cache it.
@@ -160,9 +161,8 @@ export function createContainer(parentContainer) {
160
161
  if (hasValue) {
161
162
  return value;
162
163
  }
163
- const parentResolver = parentContainer === null || parentContainer === void 0 ? void 0 : parentContainer.get(RESOLVER);
164
- if (parentResolver) {
165
- return parentResolver(token, origin);
164
+ if (parents) {
165
+ return parentContainersResolveToken(parents, token, origin);
166
166
  }
167
167
  return NOT_FOUND;
168
168
  }
@@ -179,11 +179,33 @@ export function createContainer(parentContainer) {
179
179
  values.set(CONTAINER.symbol, container);
180
180
  values.set(RESOLVER.symbol, resolver);
181
181
  values.set(FACTORIES_MAP.symbol, factories);
182
- if (parentContainer) {
183
- values.set(PARENT_CONTAINER.symbol, parentContainer);
182
+ if (parents) {
183
+ values.set(PARENT_CONTAINERS.symbol, parents);
184
184
  }
185
185
  }
186
186
  bindInternalTokens();
187
187
  return container;
188
188
  }
189
+ function parentContainersHaveToken(parents, token) {
190
+ if (!parents)
191
+ return false;
192
+ for (let i = 0; i < parents.length; i++) {
193
+ if (parents[i].hasToken(token)) {
194
+ return true;
195
+ }
196
+ }
197
+ return false;
198
+ }
199
+ function parentContainersResolveToken(parents, token, origin) {
200
+ for (const parentContainer of parents) {
201
+ const parentResolver = parentContainer.get(RESOLVER);
202
+ if (parentResolver) {
203
+ const resolved = parentResolver(token, origin);
204
+ if (resolved !== NOT_FOUND) {
205
+ return resolved;
206
+ }
207
+ }
208
+ }
209
+ return NOT_FOUND;
210
+ }
189
211
  //# sourceMappingURL=container.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"container.js","sourceRoot":"","sources":["../../src/container.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,EAAC,MAAM,UAAU,CAAC;AAEtC;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAClD,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AAsED,gBAAgB;AAChB,MAAM,CAAC,MAAM,SAAS,GAAqB,KAAK,CAAC,iBAAiB,CAAC,CAAC;AACpE,gBAAgB;AAChB,MAAM,CAAC,MAAM,gBAAgB,GAAqB,KAAK,CACrD,uBAAuB,CACxB,CAAC;AACF,gBAAgB;AAChB,MAAM,CAAC,MAAM,QAAQ,GAAoB,KAAK,CAAC,gBAAgB,CAAC,CAAC;AAEjE,gBAAgB;AAChB,MAA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CAAC,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;QACxC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QACtC,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;QAE5C,IAAI,OAAO,EAAE;YACX,MAAM,CAAC,GAAG,CAAC,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;SAC/C;IACH,CAAC;IAED,kBAAkB,EAAE,CAAC;IACrB,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,yBAAyB,CAChC,OAA6C,EAC7C,KAAe;IAEf,IAAI,CAAC,OAAO;QAAE,OAAO,KAAK,CAAC;IAE3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACvC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;YAC9B,OAAO,IAAI,CAAC;SACb;KACF;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,4BAA4B,CACnC,OAAiC,EACjC,KAAe,EACf,MAAiB;IAEjB,KAAK,MAAM,eAAe,IAAI,OAAO,EAAE;QACrC,MAAM,cAAc,GAAG,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACrD,IAAI,cAAc,EAAE;YAClB,MAAM,QAAQ,GAAG,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAC/C,IAAI,QAAQ,KAAK,SAAS,EAAE;gBAC1B,OAAO,QAAQ,CAAC;aACjB;SACF;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -59,16 +59,17 @@ export type AnyModuleDeclaration = ModuleDeclaration<Module<AnyObject>>;
59
59
  * Options for module binding.
60
60
  *
61
61
  * `scope` types:
62
- * - `singleton` - **This is the default**. The module is created and cached by the container which registered the factory.
63
- * - `scoped` - The module is created and cached by the container which starts resolving.
62
+ * - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
63
+ * - `scoped` - The value is created and cached by the nearest container which owns the factory function.
64
64
  */
65
65
  export type BindModuleOptions = {
66
66
  scope?: 'scoped' | 'singleton';
67
67
  };
68
- export type ModuleBindingEntry = ModuleDeclaration<AnyObject> | {
68
+ type ModuleDeclarationWithOptions = {
69
69
  module: ModuleDeclaration<AnyObject>;
70
70
  options: BindModuleOptions;
71
71
  };
72
+ export type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
72
73
  /**
73
74
  * Binds the dependency module to the container
74
75
  * @param container - Dependency container.
@@ -1,5 +1,5 @@
1
- import { injectable } from './utils';
2
1
  import { token } from './tokens';
2
+ import { injectable } from './utils';
3
3
  /**
4
4
  * Binds the dependency module to the container
5
5
  * @param container - Dependency container.
@@ -12,39 +12,73 @@ import { token } from './tokens';
12
12
  * ```
13
13
  */
14
14
  export function bindModule(container, moduleDeclaration, options) {
15
- const { token, imports, factory, beforeBinding, afterBinding } = moduleDeclaration;
16
- const exports = moduleDeclaration.exports;
17
- const scope = options === null || options === void 0 ? void 0 : options.scope;
18
- if (beforeBinding) {
19
- beforeBinding(container);
15
+ var _a, _b, _c;
16
+ const rootEntry = {
17
+ module: moduleDeclaration,
18
+ options: options !== null && options !== void 0 ? options : {},
19
+ };
20
+ const bfsVisits = new Set([rootEntry]);
21
+ const bfsQueue = [rootEntry];
22
+ let bfsIndex = 0;
23
+ while (bfsIndex < bfsQueue.length) {
24
+ const entry = bfsQueue[bfsIndex];
25
+ const m = 'module' in entry ? entry.module : entry;
26
+ (_a = m.imports) === null || _a === void 0 ? void 0 : _a.forEach((depEntry) => {
27
+ if (!bfsVisits.has(depEntry)) {
28
+ bfsVisits.add(depEntry);
29
+ bfsQueue.push(depEntry);
30
+ }
31
+ });
32
+ bfsIndex++;
33
+ }
34
+ for (let i = 0; i < bfsQueue.length; i++) {
35
+ const entry = bfsQueue[i];
36
+ const m = 'module' in entry ? entry.module : entry;
37
+ (_b = m.beforeBinding) === null || _b === void 0 ? void 0 : _b.call(m, container);
20
38
  }
21
- if (imports) {
22
- bindModules(container, imports);
39
+ for (let i = 0; i < bfsQueue.length; i++) {
40
+ const entry = bfsQueue[i];
41
+ bindModuleEntry(container, entry);
23
42
  }
43
+ for (let i = bfsQueue.length - 1; i >= 0; i--) {
44
+ const entry = bfsQueue[i];
45
+ const m = 'module' in entry ? entry.module : entry;
46
+ (_c = m.afterBinding) === null || _c === void 0 ? void 0 : _c.call(m, container);
47
+ }
48
+ }
49
+ function bindModuleEntry(container, entry) {
50
+ let module;
51
+ let options;
52
+ if ('module' in entry) {
53
+ module = entry.module;
54
+ options = entry.options;
55
+ }
56
+ else {
57
+ module = entry;
58
+ }
59
+ const scope = options === null || options === void 0 ? void 0 : options.scope;
24
60
  const exportedValueTokens = new Set();
25
- if (exports) {
26
- const keys = Object.keys(exports);
61
+ const moduleExports = module.exports;
62
+ if (moduleExports) {
63
+ const keys = Object.keys(moduleExports);
27
64
  keys.forEach((valueKey) => {
28
- const valueToken = exports[valueKey];
65
+ const valueToken = moduleExports[valueKey];
29
66
  if (valueToken) {
30
67
  exportedValueTokens.add(valueToken);
31
- container.bindFactory(valueToken, injectable((module) => module[valueKey], token), { scope });
68
+ container.bindFactory(valueToken, injectable((module) => module[valueKey], module.token), { scope });
32
69
  }
33
70
  });
34
71
  }
35
- container.bindFactory(token, factory, {
72
+ container.bindFactory(module.token, module.factory, {
36
73
  scope,
37
- onRemoved: (module) => {
38
- if (module.destroy) {
39
- module.destroy();
74
+ onRemoved: (moduleInstance) => {
75
+ if (moduleInstance.destroy) {
76
+ moduleInstance.destroy();
40
77
  }
41
78
  exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
42
79
  exportedValueTokens.clear();
43
80
  },
44
81
  });
45
- if (afterBinding) {
46
- afterBinding(container);
47
- }
48
82
  }
49
83
  /**
50
84
  * Binds dependency modules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qBAAqB,CACnC,OAA0C;IAE1C,OAAO,aAAa,CAAC;QACnB,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;QACnB,OAAO,EAAE,OAAO;KACjB,CAAC,CAAC;AACL,CAAC"}
@@ -69,8 +69,8 @@ type FactoryScope = 'scoped' | 'singleton' | 'transient';
69
69
  * Options for factory binding.
70
70
  *
71
71
  * `scope` types:
72
- * - `singleton` - **This is the default**. The value is created and cached by the container which registered the factory.
73
- * - `scoped` - The value is created and cached by the container which starts resolving.
72
+ * - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
73
+ * - `scoped` - The value is created and cached by the nearest container which owns the factory function.
74
74
  * - `transient` - The value is created every time it is resolved.
75
75
  *
76
76
  * `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
@@ -102,7 +102,7 @@ type Container = {
102
102
  */
103
103
  get<T>(token: Token<T>): T | undefined;
104
104
  /**
105
- * Returns a resolved value by the token, or throws `ResolverError` in case the token is not found.
105
+ * Returns a resolved value by the token or throws `ResolverError` in case the token is not found.
106
106
  */
107
107
  resolve<T>(token: Token<T>): T;
108
108
  /**
@@ -119,9 +119,9 @@ type Container = {
119
119
  *
120
120
  * Container can have an optional parent to chain token resolution. The parent is used in case the current container does not have a registered token.
121
121
  *
122
- * @param parentContainer - Optional parent container.
122
+ * @param parentArg - Optional parent container or an array of containers.
123
123
  */
124
- declare function createContainer(parentContainer?: Container): Container;
124
+ declare function createContainer(parentArg?: Container | ReadonlyArray<Container>): Container;
125
125
 
126
126
  type ValuesProps = {
127
127
  [key: string]: unknown;
@@ -316,16 +316,17 @@ type ModuleDeclaration<T extends Module<AnyObject>> = {
316
316
  * Options for module binding.
317
317
  *
318
318
  * `scope` types:
319
- * - `singleton` - **This is the default**. The module is created and cached by the container which registered the factory.
320
- * - `scoped` - The module is created and cached by the container which starts resolving.
319
+ * - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
320
+ * - `scoped` - The value is created and cached by the nearest container which owns the factory function.
321
321
  */
322
322
  type BindModuleOptions = {
323
323
  scope?: 'scoped' | 'singleton';
324
324
  };
325
- type ModuleBindingEntry = ModuleDeclaration<AnyObject> | {
325
+ type ModuleDeclarationWithOptions = {
326
326
  module: ModuleDeclaration<AnyObject>;
327
327
  options: BindModuleOptions;
328
328
  };
329
+ type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
329
330
  /**
330
331
  * Binds the dependency module to the container
331
332
  * @param container - Dependency container.
package/dist/umd/index.js CHANGED
@@ -1,2 +1,2 @@
1
- !function(o,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((o="undefined"!=typeof globalThis?globalThis:o||self).Ditox={})}(this,(function(o){"use strict";function n(o){var n="string"==typeof o?{description:o}:o;return{symbol:(null==n?void 0:n.key)?Symbol.for(n.key):Symbol(null==n?void 0:n.description)}}var t=function(o,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(o,n){o.__proto__=n}||function(o,n){for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(o[t]=n[t])},t(o,n)};var e=function(){return e=Object.assign||function(o){for(var n,t=1,e=arguments.length;t<e;t++)for(var r in n=arguments[t])Object.prototype.hasOwnProperty.call(n,r)&&(o[r]=n[r]);return o},e.apply(this,arguments)};function r(o,n,t){if(t||2===arguments.length)for(var e,r=0,i=n.length;r<i;r++)!e&&r in n||(e||(e=Array.prototype.slice.call(n,0,r)),e[r]=n[r]);return o.concat(e||Array.prototype.slice.call(n))}var i=function(o){function n(n){var t=this.constructor,e=o.call(this,n)||this;return Object.setPrototypeOf(e,t.prototype),e.name="ResolverError",e}return function(o,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function e(){this.constructor=o}t(o,n),o.prototype=null===n?Object.create(n):(e.prototype=n.prototype,new e)}(n,o),n}(Error),l=n("ditox.Container"),u=n("ditox.ParentContainer"),a=n("ditox.Resolver"),s=Symbol(),c=function(){throw new Error("FAKE_FACTORY")},f="singleton",y=n("ditox.FactoriesMap");function p(o){return o.symbol===l.symbol||o.symbol===u.symbol||o.symbol===a.symbol}function d(o){return null!=o&&"object"==typeof o&&"symbol"in o&&"symbol"==typeof o.symbol}function v(o,n){if(d(n))return o.get(n);var t={};return Object.keys(n).forEach((function(e){return t[e]=o.get(n[e])})),t}function b(o,n){if(d(n))return o.resolve(n);var t={};return Object.keys(n).forEach((function(e){return t[e]=o.resolve(n[e])})),t}function m(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return b(o,n)}))}function h(o){for(var n=this,t=[],e=1;e<arguments.length;e++)t[e-1]=arguments[e];return function(e){var i=m.apply(void 0,r([e],t,!1));return o.apply(n,i)}}function g(o,n,t){var e=n.token,r=n.imports,i=n.factory,l=n.beforeBinding,u=n.afterBinding,a=n.exports,s=null==t?void 0:t.scope;l&&l(o),r&&O(o,r);var c=new Set;a&&Object.keys(a).forEach((function(n){var t=a[n];t&&(c.add(t),o.bindFactory(t,h((function(o){return o[n]}),e),{scope:s}))}));o.bindFactory(e,i,{scope:s,onRemoved:function(n){n.destroy&&n.destroy(),c.forEach((function(n){return o.remove(n)})),c.clear()}}),u&&u(o)}function O(o,n){n.forEach((function(n){"module"in n?g(o,n.module,n.options):g(o,n)}))}function k(o){var t;return e(e({},o),{token:null!==(t=o.token)&&void 0!==t?t:n()})}o.ResolverError=i,o.bindModule=g,o.bindModules=O,o.bindMultiValue=function(o,n,t){var e,i=r(r([],null!==(e=o.get(n))&&void 0!==e?e:[],!0),[t],!1);o.bindValue(n,i)},o.createContainer=function(o){var n=new Map,t=new Map,e={bindValue:function(o,t){p(o)||n.set(o.symbol,t)},bindFactory:function(o,n,e){p(o)||t.set(o.symbol,{factory:n,options:e})},remove:function(o){var e;if(!p(o)){var r=null===(e=t.get(o.symbol))||void 0===e?void 0:e.options;r&&d(o.symbol,r),n.delete(o.symbol),t.delete(o.symbol)}},removeAll:function(){t.forEach((function(o,n){o.options&&d(n,o.options)})),n.clear(),t.clear(),v()},hasToken:function(e){var r;return n.has(e.symbol)||t.has(e.symbol)||null!==(r=null==o?void 0:o.hasToken(e))&&void 0!==r&&r},get:function(o){var n=r(o,e);return n!==s?n:o.isOptional?o.optionalValue:void 0},resolve:function(o){var n,t=r(o,e);if(t!==s)return t;if(o.isOptional)return o.optionalValue;throw new i('Token "'.concat(null!==(n=o.symbol.description)&&void 0!==n?n:"",'" is not provided'))}};function r(r,i){var l=n.get(r.symbol),u=void 0!==l||n.has(r.symbol);if(u&&i===e)return l;var y,p,d=t.get(r.symbol);if(d&&d.factory!==c)switch(y=d.options,null!==(p=null==y?void 0:y.scope)&&void 0!==p?p:f){case"singleton":if(u)return l;if(null==o?void 0:o.hasToken(r))break;var v=d.factory(e);return e.bindValue(r,v),v;case"scoped":var b=d.factory(i);return i.bindValue(r,b),i!==e&&i.bindFactory(r,c,d.options),b;case"transient":return d.factory(i)}if(u)return l;var m=null==o?void 0:o.get(a);return m?m(r,i):s}function d(o,t){var e=function(o){return void 0===o.scope||"scoped"===o.scope||"singleton"===o.scope?o.onRemoved:void 0}(t);if(e){var r=n.get(o);(void 0!==r||n.has(o))&&e(r)}}function v(){n.set(l.symbol,e),n.set(a.symbol,r),n.set(y.symbol,t),o&&n.set(u.symbol,o)}return v(),e},o.declareModule=k,o.declareModuleBindings=function(o){return k({factory:function(){return{}},imports:o})},o.injectable=h,o.injectableClass=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return h.apply(void 0,r([function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return new(o.bind.apply(o,r([void 0],n,!1)))}],n,!1))},o.isToken=d,o.optional=function(o,n){return{symbol:o.symbol,isOptional:!0,optionalValue:n}},o.resolveValue=b,o.resolveValues=m,o.token=n,o.tryResolveValue=v,o.tryResolveValues=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return v(o,n)}))}}));
1
+ !function(o,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((o="undefined"!=typeof globalThis?globalThis:o||self).Ditox={})}(this,(function(o){"use strict";function n(o){var n="string"==typeof o?{description:o}:o;return{symbol:(null==n?void 0:n.key)?Symbol.for(n.key):Symbol(null==n?void 0:n.description)}}var t=function(o,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(o,n){o.__proto__=n}||function(o,n){for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(o[t]=n[t])},t(o,n)};var e=function(){return e=Object.assign||function(o){for(var n,t=1,e=arguments.length;t<e;t++)for(var r in n=arguments[t])Object.prototype.hasOwnProperty.call(n,r)&&(o[r]=n[r]);return o},e.apply(this,arguments)};function r(o,n,t){if(t||2===arguments.length)for(var e,r=0,i=n.length;r<i;r++)!e&&r in n||(e||(e=Array.prototype.slice.call(n,0,r)),e[r]=n[r]);return o.concat(e||Array.prototype.slice.call(n))}var i=function(o){function n(n){var t=this.constructor,e=o.call(this,n)||this;return Object.setPrototypeOf(e,t.prototype),e.name="ResolverError",e}return function(o,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function e(){this.constructor=o}t(o,n),o.prototype=null===n?Object.create(n):(e.prototype=n.prototype,new e)}(n,o),n}(Error),l=n("ditox.Container"),u=n("ditox.ParentContainer"),a=n("ditox.Resolver"),s=Symbol(),c="singleton",f=n("ditox.FactoriesMap");function d(o){return o.symbol===l.symbol||o.symbol===u.symbol||o.symbol===a.symbol}function p(o,n){if(!o)return!1;for(var t=0;t<o.length;t++)if(o[t].hasToken(n))return!0;return!1}function y(o){return null!=o&&"object"==typeof o&&"symbol"in o&&"symbol"==typeof o.symbol}function v(o,n){if(y(n))return o.get(n);var t={};return Object.keys(n).forEach((function(e){return t[e]=o.get(n[e])})),t}function b(o,n){if(y(n))return o.resolve(n);var t={};return Object.keys(n).forEach((function(e){return t[e]=o.resolve(n[e])})),t}function m(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return b(o,n)}))}function h(o){for(var n=this,t=[],e=1;e<arguments.length;e++)t[e-1]=arguments[e];return function(e){var i=m.apply(void 0,r([e],t,!1));return o.apply(n,i)}}function g(o,n,t){for(var e,r,i,l={module:n,options:null!=t?t:{}},u=new Set([l]),a=[l],s=0;s<a.length;){null===(e=(d="module"in(f=a[s])?f.module:f).imports)||void 0===e||e.forEach((function(o){u.has(o)||(u.add(o),a.push(o))})),s++}for(var c=0;c<a.length;c++){null===(r=(d="module"in(f=a[c])?f.module:f).beforeBinding)||void 0===r||r.call(d,o)}for(c=0;c<a.length;c++){O(o,f=a[c])}for(c=a.length-1;c>=0;c--){var f,d;null===(i=(d="module"in(f=a[c])?f.module:f).afterBinding)||void 0===i||i.call(d,o)}}function O(o,n){var t,e;"module"in n?(t=n.module,e=n.options):t=n;var r=null==e?void 0:e.scope,i=new Set,l=t.exports;l&&Object.keys(l).forEach((function(n){var e=l[n];e&&(i.add(e),o.bindFactory(e,h((function(o){return o[n]}),t.token),{scope:r}))}));o.bindFactory(t.token,t.factory,{scope:r,onRemoved:function(n){n.destroy&&n.destroy(),i.forEach((function(n){return o.remove(n)})),i.clear()}})}function k(o){var t;return e(e({},o),{token:null!==(t=o.token)&&void 0!==t?t:n()})}o.ResolverError=i,o.bindModule=g,o.bindModules=function(o,n){n.forEach((function(n){"module"in n?g(o,n.module,n.options):g(o,n)}))},o.bindMultiValue=function(o,n,t){var e,i=r(r([],null!==(e=o.get(n))&&void 0!==e?e:[],!0),[t],!1);o.bindValue(n,i)},o.createContainer=function(o){var n=o?Array.isArray(o)?r([],o,!0):[o]:void 0,t=new Map,e=new Map,y={bindValue:function(o,n){d(o)||t.set(o.symbol,n)},bindFactory:function(o,n,t){d(o)||e.set(o.symbol,{factory:n,options:t})},remove:function(o){var n;if(!d(o)){var r=null===(n=e.get(o.symbol))||void 0===n?void 0:n.options;r&&b(o.symbol,r),t.delete(o.symbol),e.delete(o.symbol)}},removeAll:function(){e.forEach((function(o,n){o.options&&b(n,o.options)})),t.clear(),e.clear(),m()},hasToken:function(o){return t.has(o.symbol)||e.has(o.symbol)||p(n,o)},get:function(o){var n=v(o,y);return n!==s?n:o.isOptional?o.optionalValue:void 0},resolve:function(o){var n,t=v(o,y);if(t!==s)return t;if(o.isOptional)return o.optionalValue;throw new i('Token "'.concat(null!==(n=o.symbol.description)&&void 0!==n?n:"",'" is not provided'))}};function v(o,r){var i=t.get(o.symbol),l=void 0!==i||t.has(o.symbol);if(l&&r===y)return i;var u,f,d=e.get(o.symbol);if(d)switch(u=d.options,null!==(f=null==u?void 0:u.scope)&&void 0!==f?f:c){case"singleton":if(l)return i;if(p(n,o))break;var v=d.factory(y);return y.bindValue(o,v),v;case"scoped":if(l)return i;var b=d.factory(y);return y.bindValue(o,b),b;case"transient":return d.factory(r)}return l?i:n?function(o,n,t){for(var e=0,r=o;e<r.length;e++){var i=r[e].get(a);if(i){var l=i(n,t);if(l!==s)return l}}return s}(n,o,r):s}function b(o,n){var e=function(o){return void 0===o.scope||"scoped"===o.scope||"singleton"===o.scope?o.onRemoved:void 0}(n);if(e){var r=t.get(o);(void 0!==r||t.has(o))&&e(r)}}function m(){t.set(l.symbol,y),t.set(a.symbol,v),t.set(f.symbol,e),n&&t.set(u.symbol,n)}return m(),y},o.declareModule=k,o.declareModuleBindings=function(o){return k({factory:function(){return{}},imports:o})},o.injectable=h,o.injectableClass=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return h.apply(void 0,r([function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return new(o.bind.apply(o,r([void 0],n,!1)))}],n,!1))},o.isToken=y,o.optional=function(o,n){return{symbol:o.symbol,isOptional:!0,optionalValue:n}},o.resolveValue=b,o.resolveValues=m,o.token=n,o.tryResolveValue=v,o.tryResolveValues=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return v(o,n)}))}}));
2
2
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ditox",
3
- "version": "2.4.1",
3
+ "version": "3.1.0",
4
4
  "description": "Dependency injection for modular web applications",
5
5
  "license": "MIT",
6
6
  "author": "Mikhail Nasyrov <mikhail@mnasyrov.com> (https://github.com/mnasyrov)",
@@ -45,12 +45,11 @@
45
45
  ],
46
46
  "scripts": {
47
47
  "clean": "shx rm -rf dist lib",
48
- "test-deno": "npm run build:rollup && deno test test-deno",
49
48
  "build": "npm run build:cjs && npm run build:esm && npm run build:rollup",
50
49
  "build:cjs": "tsc -p tsconfig.build.json --outDir dist/cjs --module commonjs",
51
50
  "build:esm": "tsc -p tsconfig.build.json --outDir dist/esm --module es2015",
52
51
  "build:rollup": "rollup -c",
53
52
  "typedoc": "typedoc"
54
53
  },
55
- "gitHead": "e9c66f51c861a7598ab73415d7b2041700bc6d69"
54
+ "gitHead": "e9d435838d465b1a878d3b7bc5c7cc6e90fd241a"
56
55
  }