Skip to content

Function: Singleton() ​

Singleton<ClassConstructor>(lock?): ClassDecorator<ClassConstructor>

Make the class a singleton: the container registering it creates one instance, on first request, and shares it with all its users (the scopes included) until the container is destroyed. Shorthand of @Lifetime('SINGLETON', lock).

Type Parameters ​

ClassConstructor ​

ClassConstructor extends typeof BaseObject

Parameters ​

lock? ​

boolean

Whether the subclasses are forbidden to change the lifetime (they may still declare the same one). Default false.

Returns ​

ClassDecorator<ClassConstructor>

The class decorator.

Throws ​

When the class is defined, if a parent class locked another lifetime.

Example ​

typescript
import {BaseObject} from 'lakutata'
import {Singleton} from 'lakutata/decorator/di'

//One cache shared by the whole application
@Singleton()
export class Cache extends BaseObject {
    public readonly entries: Map<string, unknown> = new Map()
}

Released under the MIT License.