Style Guide
The style guide is mostly based on author experience in using Marble.js on large production-ready projects following microservice/event-driven architecture style.
Marble.js framework doesn't define any strict file structures and conventions from the organization-level perspective that each developer should enforce. Below you can find some useful hints that you can follow when organizing your Marble.js app.
From the framework perspective, listeners and servers are separate layers that are responsible for different things. #createServer and similar factory functions are responsible for handling transport-layer-related processes, like: bootstrapping server and bounded context, listening for incoming messages or reacting for transport-layer events, where for the contrast, listeners are responsible for processing I/O messages that go through underlying transport layer in order to fulfill business needs. Basically, it is just about the single responsibility principle.
By convention Marble.js follows suffixed file naming which results in:
Server | Listener |
http.server.ts | http.listener.ts |
microservice.server.ts | microservice.listener.ts |
websocket.server.ts | websocket.listener.ts |
NONE | eventbus.listener.ts |
In Marble.js HTTP effects are tightly connected to the route on which they operate. Having them in a separation makes the code less readable and less understandable. Always try to keep them as a one unit. Every HttpEffect, that comes through
r.pipe
route builder, is accessible via .effect
property of the returned RouteEffect
object. It if you want to unit test only the effect function, that's the way of accessing it.❌ Bad
getFoo.route.ts
getFoo.effect.ts
import { r } from '@marblejs/http';
import { getFooEffect } from './getFoo.effect';
const getFoo = r.pipe(
r.matchPath('/foo'),
r.matchType('GET'),
r.useEffect(getFooEffect));
import { HttpEffect } from '@marblejs/http';
export const getFooEffect: HttpEffect = (req$, ctx) => req$.pipe(
...
);
✅ Good
getFoo.effect.ts
import { r } from '@marblejs/http';
const getFoo$ = r.pipe(
r.matchPath('/foo'),
r.matchType('GET'),
r.useEffect((req$, ctx) => req$.pipe(
...
)));
Index files are good for combining and re-exporting related files as a one module.
/user
+ ── /getUserList
│ + ── getUserList.effect.spec.ts
│ └─── getUserList.effect.ts
│
+ ── /getUser
│ + ── getUser.effect.spec.ts
│ └─── getUser.effect.ts
│
+ ── /postUser
│ + ── getUser.effect.spec.ts
│ └─── getUser.effect.ts
│
└─── index.ts
index.ts
http.listener.ts
import { combineRoutes } from '@marblejs/http';
export const user$ = combineRoutes('/user', {
middlewares: [
authorize$,
],
effects: [
getUserList$,
getUser$,
postUser$,
],
});
import { httpListener } from '@marblejs/http';
import { user$ } from './user';
export const listener = httpListener({
effects: [
user$,
],
});
Or in case of messaging/websockets effects:
index.ts
eventBus.listener.ts
import { combineEffects } from '@marblejs/core';
export const user$ = combineEffects(
userCreated$,
userRemoved$,
...
);
import { messagingListener } from '@marblejs/messaging';
import { user$ } from './user';
export const listener = messagingListener({
effects: [
user$,
],
});
- Use PascalCase naming convention with
Token
suffix for token definitions. - Always remember to define a context token name identifier. It will help you quickly recognize what dependency is missing when asking for it via
useContext
hook function. - Place token next to reader definition. It is easier to navigate to reader implementation via popular "Go to Implementation" mechanism.
❌ Bad
tokens.ts
user.repository.ts
import { createContextToken } from '@marblejs/core';
export const userRepository = createContextToken<UserRepository>();
import { createReader } from '@marblejs/core';
export const userRepository = createReader(() => ...);
✅ Good
user.repository
import { createContextToken, createReader } from '@marblejs/core';
export type UserRepository = ReturnType<typeof UserRepository>;
export const UserRepository = createReader(() => ...);
export const UserRepositoryToken = createContextToken<UserRepository>('UserRepository');
Identify which dependencies should be bound to the app context eagerly (before app startup) and which lazily.
Given:
import { createContextToken, createReader } from '@marblejs/core';
export type DatabaseConnection = Connection;
export const DatabaseConnection = createReader(async _ => ...);
export const DatabaseConnectionToken = createContextToken<DatabaseConnection>('DatabaseConnection');
Note that in case of async readers the type of created reader will be:
Reader<Context, Promise<Connection>>
❌ Bad
import { bindTo, bindLazilyTo, useContext } from '@marblejs/core';
const dependencies = [
bindTo(DatabaseConnectionToken)(DatabaseConnection),
// or
bindLazilyTo(DatabaseConnectionToken)(DatabaseConnection),
];
...
const connection = useContext(DatabaseConnectionToken)(ask);
// typeof connection === Promise<Connection>;
✅ Good
import { bindEagerlyTo, useContext } from '@marblejs/core';
const dependencies = [
bindEagerlyTo(DatabaseConnectionToken)(DatabaseConnection),
];
...
const connection = useContext(DatabaseConnectionToken)(ask);
// typeof connection === Connection;
Always try to inject bound dependencies at the top level of your effects (before returned Observable stream). All effects are evaluated eagerly, so in case of missing context dependency the framework will be able to spot issues during initial bootstrap.
❌ Bad
const postUser$ = r.pipe(
r.matchPath('/'),
r.matchType('POST'),
r.useEffect((req$, ask) => req$.pipe(
validateRequest,
mergeMap(req => {
const userRepository = useContext(UserRepositoryToken)(ask);
const { body } = req;
return userRepository
.persist(body)
.pipe(
mergeMap(userRepository.getById),
map(user => ({ body: user })),
);
}),
));
✅ Good
const postUser$ = r.pipe(
r.matchPath('/'),
r.matchType('POST'),
r.useEffect((req$, ask) => {
const userRepository = useContext(UserRepositoryToken)(ask);
return req$.pipe(
validateRequest,
map(req => req.body),
mergeMap(userRepository.persist),
mergeMap(userRepository.getById),
map(user => ({ body: user })),
);
}));
❌ Bad
user.event.ts
import { Event } from '@marblejs/core';
import * as t from 'io-ts';
export const UserCreated = (id: string): Event => ({
type: 'UserCreated',
payload: { id },
});
export const UserUpdated = (id: string): Event => ({
type: 'UserUpdated',
payload: { id },
});
export const UserCreatedCodec = t.type({
type: t.literal('UserCreated'),
payload: t.type({ id: t.string }),
});
export const UserUpdatedCodec = t.type({
type: t.literal('UserUpdated'),
payload: t.type({ id: t.string }),
});
✅ Good
import { event } from '@marblejs/core';
import * as t from 'io-ts';
export enum UserEventType {
USER_CREATED = 'USER_CREATED',
USER_UPDATED = 'USER_UPDATED',
}
export const UserCreatedEvent =
event(UserEventType.USER_CREATED)(t.type({
id: t.string,
}));
export const UserUpdatedEvent =
event(UserEventType.USER_UPDATED)(t.type({
id: t.string,
}));
- Always try match events by event I/O codec - avoid raw literals since they don't carry the actual event payload type
- Event-based communication follows the same laws as request-based communication - each incoming event should be validated before usage (eg. using previously mentioned
event
codec).
❌ Bad
userCreated.effect.ts
import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
export const userCreated$: MsgEffect = event$ =>
event$.pipe(
// event payload is unknown, no type is inferred...
matchEvent('USER_CREATED'),
act(event => ...),
);
❌ / ✅ Better
userCreated.effect.ts
import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { UserCreatedEvent } from './user.event.ts';
export const userCreated$: MsgEffect = event$ =>
event$.pipe(
// type is inferred but event still requires validation...
matchEvent(UserCreatedEvent),
act(event => ...),
);
✅ Good
userCreated.effect.ts
import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { UserCreatedEvent } from './user.event.ts';
export const userCreated$: MsgEffect = event$ =>
event$.pipe(
matchEvent(UserCreatedEvent),
act(eventValidator$(UserCreatedEvent)),
act(event => ...),
);
- Each processed event should be mapped to a different event type to avoid infinite-loops.
- In case you don't want to emit anything in the effect stream you can skip emitted values, eg. by
ignoreElements
operator.
❌ Bad
import { matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
const foo$: MsgEffect = event$ =>
event$.pipe(
matchEvent('FOO'),
tap(doSomeWork),
);
✅ Good
import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { pipe } from 'fp-ts/lib/function';
const foo$: MsgEffect = event$ =>
event$.pipe(
matchEvent('FOO'),
act(event => pipe(
doSomeWork(event),
map(payload => ({ type: 'FOO_RESULT', payload })),
)),
);
- Each messaging effect should handle errors in a disposable streams either via
mergeMap/switchMap/...
operators with combination ofcatchError
or viaact
operator.
❌ Bad
import { matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
const foo$: MsgEffect = event$ =>
event$.pipe(
matchEvent('FOO'),
mergeMap(doSomeWork),
map(payload => ({ type: 'FOO_RESULT', payload }),
);
✅ Good
import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { pipe } from 'fp-ts/lib/function';
import { catchError } from 'rxjs/operators';
const foo$: MsgEffect = event$ =>
event$.pipe(
matchEvent('FOO'),
mergeMap(event => pipe(
doSomeWork(event),
map(payload => ({ type: 'FOO_RESULT', payload })),
catchError(error => ({ type: 'FOO_ERROR', error })),
)),
);
✅ Even better
import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { pipe } from 'fp-ts/lib/function';
const foo$: MsgEffect = event$ =>
event$.pipe(
matchEvent('FOO'),
act(event => pipe(
doSomeWork(event),
map(payload => ({ type: 'FOO_RESULT', payload })),
)),
);
Last modified 2yr ago