Skip to content

Instantly share code, notes, and snippets.

@JozefFlakus
Last active February 11, 2021 18:59
Show Gist options
  • Save JozefFlakus/876fc23ac0b6b762a2d15d40b0d26525 to your computer and use it in GitHub Desktop.
Save JozefFlakus/876fc23ac0b6b762a2d15d40b0d26525 to your computer and use it in GitHub Desktop.
Marble.js 3.0 - CQRS example
// 📄 postOfferFile.effect.ts
const postOffersFile$ = r.pipe(
r.matchPath('/offers/:id/file'),
r.matchType('POST'),
r.useEffect((req$, ctx) => {
const eventBusClient = useContext(EventBusClientToken)(ctx.ask);
return req$.pipe(
validateRequest,
map(req => req.params.id),
tap(id => eventBusClient.emit(OfferCommand.generateOfferFile(id)),
mapTo({ status: HttpStatus.ACCEPTED }),
);
}));
// 📄 generateOfferFile.effect.ts
const generateOfferFile$: MsgEffect = (event$, ctx) =>
event$.pipe(
matchEvent(GenerateOfferFileCommand),
act(eventValidator$(GenerateOfferFileCommand)),
act(event => of(event).pipe(
map(event => event.payload.offerId),
// ...
map(id => OfferFileCreatedEvent.create(id)),
)),
);
// 📄 sendOffer.effect.ts
const sendOffer$: MsgEffect = (event$, ctx) =>
event$.pipe(
matchEvent(OfferFileCreatedEvent),
act(eventValidator$(OfferFileCreatedEvent)),
act(event => of(event).pipe(
map(event => event.payload.offerId),
// ...
map(id => OfferSentEvent.create(id)),
)),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment