Created
June 30, 2021 17:11
-
-
Save pchab/9eae225a3ea8d965949544f9e1e7002e to your computer and use it in GitHub Desktop.
validation code saga with user grouping and throttling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const VALIDATION_CODE_THROTTLE_TIME_IN_MS = 5000; | |
const getUserId = ({ payload: { user } }: ValidationCodeGeneratedEvent): User["id"] => user.id; | |
@Injectable() | |
export class ValidationCodeSagas { | |
@Saga() | |
ValidationCodeRequested = (event$: Observable<IEvent>): Observable<SendValidationCodeCommand> => { | |
return event$.pipe( | |
ofType(ValidationCodeGeneratedEvent), | |
groupBy(getUserId), | |
mergeMap((group$) => group$.pipe(throttleTime(VALIDATION_CODE_THROTTLE_TIME_IN_MS))), | |
map(({ payload }) => new SendValidationCodeCommand(payload)), | |
); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment