trilium/src/services/import_context.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-02-21 06:07:57 +08:00
"use strict";
const messagingService = require('./messaging');
// importId => ImportContext
const importContexts = {};
2019-02-21 06:07:57 +08:00
class ImportContext {
2019-02-24 19:24:28 +08:00
constructor(importId, options) {
2019-02-21 06:07:57 +08:00
// importId is to distinguish between different import events - it is possible (though not recommended)
// to have multiple imports going at the same time
this.importId = importId;
2019-02-24 19:24:28 +08:00
this.safeImport = options.safeImport;
this.shrinkImages = options.shrinkImages;
this.codeImportedAsCode = options.codeImportedAsCode;
this.textImportedAsText = options.textImportedAsText;
2019-02-21 06:07:57 +08:00
// // count is mean to represent count of exported notes where practical, otherwise it's just some measure of progress
this.progressCount = 0;
this.lastSentCountTs = Date.now();
}
/** @return {ImportContext} */
2019-02-24 19:24:28 +08:00
static getInstance(importId, options) {
if (!importContexts[importId]) {
2019-02-24 19:24:28 +08:00
importContexts[importId] = new ImportContext(importId, options);
}
return importContexts[importId];
}
2019-02-21 06:07:57 +08:00
async increaseProgressCount() {
this.progressCount++;
if (Date.now() - this.lastSentCountTs >= 500) {
this.lastSentCountTs = Date.now();
await messagingService.sendMessageToAllClients({
importId: this.importId,
type: 'import-progress-count',
progressCount: this.progressCount
});
}
}
// must remaing non-static
async reportError(message) {
await messagingService.sendMessageToAllClients({
type: 'import-error',
message: message
});
}
}
module.exports = ImportContext;