Is there a way to update a vuex store by committing changes from the main process? Here is an example:
In the main thread:
import store from '../store'
ipc.on('someevent', (event, args) => {
// do something with args
store.commit('update-things')
})
These modifications are then used to update components in the renderer.
Edit: Actual 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)
})
The store mutation for adding songs is as follows:
addSongs (state, newSongs) {
newSongs.forEach(function (song) {
state.songs.push(song)
})
}
The store is located in a different directory from main.js if that information helps.
The component that utilizes the store is:
**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>