fivem-ts

a library to speed up the development of Fivem scripts and frameworks


Project maintained by Purpose-Dev Hosted on GitHub Pages — Theme by mattgraham

fivem-ts - Documentation v0.7.5Docs


fivem-ts - Documentation v0.7.5 / Server / Scoped

Function: 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

Parameters

scope: Scope

The scope to use for the class. Determines how instances of the class are managed and shared:

Returns

Function

A decorator function that registers the class with the specified scope in the DI container.

Type Parameters

T extends (…args) => object

Parameters

constructor: T

Returns

void

Defined in

server/ioc/Scoped.ts:62