From 55c3854c2249017d3276337b14864f7d18daea8f Mon Sep 17 00:00:00 2001 From: root Date: Fri, 12 Nov 2021 08:34:51 +0530 Subject: [PATCH] Cross-tab syncing --- src/store/authentication-store.js | 13 +++++++++++-- src/store/index.js | 7 +++++-- src/store/prefs-store.js | 13 +++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/store/authentication-store.js b/src/store/authentication-store.js index d386a9c..4f3c3dd 100644 --- a/src/store/authentication-store.js +++ b/src/store/authentication-store.js @@ -122,6 +122,15 @@ const AuthenticationStore = { } } -export { - AuthenticationStore +function initializeAuthEvents (store) { + window.addEventListener('storage', (storageEv) => { + if (storageEv.key === 'AUTH') { + store.commit('auth/replaceAuth', JSON.parse(storageEv.newValue)) + } + }) +} + +export { + AuthenticationStore, + initializeAuthEvents } diff --git a/src/store/index.js b/src/store/index.js index b092a21..ead76ad 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,8 +1,8 @@ import Vue from 'vue' import Vuex from 'vuex' -import { PrefsStore } from '@/store/prefs-store' -import { AuthenticationStore } from '@/store/authentication-store' +import { PrefsStore, initializePrefEvents } from '@/store/prefs-store' +import { AuthenticationStore, initializeAuthEvents } from '@/store/authentication-store' Vue.use(Vuex) @@ -13,4 +13,7 @@ const store = new Vuex.Store({ } }) +initializePrefEvents(store) +initializeAuthEvents(store) + export default store diff --git a/src/store/prefs-store.js b/src/store/prefs-store.js index 2038a92..b1b6589 100644 --- a/src/store/prefs-store.js +++ b/src/store/prefs-store.js @@ -77,6 +77,15 @@ const PrefsStore = { } } -export { - PrefsStore +function initializePrefEvents (store) { + window.addEventListener('storage', (storageEv) => { + if (storageEv.key === 'PREFERENCES') { + store.commit('prefs/replacePrefs', JSON.parse(storageEv.newValue)) + } + }) +} + +export { + PrefsStore, + initializePrefEvents }