Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { OperatorFunction, pipe } from "rxjs";
- import { filter } from "rxjs/operators";
- interface Action {
- type: string;
- }
- export type GenerateIdFn = () => string | number;
- export type ActionWithCorrelationId<T extends Action = Action> = T & {
- correlationId: string | number;
- };
- let id = 0;
- function defaultGenerateId() {
- return id++;
- }
- export function withCorrelationId<T extends Action>(
- action: T,
- generateId: GenerateIdFn = defaultGenerateId
- ): ActionWithCorrelationId<T> {
- return Object.assign({}, action, { correlationId: generateId() });
- }
- export function filterActionsWithCorrelationId(
- action: ActionWithCorrelationId,
- allowedTypes: string[]
- ): OperatorFunction<Action, ActionWithCorrelationId> {
- return pipe(
- filter(
- (action: Action): action is ActionWithCorrelationId =>
- "correlationId" in action
- ),
- filter((action: ActionWithCorrelationId) =>
- allowedTypes.includes(action.type)
- )
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement