|
|
@@ -11,6 +11,8 @@ let memoryCache = {};
|
|
|
|
|
|
const loginFilePath = path.join(app.getPath('userData'), 'loginSession.json');
|
|
|
const symbolFilePath = path.join(app.getPath('userData'), 'symbolSession.json');
|
|
|
+const dbBasePath = path.join(app.getPath('userData'))
|
|
|
+
|
|
|
function readData(filePath) {
|
|
|
try {
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
@@ -102,6 +104,9 @@ function createWindow() {
|
|
|
console.log(Object.keys(memoryCache))
|
|
|
}
|
|
|
|
|
|
+let memoryDbData = []
|
|
|
+let memoryDbPath = undefined
|
|
|
+
|
|
|
app.whenReady().then(() => {
|
|
|
ipcMain.handle('get-login-info-data', () => {
|
|
|
return readData(loginFilePath);
|
|
|
@@ -118,6 +123,32 @@ app.whenReady().then(() => {
|
|
|
ipcMain.handle('set-symbol-data', (event, newData) => {
|
|
|
writeData(symbolFilePath, newData);
|
|
|
});
|
|
|
+
|
|
|
+ ipcMain.handle('set-db-data', (event, dataList, symbol) => {
|
|
|
+ const finalPath = path.join(dbBasePath, `${symbol}.json`)
|
|
|
+ writeData(finalPath, dataList)
|
|
|
+
|
|
|
+ console.log(`Local db is set. ${finalPath}`)
|
|
|
+ })
|
|
|
+
|
|
|
+ ipcMain.handle('get-db-data', (event, symbol) => {
|
|
|
+ const finalPath = path.join(dbBasePath, `${symbol}.json`)
|
|
|
+ const rst = readData(finalPath)
|
|
|
+
|
|
|
+ if (JSON.stringify(rst) === '{}') {
|
|
|
+ console.log(`No local db is found:${finalPath}`)
|
|
|
+ return []
|
|
|
+ } else {
|
|
|
+ console.log(`Local db is found:${finalPath}, length: ${rst.length}`)
|
|
|
+ return rst
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ ipcMain.handle('flush-memory-db-data', (event, data, symbol) => {
|
|
|
+ memoryDbData = data
|
|
|
+ if (!memoryDbPath) memoryDbPath = path.join(dbBasePath, `${symbol}.json`)
|
|
|
+ // console.log(`Memory db is flush。length: ${memoryDbData.length}, symbol: ${symbol}`)
|
|
|
+ })
|
|
|
});
|
|
|
|
|
|
app.on('ready', () => {
|
|
|
@@ -130,36 +161,36 @@ app.on('ready', () => {
|
|
|
if (isDev && relativePath.indexOf('react-stock-heatmap/example') !== -1) {
|
|
|
relativePath = relativePath.split('react-stock-heatmap/example')[1]
|
|
|
}
|
|
|
- console.log(relativePath)
|
|
|
+ // console.log(relativePath)
|
|
|
|
|
|
if (memoryCache[relativePath]) {
|
|
|
let sanitizedRelativePath = relativePath.replace('app.asar', 'app_asar'); // 避免路径中包含 app.asar
|
|
|
- console.log(app.getPath('temp'), sanitizedRelativePath)
|
|
|
+ // console.log(app.getPath('temp'), sanitizedRelativePath)
|
|
|
const tempFilePath = path.join(app.getPath('temp'), sanitizedRelativePath);
|
|
|
- console.log(`Temp File Path: ${tempFilePath}`);
|
|
|
+ // console.log(`Temp File Path: ${tempFilePath}`);
|
|
|
|
|
|
const tempDir = path.dirname(tempFilePath);
|
|
|
- console.log(`Temp Directory Path: ${tempDir}`);
|
|
|
+ // console.log(`Temp Directory Path: ${tempDir}`);
|
|
|
|
|
|
// 确保临时目录路径存在
|
|
|
if (!fs.existsSync(tempDir)) {
|
|
|
- console.log(`Creating directory: ${tempDir}`);
|
|
|
+ // console.log(`Creating directory: ${tempDir}`);
|
|
|
fs.mkdirSync(tempDir, { recursive: true });
|
|
|
- console.log(`Directory created: ${tempDir}`);
|
|
|
+ // console.log(`Directory created: ${tempDir}`);
|
|
|
} else {
|
|
|
- console.log(`Directory already exists: ${tempDir}`);
|
|
|
+ // console.log(`Directory already exists: ${tempDir}`);
|
|
|
}
|
|
|
|
|
|
// 确保目录存在后再写入文件
|
|
|
if (fs.existsSync(tempDir)) {
|
|
|
- console.log(`Writing file: ${tempFilePath}`);
|
|
|
+ // console.log(`Writing file: ${tempFilePath}`);
|
|
|
fs.writeFileSync(tempFilePath, memoryCache[relativePath]);
|
|
|
- console.log(`File written: ${tempFilePath}`);
|
|
|
+ // console.log(`File written: ${tempFilePath}`);
|
|
|
|
|
|
- console.log(`Intercepting request for: ${relativePath}, redirectURL: ${tempFilePath}`);
|
|
|
+ // console.log(`Intercepting request for: ${relativePath}, redirectURL: ${tempFilePath}`);
|
|
|
callback({ cancel: false, redirectURL: tempFilePath });
|
|
|
} else {
|
|
|
- console.error(`Directory does not exist after creation attempt: ${tempDir}`);
|
|
|
+ // console.error(`Directory does not exist after creation attempt: ${tempDir}`);
|
|
|
callback({ cancel: false });
|
|
|
}
|
|
|
} else {
|
|
|
@@ -171,6 +202,11 @@ app.on('ready', () => {
|
|
|
});
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
+ if (memoryDbData.length > 0 && memoryDbPath) {
|
|
|
+ writeData(memoryDbPath, memoryDbData);
|
|
|
+ console.log(`Exit saved db is ok. length: ${memoryDbData.length}, path:${memoryDbPath}`)
|
|
|
+ }
|
|
|
+
|
|
|
if (process.platform !== 'darwin') {
|
|
|
app.quit();
|
|
|
}
|