Remote state
This commit is contained in:
@@ -2,19 +2,15 @@ import { reactive, watch } from 'vue';
|
||||
|
||||
import vista from './vista.mjs';
|
||||
import cookie from './cookie.mjs';
|
||||
import { debounce } from './util.mjs';
|
||||
import { lab_parse, lab_reparse_results, measurement_parse, order_parse } from './reportparser.mjs';
|
||||
import { TplFS, EncFS, randpassword as tplfs_randpassword } from './tplfs.mjs';
|
||||
|
||||
export const state = reactive(cookie.get('state') ? JSON.parse(cookie.get('state')) : {});
|
||||
if((!state.secret) && (cookie.get('secret'))) state.resources = cookie.get('secret');
|
||||
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'));
|
||||
export const localstate = reactive(cookie.get('state') ? JSON.parse(cookie.get('state')) : {});
|
||||
window.addEventListener('storage', function(evt) {
|
||||
if((evt.storageArea == window.localStorage) && (evt.key == 'state') && (evt.newValue)) Object.assign(state, JSON.parse(evt.newValue));
|
||||
if((evt.storageArea == window.localStorage) && (evt.key == 'state') && (evt.newValue)) Object.assign(localstate, JSON.parse(evt.newValue));
|
||||
});
|
||||
watch(state, function(value) {
|
||||
watch(localstate, function(value) {
|
||||
cookie.set('state', value = JSON.stringify(value), 45);
|
||||
window.localStorage.setItem('state', value);
|
||||
}, { immediate: true, deep: true });
|
||||
@@ -151,9 +147,27 @@ export function Client(cid, secret) {
|
||||
this.userinfo = () => vista.userinfo(cid);
|
||||
this.authenticate = (avcode=null) => vista.authenticate(cid, avcode);
|
||||
|
||||
if(!state.encfs) state.encfs = tplfs_randpassword();
|
||||
if(!localstate.encfs) localstate.encfs = tplfs_randpassword();
|
||||
this.tplfs = async () => this._tplfs ? this._tplfs : (this._tplfs = await TplFS.fromUser(this, (await this.userinfo()).result[0]));
|
||||
this.encfs = async () => this._encfs ? this._encfs : (this._encfs = await EncFS.fromPassword(await this.tplfs(), state.encfs));
|
||||
this.encfs = async () => this._encfs ? this._encfs : (this._encfs = await EncFS.fromPassword(await this.tplfs(), localstate.encfs));
|
||||
|
||||
this.remotestate = reactive({});
|
||||
this.setup_remotestate = async () => {
|
||||
var fs = await this.encfs();
|
||||
try {
|
||||
Object.assign(this.remotestate, JSON.parse(await fs.cat('state')));
|
||||
} catch(ex) {
|
||||
console.error(ex);
|
||||
await fs.create('state', JSON.stringify(this.remotestate));
|
||||
}
|
||||
watch(this.remotestate, debounce(function(value) {
|
||||
fs.update('state', JSON.stringify(value));
|
||||
}, 1000), { deep: true });
|
||||
if((!this.remotestate.resources) && (localstate.resources)) this.remotestate.resources = localstate.resources;
|
||||
if((!this.remotestate.practitioner) && (localstate.practitioner)) this.remotestate.practitioner = localstate.practitioner;
|
||||
if(localstate.resources) delete localstate.resources;
|
||||
if(localstate.practitioner) delete localstate.practitioner;
|
||||
};
|
||||
|
||||
this.XWB_IM_HERE = unwrapped(logged(() => this.call('XWB_IM_HERE'), 'XWB_IM_HERE'));
|
||||
|
||||
@@ -209,57 +223,57 @@ Client.fromScratch = async function(secret, host='vista.northport.med.va.gov', p
|
||||
};
|
||||
|
||||
Client.fromCookie = async function(secret, defaulthost='vista.northport.med.va.gov:19209') {
|
||||
if(!secret) secret = state.secret;
|
||||
if(!secret) secret = localstate.secret;
|
||||
if(secret) {
|
||||
var host = state.host;
|
||||
var host = localstate.host;
|
||||
host = (host || defaulthost).split(':');
|
||||
if(secret != state.secret) {
|
||||
if(secret != localstate.secret) {
|
||||
console.log('Using new secret', secret);
|
||||
var client = await Client.fromScratch(secret, host[0], host[1]);
|
||||
if(client) {
|
||||
state.host = host.join(':');
|
||||
state.secret = secret;
|
||||
state.cid = client.cid;
|
||||
localstate.host = host.join(':');
|
||||
localstate.secret = secret;
|
||||
localstate.cid = client.cid;
|
||||
console.log('Established connection', client.cid);
|
||||
return client;
|
||||
} else {
|
||||
delete state.secret;
|
||||
delete state.cid;
|
||||
delete localstate.secret;
|
||||
delete localstate.cid;
|
||||
console.log('Failed to connect');
|
||||
return null;
|
||||
}
|
||||
} else if(!state.cid) {
|
||||
} else if(!localstate.cid) {
|
||||
console.log('Using saved secret', secret);
|
||||
var client = await Client.fromScratch(secret, host[0], host[1]);
|
||||
if(client) {
|
||||
state.host = host.join(':');
|
||||
state.secret = secret;
|
||||
state.cid = client.cid;
|
||||
localstate.host = host.join(':');
|
||||
localstate.secret = secret;
|
||||
localstate.cid = client.cid;
|
||||
console.log('Established connection', client.cid);
|
||||
return client;
|
||||
} else {
|
||||
delete state.secret;
|
||||
delete state.cid;
|
||||
delete localstate.secret;
|
||||
delete localstate.cid;
|
||||
console.log('Failed connection');
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
console.log('Using saved secret and connection', secret);
|
||||
var cid = state.cid;
|
||||
var cid = localstate.cid;
|
||||
var client = Client.fromID(cid, secret);
|
||||
if((await vista.call(cid, 'XWB_IM_HERE')).result == '1') {
|
||||
var server = await client.serverinfo();
|
||||
if((host[0] == server.result.host) && (host[1] == server.result.port)) {
|
||||
state.host = host.join(':');
|
||||
localstate.host = host.join(':');
|
||||
return client;
|
||||
} else console.log('Rejecting previous connection to different server', server);
|
||||
}
|
||||
delete state.cid;
|
||||
delete localstate.cid;
|
||||
return await Client.fromCookie(secret, host.join(':'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default window.vistax = {
|
||||
state, RPCError, Client, connect: Client.fromCookie
|
||||
localstate, RPCError, Client, connect: Client.fromCookie
|
||||
};
|
||||
|
Reference in New Issue
Block a user