MetaFor - v0.3.1
    Preparing search index...

    Type Alias ReactionUpdate<C, S, Core>

    ReactionUpdate: (
        args: {
            update: (values: Partial<ExtractValues<C>>) => void;
            context: ExtractValues<C>;
            core: Core;
            meta: MetaDataMessage;
            patch: JsonPatch;
            state: S;
        },
    ) => void

    Функция обновления контекста

    Вызывается когда реакция срабатывает и фильтр прошел успешно. Получает все необходимые данные для обработки события.

    import { ReactionRegistry, createReactionsChain } from "../index"
    import type { Update, ExtractValues } from "../../context/index.t"
    import { test, expect } from "bun:test"
    import type { JsonPatch, MetaDataMessage } from "../../message"

    type Ctx = {
    value: { type: "number"; required: true }
    name: { type: "string"; required: true }
    isActive: { type: "boolean"; required: true }
    tags: { type: "array"; required: true }
    }
    type State = "idle" | "active" | "error"

    test("Создание уникальных реакций", () => {
    const registry = new ReactionRegistry<Ctx, State>((reaction) => [
    [
    ["idle", "active"],
    reaction({ title: "inc" })
    .filter({
    tag: "test",
    op: "replace",
    path: "/context",
    value: 1,
    })
    .equal(({ update, context }) => update({ value: context.value + 1 })),
    ],
    [
    ["error"],
    reaction({ title: "reset" })
    .filter({ tag: "any" })
    .equal(({ update }) => update({ value: 0 })),
    ],
    ])

    const all = registry.getAllReactions()
    expect(all?.length, "уникальные реакции").toBe(2)
    expect(all?.[0]?.title, "первая реакция").toBe("inc")
    expect(all?.[1]?.title, "вторая реакция").toBe("reset")
    })

    Type Parameters

    • C extends ContextSchema

      схема контекста

    • S extends string

      строковые ключи состояний

    • Core = Record<string, any>

      тип core объекта

    Type declaration

    const updateFn: ReactionUpdate<MyContext, "idle" | "loading"> = ({
    update, // Функция для обновления контекста
    context, // Текущий контекст
    core, // Core объект
    meta, // Метаданные сообщения
    patch, // JSON Patch
    state // Текущее состояние
    }) => {
    // Обработка события
    update({
    lastMessage: patch.value,
    messageCount: context.messageCount + 1
    })
    }