operator: matchEvent

Effect operator for matching incoming events.

Importing

import { matchEvent } from '@marblejs/core';

Type declaration

matchEvent :: (EventLike | EventCreator) -> Observable<Event> -> Observable<Event>

Example

WsEffect:

import { matchEvent } from '@marblejs/core';
import { WsEffect } from '@marblejs/websockets';
import { map } from 'rxjs/operators';

const add$: WsEffect = event$ =>
  event$.pipe(
    matchEvent('ADD'),
    map(event => event.payload), // (typeof payload) = unknown
    // ...
  );

HttpServerEffect:

import { matchEvent } from '@marblejs/core';
import { HttpServerEffect, ServerEvent } from '@marblejs/http';
import { map } from 'rxjs/operators';

const listening$: HttpServerEffect = event$ =>
  event$.pipe(
    matchEvent(ServerEvent.listening),
    map(event => event.payload), // (typeof payload) = { port: number; host: string; }
    // ...
  );

MsgEffect:

import { matchEvent, event, EventsUnion } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import * as t from 'io-ts';
import { map } from 'rxjs/operators';

// Commands definition

export enum OfferCommandType {
  GENERATE_OFFER_DOCUMENT = 'GENERATE_OFFER_DOCUMENT',
}

export const GenerateOfferDocumentCommand =
  event(OfferCommandType.GENERATE_OFFER_DOCUMENT)(t.type({ offerId: t.string }));

// Effect definition

const generateOfferDocument$: MsgEffect = event$ =>
  event$.pipe(
    matchEvent(GenerateOfferDocumentCommand),
    map(event => event.payload), // (typeof payload) = { offerId: string };
    // ...
  );

Last updated