Skip to content

Commit

Permalink
- finished caching
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskovar-m2ms committed Dec 8, 2020
1 parent 1e45618 commit 56bdf61
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 68 deletions.
39 changes: 7 additions & 32 deletions js/components/datasets/datasetMoleculeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { isString } from 'lodash';
import { SvgTooltip } from '../common';
import { OBJECT_TYPE } from '../nglView/constants';
import { getRepresentationsByType } from '../nglView/generatingObjects';
import { getMolImage } from '../preview/molecule/redux/dispatchActions';
import { MOL_TYPE } from '../preview/molecule/redux/constants';

const useStyles = makeStyles(theme => ({
container: {
Expand Down Expand Up @@ -312,35 +314,9 @@ export const DatasetMoleculeView = memo(

// componentDidMount
useEffect(() => {
if (/*refOnCancelImage.current === undefined && */ data && data.smiles) {
let onCancel = () => {};
let url = new URL(`${base_url}/viewer/img_from_smiles/`);
const params = {
width: imageHeight,
height: imageWidth,
smiles: data.smiles
};
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));

api({
url,
cancel: onCancel
})
.then(response => {
if (response.data !== undefined) {
setImage(response.data);
}
})
.catch(error => {
throw new Error(error);
});
refOnCancelImage.current = onCancel;
}
return () => {
if (refOnCancelImage) {
refOnCancelImage.current();
}
};
dispatch(getMolImage(data.smiles, MOL_TYPE.DATASET, imageHeight, imageWidth)).then(i => {
setImage(i);
});
}, [
C,
currentID,
Expand All @@ -350,7 +326,8 @@ export const DatasetMoleculeView = memo(
imageWidth,
data.smiles,
data.id,
filteredDatasetMoleculeList
filteredDatasetMoleculeList,
dispatch
]);

const svg_image = (
Expand Down Expand Up @@ -552,7 +529,6 @@ export const DatasetMoleculeView = memo(
dispatch(moveSelectedMoleculeInspirationsSettings(data, nextItem));
const inspirations = getInspirationsForMol(datasetID, nextItem.id);
dispatch(setInspirationMoleculeDataList(inspirations));
// dispatch(setInspirationFragmentList(nextItem.computed_inspirations));
dispatch(setCrossReferenceCompoundName(moleculeTitleNext));
if (setRef && ref.current) {
setRef(refNext);
Expand All @@ -576,7 +552,6 @@ export const DatasetMoleculeView = memo(
dispatch(moveSelectedMoleculeInspirationsSettings(data, previousItem));
const inspirations = getInspirationsForMol(datasetID, previousItem.id);
dispatch(setInspirationMoleculeDataList(inspirations));
// dispatch(setInspirationFragmentList(previousItem.computed_inspirations));
dispatch(setCrossReferenceCompoundName(moleculeTitlePrev));
if (setRef && ref.current) {
setRef(refPrevious);
Expand Down
39 changes: 7 additions & 32 deletions js/components/preview/molecule/moleculeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ import {
removeDensity,
addLigand,
removeLigand,
searchMoleculeGroupByMoleculeID
searchMoleculeGroupByMoleculeID,
getMolImage
} from './redux/dispatchActions';
import { base_url } from '../../routes/constants';
import { moleculeProperty } from './helperConstants';
import { centerOnLigandByMoleculeID } from '../../../reducers/ngl/dispatchActions';
import { SvgTooltip } from '../../common';
import { OBJECT_TYPE } from '../../nglView/constants';
import { getRepresentationsByType } from '../../nglView/generatingObjects';
import {MOL_TYPE} from './redux/constants';

const useStyles = makeStyles(theme => ({
container: {
Expand Down Expand Up @@ -280,37 +282,10 @@ const MoleculeView = memo(

// componentDidMount
useEffect(() => {
if (refOnCancel.current === undefined) {
let onCancel = () => {};
Promise.all([
loadFromServer({
width: imageHeight,
height: imageWidth,
key,
old_url: oldUrl.current,
setImg_data,
setOld_url: newUrl => setOldUrl(newUrl),
url,
cancel: onCancel
})
/* api({ url: `${base_url}/api/vector/${data.id}` }).then(response => {
const vectors = response.data.vectors['3d'];
setCountOfVectors(generateObjectList(vectors).length);
}),
api({ url: `${base_url}/api/graph/${data.id}` }).then(response => {
setCmpds(getTotalCountOfCompounds(response.data.graph));
})*/
]).catch(error => {
throw new Error(error);
});
refOnCancel.current = onCancel;
}
return () => {
if (refOnCancel) {
refOnCancel.current();
}
};
}, [data.id, data.smiles, imageHeight, url, imageWidth]);
dispatch(getMolImage(data.id, MOL_TYPE.HIT, imageHeight, imageWidth)).then(i => {
setImg_data(i);
});
}, [data.id, data.smiles, imageHeight, url, imageWidth, dispatch]);

useEffect(() => {
if (searchMoleculeGroup) {
Expand Down
10 changes: 6 additions & 4 deletions js/components/preview/molecule/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,18 @@ export const applyDirectSelection = (stage, stageSummaryView) => (dispatch, getS
export const getMolImage = (molId, molType, width, height) => (dispatch, getState) => {
const state = getState();

const imageCache = state.molecule.imageCache;
const imageCache = state.previewReducers.molecule.imageCache;

const molIdStr = molId.toString();
if (imageCache.hasOwnProperty(molIdStr)) {
return new Promise((resolve, reject) => {
resolve(imageCache[molIdStr]);
});
} else {
loadMolImage(molId, molType, width, height).then(i => {
dispatch(addImageToCache(molId.toString(), i));
return loadMolImage(molId, molType, width, height).then(i => {
if (!imageCache.hasOwnProperty(molIdStr)) {
dispatch(addImageToCache(molId.toString(), i));
};
return i;
});
}
Expand All @@ -488,7 +490,7 @@ export const loadMolImage = (molId, molType, width, height) => {
}

let onCancel = () => {};
api({
return api({
url,
onCancel
}).then(response => {
Expand Down

0 comments on commit 56bdf61

Please sign in to comment.