Skip to content

Commit

Permalink
1.rename local-storage to electron-storage
Browse files Browse the repository at this point in the history
  • Loading branch information
cwangsmv committed Jan 22, 2025
1 parent 95f524b commit a391899
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';

class LocalStorage {
class ElectronStorage {
_buffer: Record<string, string> = {};
_timeouts: Record<string, NodeJS.Timeout> = {};
_basePath: string | null = null;
Expand All @@ -11,7 +11,7 @@ class LocalStorage {
// Debounce writes on a per key basis
fs.mkdirSync(basePath, { recursive: true });

console.log(`[localstorage] Initialized at ${basePath}`);
console.log(`[ElectronStorage] Initialized at ${basePath}`);
}

setItem<T>(key: string, obj?: T) {
Expand Down Expand Up @@ -39,7 +39,7 @@ class LocalStorage {
try {
return JSON.parse(contents);
} catch (error) {
console.error(`[localstorage] Failed to parse item from LocalStorage: ${error}`);
console.error(`[ElectronStorage] Failed to parse item from electron storage: ${error}`);
return defaultObj;
}
}
Expand All @@ -61,7 +61,7 @@ class LocalStorage {
try {
fs.writeFileSync(path, contents);
} catch (error) {
console.error(`[localstorage] Failed to save to LocalStorage: ${error}`);
console.error(`[ElectronStorage] Failed to save to electron storage: ${error}`);
}
}
}
Expand All @@ -72,4 +72,4 @@ class LocalStorage {
}
}

export default LocalStorage;
export default ElectronStorage;
30 changes: 15 additions & 15 deletions packages/insomnia/src/main/window-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ import { docsBase } from '../common/documentation';
import * as log from '../common/log';
import { APP_START_TIME, SentryMetrics } from '../common/sentry';
import { invariant } from '../utils/invariant';
import ElectronStorage from './electron-storage';
import { ipcMainOn } from './ipc/electron';
import LocalStorage from './local-storage';

const DEFAULT_WIDTH = 1280;
const DEFAULT_HEIGHT = 720;
const MINIMUM_WIDTH = 500;
const MINIMUM_HEIGHT = 400;

const browserWindows = new Map<'Insomnia' | 'HiddenBrowserWindow', ElectronBrowserWindow>();
let localStorage: LocalStorage | null = null;
let electronStorage: ElectronStorage | null = null;
let hiddenWindowIsBusy = false;

interface Bounds {
Expand All @@ -50,7 +50,7 @@ interface Bounds {
}

export function init() {
initLocalStorage();
initElectronStorage();
}
const stopAndWaitForHiddenBrowserWindow = async (runningHiddenBrowserWindow: BrowserWindow) => {
return await new Promise<void>(resolve => {
Expand Down Expand Up @@ -741,11 +741,11 @@ function saveBounds() {

// Only save the size if we're not in fullscreen
if (!fullscreen) {
localStorage?.setItem('bounds', browserWindow?.getBounds());
localStorage?.setItem('maximize', browserWindow?.isMaximized());
localStorage?.setItem('fullscreen', false);
electronStorage?.setItem('bounds', browserWindow?.getBounds());
electronStorage?.setItem('maximize', browserWindow?.isMaximized());
electronStorage?.setItem('fullscreen', false);
} else {
localStorage?.setItem('fullscreen', true);
electronStorage?.setItem('fullscreen', true);
}
}

Expand All @@ -755,9 +755,9 @@ function getBounds() {
let maximize = false;

try {
bounds = localStorage?.getItem('bounds', {});
fullscreen = localStorage?.getItem('fullscreen', false);
maximize = localStorage?.getItem('maximize', false);
bounds = electronStorage?.getItem('bounds', {});
fullscreen = electronStorage?.getItem('fullscreen', false);
maximize = electronStorage?.getItem('maximize', false);
} catch (error) {
// This should never happen, but if it does...!
console.error('Failed to parse window bounds', error);
Expand All @@ -776,7 +776,7 @@ const ZOOM_MIN = 0.05;

const getZoomFactor = () => {
try {
return localStorage?.getItem('zoomFactor', ZOOM_DEFAULT);
return electronStorage?.getItem('zoomFactor', ZOOM_DEFAULT);
} catch (error) {
// This should never happen, but if it does...!
console.error('Failed to parse zoomFactor', error);
Expand All @@ -797,12 +797,12 @@ export const setZoom = (transformer: (current: number) => number) => () => {
const actual = Math.min(Math.max(ZOOM_MIN, desired), ZOOM_MAX);

browserWindow.webContents.setZoomLevel(actual);
localStorage?.setItem('zoomFactor', actual);
electronStorage?.setItem('zoomFactor', actual);
};

function initLocalStorage() {
const localStoragePath = path.join(process.env['INSOMNIA_DATA_PATH'] || app.getPath('userData'), 'localStorage');
localStorage = new LocalStorage(localStoragePath);
function initElectronStorage() {
const electronStoragePath = path.join(process.env['INSOMNIA_DATA_PATH'] || app.getPath('userData'), 'localStorage');
electronStorage = new ElectronStorage(electronStoragePath);
}

export function createWindowsAndReturnMain({ firstLaunch }: { firstLaunch?: boolean } = {}) {
Expand Down

0 comments on commit a391899

Please sign in to comment.