Skip to content

Commit

Permalink
fix: messages sync is not threadsafe (janhq#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Nov 30, 2023
1 parent 3b20d48 commit 70a8d03
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 61 deletions.
84 changes: 31 additions & 53 deletions electron/handlers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,11 @@ export function handleFsIPCs() {
ipcMain.handle(
'writeFile',
async (event, path: string, data: string): Promise<void> => {
return new Promise((resolve, reject) => {
fs.writeFile(join(userSpacePath, path), data, 'utf8', (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
try {
await fs.writeFileSync(join(userSpacePath, path), data, 'utf8')
} catch (err) {
console.error(`writeFile ${path} result: ${err}`)
}
}
)

Expand All @@ -79,15 +75,11 @@ export function handleFsIPCs() {
* @returns A promise that resolves when the directory has been created.
*/
ipcMain.handle('mkdir', async (event, path: string): Promise<void> => {
return new Promise((resolve, reject) => {
fs.mkdir(join(userSpacePath, path), { recursive: true }, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
try {
fs.mkdirSync(join(userSpacePath, path), { recursive: true })
} catch (err) {
console.error(`mkdir ${path} result: ${err}`)
}
})

/**
Expand All @@ -97,15 +89,11 @@ export function handleFsIPCs() {
* @returns A promise that resolves when the directory is removed successfully.
*/
ipcMain.handle('rmdir', async (event, path: string): Promise<void> => {
return new Promise((resolve, reject) => {
fs.rm(join(userSpacePath, path), { recursive: true }, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
try {
await fs.rmSync(join(userSpacePath, path), { recursive: true })
} catch (err) {
console.error(`rmdir ${path} result: ${err}`)
}
})

/**
Expand Down Expand Up @@ -136,23 +124,11 @@ export function handleFsIPCs() {
* @returns A string indicating the result of the operation.
*/
ipcMain.handle('deleteFile', async (_event, filePath) => {
const fullPath = join(userSpacePath, filePath)

let result = 'NULL'
fs.unlink(fullPath, function (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
try {
await fs.unlinkSync(join(userSpacePath, filePath))
} catch (err) {
console.error(`unlink ${filePath} result: ${err}`)
}
})

/**
Expand All @@ -163,17 +139,19 @@ export function handleFsIPCs() {
* @returns A promise that resolves when the file has been written.
*/
ipcMain.handle('appendFile', async (_event, path: string, data: string) => {
return new Promise((resolve, reject) => {
fs.appendFile(join(userSpacePath, path), data, 'utf8', (err) => {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
try {
await fs.appendFileSync(join(userSpacePath, path), data, 'utf8')
} catch (err) {
console.error(`appendFile ${path} result: ${err}`)
}
})

/**
* 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) => {
const fullPath = join(userSpacePath, path)

Expand Down
3 changes: 1 addition & 2 deletions electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"renderer/**/*",
"build/*.{js,map}",
"build/**/*.{js,map}",
"core/pre-install",
"core/plugin-manager/facade"
"core/pre-install"
],
"asarUnpack": [
"core/pre-install"
Expand Down
8 changes: 2 additions & 6 deletions web/services/coreService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as cn from './cloudNativeService'
import * as restAPI from './cloudNativeService'
import { EventEmitter } from './eventsService'
export const setupCoreServices = () => {
if (typeof window === 'undefined') {
Expand All @@ -13,11 +13,7 @@ export const setupCoreServices = () => {
}
window.coreAPI = {}
window.coreAPI = window.electronAPI ?? {
invokePluginFunc: cn.invokePluginFunc,
downloadFile: cn.downloadFile,
deleteFile: cn.deleteFile,
appVersion: cn.appVersion,
openExternalUrl: cn.openExternalUrl,
...restAPI,
}
}
}

0 comments on commit 70a8d03

Please sign in to comment.