Learn

Composition and diagnostics

Compose typed modules, inspect runtime/cache activity and understand close progress.

v1.3.1Intermediateark_di

These APIs are part of Ark DI 1.3. Use the version range in the package README.

Why modules are useful

An application can keep infrastructure, data and feature bindings in separate classes instead of growing one registration callback. Each class implements DiModule.configure(DiBinder). A DiConfiguration copies a typed module list and applies its entries in declaration order. Pass its configure method to DiContainer.build, buildAsync, createChild or createChildAsync.

The complete modular cart example demonstrates an application module, a cached feature module, state reuse and final shutdown.

Configuration does not introduce another registry. All modules receive the same binder in one configuration transaction. Duplicate bindings still fail; later modules do not silently replace earlier ones. Parent overrides still require overrideParent: true. If any module throws, accepted owned instances from earlier modules participate in the same registration rollback.

Modules describe creation, not ownership. Construct scope-owned resources inside configuration or factories. Do not reuse a module that captures an already owned live client across independent roots and then give both roots ownership of that same client. An immutable module list cannot make arbitrary module implementations or their captured objects immutable.

Empty configurations are valid for an intentionally empty DI scope. Nested DiConfiguration objects preserve declaration order; they do not introduce a new container or a new transaction. There is no scanning, reflection, sorting, automatic activation or change to singleton initialization timing.

Reading container activity

Use container.diagnostics at an administrative composition boundary. It returns an immutable DiContainerSnapshot that does not retain the container.

  • activeAsyncResolutionCount counts tracked asynchronous calls at this container, including nested contextual calls. It is not a factory count or a recursive total of all child scopes.
  • initializingSingletonCount counts local lazy singleton registrations whose initialization is in progress. Two callers can share one initialization.
  • pendingConfigurationCount counts accepted child configuration/rollback tracking operations that have not finished. A successful synchronous child can briefly remain counted until its tracking microtask completes.
  • ownedInstanceCount counts initialized local managed instances, including instances without a disposer. It is not a memory measurement.
  • attachedChildCount includes real cache-owned children; ownedCacheCount counts the directly owned caches, not their graph count.

Synchronous calls and arbitrary methods on resolved objects are not added to the async call counter. Diagnostics never invokes factories merely to inspect their output. Existing visibleRegistrations remains the way to inspect effective registrations, including inherited ones.

Explaining a slow close

container.diagnostics.closeProgress describes the current step:

  1. scopeCaches: close directly owned caches.
  2. configurationRollback: await child setup/rollback cleanup.
  3. childScopes: close remaining attached children.
  4. asyncResolutions: await accepted asynchronous DI calls.
  5. ownedInstances: dispose local instances in dependency order.
  6. completed: every cleanup attempt finished, even if some failed.

Before close the phase is notStarted. While disposing, currentDependency contains only its type/key descriptor, never the instance. The attempted counters increase as close collects completed operation results, successfully or otherwise. A concurrently closing cache may finish before its result is collected. failureCount counts collected cleanup failures, not error objects. totalInstances is unknown until accepted resolutions finish and the disposal plan is built. There is no percentage or time-to-completion estimate.

A failure belonging to a failed registration is reported through the rollback channel; it is not counted again as a parent-close failure. A retained snapshot never updates itself. Request a new snapshot to observe progress. Closed containers expose empty runtime counts but keep detached final close counters.

Inspecting a retained feature

cache.diagnostics reports physical occupied slots, the configured capacity and the count of distinct keys with acquisition pending. Waiters for one key do not each consume another slot. During LRU eviction, the retiring victim and its reserved replacement share one physical slot.

Each ready/retiring graph has a cache-local generation number, a state and separate lease/view-pin counts. A view pin can remain during child rollback or cleanup after access has already been revoked. Idle age is a monotonic interval, not a wall-clock timestamp. Pending creation appears in the aggregate pending count; there is no partially configured graph to expose.

Snapshots contain no definition objects, business arguments, resolvers, containers, exceptions or callbacks. Generation numbers are diagnostic labels, not acquisition keys. No live event stream, log history or global observer is installed. Metadata can still contain application-chosen type/key names, so applications decide what to send to external telemetry.

Test tooling boundary

The separate ark_di_test package owns test fixtures and metadata matchers. It is not a core dependency. Its test roots/children use these same composition, rollback and close rules. A child fake does not rewire a parent-owned factory; register the tested service locally when it must consume local overrides.