Skip to content

Commit

Permalink
Small further refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Feb 8, 2022
1 parent b754812 commit b15e28a
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions products/jbrowse-desktop/public/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { unlink, readFile, copyFile, readdir, writeFile } = fs.promises

const { app, ipcMain, shell, BrowserWindow, Menu } = electron

interface Session {
interface RecentSession {
path: string
updated: number
name: string
Expand All @@ -25,7 +25,7 @@ function stringify(obj: unknown) {
return JSON.stringify(obj, null, 2)
}

async function readSessions(): Promise<Session[]> {
async function readRecentSessions(): Promise<RecentSession[]> {
let data: string
try {
data = await readFile(recentSessionsPath, 'utf8')
Expand All @@ -38,6 +38,32 @@ async function readSessions(): Promise<Session[]> {
}
}

async function readSession(sessionPath: string): Promise<any> {
let data: string
try {
data = await readFile(sessionPath, 'utf8')
return JSON.parse(data)
} catch (e) {
console.error(
`Failed to parse session at "${sessionPath}", data was ${data}`,
)
return e
}
}

async function readQuickstart(quickstartPath: string): Promise<any> {
let data: string
try {
data = await readFile(quickstartPath, 'utf8')
return JSON.parse(data)
} catch (e) {
console.error(
`Failed to parse quickstart at "${quickstartPath}", data was ${data}`,
)
return e
}
}

// manual auto-updates https://github.com/electron-userland/electron-builder/blob/docs/encapsulated%20manual%20update%20via%20menu.js
autoUpdater.autoDownload = false

Expand Down Expand Up @@ -351,9 +377,7 @@ ipcMain.handle(
ipcMain.handle(
'listSessions',
async (_event: unknown, showAutosaves: boolean) => {
const sessions = JSON.parse(await readFile(recentSessionsPath, 'utf8')) as {
path: string
}[]
const sessions = await readRecentSessions()

if (!showAutosaves) {
return sessions.filter(f => !f.path.startsWith(autosaveDir))
Expand All @@ -368,7 +392,7 @@ ipcMain.handle('loadExternalConfig', (_event: unknown, sessionPath) => {
})

ipcMain.handle('loadSession', async (_event: unknown, sessionPath: string) => {
const sessionSnapshot = JSON.parse(await readFile(sessionPath, 'utf8'))
const sessionSnapshot = await readSession(sessionPath)
if (!sessionSnapshot.assemblies) {
throw new Error(
`File at ${sessionPath} does not appear to be a JBrowse session. It does not contain any assemblies.`,
Expand Down Expand Up @@ -407,7 +431,7 @@ ipcMain.handle(
)

ipcMain.handle('getQuickstart', async (_event: unknown, name: string) => {
return JSON.parse(await readFile(getQuickstartPath(name), 'utf8'))
return readQuickstart(getQuickstartPath(name))
})

ipcMain.handle(
Expand Down Expand Up @@ -443,7 +467,7 @@ ipcMain.handle(
ipcMain.handle(
'createInitialAutosaveFile',
async (_event: unknown, snap: SessionSnap) => {
const rows = await readSessions()
const rows = await readRecentSessions()
const idx = rows.findIndex(r => r.path === path)
const path = getAutosavePath(`${+Date.now()}`)
const entry = {
Expand Down Expand Up @@ -471,7 +495,7 @@ ipcMain.handle(
async (_event: unknown, path: string, snap: SessionSnap) => {
const [page, rows] = await Promise.all([
mainWindow?.capturePage(),
readSessions(),
readRecentSessions(),
])
const idx = rows.findIndex(r => r.path === path)
const png = page?.resize({ width: 500 }).toDataURL()
Expand Down Expand Up @@ -525,15 +549,12 @@ ipcMain.handle('promptSessionSaveAs', async (_event: unknown) => {
ipcMain.handle(
'deleteSessions',
async (_event: unknown, sessionPaths: string[]) => {
const sessions = await readSessions()
const indexes: number[] = []
sessions.forEach((row, idx) => {
if (sessionPaths.includes(row.path)) {
indexes.push(idx)
}
})
for (let i = indexes.length - 1; i >= 0; i--) {
sessions.splice(indexes[i], 1)
const sessions = await readRecentSessions()
const indices = sessions
.map((r, i) => (sessionPaths.includes(r.path) ? i : undefined))
.filter(f => f !== undefined)
for (let i = indices.length - 1; i >= 0; i--) {
sessions.splice(indices[i], 1)
}

await Promise.all([
Expand All @@ -551,7 +572,7 @@ ipcMain.handle(
ipcMain.handle(
'renameSession',
async (_event: unknown, path: string, newName: string) => {
const sessions = await readSessions()
const sessions = await readRecentSessions()
const session = JSON.parse(await readFile(path, 'utf8'))
const idx = sessions.findIndex(row => row.path === path)
if (idx !== -1) {
Expand Down

0 comments on commit b15e28a

Please sign in to comment.