Skip to content

Commit

Permalink
Delete legacy data from pre-rewrite versions of the app to save disk …
Browse files Browse the repository at this point in the history
…space
  • Loading branch information
GarboMuffin committed Nov 25, 2023
1 parent 014a32f commit 5feb544
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src-main/l10n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@
"string": "Finalizing...",
"developer_comment": "Status message that appears while finishing an update."
},
"migrate.delete-legacy": {
"string": "Deleting old data...",
"developer_comment": "Status message that appears while finishing an update. Old data refers to data stored by old versions of the app that is now redundant after updates."
},
"update.window-title": {
"string": "Update Available",
"developer_comment": "Title of update available window"
Expand Down
7 changes: 5 additions & 2 deletions src-main/windows/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ const {APP_NAME} = require('../brand');
const EMAIL = '[email protected]';

class MigrateWindow extends BaseWindow {
static LATEST_VERSION = 2;
static LATEST_VERSION = 3;

constructor () {
super();

const oldDataVersion = settings.dataVersion;

this.promise = new Promise((resolve) => {
this.resolveCallback = resolve;
});

const ipc = this.window.webContents.ipc;

ipc.on('get-strings', (event) => {
ipc.on('get-info', (event) => {
event.returnValue = {
oldDataVersion,
locale: getLocale(),
strings: getStrings()
};
Expand Down
2 changes: 1 addition & 1 deletion src-preload/migrate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {contextBridge, ipcRenderer} = require('electron');

contextBridge.exposeInMainWorld('MigratePreload', {
getStrings: () => ipcRenderer.sendSync('get-strings'),
getInfo: () => ipcRenderer.sendSync('get-info'),
done: () => ipcRenderer.invoke('done'),
continueAnyways: () => ipcRenderer.invoke('continue-anyways')
});
38 changes: 34 additions & 4 deletions src-renderer/migrate/migrate.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ <h1 class="title"></h1>
TW_AutoSave -> abandon (likely to be very large)
*/

const {strings, locale} = MigratePreload.getStrings();
const {oldDataVersion, strings, locale} = MigratePreload.getInfo();
document.documentElement.lang = locale;

document.querySelector('.title').textContent = strings['migrate.title'];
Expand Down Expand Up @@ -360,6 +360,31 @@ <h1 class="title"></h1>
});
};

const deleteDatabase = (databaseName) => new Promise((resolve, reject) => {
const request = indexedDB.deleteDatabase(databaseName);
request.onerror = () => {
reject(new Error(`Failed to delete IDB database ${databaseName}: ${request.error}`));
};
request.onsuccess = () => {
resolve();
};
});

const deleteLegacyData = async () => {
localStorage.clear();
sessionStorage.clear();

for (let i = 0; i < databases.length; i++) {
await deleteDatabase(databases[i].name);

// i starts from 0, so add 1 for the database we just deleted
setStepProgress(i + 1, databases.length);

// Give the browser some time to clean up; deleting large databases might be intensive
await new Promise((resolve) => setTimeout(resolve, 250))
}
};

const finish = async () => {
await MigratePreload.done();
};
Expand Down Expand Up @@ -423,13 +448,18 @@ <h1 class="title"></h1>

await runStep(strings['migrate.preparing'], gatherMetadata);

if (!anyError) {
// Preparing must succeed for both of these, but each of these can fail
// independently.
if (!anyError && oldDataVersion < 2) {
// V2: Migrate data from file:// to tw-*://
// Preparing must succeed for both of these, but each can fail independently.
await runStep(strings['migrate.transfer-editor'], migrateEditor);
await runStep(strings['migrate.transfer-packager'], migratePackager);
}

if (!anyError && oldDataVersion < 3) {
// V3: Removing leftover data on file://
await runStep(strings['migrate.delete-legacy'], deleteLegacyData);
}

if (!anyError) {
await runStep(strings['migrate.finalizing'], finish);
}
Expand Down

0 comments on commit 5feb544

Please sign in to comment.