Skip to content

Commit

Permalink
Remove legacy data
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Nov 22, 2023
1 parent 3c7e344 commit e5a08e5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 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 copies of data used by old versions of the app that are no longer needed."
},
"update.window-title": {
"string": "Update Available",
"developer_comment": "Title of update available window"
Expand Down
8 changes: 5 additions & 3 deletions src-main/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const path = require('path');
const settings = require('./settings');
const MigrateWindow = require('./windows/migrate');

// This needs to run before the app is ready
const userData = app.getPath('userData');
// Avoid running migrate logic on fresh installs when we can. Not required, just helps
// user experience. This must run before the ready event.
const isFirstLaunch = (
settings.dataVersion !== MigrateWindow.LATEST_VERSION &&
!fs.existsSync(path.join(userData, 'Cache'))
// Electron will always create the Cache directory in userData after the ready event,
// so if it doesn't exist yet, this is probably a fresh install.
!fs.existsSync(path.join(app.getPath('userData'), 'Cache'))
);

const migrate = async () => {
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')
});
45 changes: 41 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 @@ -354,6 +354,38 @@ <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 () => {
// Give the browser some time to clean up; deleting large databases might be intensive
const sleep = () => new Promise((resolve) => setTimeout(resolve, 250));

// 1 for local storages + 1 for each IDB
const totalOperations = 1 + databases.length;
setStepProgress(0, totalOperations);

localStorage.clear();
sessionStorage.clear();

await sleep();
setStepProgress(1, totalOperations);

for (let i = 0; i < databases.length; i++) {
await deleteDatabase(databases[i].name);
await sleep();
// 1 for local storage + 1 for the DB we just deleted (i starts from 0)
setStepProgress(2 + i, totalOperations);
}
};

const finish = async () => {
await MigratePreload.done();
};
Expand Down Expand Up @@ -417,13 +449,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 e5a08e5

Please sign in to comment.