All files / src/internal/holder instance-holder.mts

100% Statements 6/6
100% Branches 2/2
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108          20x   20x   20x   20x   20x                                                                                                                                                                                            
import type { InjectableScope, InjectableType } from '../../enums/index.mjs'
 
/**
 * Represents the lifecycle status of an instance holder.
 */
export enum InstanceStatus {
  /** Instance has been successfully created and is ready for use */
  Created = 'created',
  /** Instance is currently being created (async initialization in progress) */
  Creating = 'creating',
  /** Instance is being destroyed (cleanup in progress) */
  Destroying = 'destroying',
  /** Instance creation failed with an error */
  Error = 'error',
}
 
/** Callback function for instance destruction listeners */
export type InstanceDestroyListener = () => void | Promise<void>
 
/**
 * Instance holder in the Creating state.
 * The instance is null while creation is in progress.
 */
export interface InstanceHolderCreating<Instance> {
  status: InstanceStatus.Creating
  name: string
  instance: null
  creationPromise: Promise<[undefined, Instance]> | null
  destroyPromise: null
  type: InjectableType
  scope: InjectableScope
  deps: Set<string>
  destroyListeners: InstanceDestroyListener[]
  createdAt: number
  /** Tracks which services this holder is currently waiting for (for circular dependency detection) */
  waitingFor: Set<string>
}
 
/**
 * Instance holder in the Created state.
 * The instance is available and ready for use.
 */
export interface InstanceHolderCreated<Instance> {
  status: InstanceStatus.Created
  name: string
  instance: Instance
  creationPromise: null
  destroyPromise: null
  type: InjectableType
  scope: InjectableScope
  deps: Set<string>
  destroyListeners: InstanceDestroyListener[]
  createdAt: number
  /** Tracks which services this holder is currently waiting for (for circular dependency detection) */
  waitingFor: Set<string>
}
 
/**
 * Instance holder in the Destroying state.
 * The instance may still be available but is being cleaned up.
 */
export interface InstanceHolderDestroying<Instance> {
  status: InstanceStatus.Destroying
  name: string
  instance: Instance | null
  creationPromise: null
  destroyPromise: Promise<void>
  type: InjectableType
  scope: InjectableScope
  deps: Set<string>
  destroyListeners: InstanceDestroyListener[]
  createdAt: number
  /** Tracks which services this holder is currently waiting for (for circular dependency detection) */
  waitingFor: Set<string>
}
 
/**
 * Instance holder in the Error state.
 * The instance field contains the error that occurred during creation.
 */
export interface InstanceHolderError {
  status: InstanceStatus.Error
  name: string
  instance: Error
  creationPromise: null
  destroyPromise: null
  type: InjectableType
  scope: InjectableScope
  deps: Set<string>
  destroyListeners: InstanceDestroyListener[]
  createdAt: number
  /** Tracks which services this holder is currently waiting for (for circular dependency detection) */
  waitingFor: Set<string>
}
 
/**
 * Holds the state of a service instance throughout its lifecycle.
 *
 * Tracks creation/destruction promises, dependency relationships,
 * destroy listeners, and current status (Creating, Created, Destroying, Error).
 */
export type InstanceHolder<Instance = unknown> =
  | InstanceHolderCreating<Instance>
  | InstanceHolderCreated<Instance>
  | InstanceHolderDestroying<Instance>
  | InstanceHolderError