Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import { OperatorFunction, pipe } from "rxjs";
  2. import { filter } from "rxjs/operators";
  3.  
  4. interface Action {
  5. type: string;
  6. }
  7.  
  8. export type GenerateIdFn = () => string | number;
  9.  
  10. export type ActionWithCorrelationId<T extends Action = Action> = T & {
  11. correlationId: string | number;
  12. };
  13.  
  14. let id = 0;
  15. function defaultGenerateId() {
  16. return id++;
  17. }
  18.  
  19. export function withCorrelationId<T extends Action>(
  20. action: T,
  21. generateId: GenerateIdFn = defaultGenerateId
  22. ): ActionWithCorrelationId<T> {
  23. return Object.assign({}, action, { correlationId: generateId() });
  24. }
  25.  
  26. export function filterActionsWithCorrelationId(
  27. action: ActionWithCorrelationId,
  28. allowedTypes: string[]
  29. ): OperatorFunction<Action, ActionWithCorrelationId> {
  30. return pipe(
  31. filter(
  32. (action: Action): action is ActionWithCorrelationId =>
  33. "correlationId" in action
  34. ),
  35. filter((action: ActionWithCorrelationId) =>
  36. allowedTypes.includes(action.type)
  37. )
  38. );
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement