Class View<S, E>

View is a datatype that represents the event handling algorithm, responsible for translating the events into denormalized state, which is more adequate for querying.

Author

Иван Дугалић / Ivan Dugalic /

Idugalic

Type Parameters

  • S

    State

  • E

    Event

    Example

    export const orderView: View<OrderView | null, OrderEvent> = new View<
    OrderView | null,
    OrderEvent
    >(
    (currentState, event) => {
    switch (event.kind) {
    case "OrderCreatedEvent":
    return {
    orderId: event.id,
    restaurantId: event.restaurantId,
    menuItems: event.menuItems,
    status: "CREATED",
    };
    case "OrderNotCreatedEvent":
    return currentState;
    case "OrderPreparedEvent":
    return currentState !== null
    ? {
    orderId: currentState.orderId,
    restaurantId: currentState.restaurantId,
    menuItems: currentState.menuItems,
    status: "PREPARED",
    }
    : currentState;
    case "OrderNotPreparedEvent":
    return currentState;
    default:
    // Exhaustive matching of the event type
    const _: never = event;
    return currentState;
    }
    },
    null,
    );

Implements

Constructors

  • Type Parameters

    • S
    • E

    Parameters

    • evolve: ((state, event) => S)
        • (state, event): S
        • Parameters

          • state: S
          • event: E

          Returns S

    • initialState: S

    Returns View<S, E>

Properties

evolve: ((state, event) => S)

Type declaration

    • (state, event): S
    • Parameters

      • state: S
      • event: E

      Returns S

initialState: S

Methods

  • Combines Views into one bigger View - Monoid

    Combines state via intersection (S & S2). Check alternative method combineViaTuples.

    1. Flexibility: If you anticipate needing to access individual components of the combined state separately, using tuples might be more appropriate, as it allows you to maintain separate types for each component. However, if you primarily need to treat the combined state as a single entity with all properties accessible at once, intersections might be more suitable.

    2. Readability: Consider which approach makes your code more readable and understandable to other developers who may be working with your codebase. Choose the approach that best communicates your intentions and the structure of your data.

    3. Compatibility: Consider the compatibility of your chosen approach with other libraries, frameworks, or tools you're using in your TypeScript project. Some libraries or tools might work better with one approach over the other.

    Type Parameters

    • S2
    • E2

    Parameters

    Returns View<S & S2, E | E2>

  • Combines Views into one bigger View - Monoid

    Combines state via tuple [S, S2]. Check alternative method combine

    1. Flexibility: If you anticipate needing to access individual components of the combined state separately, using tuples might be more appropriate, as it allows you to maintain separate types for each component. However, if you primarily need to treat the combined state as a single entity with all properties accessible at once, intersections might be more suitable.

    2. Readability: Consider which approach makes your code more readable and understandable to other developers who may be working with your codebase. Choose the approach that best communicates your intentions and the structure of your data.

    3. Compatibility: Consider the compatibility of your chosen approach with other libraries, frameworks, or tools you're using in your TypeScript project. Some libraries or tools might work better with one approach over the other.

    Type Parameters

    • S2
    • E2

    Parameters

    Returns View<readonly [S, S2], E | E2>

  • Dimap on S/State parameter - Profunctor

    Type Parameters

    • Sn

      New State

    Parameters

    • fl: ((sn) => S)
        • (sn): S
        • Parameters

          Returns S

    • fr: ((s) => Sn)
        • (s): Sn
        • Parameters

          Returns Sn

    Returns View<Sn, E>

  • Contra (Left) map on E/Event parameter - Contravariant

    Type Parameters

    • En

      New Event

    Parameters

    • f: ((en) => E)
        • (en): E
        • Parameters

          Returns E

    Returns View<S, En>

Generated using TypeDoc