a library to speed up the development of Fivem scripts and frameworks
fivem-ts - Documentation v0.7.5 • Docs
fivem-ts - Documentation v0.7.5 / Server / Scoped
Scoped(
scope
): <T
>(constructor
) =>void
Marks a class as injectable with a specific scope and registers it in the DI container.
This decorator allows you to specify the scope for a class when registering it with the DI container. Depending on the provided scope, the class will be registered either as a singleton, transient, request-scoped, or session-scoped:
This is useful for controlling the lifecycle and sharing behavior of service instances in your application.
Example usage:
@Scoped(Scope.Singleton)
class MyService {
constructor(private dependency: AnotherService) {}
}
@Scoped(Scope.Transient)
class MyOtherService {
constructor(private dependency: AnotherService) {}
}
@Scoped(Scope.Request)
class MyRequestService {
constructor(private dependency: AnotherService) {}
}
@Scoped(Scope.Session)
class MySessionService {
constructor(private dependency: AnotherService) {}
}
// Register AnotherService
container.registerSingleton(AnotherService);
// Resolve instances of MyService, MyOtherService, MyRequestService, and MySessionService
const myService = container.resolve(MyService); // Singleton instance
const myOtherService = container.resolve(MyOtherService); // New instance
const myRequestService = container.resolve(MyRequestService); // Request-scoped instance
const mySessionService = container.resolve(MySessionService); // Session-scoped instance
• scope: Scope
The scope to use for the class. Determines how instances of the class are managed and shared:
Scope.Singleton
: Register the class as a singleton.Scope.Transient
: Register the class as transient.Scope.Request
: Register the class as request-scoped.Scope.Session
: Register the class as session-scoped.Function
A decorator function that registers the class with the specified scope in the DI container.
• T extends (…args
) => object
• constructor: T
void