Memoized the loading of the server version, assuming it will not change at runtime

This commit is contained in:
Alejandro Celaya 2020-03-16 13:34:24 +01:00
parent 25c67f1c3e
commit 5145a41dac
2 changed files with 454 additions and 449 deletions

890
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
import { createAction, handleActions } from 'redux-actions'; import { createAction, handleActions } from 'redux-actions';
import { pipe } from 'ramda'; import { identity, memoizeWith, pipe } from 'ramda';
import { resetShortUrlParams } from '../../short-urls/reducers/shortUrlsListParams'; import { resetShortUrlParams } from '../../short-urls/reducers/shortUrlsListParams';
import { versionToPrintable, versionToSemVer as toSemVer } from '../../utils/versionHelpers'; import { versionToPrintable, versionToSemVer as toSemVer } from '../../utils/versionHelpers';
@ -36,14 +36,14 @@ export const selectServer = ({ findServerById }, buildShlinkApiClient) => (serve
try { try {
const { health } = buildShlinkApiClient(selectedServer); const { health } = buildShlinkApiClient(selectedServer);
const { version } = await health(); const { version, printableVersion } = await getServerVersion(serverId, health);
dispatch({ dispatch({
type: SELECT_SERVER, type: SELECT_SERVER,
selectedServer: { selectedServer: {
...selectedServer, ...selectedServer,
version: versionToSemVer(version), version,
printableVersion: versionToPrintable(version), printableVersion,
}, },
}); });
} catch (e) { } catch (e) {
@ -54,6 +54,11 @@ export const selectServer = ({ findServerById }, buildShlinkApiClient) => (serve
} }
}; };
const getServerVersion = memoizeWith(identity, (serverId, health) => health().then(({ version }) => ({
version: versionToSemVer(version),
printableVersion: versionToPrintable(version),
})));
export default handleActions({ export default handleActions({
[RESET_SELECTED_SERVER]: () => initialState, [RESET_SELECTED_SERVER]: () => initialState,
[SELECT_SERVER]: (state, { selectedServer }) => selectedServer, [SELECT_SERVER]: (state, { selectedServer }) => selectedServer,