Lifetimes
TinyDI supports exactly two lifetimes, via the ServiceLifetime enum. There is no third option, and no automatic detection — you state the lifetime you want.
enum ServiceLifetime {
Singleton,
Transient,
}
Singleton
The default. The first time a token is resolved, its factory runs once; every later resolve() call for the same token returns that same instance.
container.registerFactory(ClockToken, () => new SystemClock()); // Singleton by default
container.resolve(ClockToken) === container.resolve(ClockToken); // true
registerInstance is always Singleton — you are handing the container an instance that already exists, so there is nothing else it could be.
Transient
A new instance is created on every resolve() call.
container.registerFactory(RequestIdToken, () => crypto.randomUUID(), ServiceLifetime.Transient);
container.resolve(RequestIdToken) === container.resolve(RequestIdToken); // false
Choosing between them
- Default to Singleton for stateless services and shared resources (repositories, HTTP clients, loggers).
- Reach for Transient only when each resolution genuinely needs a fresh instance — a per-operation identifier, a builder object that accumulates state during one use.
No Scoped lifetime yet
This version deliberately ships only Singleton and Transient. A future Scoped lifetime (per-request instances) is a natural extension the design does not preclude, but it is not part of the current API.