@navios/di 0.3.0 → 0.3.1

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.
@@ -146,7 +146,7 @@ declare class DefaultRequestContextHolder implements RequestContextHolder {
146
146
  readonly metadata: Map<string, any>;
147
147
  readonly createdAt: number;
148
148
  constructor(requestId: string, priority?: number, initialMetadata?: Record<string, any>);
149
- addInstance(instanceName: string, instance: any, holder: ServiceLocatorInstanceHolder): void;
149
+ addInstance(instanceName: string | InjectionToken<any, undefined>, instance: any, holder?: ServiceLocatorInstanceHolder): void;
150
150
  getInstance(instanceName: string): any | undefined;
151
151
  getHolder(instanceName: string): ServiceLocatorInstanceHolder | undefined;
152
152
  hasInstance(instanceName: string): boolean;
@@ -580,6 +580,10 @@ declare interface RequestContextHolder {
580
580
  * Adds a pre-prepared instance to this context.
581
581
  */
582
582
  addInstance(instanceName: string, instance: any, holder: ServiceLocatorInstanceHolder): void;
583
+ /**
584
+ * Adds a pre-prepared instance to this context.
585
+ */
586
+ addInstance(token: InjectionToken<any, undefined>, instance: any): void;
583
587
  /**
584
588
  * Gets a pre-prepared instance from this context.
585
589
  */
@@ -146,7 +146,7 @@ declare class DefaultRequestContextHolder implements RequestContextHolder {
146
146
  readonly metadata: Map<string, any>;
147
147
  readonly createdAt: number;
148
148
  constructor(requestId: string, priority?: number, initialMetadata?: Record<string, any>);
149
- addInstance(instanceName: string, instance: any, holder: ServiceLocatorInstanceHolder): void;
149
+ addInstance(instanceName: string | InjectionToken<any, undefined>, instance: any, holder?: ServiceLocatorInstanceHolder): void;
150
150
  getInstance(instanceName: string): any | undefined;
151
151
  getHolder(instanceName: string): ServiceLocatorInstanceHolder | undefined;
152
152
  hasInstance(instanceName: string): boolean;
@@ -580,6 +580,10 @@ declare interface RequestContextHolder {
580
580
  * Adds a pre-prepared instance to this context.
581
581
  */
582
582
  addInstance(instanceName: string, instance: any, holder: ServiceLocatorInstanceHolder): void;
583
+ /**
584
+ * Adds a pre-prepared instance to this context.
585
+ */
586
+ addInstance(token: InjectionToken<any, undefined>, instance: any): void;
583
587
  /**
584
588
  * Gets a pre-prepared instance from this context.
585
589
  */
package/lib/index.js CHANGED
@@ -619,6 +619,15 @@ var ServiceInstantiator = class {
619
619
  }
620
620
  };
621
621
 
622
+ // src/service-locator-instance-holder.mts
623
+ var ServiceLocatorInstanceHolderStatus = /* @__PURE__ */ ((ServiceLocatorInstanceHolderStatus2) => {
624
+ ServiceLocatorInstanceHolderStatus2["Created"] = "created";
625
+ ServiceLocatorInstanceHolderStatus2["Creating"] = "creating";
626
+ ServiceLocatorInstanceHolderStatus2["Destroying"] = "destroying";
627
+ ServiceLocatorInstanceHolderStatus2["Error"] = "error";
628
+ return ServiceLocatorInstanceHolderStatus2;
629
+ })(ServiceLocatorInstanceHolderStatus || {});
630
+
622
631
  // src/request-context-holder.mts
623
632
  var DefaultRequestContextHolder = class {
624
633
  constructor(requestId, priority = 100, initialMetadata) {
@@ -635,8 +644,28 @@ var DefaultRequestContextHolder = class {
635
644
  metadata = /* @__PURE__ */ new Map();
636
645
  createdAt = Date.now();
637
646
  addInstance(instanceName, instance, holder) {
638
- this.instances.set(instanceName, instance);
639
- this.holders.set(instanceName, holder);
647
+ if (instanceName instanceof InjectionToken) {
648
+ this.instances.set(instanceName.toString(), instance);
649
+ this.holders.set(instanceName.toString(), {
650
+ instance,
651
+ status: "created" /* Created */,
652
+ creationPromise: null,
653
+ destroyPromise: null,
654
+ destroyListeners: [],
655
+ deps: /* @__PURE__ */ new Set(),
656
+ name: instanceName.toString(),
657
+ type: "Class" /* Class */,
658
+ scope: "Singleton" /* Singleton */,
659
+ createdAt: Date.now(),
660
+ ttl: Infinity
661
+ });
662
+ } else {
663
+ if (!holder) {
664
+ throw new Error("Holder is required when adding an instance by name");
665
+ }
666
+ this.instances.set(instanceName, instance);
667
+ this.holders.set(instanceName, holder);
668
+ }
640
669
  }
641
670
  getInstance(instanceName) {
642
671
  return this.instances.get(instanceName);
@@ -714,15 +743,6 @@ var ServiceLocatorEventBus = class {
714
743
  }
715
744
  };
716
745
 
717
- // src/service-locator-instance-holder.mts
718
- var ServiceLocatorInstanceHolderStatus = /* @__PURE__ */ ((ServiceLocatorInstanceHolderStatus2) => {
719
- ServiceLocatorInstanceHolderStatus2["Created"] = "created";
720
- ServiceLocatorInstanceHolderStatus2["Creating"] = "creating";
721
- ServiceLocatorInstanceHolderStatus2["Destroying"] = "destroying";
722
- ServiceLocatorInstanceHolderStatus2["Error"] = "error";
723
- return ServiceLocatorInstanceHolderStatus2;
724
- })(ServiceLocatorInstanceHolderStatus || {});
725
-
726
746
  // src/service-locator-manager.mts
727
747
  var ServiceLocatorManager = class {
728
748
  constructor(logger = null) {