navi-di 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.
@@ -2,13 +2,19 @@ import type { ContainerIdentifier, Metadata, ServiceIdentifier } from '../types'
2
2
  export declare class Container {
3
3
  readonly id: ContainerIdentifier;
4
4
  private metadataMap;
5
+ private bindingMap;
5
6
  private resolving;
6
7
  private resolvingPath;
7
8
  constructor(id: ContainerIdentifier);
8
9
  static of(id?: ContainerIdentifier): Container;
9
- set<T>(metadata: Metadata<T>): void;
10
+ register<T>(metadata: Metadata<T>): this;
10
11
  has(id: ServiceIdentifier): boolean;
11
- reset(strategy?: 'value' | 'service'): void;
12
+ set<T>(id: ServiceIdentifier<T>, value: T): this;
13
+ remove(id: ServiceIdentifier): this;
14
+ reset(strategy?: 'value' | 'service'): this;
12
15
  get<T>(id: ServiceIdentifier<T>): T;
13
16
  private isDefault;
17
+ static reset(containerId: ContainerIdentifier, options?: {
18
+ strategy?: 'value' | 'service';
19
+ }): void;
14
20
  }
@@ -4,6 +4,7 @@ import { ContainerRegistry } from './registry';
4
4
  export class Container {
5
5
  id;
6
6
  metadataMap = new Map();
7
+ bindingMap = new Map();
7
8
  resolving = new Set();
8
9
  resolvingPath = [];
9
10
  constructor(id) {
@@ -20,11 +21,11 @@ export class Container {
20
21
  ContainerRegistry.registerContainer(container);
21
22
  return container;
22
23
  }
23
- set(metadata) {
24
+ register(metadata) {
24
25
  if (metadata.scope === 'singleton' && !this.isDefault()) {
25
- ContainerRegistry.defaultContainer.set(metadata);
26
+ ContainerRegistry.defaultContainer.register(metadata);
26
27
  this.metadataMap.delete(metadata.id);
27
- return;
28
+ return this;
28
29
  }
29
30
  const newMetadata = {
30
31
  ...metadata,
@@ -36,21 +37,37 @@ export class Container {
36
37
  else {
37
38
  this.metadataMap.set(newMetadata.id, newMetadata);
38
39
  }
40
+ return this;
39
41
  }
40
42
  has(id) {
41
43
  return this.metadataMap.has(id);
42
44
  }
45
+ set(id, value) {
46
+ this.bindingMap.set(id, value);
47
+ return this;
48
+ }
49
+ remove(id) {
50
+ this.bindingMap.delete(id);
51
+ this.metadataMap.delete(id);
52
+ return this;
53
+ }
43
54
  reset(strategy = 'value') {
44
55
  if (strategy === 'value') {
45
56
  this.metadataMap.forEach((metadata) => {
46
57
  metadata.value = EMPTY_VALUE;
47
58
  });
59
+ return this;
48
60
  }
49
61
  else {
62
+ this.bindingMap.clear();
50
63
  this.metadataMap.clear();
64
+ return this;
51
65
  }
52
66
  }
53
67
  get(id) {
68
+ if (this.bindingMap.has(id)) {
69
+ return this.bindingMap.get(id);
70
+ }
54
71
  let metadata = this.metadataMap.get(id);
55
72
  if (!metadata && !this.isDefault()) {
56
73
  const defaultMetadata = ContainerRegistry.defaultContainer.metadataMap.get(id);
@@ -105,4 +122,8 @@ export class Container {
105
122
  isDefault() {
106
123
  return this === ContainerRegistry.defaultContainer;
107
124
  }
125
+ static reset(containerId, options) {
126
+ const container = ContainerRegistry.getContainer(containerId);
127
+ container?.reset(options?.strategy);
128
+ }
108
129
  }
@@ -13,7 +13,7 @@ export function Service(idOrOptions) {
13
13
  return function (target, context) {
14
14
  const options = normalizeArguments(idOrOptions);
15
15
  const injections = (context.metadata[INJECTION_KEY] ?? []);
16
- ContainerRegistry.defaultContainer.set({
16
+ ContainerRegistry.defaultContainer.register({
17
17
  id: options?.id ?? target,
18
18
  Class: target,
19
19
  name: context.name,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "navi-di",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Dependency injection for standard ECMAScript decorators.",
5
5
  "author": "naviary-sanctuary",
6
6
  "keywords": [