From f831f905a5d88a1123443fb337a58dcec3a28549 Mon Sep 17 00:00:00 2001 From: inportb Date: Wed, 28 Sep 2022 02:00:59 -0400 Subject: [PATCH] Cross-tab state synchronization with localStorage --- htdocs/vistax.mjs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/htdocs/vistax.mjs b/htdocs/vistax.mjs index a231612..e8e0649 100644 --- a/htdocs/vistax.mjs +++ b/htdocs/vistax.mjs @@ -10,7 +10,13 @@ if((!state.cid) && (cookie.get('cid'))) state.resources = cookie.get('cid'); if((!state.host) && (cookie.get('host'))) state.resources = cookie.get('host'); if((!state.resources) && (cookie.get('vista.resources'))) state.resources = cookie.get('vista.resources'); if((!state.practitioner) && (cookie.get('vista.practitioner'))) state.practitioner = JSON.parse(cookie.get('vista.practitioner')); -watch(state, value => cookie.set('state', JSON.stringify(value), 45), { immediate: true, deep: true }); +window.addEventListener('storage', function(evt) { + if((evt.storageArea == window.localStorage) && (evt.key == 'state') && (evt.newValue)) Object.assign(state, JSON.parse(evt.newValue)); +}); +watch(state, function(value) { + cookie.set('state', value = JSON.stringify(value), 45); + window.localStorage.setItem('state', value); +}, { immediate: true, deep: true }); function RPCError(type, ...args) { this.name = type;