Skip to content

Context API Contexts

dev2alert edited this page Feb 4, 2022 · 2 revisions

Home Context API ▸ Contexts

Definition

Context is a class whose instance is created when initializing something (for example: when connecting a player).

Child contexts

A context can consist of child contexts:

// index.ts
import {GameMode} from "@sa-mp/core";
import {Context} from "@sa-mp/decorators";
import {A} from "./a.gtx";
import {B} from "./b.gtx";

@Context([A, B])
export class Mode extends GameMode.Context {
    public onInit(): void {
        console.log("[Mode] Init!");
    }
}

// a.gtx.ts
import {GameMode} from "@sa-mp/core";
import {Context} from "@sa-mp/decorators";

@Context()
export class A extends GameMode.Context {
    public onInit(): void {
        console.log("[Mode][A] Init!");
    }
}

// b.gtx.ts
import {GameMode} from "@sa-mp/core";
import {Context} from "@sa-mp/decorators";

@Context()
export class B extends GameMode.Context {
    public onInit(): void {
        console.log("[Mode][B] Init!");
    }
}

Output:

[Mode] Init!
[Mode][A] Init!
[Mode][B] Init!

Importing contexts

Importing a context is getting an instance of a context.

Import between contexts:

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

@Context([A, B])
export class Mode extends GameMode.Context {
    @Import(() => A)
    public readonly a: A;

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

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

// a.gtx.ts
import {GameMode} from "@sa-mp/core";
import {Context, Import} from "@sa-mp/decorators";
import {B} from "./b.gtx";

@Context()
export class A extends GameMode.Context {
    @Import(() => B)
    public readonly b: B;

    public onInit(): void {
        console.log("[Mode][A] Init!");
        console.log(`[Mode][A] 12 - 7 = ${this.b.minus(12, 7)};`);
    }

    public plus(a: number, b: number): number {
        return a + b;
    }
}

// b.gtx.ts
import {GameMode} from "@sa-mp/core";
import {Context, Import} from "@sa-mp/decorators";
import {Mode} from "./mode.gctx";

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

    public onInit(): void {
        console.log("[Mode][B] Init!");
        console.log(`[Mode][B] 3 * 4 = ${this.mode.multi(3, 4)};`);
    }

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

Output:

[Mode] Init!
[Mode] 2 + 3 = 5;
[Mode][B] Init!
[Mode][B] 3 * 4 = 12;
[Mode][A] Init!
[Mode][A] 12 - 7 = 5;

Import is possible without passing arguments () => A:

Make sure that the compilation flag "emitDecoratorMetadata": true is enabled!

import {GameMode} from "@sa-mp/core";
import {Context, Import} from "@sa-mp/decorators";
import {A} from "./a.gtx";
import {B} from "./b.gtx";

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

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

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

Context event handlers

Example:

import {GameMode} from "@sa-mp/core";
import {Context} from "@sa-mp/decorators";

@Context()
export class Mode extends GameMode.Context {
    public handleContextCreate(): void {
        super.handleContextCreate();
        console.log("handleContextCreate");
    }

    public handleContextDestroy(): void {
        super.handleContextDestroy();
        console.log("handleContextDestroy");
    }
}

Where handleContextCreate is a context creation handler, handleContextDestroy is a context destruction handler.

Frozen contexts

Frozen context is a context that does not trigger events.

The freeze extends to child contexts.

There are 2 ways to freeze the context:

1 way

Use the provided functionality:

import {GameMode} from "@sa-mp/core";
import {Context, Import} from "@sa-mp/decorators";
import {A} from "./a.gtx";
import {B} from "./b.gtx";

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

    public handleContextCreate(): void {
        super.handleContextCreate();
        this.freeze();
    }

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

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

Where freeze is the method that freezes the context.

To unfreeze the context, use the unfreeze method.

The output will be empty.

2 way

Overriding the method that checks whether the context is frozen:

import {GameMode} from "@sa-mp/core";
import {Context, Import} from "@sa-mp/decorators";
import {A} from "./a.gtx";
import {B} from "./b.gtx";

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

    public isFrozen(): boolean {
        return true;
    }

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

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

Where isFrozen is a method that checks whether the context is frozen.

The output will be empty.

To check if the context and its parents are frozen, use the isFrozenStrict method.

Context event loop

There are 6 event loop methods available in the context setTimeout, clearTimeout, setInterval, clearInterval, setImmediate and clearImmediate.

Differences from the usual event loop:

  • Not executed when the context is frozen.
  • Cleared when the context is destroyed.

This is an additional functionality for contexts!

Example:

import {GameMode, Rcon} from "@sa-mp/core";
import {Context} from "@sa-mp/decorators";

@Context()
export class Mode extends GameMode.Context {
    public handleContextCreate(): void {
        super.handleContextCreate();
        console.log("[Mode] handleContextCreate");
    }

    public handleContextDestroy(): void {
        console.log("[Mode] handleContextDestroy");
        super.handleContextDestroy();
    }

    public onInit(): void {
        console.log("[Mode] Init!");
        this.setInterval(() => console.log("[Mode] 1s"), 1000);
        this.setTimeout(() => Rcon.send("exit"), 3000);
    }

    public onExit(): void {
        console.log("[Mode] Exit!");
    }
}
Clone this wiki locally