fix: messages sync is not threadsafe (#784)

This commit is contained in:
Louis 2023-11-30 11:19:17 +07:00 committed by GitHub
parent 3b20d48de3
commit 70a8d03c9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 61 deletions

View File

@ -60,15 +60,11 @@ export function handleFsIPCs() {
ipcMain.handle( ipcMain.handle(
'writeFile', 'writeFile',
async (event, path: string, data: string): Promise<void> => { async (event, path: string, data: string): Promise<void> => {
return new Promise((resolve, reject) => { try {
fs.writeFile(join(userSpacePath, path), data, 'utf8', (err) => { await fs.writeFileSync(join(userSpacePath, path), data, 'utf8')
if (err) { } catch (err) {
reject(err) console.error(`writeFile ${path} result: ${err}`)
} else {
resolve()
} }
})
})
} }
) )
@ -79,16 +75,12 @@ export function handleFsIPCs() {
* @returns A promise that resolves when the directory has been created. * @returns A promise that resolves when the directory has been created.
*/ */
ipcMain.handle('mkdir', async (event, path: string): Promise<void> => { ipcMain.handle('mkdir', async (event, path: string): Promise<void> => {
return new Promise((resolve, reject) => { try {
fs.mkdir(join(userSpacePath, path), { recursive: true }, (err) => { fs.mkdirSync(join(userSpacePath, path), { recursive: true })
if (err) { } catch (err) {
reject(err) console.error(`mkdir ${path} result: ${err}`)
} else {
resolve()
} }
}) })
})
})
/** /**
* Removes a directory in the user data directory. * Removes a directory in the user data directory.
@ -97,16 +89,12 @@ export function handleFsIPCs() {
* @returns A promise that resolves when the directory is removed successfully. * @returns A promise that resolves when the directory is removed successfully.
*/ */
ipcMain.handle('rmdir', async (event, path: string): Promise<void> => { ipcMain.handle('rmdir', async (event, path: string): Promise<void> => {
return new Promise((resolve, reject) => { try {
fs.rm(join(userSpacePath, path), { recursive: true }, (err) => { await fs.rmSync(join(userSpacePath, path), { recursive: true })
if (err) { } catch (err) {
reject(err) console.error(`rmdir ${path} result: ${err}`)
} else {
resolve()
} }
}) })
})
})
/** /**
* Lists the files in a directory in the user data directory. * Lists the files in a directory in the user data directory.
@ -136,23 +124,11 @@ export function handleFsIPCs() {
* @returns A string indicating the result of the operation. * @returns A string indicating the result of the operation.
*/ */
ipcMain.handle('deleteFile', async (_event, filePath) => { ipcMain.handle('deleteFile', async (_event, filePath) => {
const fullPath = join(userSpacePath, filePath) try {
await fs.unlinkSync(join(userSpacePath, filePath))
let result = 'NULL' } catch (err) {
fs.unlink(fullPath, function (err) { console.error(`unlink ${filePath} result: ${err}`)
if (err && err.code == 'ENOENT') {
result = `File not exist: ${err}`
} else if (err) {
result = `File delete error: ${err}`
} else {
result = 'File deleted successfully'
} }
console.debug(
`Delete file ${filePath} from ${fullPath} result: ${result}`
)
})
return result
}) })
/** /**
@ -163,17 +139,19 @@ export function handleFsIPCs() {
* @returns A promise that resolves when the file has been written. * @returns A promise that resolves when the file has been written.
*/ */
ipcMain.handle('appendFile', async (_event, path: string, data: string) => { ipcMain.handle('appendFile', async (_event, path: string, data: string) => {
return new Promise((resolve, reject) => { try {
fs.appendFile(join(userSpacePath, path), data, 'utf8', (err) => { await fs.appendFileSync(join(userSpacePath, path), data, 'utf8')
if (err) { } catch (err) {
reject(err) console.error(`appendFile ${path} result: ${err}`)
} else {
resolve(data)
} }
}) })
})
})
/**
* Reads a file line by line.
* @param event - The event object.
* @param path - The path of the file to read.
* @returns A promise that resolves with the contents of the file.
*/
ipcMain.handle('readLineByLine', async (_event, path: string) => { ipcMain.handle('readLineByLine', async (_event, path: string) => {
const fullPath = join(userSpacePath, path) const fullPath = join(userSpacePath, path)

View File

@ -13,8 +13,7 @@
"renderer/**/*", "renderer/**/*",
"build/*.{js,map}", "build/*.{js,map}",
"build/**/*.{js,map}", "build/**/*.{js,map}",
"core/pre-install", "core/pre-install"
"core/plugin-manager/facade"
], ],
"asarUnpack": [ "asarUnpack": [
"core/pre-install" "core/pre-install"

View File

@ -1,4 +1,4 @@
import * as cn from './cloudNativeService' import * as restAPI from './cloudNativeService'
import { EventEmitter } from './eventsService' import { EventEmitter } from './eventsService'
export const setupCoreServices = () => { export const setupCoreServices = () => {
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
@ -13,11 +13,7 @@ export const setupCoreServices = () => {
} }
window.coreAPI = {} window.coreAPI = {}
window.coreAPI = window.electronAPI ?? { window.coreAPI = window.electronAPI ?? {
invokePluginFunc: cn.invokePluginFunc, ...restAPI,
downloadFile: cn.downloadFile,
deleteFile: cn.deleteFile,
appVersion: cn.appVersion,
openExternalUrl: cn.openExternalUrl,
} }
} }
} }