Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move store variables to agent specific #603

Merged
merged 12 commits into from
Dec 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: restructure store IPC setup for improved initialization and…
… error handling
mohandast52 committed Dec 17, 2024
commit 1f1455127efa3676b2678e2553f52d3dd04d1dbd
14 changes: 8 additions & 6 deletions electron/main.js
Original file line number Diff line number Diff line change
@@ -338,12 +338,14 @@ const createMainWindow = async () => {
mainWindow.hide();
});

try {
logger.electron('Setting up store IPC');
setupStoreIpc(ipcMain, mainWindow);
} catch (e) {
logger.electron('Store IPC failed:', JSON.stringify(e));
}
app.on('ready', () => {
try {
logger.electron('Setting up store IPC');
setupStoreIpc(ipcMain, mainWindow);
} catch (e) {
logger.electron('Store IPC failed:', JSON.stringify(e));
}
});

if (isDev) {
mainWindow.webContents.openDevTools();
36 changes: 20 additions & 16 deletions electron/store.js
Original file line number Diff line number Diff line change
@@ -8,23 +8,22 @@ const defaultAgentSettings = {
currentStakingProgram: { type: 'string', default: '' },
};

// set schema to validate store data
// Schema for validating store data
const schema = {
environmentName: { type: 'string', default: '' },
lastSelectedAgentType: { type: 'string', default: 'trader' },
isInitialFunded_trader: { type: 'boolean', default: false },
isInitialFunded_memeooorr: { type: 'boolean', default: false },

// each agent has its own settings
trader: { ...defaultAgentSettings },
memeooorr: { ...defaultAgentSettings },
// Each agent has its own settings
trader: { type: 'object', default: defaultAgentSettings },
memeooorr: { type: 'object', default: defaultAgentSettings },
};

/**
* Sets up the IPC communication and initializes the Electron store with default values and schema.
* @param {Electron.IpcMain} ipcMain - The IPC channel for communication.
* @param {Electron.BrowserWindow} mainWindow - The main Electron browser window.
* @returns {Promise<void>} - A promise that resolves once the store is set up.
*/
const setupStoreIpc = (ipcMain, mainWindow) => {
const store = new Store({ schema });
@@ -36,46 +35,51 @@ const setupStoreIpc = (ipcMain, mainWindow) => {
* The following code migrates the old store to the new store schema.
*/
const traderAgent = {
...store(store.get('trader') || {}),
...(store.get('trader') || {}),
isInitialFunded:
store.get('isInitialFunded_trader') || store.get('isInitialFunded'),
firstRewardNotificationShown: store.get('firstRewardNotificationShown'),
agentEvictionAlertShown: store.get('agentEvictionAlertShown'),
currentStakingProgram: store.get('currentStakingProgram'),
};

// set the agent & delete old keys
// Set the trader agent and delete old keys
store.set('trader', traderAgent);
store.delete('isInitialFunded');
store.delete('isInitialFunded_trader');
store.delete('firstStakingRewardAchieved');
store.delete('firstRewardNotificationShown');
store.delete('agentEvictionAlertShown');
store.delete('currentStakingProgram');
[
'isInitialFunded',
'isInitialFunded_trader',
'firstRewardNotificationShown',
'agentEvictionAlertShown',
'currentStakingProgram',
].forEach((key) => store.delete(key));

/**
* agent: memeooorr Migration
*/
if (store.has('isInitialFunded_memeooorr')) {
const memeooorrAgent = store.get('memeooorr');
const memeooorrAgent = store.get('memeooorr') || {};
store.set('memeooorr', {
...memeooorrAgent,
isInitialFunded: store.get('isInitialFunded_memeooorr') || false,
});
store.delete('isInitialFunded_memeooorr');
}

// Notify renderer process when store changes
store.onDidAnyChange((data) => {
if (mainWindow?.webContents)
if (mainWindow?.webContents) {
mainWindow.webContents.send('store-changed', data);
}
});

// exposed to electron browser window
ipcMain.handle('store', () => store.store);
ipcMain.handle('store-get', (_, key) => store.get(key));
ipcMain.handle('store-set', (_, key, value) => store.set(key, value));
ipcMain.handle('store-delete', (_, key) => store.delete(key));
ipcMain.handle('store-clear', (_) => store.clear());
ipcMain.handle('store-clear', () => store.clear());

console.log('[Store] IPC handlers registered successfully.');
};

module.exports = { setupStoreIpc };