From dd2c210cfb8f767f7e6fecba1707038116b0ef88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 14 Nov 2019 16:14:48 +0100 Subject: [PATCH] electron-main: Rework the event index initialization and deletion. --- electron_app/package.json | 1 - electron_app/src/electron-main.js | 26 +++++++++++++++++++------ src/vector/platform/ElectronPlatform.js | 4 ++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/electron_app/package.json b/electron_app/package.json index 63a3610fa0..cd1b8b2f35 100644 --- a/electron_app/package.json +++ b/electron_app/package.json @@ -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" } } diff --git a/electron_app/src/electron-main.js b/electron_app/src/electron-main.js index 76b8006a1a..af174fe7ab 100644 --- a/electron_app/src/electron-main.js +++ b/electron_app/src/electron-main.js @@ -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(); diff --git a/src/vector/platform/ElectronPlatform.js b/src/vector/platform/ElectronPlatform.js index 1d405c76cd..3db0e4f7ca 100644 --- a/src/vector/platform/ElectronPlatform.js +++ b/src/vector/platform/ElectronPlatform.js @@ -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'); }