Skip to content

Context API Services RU

dev2alert edited this page Jan 30, 2022 · 4 revisions

Главная Context API ▸ Сервисы

Определение

Сервис - это класс, который содержит в себе логику для контекста.

Сервис аналогичен контекстам, за исключением того, что у него не выполняются события и он не может быть замороженным.

Убедитесь, что включён флаг компиляции "emitDecoratorMetadata": true, без этого флага вам придётся передавать аргументом класс в Import и ImportService!

Создание сервиса

Пример:

// mode.gsv.ts
import {GameMode} from "@sa-mp/core";
import {Service} from "@sa-mp/decorators";

@Service()
export class ModeService extends GameMode.Service {
    public divide(a: number, b: number): number {
        return a / b;
    }
}

Использование сервиса в контексте

Пример:

// mode.gctx.ts
import {GameMode} from "@sa-mp/core";
import {Context, Import, ImportService} from "@sa-mp/decorators";
import {A} from "./a.gtx";
import {B} from "./b.gtx";
import {ModeService} from "./mode.gsv";

@Context({
    children: [A, B],
    services: [ModeService]
})
export class Mode extends GameMode.Context {
    @Import()
    public readonly a: A;

    @ImportService()
    public readonly modeService: ModeService;

    public onInit(): void {
        console.log("[Mode] Init!");
        console.log(`[Mode] 2 + 3 = ${this.a.plus(2, 3)};`);
        console.log(`[Mode] 12 / 4 = ${this.modeService.divide(12, 4)};`);
    }

    public multi(a: number, b: number): number {
        return a * b;
    }
}

Импорт сервисов и контекстов

Пример:

// mode.gctx.ts
import {GameMode} from "@sa-mp/core";
import {Context, ImportService} from "@sa-mp/decorators";
import {MinusService} from "./minus.gsv";
import {PlusService} from "./plus.gsv";
import {SumService} from "./sum.gsv";

@Context({
    services: [PlusService, MinusService, SumService]
})
export class Mode extends GameMode.Context {
    @ImportService()
    public readonly sum: SumService;

    @ImportService()
    public readonly minusService: MinusService;

    public onInit(): void {
        console.log("[Mode] Init!");
        console.log(`[Mode] 2 + 3 = ${this.sum.sum(2, 3)};`);
        console.log(`[Mode] 10 - 3 = ${this.minusService.minus(10, 3)};`);
    }

    public minus(a: number, b: number): number {
        return a - b;
    }
}

// plus.gsv.ts
import {GameMode} from "@sa-mp/core";
import {Service} from "@sa-mp/decorators";

@Service()
export class PlusService extends GameMode.Service {
    public plus(a: number, b: number): number {
        return a + b;
    }
}

// minus.gsv.ts
import {GameMode} from "@sa-mp/core";
import {Import, Service} from "@sa-mp/decorators";
import {Mode} from "./mode.gctx";

@Service()
export class MinusService extends GameMode.Service {
    @Import(() => Mode)
    public readonly mode: Mode;

    public minus(a: number, b: number): number {
        return this.mode.minus(a, b);
    }
}

// sum.gsv.ts
import {GameMode} from "@sa-mp/core";
import {ImportService, Service} from "@sa-mp/decorators";
import {PlusService} from "./plus.gsv";

@Service()
export class SumService extends GameMode.Service {
    @ImportService()
    public readonly plus: PlusService;

    public sum(a: number, b: number): number {
        return this.plus.plus(a, b);
    }
}
Clone this wiki locally