1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-09 08:45:55 +02:00
joplin/packages/lib/services/ModelCache.js
2020-11-05 16:58:23 +00:00

37 lines
699 B
JavaScript

const BaseItem = require('../models/BaseItem');
class ModelCache {
constructor() {
this.cache_ = {};
}
async byIds(itemType, ids) {
const ModelClass = BaseItem.getClassByItemType(itemType);
const output = [];
const remainingIds = [];
for (let i = 0; i < ids.length; i++) {
const id = ids[i];
if (!this.cache_[id]) {
remainingIds.push(id);
} else {
output.push(this.cache_[id].model);
}
}
const models = await ModelClass.byIds(remainingIds);
for (let i = 0; i < models.length; i++) {
this.cache_[models[i].id] = {
model: models[i],
timestamp: Date.now(),
};
output.push(models[i]);
}
return output;
}
}
module.exports = ModelCache;