@adviser/cement - v0.0.0
    Preparing search index...

    Class Evento

    Event handling system with validation and encoding support. Manages regular and wildcard handlers with customizable execution order.

    Regular handlers are processed first. If any regular handler matches and processes the event, wildcard handlers are skipped. Wildcard handlers only run if no regular handlers matched.

    // Define your types
    interface MyRequest { action: string; value: number; }
    interface MyResponse { success: boolean; data?: unknown; }

    // Create an encoder/decoder
    class MyEncoder implements EventoEnDecoder<MyRequest, MyResponse> {
    async encode(args: MyRequest): Promise<Result<unknown>> {
    return Result.Ok(args);
    }
    async decode(data: unknown): Promise<Result<MyResponse>> {
    return Result.Ok(data as MyResponse);
    }
    }

    // Create a send implementation
    class MySend implements EventoSend<MyRequest, MyRequest, MyResponse> {
    async send<IS, OS>(trigger: HandleTriggerCtx<MyRequest, MyRequest, MyResponse>, data: IS): Promise<Result<OS>> {
    // Send the response data
    console.log('Sending:', data);
    return Result.Ok(data as OS);
    }
    }

    // Initialize Evento
    const evento = new Evento(new MyEncoder());
    const send = new MySend();

    // Register regular handlers (run first)
    evento.push({
    hash: 'update-handler',
    validate: async (ctx) => {
    const req = ctx.enRequest as MyRequest;
    // Only handle "update" actions
    if (req.action === 'update') {
    return Result.Ok(Option.Some(req));
    }
    return Result.Ok(Option.None());
    },
    handle: async (ctx) => {
    await ctx.send.send(ctx, {
    success: true,
    data: { updated: ctx.validated.value }
    });
    return Result.Ok(EventoResult.Continue);
    }
    });

    // Register wildcard handler (runs if no regular handlers match)
    evento.push({
    hash: 'default-handler',
    type: EventoType.WildCard,
    handle: async (ctx) => {
    await ctx.send.send(ctx, { success: false });
    return Result.Ok(EventoResult.Stop);
    }
    });

    // Trigger an event
    await evento.trigger({
    send,
    request: { action: 'update', value: 42 }
    });
    Index

    Constructors

    Properties

    actions: EventoHandler<unknown, unknown, unknown>[] = []
    wildcards: EventoHandler<unknown, unknown, unknown>[] = []
    errors: EventoHandler<unknown, unknown, unknown>[] = []
    encoder: EventoEnDecoder<unknown, unknown>

    Methods

    • Returns copies of the current handler lists.

      Returns {
          actions: EventoHandler<unknown, unknown, unknown>[];
          wildcards: EventoHandler<unknown, unknown, unknown>[];
      }

      An object containing arrays of regular actions and wildcard handlers

    • Registers handlers at the end of their respective lists.

      Parameters

      • ...hdls: (
            | EventoHandler<unknown, unknown, unknown>
            | EventoHandler<unknown, unknown, unknown>[]
        )[]

        One or more handlers or arrays of handlers

      Returns (() => void)[]

      Array of unregister functions, one for each handler

    • Registers handlers at the beginning of their respective lists.

      Parameters

      • ...hdls: (
            | EventoHandler<unknown, unknown, unknown>
            | EventoHandler<unknown, unknown, unknown>[]
        )[]

        One or more handlers or arrays of handlers

      Returns (() => void)[]

      Array of unregister functions, one for each handler

    • Registers handlers with specific operations. If a handler with the same hash already exists, returns its unregister function without adding a duplicate.

      Parameters

      • ...hdls: EventoHandlerOp[]

        Handler operations specifying where to place each handler

      Returns (() => void)[]

      Array of unregister functions, one for each handler

      Error if an unknown operation is specified

    • Triggers event processing through registered handlers.

      Process flow:

      1. Encodes the request if not already encoded
      2. Validates against each handler (regular handlers first)
      3. For matching handlers, calls handle() method
      4. If any regular handler matches, wildcard handlers are skipped
      5. Calls optional start/done lifecycle hooks on the send interface

      Type Parameters

      • INREQ

        The input request type

      • REQ

        The validated request type

      • RES

        The response type

      Parameters

      Returns Promise<Result<TriggerResult<INREQ, REQ, RES>, Error>>

      A Result containing an array of handler hashes that processed the event