Storing data within electron's main.js
to display reactively in a vue window has been a bit challenging. I have a store set up in store/index.js
with state and mutations, which works fine when accessed individually from electron and vue. However, the issue is that the data is not shared between these two contexts - vue does not receive the data committed within electron.
How can I successfully commit to a store from my main electron environment and ensure that the changes are reactively displayed in my vue window?
store/index.js
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export default new Vuex.Store({
state: {value: ''},
mutations: {
set: (state, payload) => {
state.value = payload;
}
}
});
main.js
import Vue from "vue";
import App from "./App.vue";
import "./registerServiceWorker";
import router from "./router";
import store from "./store";
import vuetify from "./plugins/vuetify";
new Vue({
router,
store,
vuetify,
render: (h) => h(App),
}).$mount("#app");
App.vue
<template>
<div>
{{ $store.state.value }}
</div>
</template>
<script>
import store from "./store";
export default {
data: () => ({
}),
created() {
// Committing here displays the value
store.commit("set", 'dummy1');
}
}
</script>
background.js
'use strict'
import store from "./store";
import { app, protocol, BrowserWindow } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
])
async function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION
}
})
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
} else {
createProtocol('app')
// Load the index.html when not in development
win.loadURL('app://./index.html')
}
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
createWindow()
// Committing here does not display the changes
store.commit("set", 'dummy2');
})
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
app.quit()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
})
}
}
It seems that there may be two separate stores that do not share their contents as expected