【问题标题】:Asynchronously read a file line by line with Electron-vue使用 Electron-vue 异步逐行读取文件
【发布时间】:2019-09-22 19:40:12
【问题描述】:

我正在使用 Electron-vue 进行开发。我想逐行读取用户提供的大文本文件。另外,我想在阅读过程中显示一个加载图标(这意味着阅读应该是异步的)。
请注意,文本文件是用 shift-jis 编码的。

起初,我尝试了以下方法:
src/renderer/components/Import.vue

const dialog = require('electron').remote.dialog
export default {
  name: 'import',
  data () {
    return {
      loading: false
    }
  },
  methods: {
    mounted () {
      this.$refs.search.focus()
    },
    openDialog () {
      let filePath = dialog.showOpenDialog({
        properties: ['openFile'],
      })
      this.loading = true
      let that = this
      // Use the async function.
      this.getResultFileRead(that, filePath[0]).then(function (res) {
        // When the async function is completed, remove the loading icon.
        that.loading = false
        console.log(res)
      })
    },
    // Define async function here.
    async getResultFileRead (path) {
      return this.$electron.ipcRenderer.sendSync('readfile', path)
    }
  }
}

src/main/index.js

import fs from 'fs'
import readline from 'readline'
import iconv from 'iconv-lite'

ipcMain.on('readfile', (event, arg) => {
  // Using stream to decode shift-jis file.
  stream = fs.createReadStream(filePath).pipe(iconv.decodeStream('shift-jis'))
  let reader = readline.createInterface(stream, {})
  reader.on('line', line => {
     insertLine(line)
  })
  reader.on('close', () => {
    // do other stuff
  })
  event.returnValue = 'complete'
})

但这些都不起作用。当一个文件被提供时,文件被异步读取,但是一旦文件被提供(即在完成操作之前),readfile 函数就会返回“完成”,所以加载图标永远不会出现。

如何在 electron-vue 上同时实现“使用 shift-jis 异步读取文件”和“在读取文件时显示加载图标”? 请注意,Electron-vue 似乎使用的是 Node.js 版本 8.9.3。

【问题讨论】:

    标签: electron electron-vue


    【解决方案1】:

    我解决了这个问题如下。重点是close事件中的resolve函数。
    src/main/index.js

    import fs from 'fs'
    import readline from 'readline'
    import iconv from 'iconv-lite'
    
    ipcMain.on('readfile', async (event, arg) => {
        await readLines()
        event.sender.send('readfile-reply', 'complete')
    })
    
    function readLines() {
        return new Promise((resolve, reject) => {
            stream = fs.createReadStream(filePath).pipe(iconv.decodeStream('shift-jis'))
            let reader = readline.createInterface(stream, {})
            reader
                .on('line', line => {
                    insertLine(line)
                })
                .on('close', () => {
                    resolve() // "Resolve" in the close event.
                })
                .on('error', function(err) {
                    reject(err)
                })
        })
    }
    

    那么函数就可以异步使用了:
    src/renderer/components/Import.vue

    const dialog = require('electron').remote.dialog
    export default {
        name: 'import',
        data() {
            return {
                loading: false
            }
        },
        methods: {
            mounted() {
                this.$refs.search.focus()
            },
            openDialog() {
                let filePath = dialog.showOpenDialog({
                    properties: ['openFile'],
                })
                this.loading = true
                let that = this
                // Use the async function.
                this.asyncReadFile(that, filePath[0]).then(function(res) {
                    // When the async function is completed, remove the loading icon.
                    that.loading = false
                    console.log(res)
                    that.$electron.ipcRenderer.removeAllListeners('readfile-reply')
                })
            },
            asyncReadFile(that, path) {
                return new Promise(resolve => {
                    this.$electron.ipcRenderer.send('readfile', path)
                    this.$electron.ipcRenderer.on('readfile-reply', (event, result) => {
                        resolve(result)
                    })
                })
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2015-09-04
      • 1970-01-01
      相关资源
      最近更新 更多