ts-ioc-container 57.2.0 → 57.4.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.
@@ -8,11 +8,12 @@ const AliasMap_1 = require("./AliasMap");
8
8
  const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
9
9
  const basic_1 = require("../utils/basic");
10
10
  const array_1 = require("../utils/array");
11
+ const proxy_1 = require("../utils/proxy");
11
12
  class Container {
12
13
  isDisposed = false;
13
14
  parent;
14
15
  scopes = [];
15
- instances = [];
16
+ instances = new Set();
16
17
  registrations = [];
17
18
  tags;
18
19
  providers = new Map();
@@ -108,7 +109,7 @@ class Container {
108
109
  }
109
110
  this.providers.clear();
110
111
  this.aliases.destroy();
111
- this.instances = [];
112
+ this.instances.clear();
112
113
  this.registrations = [];
113
114
  this.onConstructHookList.length = 0;
114
115
  this.onScopeCreatedHookList.length = 0;
@@ -137,7 +138,7 @@ class Container {
137
138
  return this;
138
139
  }
139
140
  addInstance(instance) {
140
- this.instances.push(instance);
141
+ this.instances.add(instance);
141
142
  for (const onConstruct of this.onConstructHookList) {
142
143
  onConstruct(instance, this);
143
144
  }
@@ -146,7 +147,7 @@ class Container {
146
147
  return [...this.scopes];
147
148
  }
148
149
  hasInstance(instance) {
149
- return this.instances.includes(instance);
150
+ return this.instances.has((0, proxy_1.unwrapProxyTarget)(instance));
150
151
  }
151
152
  getScopeByInstanceOrFail(instance) {
152
153
  this.validateContainer();
package/cjm/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createHookContext = exports.createHookContextFactory = exports.HookContext = exports.prepend = exports.append = exports.prependHooks = exports.appendHooks = exports.hasHooks = exports.hook = exports.getHooks = exports.UnsupportedTokenTypeError = exports.CannonSingletonApplyTwiceError = exports.UnexpectedHookResultError = exports.ProviderDisposedError = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyMissingKeyError = exports.ContainerNotFoundError = exports.DependencyNotFoundError = exports.ContainerError = exports.Registration = exports.appendArgsFn = exports.appendArgs = exports.decorate = exports.singleton = exports.autoResolve = exports.lazy = exports.scopeAccess = exports.scope = exports.bindTo = exports.register = exports.toRegistrationFn = exports.toProviderFn = exports.toBindToken = exports.registerPipe = exports.isProviderPipe = exports.Provider = exports.ProxyInjector = exports.SimpleInjector = exports.resolveArgs = exports.argsFn = exports.args = exports.arg = exports.inject = exports.MetadataInjector = exports.Injector = exports.EmptyContainer = exports.AutoResolveModule = exports.Container = exports.isDependencyKey = void 0;
4
4
  exports.pipe = exports.select = exports.once = exports.shallowCache = exports.debounce = exports.throttle = exports.handleAsyncError = exports.handleError = exports.getMethodTags = exports.addMethodTag = exports.getMethodLabels = exports.addMethodLabel = exports.getMethodMeta = exports.addMethodMeta = exports.getParamTags = exports.addParamTag = exports.getParamLabels = exports.addParamLabel = exports.getParamMeta = exports.addParamMeta = exports.getClassTags = exports.addClassTag = exports.getClassLabels = exports.addClassLabel = exports.getClassMeta = exports.addClassMeta = exports.GroupInstanceToken = exports.ConstantToken = exports.FunctionToken = exports.SingleToken = exports.ClassToken = exports.toSingleAlias = exports.SingleAliasToken = exports.toGroupAlias = exports.GroupAliasToken = exports.argToToken = exports.toMappedToken = exports.toToken = exports.InjectionToken = exports.HooksRunner = exports.AddOnDisposeHookModule = exports.onContainerDisposed = exports.onContainerDisposedHooksRunner = exports.AddOnConstructAsyncHookModule = exports.onConstructAsync = exports.onConstructAsyncHooksRunner = exports.AddOnConstructHookModule = exports.onConstruct = exports.onConstructHooksRunner = exports.injectProp = void 0;
5
- exports.resolveConstructor = exports.Is = void 0;
5
+ exports.resolveConstructor = exports.Is = exports.isProxy = exports.getProxyTarget = exports.createProxy = void 0;
6
6
  var IContainer_1 = require("./container/IContainer");
7
7
  Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
8
8
  var Container_1 = require("./container/Container");
@@ -150,6 +150,10 @@ var select_1 = require("./select");
150
150
  Object.defineProperty(exports, "select", { enumerable: true, get: function () { return select_1.select; } });
151
151
  var fp_1 = require("./utils/fp");
152
152
  Object.defineProperty(exports, "pipe", { enumerable: true, get: function () { return fp_1.pipe; } });
153
+ var proxy_1 = require("./utils/proxy");
154
+ Object.defineProperty(exports, "createProxy", { enumerable: true, get: function () { return proxy_1.createProxy; } });
155
+ Object.defineProperty(exports, "getProxyTarget", { enumerable: true, get: function () { return proxy_1.getProxyTarget; } });
156
+ Object.defineProperty(exports, "isProxy", { enumerable: true, get: function () { return proxy_1.isProxy; } });
153
157
  var basic_1 = require("./utils/basic");
154
158
  Object.defineProperty(exports, "Is", { enumerable: true, get: function () { return basic_1.Is; } });
155
159
  Object.defineProperty(exports, "resolveConstructor", { enumerable: true, get: function () { return basic_1.resolveConstructor; } });
@@ -1,24 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unwrapProxyTarget = void 0;
3
4
  exports.isProxy = isProxy;
4
5
  exports.getProxyTarget = getProxyTarget;
6
+ exports.createProxy = createProxy;
5
7
  exports.lazyProxy = lazyProxy;
6
8
  exports.toLazyIf = toLazyIf;
7
9
  const proxyStateMap = new WeakMap();
8
10
  const unwrapProxyTarget = (value) => {
9
11
  return isProxy(value) ? getProxyTarget(value) : value;
10
12
  };
13
+ exports.unwrapProxyTarget = unwrapProxyTarget;
11
14
  function isProxy(value) {
12
15
  return proxyStateMap.has(value);
13
16
  }
14
17
  function getProxyTarget(value) {
15
- return proxyStateMap.get(value).getTarget();
18
+ const target = proxyStateMap.get(value).getTarget();
19
+ return (0, exports.unwrapProxyTarget)(target);
20
+ }
21
+ function createProxy(target, handler = {}) {
22
+ const proxy = new Proxy(target, handler);
23
+ proxyStateMap.set(proxy, { getTarget: () => target });
24
+ return proxy;
16
25
  }
17
26
  function lazyProxy(resolveInstance) {
18
27
  let instance;
19
28
  const state = {
20
29
  getTarget: () => {
21
- instance = instance ?? unwrapProxyTarget(resolveInstance());
30
+ instance = instance ?? (0, exports.unwrapProxyTarget)(resolveInstance());
22
31
  return instance;
23
32
  },
24
33
  };
@@ -5,11 +5,12 @@ import { AliasMap } from './AliasMap.js';
5
5
  import { DependencyNotFoundError } from '../errors/DependencyNotFoundError.js';
6
6
  import { Is } from '../utils/basic.js';
7
7
  import { Filter as F } from '../utils/array.js';
8
+ import { unwrapProxyTarget } from '../utils/proxy.js';
8
9
  export class Container {
9
10
  isDisposed = false;
10
11
  parent;
11
12
  scopes = [];
12
- instances = [];
13
+ instances = new Set();
13
14
  registrations = [];
14
15
  tags;
15
16
  providers = new Map();
@@ -105,7 +106,7 @@ export class Container {
105
106
  }
106
107
  this.providers.clear();
107
108
  this.aliases.destroy();
108
- this.instances = [];
109
+ this.instances.clear();
109
110
  this.registrations = [];
110
111
  this.onConstructHookList.length = 0;
111
112
  this.onScopeCreatedHookList.length = 0;
@@ -134,7 +135,7 @@ export class Container {
134
135
  return this;
135
136
  }
136
137
  addInstance(instance) {
137
- this.instances.push(instance);
138
+ this.instances.add(instance);
138
139
  for (const onConstruct of this.onConstructHookList) {
139
140
  onConstruct(instance, this);
140
141
  }
@@ -143,7 +144,7 @@ export class Container {
143
144
  return [...this.scopes];
144
145
  }
145
146
  hasInstance(instance) {
146
- return this.instances.includes(instance);
147
+ return this.instances.has(unwrapProxyTarget(instance));
147
148
  }
148
149
  getScopeByInstanceOrFail(instance) {
149
150
  this.validateContainer();
package/esm/index.js CHANGED
@@ -45,4 +45,5 @@ export { shallowCache } from './utils/shallowCache.js';
45
45
  export { once } from './utils/once.js';
46
46
  export { select } from './select.js';
47
47
  export { pipe } from './utils/fp.js';
48
+ export { createProxy, getProxyTarget, isProxy } from './utils/proxy.js';
48
49
  export { Is, resolveConstructor } from './utils/basic.js';
@@ -1,12 +1,18 @@
1
1
  const proxyStateMap = new WeakMap();
2
- const unwrapProxyTarget = (value) => {
2
+ export const unwrapProxyTarget = (value) => {
3
3
  return isProxy(value) ? getProxyTarget(value) : value;
4
4
  };
5
5
  export function isProxy(value) {
6
6
  return proxyStateMap.has(value);
7
7
  }
8
8
  export function getProxyTarget(value) {
9
- return proxyStateMap.get(value).getTarget();
9
+ const target = proxyStateMap.get(value).getTarget();
10
+ return unwrapProxyTarget(target);
11
+ }
12
+ export function createProxy(target, handler = {}) {
13
+ const proxy = new Proxy(target, handler);
14
+ proxyStateMap.set(proxy, { getTarget: () => target });
15
+ return proxy;
10
16
  }
11
17
  export function lazyProxy(resolveInstance) {
12
18
  let instance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "57.2.0",
3
+ "version": "57.4.0",
4
4
  "description": "Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -9,7 +9,7 @@ export declare class Container implements IContainer {
9
9
  isDisposed: boolean;
10
10
  private parent;
11
11
  private scopes;
12
- private instances;
12
+ private readonly instances;
13
13
  private registrations;
14
14
  private readonly tags;
15
15
  private readonly providers;
@@ -47,4 +47,5 @@ export { once } from './utils/once.js';
47
47
  export { type ExecutionContext } from './ExecutionContext.js';
48
48
  export { select } from './select.js';
49
49
  export { pipe, type MapFn } from './utils/fp.js';
50
+ export { createProxy, getProxyTarget, isProxy } from './utils/proxy.js';
50
51
  export { type constructor, type Instance, Is, resolveConstructor } from './utils/basic.js';
@@ -1,4 +1,6 @@
1
+ export declare const unwrapProxyTarget: <T extends object>(value: T) => T;
1
2
  export declare function isProxy(value: object): boolean;
2
3
  export declare function getProxyTarget<T extends object>(value: T): T;
4
+ export declare function createProxy<T extends object>(target: T, handler?: ProxyHandler<T>): T;
3
5
  export declare function lazyProxy<T extends object>(resolveInstance: () => T): T;
4
6
  export declare function toLazyIf<T extends object>(resolveInstance: () => T, isLazy?: boolean): T;