added simple memoizing cache for email step

This commit is contained in:
Ben Wheeler 2019-09-27 12:26:25 -04:00
parent 3bc6e7ff6f
commit 0656ab6a9e

View file

@ -21,6 +21,7 @@ class EmailStep extends React.Component {
'handleSetEmailRef', 'handleSetEmailRef',
'handleValidSubmit', 'handleValidSubmit',
'validateEmail', 'validateEmail',
'validateEmailRemotelyWithCache',
'validateForm', 'validateForm',
'setCaptchaRef', 'setCaptchaRef',
'captchaSolved', 'captchaSolved',
@ -30,8 +31,10 @@ class EmailStep extends React.Component {
this.state = { this.state = {
captchaIsLoading: true captchaIsLoading: true
}; };
// simple object to memoize remote requests for email addresses.
// keeps us from submitting multiple requests for same data.
this.emailRemoteCache = {};
} }
componentDidMount () { componentDidMount () {
// automatically start with focus on username field // automatically start with focus on username field
if (this.emailInput) this.emailInput.focus(); if (this.emailInput) this.emailInput.focus();
@ -78,11 +81,24 @@ class EmailStep extends React.Component {
}, },
true); true);
} }
// simple function to memoize remote requests for usernames
validateEmailRemotelyWithCache (email) {
if (this.emailRemoteCache.hasOwnProperty(email)) {
return Promise.resolve(this.emailRemoteCache[email]);
}
// email is not in our cache
return validate.validateEmailRemotely(email).then(
remoteResult => {
this.emailRemoteCache[email] = remoteResult;
return remoteResult;
}
);
}
validateEmail (email) { validateEmail (email) {
if (!email) return this.props.intl.formatMessage({id: 'general.required'}); if (!email) return this.props.intl.formatMessage({id: 'general.required'});
const localResult = validate.validateEmailLocally(email); const localResult = validate.validateEmailLocally(email);
if (!localResult.valid) return this.props.intl.formatMessage({id: localResult.errMsgId}); if (!localResult.valid) return this.props.intl.formatMessage({id: localResult.errMsgId});
return validate.validateEmailRemotely(email).then( return this.validateEmailRemotelyWithCache(email).then(
remoteResult => { remoteResult => {
if (remoteResult.valid === true) { if (remoteResult.valid === true) {
return null; return null;