rednode API - v0.80.1
    Preparing search index...

    Variable serviceConst

    service: {
        caller(): ServiceCaller | null;
        connect<T = Record<string, (...args: any[]) => any>>(
            id: string,
        ): Promise<ServiceHandle<T>>;
        describe(id: string): Promise<ServiceDescription>;
        emit(name: string, data?: unknown): void;
        onProviders(
            role: string,
            handler: (list: ProviderInfo[]) => void,
        ): () => void;
        providers(role: string): Promise<ProviderInfo[]>;
        signal(): AbortSignal | null;
    } = ...

    Other packages' services, and the tools to offer one.

    Type Declaration

    • caller: function
      • Inside a service function: who is calling. Null outside a service call, and in pages.

        Returns ServiceCaller | null

        export function reset() {
        if (service.caller()?.id !== "my-panel") throw new Error("Only My panel may reset");
        count = 0;
        }
    • connect: function
      • Connects to a package's service, by the package's id. Resolves once the provider runs (it waits up to 10 seconds for one that is starting); rejects with the reason when the package is not installed, offers no service, or is switched off. Add the package to your rednode-app.json's requires, such as { "edgebuilder": ">=0.4" }, so the Mod Manager shows it as missing before your package runs.

        Type Parameters

        • T = Record<string, (...args: any[]) => any>

        Parameters

        • id: string

        Returns Promise<ServiceHandle<T>>

        const builder = await service.connect("edgebuilder");
        console.log(builder.$info.functions);
        const places = await builder.$with({ timeoutMs: 60000 }).load("my_alley");
    • describe: function
      • What a package's service offers, described from its JSDoc (REDnode 0.35.0 or later): each function's description, its parameters with their types as JSON Schema, what it returns, and whether it is a @tool or @readonly. Waits for a provider that is starting.

        Parameters

        • id: string

        Returns Promise<ServiceDescription>

        const d = await service.describe("counter-service");
        for (const f of Object.values(d.functions)) console.log(f.name, f.description);
    • emit: function
      • Sends an event to every package that connected to this package's service. Node mods with a service file only. Data must be plain JSON-like values.

        Parameters

        • name: string
        • Optionaldata: unknown

        Returns void

        service.emit("scene-changed", { name: "my_alley", objects: 12 });
        
    • onProviders: function
      • Calls the handler with the whole list from service.providers whenever it changes: a provider installed or removed, switched on or off, its service starting or stopping, or its manifest changed by a hot reload. It is called once at the start with the list as it is, and then within a second of each change. Returns a function that stops it.

        Parameters

        Returns () => void

        const stop = service.onProviders("edgebuilder-tool", (list) => drawTools(list));
        
    • providers: function
      • Every installed package that provides a role, switched on or not, sorted by name, without the caller itself (#58; REDnode 0.71.0 or later). Unlike use, nothing is chosen and nothing is called: it is for a package that shows something for each provider, such as a button per tool. enabled says whether the player switched it on, running whether its service answers now, and manifest is its rednode-app.json, so a role can define its own block there.

        Parameters

        • role: string

        Returns Promise<ProviderInfo[]>

        import { service } from "rednode";

        for (const p of await service.providers("edgebuilder-tool")) {
        console.log(p.name, p.enabled ? "on" : "off", p.manifest.edgebuilderTool);
        }
    • signal: function
      • Inside a service function: a signal that aborts when the caller gives up (its own AbortSignal, or its timeout). Pass it on to long work, such as fetch.

        Returns AbortSignal | null