【发布时间】:2018-07-29 13:04:22
【问题描述】:
所以我正在构建一个 Electron 和 React 应用程序。我正在使用 ghost-script 创建某些 pdf 文件的 imgs,我想知道如何告诉 Node.js 在打开窗口并更改应用程序中的状态之前等待创建 imgs。这些 img 被用作组件的 src,当组件尝试加载 img 时,它在某种程度上保留了一个损坏的状态,因为当状态更新时 img src 不存在。
// this is where I set the state before sending all the data to the renderer process(the front end of the App)
function getStateReady(theState, event) {
let pdfFiles = scanDirectory(currentDir);
let pdfNames = getPdfName(pdfFiles);
imageUrls = getImgName(pdfFiles);
console.log(imageUrls);
createImg(pdfFiles, pdfNames);
switch (theState) {
case 'initialState':
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('initialState', pdfFiles, imageUrls);
})
break;
case 'secondState-reply':
event.sender.send('secondState-reply', pdfFiles, imageUrls);
break;
default:
console.log('a param was missing');
}
}
//these to functions take a pdf file path and its name to create an img
function createImg(pdfPaths, pdfNames) {
pdfNames.forEach((item, index) => {
if(fs.existsSync(path.join(rootDirectory, 'src', 'imgs', item.replace('.pdf', '.jpg')))) {
console.log('image exists');
}
else {
console.log("creating image");
child(returnProcess(pdfPaths[index], item), (err, stdout) => {
if(err) {
console.log(err)
}
console.log(stdout)
})
}
})
}
function returnProcess(pdfPath, pdfName) {
let newPdf = `"${pdfPath}"`
let output = `"${path.join(rootDirectory, 'src', 'imgs', pdfName.replace('.pdf', '.jpg'))}"`;
let mainProcess = `"C:\\Program Files\\gs\\gs9.23\\bin\\gswin64c.exe" -q -o ${output} -sDEVICE=pngalpha -dLastPage=1 ${newPdf}`
return mainProcess;
}
【问题讨论】:
-
将检查它。谢谢
-
我使用过异步和等待,但我不确定我是否正确使用它。即使执行了所有功能,问题仍然存在,因为仍在创建图像。也许问题出在幽灵脚本本身。
标签: javascript node.js reactjs electron child-process