plain-ioc 1.0.0 → 1.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.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![npm version](https://badge.fury.io/js/plain-ioc.svg?flush_cache=v1_0_0)](https://badge.fury.io/js/plain-ioc)
1
+ [![npm version](https://badge.fury.io/js/plain-ioc.svg)](https://badge.fury.io/js/plain-ioc)
2
2
 
3
3
  # plain-ioc
4
4
 
@@ -186,7 +186,7 @@ import { Container } from "plain-ioc";
186
186
  const c = new Container({ circularDependencyDetect: true });
187
187
  ```
188
188
 
189
- When enabled, `resolve()` tracks a stack of keys being resolved. If the same key appears twice in the stack, a `CircularDependencyError` is thrown with a message that includes the dependency stack.
189
+ When enabled, `resolve()` tracks a stack of keys being resolved. If the same key appears twice in the stack, a `PlainIocCircularDependencyError` is thrown with a message that includes the dependency stack.
190
190
 
191
191
  > Recommendation: Keep this enabled in development/test, and turn it off in production for minimal overhead.
192
192
 
@@ -204,26 +204,26 @@ Creates a container.
204
204
 
205
205
  Registers a **transient** factory.
206
206
 
207
- - Throws `FactoryAlreadyBoundError` if the key is already registered.
207
+ - Throws `PlainIocFactoryAlreadyBoundError` if the key is already registered.
208
208
 
209
209
  ### `container.bindSingleton<T>(key, factory)`
210
210
 
211
211
  Registers a **singleton** factory.
212
212
 
213
- - Throws `FactoryAlreadyBoundError` if the key is already registered.
213
+ - Throws `PlainIocFactoryAlreadyBoundError` if the key is already registered.
214
214
 
215
215
  ### `container.resolve<T>(key): T`
216
216
 
217
217
  Resolves an instance.
218
218
 
219
- - Throws `FactoryNotBoundError` if the key is not registered.
220
- - Throws `CircularDependencyError` when circular dependency detection is enabled and a cycle is detected.
219
+ - Throws `PlainIocFactoryNotBoundError` if the key is not registered.
220
+ - Throws `PlainIocCircularDependencyError` when circular dependency detection is enabled and a cycle is detected.
221
221
 
222
222
  ### `container.unbind(key)`
223
223
 
224
224
  Removes a registered factory and clears any cached singleton instance.
225
225
 
226
- - Throws `FactoryNotBoundError` if the key is not registered.
226
+ - Throws `PlainIocFactoryNotBoundError` if the key is not registered.
227
227
 
228
228
  ### `container.isBound(key): boolean`
229
229
 
@@ -236,21 +236,21 @@ Returns `true` if a factory is registered for `key`.
236
236
  All library errors extend the base class `PlainIocError`:
237
237
 
238
238
  - `PlainIocError`
239
- - `FactoryNotBoundError`
240
- - `FactoryAlreadyBoundError`
241
- - `CircularDependencyError`
239
+ - `PlainIocFactoryNotBoundError`
240
+ - `PlainIocFactoryAlreadyBoundError`
241
+ - `PlainIocCircularDependencyError`
242
242
 
243
243
  You can catch these specifically:
244
244
 
245
245
  ```ts
246
- import { Container, FactoryNotBoundError } from "plain-ioc";
246
+ import { Container, PlainIocFactoryNotBoundError } from "plain-ioc";
247
247
 
248
248
  const c = new Container();
249
249
 
250
250
  try {
251
251
  c.resolve("missing");
252
252
  } catch (e) {
253
- if (e instanceof FactoryNotBoundError) {
253
+ if (e instanceof PlainIocFactoryNotBoundError) {
254
254
  console.error("Not registered");
255
255
  }
256
256
  }
@@ -11,9 +11,9 @@ export declare class Container {
11
11
  protected circularDependencyStackPush(key: DependencyKey): void;
12
12
  protected circularDependencyStackPop(): void;
13
13
  protected keyToString(key: DependencyKey): string;
14
- bind<T>(key: DependencyKey, factory: DependencyFactory<T>): void;
15
- unbind(key: DependencyKey): void;
16
- bindSingleton<T>(key: DependencyKey, factory: DependencyFactory<T>): void;
14
+ bind<T>(key: DependencyKey, factory: DependencyFactory<T>): this;
15
+ bindSingleton<T>(key: DependencyKey, factory: DependencyFactory<T>): this;
16
+ unbind(key: DependencyKey): this;
17
17
  isBound(key: DependencyKey): boolean;
18
18
  resolve<T>(key: DependencyKey): T;
19
19
  }
package/dist/Container.js CHANGED
@@ -22,7 +22,7 @@ class Container {
22
22
  errorMessage += this.circularDependencyStack.map((item, index) => {
23
23
  return `>>> [${index}]: ${this.keyToString(item)}\n`;
24
24
  }).join('');
25
- throw new errors_1.CircularDependencyError(errorMessage);
25
+ throw new errors_1.PlainIocCircularDependencyError(errorMessage);
26
26
  }
27
27
  }
28
28
  circularDependencyStackPop() {
@@ -31,63 +31,69 @@ class Container {
31
31
  }
32
32
  }
33
33
  keyToString(key) {
34
- const type = typeof key;
34
+ const keyType = typeof key;
35
35
  try {
36
36
  if (typeof key === 'function') {
37
- return `"${key.name || '<anonymous>'}" (${type})`;
37
+ return `[${keyType}] "${key.name || '<anonymous>'}"`;
38
38
  }
39
- if (type === 'object') {
40
- return `"${Object.prototype.toString.call(key)}" (${type})`;
39
+ else if (typeof key === 'symbol') {
40
+ return `[${keyType}] "${Symbol.prototype.toString.call(key)}"`;
41
41
  }
42
- return `"${String(key)}" (${type})`;
42
+ else if (typeof key === 'object') {
43
+ return `[${keyType}] "${Object.prototype.toString.call(key)}"`;
44
+ }
45
+ return `[${keyType}] "${String(key)}"`;
43
46
  }
44
47
  catch (_a) {
45
- return `"<unprintable>" (${type})`;
48
+ return `[${keyType}] "<unprintable>"`;
46
49
  }
47
50
  }
48
51
  bind(key, factory) {
49
52
  if (this.dependencies.has(key)) {
50
- throw new errors_1.FactoryAlreadyBoundError(`Factory for ${this.keyToString(key)} already bound`);
53
+ throw new errors_1.PlainIocFactoryAlreadyBoundError(`Factory for ${this.keyToString(key)} already bound`);
51
54
  }
52
55
  this.dependencies.set(key, {
53
56
  factory
54
57
  });
55
- }
56
- unbind(key) {
57
- if (!this.dependencies.has(key)) {
58
- throw new errors_1.FactoryNotBoundError(`Factory not bound with ${this.keyToString(key)}`);
59
- }
60
- this.dependencies.delete(key);
61
- this.initializedInstances.delete(key);
58
+ return this;
62
59
  }
63
60
  bindSingleton(key, factory) {
64
61
  if (this.dependencies.has(key)) {
65
- throw new errors_1.FactoryAlreadyBoundError(`Factory for ${this.keyToString(key)} already bound`);
62
+ throw new errors_1.PlainIocFactoryAlreadyBoundError(`Factory for ${this.keyToString(key)} already bound`);
66
63
  }
67
64
  this.dependencies.set(key, {
68
65
  factory,
69
66
  singleton: true
70
67
  });
68
+ return this;
69
+ }
70
+ unbind(key) {
71
+ if (!this.dependencies.has(key)) {
72
+ throw new errors_1.PlainIocFactoryNotBoundError(`Factory not bound with ${this.keyToString(key)}`);
73
+ }
74
+ this.dependencies.delete(key);
75
+ this.initializedInstances.delete(key);
76
+ return this;
71
77
  }
72
78
  isBound(key) {
73
79
  return this.dependencies.has(key);
74
80
  }
75
81
  resolve(key) {
76
- if (!this.dependencies.has(key)) {
77
- throw new errors_1.FactoryNotBoundError(`Factory not bound with ${this.keyToString(key)}`);
78
- }
79
82
  const dependency = this.dependencies.get(key);
83
+ if (!dependency) {
84
+ throw new errors_1.PlainIocFactoryNotBoundError(`Factory not bound with ${this.keyToString(key)}`);
85
+ }
80
86
  if (dependency.singleton && this.initializedInstances.has(key)) {
81
87
  return this.initializedInstances.get(key);
82
88
  }
83
89
  try {
84
90
  this.circularDependencyStackPush(key);
91
+ const { factory } = dependency;
92
+ const instance = factory(this);
85
93
  if (dependency.singleton) {
86
- const instance = dependency.factory(this);
87
94
  this.initializedInstances.set(key, instance);
88
- return instance;
89
95
  }
90
- return dependency.factory(this);
96
+ return instance;
91
97
  }
92
98
  finally {
93
99
  this.circularDependencyStackPop();
@@ -0,0 +1,3 @@
1
+ import { PlainIocError } from './PlainIocError';
2
+ export declare class PlainIocCircularDependencyError extends PlainIocError {
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PlainIocCircularDependencyError = void 0;
4
+ const PlainIocError_1 = require("./PlainIocError");
5
+ class PlainIocCircularDependencyError extends PlainIocError_1.PlainIocError {
6
+ }
7
+ exports.PlainIocCircularDependencyError = PlainIocCircularDependencyError;
@@ -0,0 +1,3 @@
1
+ import { PlainIocError } from './PlainIocError';
2
+ export declare class PlainIocFactoryAlreadyBoundError extends PlainIocError {
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PlainIocFactoryAlreadyBoundError = void 0;
4
+ const PlainIocError_1 = require("./PlainIocError");
5
+ class PlainIocFactoryAlreadyBoundError extends PlainIocError_1.PlainIocError {
6
+ }
7
+ exports.PlainIocFactoryAlreadyBoundError = PlainIocFactoryAlreadyBoundError;
@@ -0,0 +1,3 @@
1
+ import { PlainIocError } from './PlainIocError';
2
+ export declare class PlainIocFactoryNotBoundError extends PlainIocError {
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PlainIocFactoryNotBoundError = void 0;
4
+ const PlainIocError_1 = require("./PlainIocError");
5
+ class PlainIocFactoryNotBoundError extends PlainIocError_1.PlainIocError {
6
+ }
7
+ exports.PlainIocFactoryNotBoundError = PlainIocFactoryNotBoundError;
@@ -1,4 +1,4 @@
1
1
  export { PlainIocError } from './PlainIocError';
2
- export { FactoryNotBoundError } from './FactoryNotBoundError';
3
- export { CircularDependencyError } from './CircularDependencyError';
4
- export { FactoryAlreadyBoundError } from './FactoryAlreadyBoundError';
2
+ export { PlainIocFactoryNotBoundError } from './PlainIocFactoryNotBoundError';
3
+ export { PlainIocCircularDependencyError } from './PlainIocCircularDependencyError';
4
+ export { PlainIocFactoryAlreadyBoundError } from './PlainIocFactoryAlreadyBoundError';
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FactoryAlreadyBoundError = exports.CircularDependencyError = exports.FactoryNotBoundError = exports.PlainIocError = void 0;
3
+ exports.PlainIocFactoryAlreadyBoundError = exports.PlainIocCircularDependencyError = exports.PlainIocFactoryNotBoundError = exports.PlainIocError = void 0;
4
4
  var PlainIocError_1 = require("./PlainIocError");
5
5
  Object.defineProperty(exports, "PlainIocError", { enumerable: true, get: function () { return PlainIocError_1.PlainIocError; } });
6
- var FactoryNotBoundError_1 = require("./FactoryNotBoundError");
7
- Object.defineProperty(exports, "FactoryNotBoundError", { enumerable: true, get: function () { return FactoryNotBoundError_1.FactoryNotBoundError; } });
8
- var CircularDependencyError_1 = require("./CircularDependencyError");
9
- Object.defineProperty(exports, "CircularDependencyError", { enumerable: true, get: function () { return CircularDependencyError_1.CircularDependencyError; } });
10
- var FactoryAlreadyBoundError_1 = require("./FactoryAlreadyBoundError");
11
- Object.defineProperty(exports, "FactoryAlreadyBoundError", { enumerable: true, get: function () { return FactoryAlreadyBoundError_1.FactoryAlreadyBoundError; } });
6
+ var PlainIocFactoryNotBoundError_1 = require("./PlainIocFactoryNotBoundError");
7
+ Object.defineProperty(exports, "PlainIocFactoryNotBoundError", { enumerable: true, get: function () { return PlainIocFactoryNotBoundError_1.PlainIocFactoryNotBoundError; } });
8
+ var PlainIocCircularDependencyError_1 = require("./PlainIocCircularDependencyError");
9
+ Object.defineProperty(exports, "PlainIocCircularDependencyError", { enumerable: true, get: function () { return PlainIocCircularDependencyError_1.PlainIocCircularDependencyError; } });
10
+ var PlainIocFactoryAlreadyBoundError_1 = require("./PlainIocFactoryAlreadyBoundError");
11
+ Object.defineProperty(exports, "PlainIocFactoryAlreadyBoundError", { enumerable: true, get: function () { return PlainIocFactoryAlreadyBoundError_1.PlainIocFactoryAlreadyBoundError; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "plain-ioc",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Plain inversion of control container",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
@@ -9,6 +9,7 @@
9
9
  ".": {
10
10
  "types": "./dist/index.d.ts",
11
11
  "require": "./dist/index.js",
12
+ "import": "./dist/index.js",
12
13
  "default": "./dist/index.js"
13
14
  }
14
15
  },
@@ -1,3 +0,0 @@
1
- import { PlainIocError } from './PlainIocError';
2
- export declare class CircularDependencyError extends PlainIocError {
3
- }
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CircularDependencyError = void 0;
4
- const PlainIocError_1 = require("./PlainIocError");
5
- class CircularDependencyError extends PlainIocError_1.PlainIocError {
6
- }
7
- exports.CircularDependencyError = CircularDependencyError;
@@ -1,3 +0,0 @@
1
- import { PlainIocError } from './PlainIocError';
2
- export declare class FactoryAlreadyBoundError extends PlainIocError {
3
- }
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FactoryAlreadyBoundError = void 0;
4
- const PlainIocError_1 = require("./PlainIocError");
5
- class FactoryAlreadyBoundError extends PlainIocError_1.PlainIocError {
6
- }
7
- exports.FactoryAlreadyBoundError = FactoryAlreadyBoundError;
@@ -1,3 +0,0 @@
1
- import { PlainIocError } from './PlainIocError';
2
- export declare class FactoryNotBoundError extends PlainIocError {
3
- }
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FactoryNotBoundError = void 0;
4
- const PlainIocError_1 = require("./PlainIocError");
5
- class FactoryNotBoundError extends PlainIocError_1.PlainIocError {
6
- }
7
- exports.FactoryNotBoundError = FactoryNotBoundError;