electron-main: Rework the event index initialization and deletion.

This commit is contained in:
Damir Jelić 2019-11-14 16:14:48 +01:00
parent 7147af8f80
commit dd2c210cfb
3 changed files with 24 additions and 7 deletions

View file

@ -15,7 +15,6 @@
"electron-window-state": "^4.1.0",
"minimist": "^1.2.0",
"png-to-ico": "^1.0.2",
"make-dir": "^3.0.0",
"matrix-seshat": "^0.3.0"
}
}

View file

@ -40,12 +40,13 @@ const { migrateFromOldOrigin } = require('./originMigrator');
const windowStateKeeper = require('electron-window-state');
const Store = require('electron-store');
const fs = require('fs');
const afs = fs.promises;
let Seshat = null;
let makeDir = null;
try {
Seshat = require('matrix-seshat');
makeDir = require('make-dir');
} catch (e) {
}
@ -255,9 +256,8 @@ ipcMain.on('seshat', async function(ev, payload) {
if (eventIndex === null) {
let p = path.normalize(eventStorePath);
try {
await makeDir(p);
await afs.mkdir(p, {recursive: true});
eventIndex = new Seshat(p, {passphrase: "DEFAULT_PASSPHRASE"});
console.log("Initialized event store");
} catch (e) {
sendError(payload.id, e);
return;
@ -265,11 +265,25 @@ ipcMain.on('seshat', async function(ev, payload) {
}
break;
case 'deleteEventIndex':
await eventIndex.delete();
case 'closeEventIndex':
eventIndex = null;
break;
case 'deleteEventIndex':
const deleteFolderRecursive = async(p) => {
for (let entry of await afs.readdir(p)) {
const curPath = path.join(p, entry);
await afs.unlink(curPath);
}
}
try {
await deleteFolderRecursive(path.normalize(eventStorePath));
} catch (e) {
}
break;
case 'isEventIndexEmpty':
if (eventIndex === null) ret = true;
else ret = await eventIndex.isEmpty();

View file

@ -149,6 +149,10 @@ class SeshatIndexerManager extends BaseEventIndexManager {
return this._ipcCall('loadCheckpoints');
}
async closeEventIndex(): Promise<> {
return this._ipcCall('closeEventIndex');
}
async deleteEventIndex(): Promise<> {
return this._ipcCall('deleteEventIndex');
}