rafaelleru
rafaelleru

Reputation: 357

update Vuex store from main proccess electron

How I can update a vuex store by commiting things from main proccess? for example:

In main thread:

import store from ../store

ipc.on('someevent', (event, args) => {
    // do stuff with args
store.commit('update-things')
})

and in the renderer update components with these modifications.

Edit: Real code:

main.js

import store from '../store'
const {ipcMain} = require('electron')
const WebTorrent = require('webtorrent')
const client = new WebTorrent()

ipcMain.on('addMagnet', (event, arg) => {
   client.add(arg, function (torrent) {
   var files = []
    torrent.files.forEach(function (file) {

    files.push({
        title: file.name,
        torrent: torrent.infoHash,
        index: torrent.files.indexOf(file),
        duration: '--:--'
    })
   })
  store.commit('addSongs', files)
})

and store mutation is like:

addSongs (state, newSongs) {
    newSongs.forEach(function (song) {
      state.songs.push(song)
    })
}

store is in diferent directory that main.js if it's helps.

the component that uses store is:

**by this component:

     <template>
      <div id="playlist">
        <div
          is="song"
          v-for="song in songs"
          v-bind:title="song.title"
          v-bind:torrent="song.torrent"
          v-bind:index="song.index">
    </div>
  </div>

</template>

<script>
import Song from './SongList/Song'

export default {
  name: 'playlist',
  components: { Song },
  computed: {
    songs () {
      return this.$store.getters.songs
    }
  }
}
</script>

Upvotes: 2

Views: 1441

Answers (1)

Jay Edwards
Jay Edwards

Reputation: 1035

I know this is an old topic, but for future searchers, you can use vuex-electron to dispatch to the store from the main thread.

https://www.npmjs.com/package/vuex-electron

Upvotes: 1

Related Questions