Skip to content

Instantly share code, notes, and snippets.

@JozefFlakus
Last active February 11, 2021 19:01
Show Gist options
  • Save JozefFlakus/6e379a33ff19a40257529dd682e64149 to your computer and use it in GitHub Desktop.
Save JozefFlakus/6e379a33ff19a40257529dd682e64149 to your computer and use it in GitHub Desktop.
Marble.js 3.0 - microservice example
// 📄 publisher - fib.effect.ts
const fib$ = r.pipe(
r.matchPath('/fib/:number'),
r.matchType('GET'),
r.useEffect((req$, ctx) => {
const client = useContext(ClientToken)(ctx.ask);
return req$.pipe(
validateRequest,
map(req => req.params.number),
mergeMap(number => forkJoin(
client.send({ type: 'FIB', payload: number + 0 }),
client.send({ type: 'FIB', payload: number + 1 }),
client.send({ type: 'FIB', payload: number + 2 }),
)),
map(body => ({ body })),
);
}),
);
// 📄 consumer - fib.effect.ts
const microservice = createMicroservice({
transport: Transport.AMQP,
options: {
host: 'amqp://localhost:5672',
queue: 'fib_queue',
},
listener: messagingListener({
effects: [fib$],
}),
});
const fib$: MsgEffect = event$ =>
event$.pipe(
matchEvent('FIB'),
act(eventValidator$(t.number)),
act(event => of(event).pipe(
map(event => fib(event.payload)),
map(payload => reply(event)({ payload })),
)),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment