Skip to content

Commit

Permalink
Don't needlessly reload resources
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Nov 28, 2017
1 parent 444c96d commit 2e8fe88
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ElectronClient/app/gui/NoteText.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ class NoteTextComponent extends React.Component {
}

async reloadNote(props) {
this.mdToHtml_ = null;

const noteId = props.noteId;
this.lastLoadedNoteId_ = noteId;
const note = noteId ? await Note.load(noteId) : null;
Expand All @@ -124,6 +122,8 @@ class NoteTextComponent extends React.Component {
if (!Object.getOwnPropertyNames(diff).length) return;
}

this.mdToHtml_ = null;

// If we are loading nothing (noteId == null), make sure to
// set webviewReady to false too because the webview component
// is going to be removed in render().
Expand Down
5 changes: 4 additions & 1 deletion ReactNativeClient/lib/MdToHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const MarkdownIt = require('markdown-it');
const Entities = require('html-entities').AllHtmlEntities;
const htmlentities = (new Entities()).encode;
const { Resource } = require('lib/models/resource.js');
const ModelCache = require('lib/ModelCache');
const { shim } = require('lib/shim.js');
const md5 = require('md5');

Expand All @@ -14,6 +15,7 @@ class MdToHtml {
this.loadedResources_ = {};
this.cachedContent_ = null;
this.cachedContentKey_ = null;
this.modelCache_ = new ModelCache();

// Must include last "/"
this.resourceBaseUrl_ = ('resourceBaseUrl' in options) ? options.resourceBaseUrl : null;
Expand Down Expand Up @@ -80,10 +82,11 @@ class MdToHtml {
this.loadedResources_[id] = {};

const resource = await Resource.load(id);
//const resource = await this.modelCache_.load(Resource, id);

if (!resource) {
// Can happen for example if an image is attached to a note, but the resource hasn't
// been download from the sync target yet.
// been downloaded from the sync target yet.
console.warn('Cannot load resource: ' + id);
return;
}
Expand Down
39 changes: 39 additions & 0 deletions ReactNativeClient/lib/ModelCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class ModelCache {

constructor(maxSize) {
this.cache_ = [];
this.maxSize_ = maxSize;
}

fromCache(ModelClass, id) {
for (let i = 0; i < this.cache_.length; i++) {
const c = this.cache_[i];
if (c.id === id && c.modelType === ModelClass.modelType()) return c
}
return null;
}

cache(ModelClass, id, model) {
if (this.fromCache(ModelClass, model.id)) return;
this.cache_.push({
id: id,
model: model,
modelType: ModelClass.modelType(),
});

if (this.cache_.length > this.maxSize_) {
this.cache_.splice(0, 1);
}
}

async load(ModelClass, id) {
const cached = this.fromCache(ModelClass, id);
if (cached) return cached.model;
const output = await ModelClass.load(id);
this.cache(ModelClass, id, output);
return output;
}

}

module.exports = ModelCache;

0 comments on commit 2e8fe88

Please sign in to comment.