Remote state

This commit is contained in:
Jiang Yio 2022-10-01 07:05:12 -04:00
parent 976a96e5e0
commit 24da5347f4
5 changed files with 57 additions and 48 deletions

View File

@ -88,10 +88,10 @@
computed: { computed: {
host: { host: {
get() { get() {
return vistax.state.host; return vistax.localstate.host;
}, },
set(value) { set(value) {
vistax.state.host = value; vistax.localstate.host = value;
} }
} }
}, },
@ -111,7 +111,10 @@
server(value) { this.x_server = value; }, server(value) { this.x_server = value; },
x_server(value) { this.$emit('update:server', value); }, x_server(value) { this.$emit('update:server', value); },
user(value) { this.x_user = value; }, user(value) { this.x_user = value; },
x_user(value) { this.$emit('update:user', value); } async x_user(value) {
if(value) await this.x_client.setup_remotestate();
this.$emit('update:user', value);
}
}, },
methods: { methods: {
async connect() { async connect() {

View File

@ -45,7 +45,6 @@
</template> </template>
<script> <script>
import { state } from './vistax.mjs';
import { groupByArray, strtr_unscramble, strHashHSL, strftime_vista, debounce } from './util.mjs'; import { groupByArray, strtr_unscramble, strHashHSL, strftime_vista, debounce } from './util.mjs';
import ViewResourceLookup from './ViewResourceLookup.vue'; import ViewResourceLookup from './ViewResourceLookup.vue';
@ -73,8 +72,8 @@
}, },
computed: { computed: {
selection: { selection: {
get() { return state.resources ? (state.resources.split(',').filter(x => x) || []) : [] }, get() { return this.client.remotestate.resources ? (this.client.remotestate.resources.split(',').filter(x => x) || []) : [] },
set(value) { state.resources = value.join(','); } set(value) { this.client.remotestate.resources = value.join(','); }
}, },
patients_lost() { patients_lost() {
return this.patients.filter(x => x.TimeLastDiff >= 0).sort((a, b) => b.TimeLastDiff - a.TimeLastDiff); return this.patients.filter(x => x.TimeLastDiff >= 0).sort((a, b) => b.TimeLastDiff - a.TimeLastDiff);

View File

@ -19,8 +19,6 @@
</template> </template>
<script> <script>
import { state } from './vistax.mjs';
import ViewResourceLookup from './ViewResourceLookup.vue'; import ViewResourceLookup from './ViewResourceLookup.vue';
import DateRangePicker from './DateRangePicker.vue'; import DateRangePicker from './DateRangePicker.vue';
import ViewSchedule from './ViewSchedule.vue'; import ViewSchedule from './ViewSchedule.vue';
@ -44,8 +42,8 @@
}, },
computed: { computed: {
selection: { selection: {
get() { return state.resources ? (state.resources.split(',').filter(x => x) || []) : [] }, get() { return this.client.remotestate.resources ? (this.client.remotestate.resources.split(',').filter(x => x) || []) : [] },
set(value) { state.resources = value.join(','); } set(value) { this.client.remotestate.resources = value.join(','); }
} }
} }
}; };

View File

@ -10,14 +10,13 @@
<td v-if="production"><router-link :to="'/patient/$' + row.HRN">{{row.Name}} <span :title="row.HRN">{{row.HRN.slice(-4)}}</span></router-link></td> <td v-if="production"><router-link :to="'/patient/$' + row.HRN">{{row.Name}} <span :title="row.HRN">{{row.HRN.slice(-4)}}</span></router-link></td>
<td v-else><router-link :title="strtr_unscramble(row.Name)" :to="'/patient/$' + row.Name.charAt(0) + row.HRN.slice(-4) + '?name=' + row.Name">{{row.Name}} ${{row.HRN}}</router-link></td> <td v-else><router-link :title="strtr_unscramble(row.Name)" :to="'/patient/$' + row.Name.charAt(0) + row.HRN.slice(-4) + '?name=' + row.Name">{{row.Name}} ${{row.HRN}}</router-link></td>
<td>{{row.NOTE}} [{{row.APPT_MADE_BY}} on {{row.DATE_APPT_MADE}}]</td> <td>{{row.NOTE}} [{{row.APPT_MADE_BY}} on {{row.DATE_APPT_MADE}}]</td>
<td><Autocomplete :value="practitioner[row.Name]" @update:value="x => set_practitioner(row.Name, x)" :items="practitioner_list" /></td> <td><Autocomplete :value="practitioner[row.Name]" @update:value="x => practitioner[row.Name] = x" :items="practitioner_list" /></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</template> </template>
<script> <script>
import { state } from './vistax.mjs';
import { uniq, strtr_unscramble, strHashHSL, strfdate_vista, debounce } from './util.mjs'; import { uniq, strtr_unscramble, strHashHSL, strfdate_vista, debounce } from './util.mjs';
import Autocomplete from './Autocomplete.vue'; import Autocomplete from './Autocomplete.vue';
@ -38,7 +37,6 @@
data() { data() {
return { return {
appointments: [], appointments: [],
practitioner: {},
production: true production: true
}; };
}, },
@ -46,6 +44,9 @@
params() { params() {
return { selection: this.selection, date_begin: this.date_begin, date_end: this.date_end }; return { selection: this.selection, date_begin: this.date_begin, date_end: this.date_end };
}, },
practitioner() {
return this.client.remotestate.practitioner || (this.client.remotestate.practitioner = {});
},
practitioner_list() { practitioner_list() {
return this.practitioner ? uniq(Object.values(this.practitioner)).sort() : []; return this.practitioner ? uniq(Object.values(this.practitioner)).sort() : [];
} }
@ -57,18 +58,12 @@
}, },
methods: { methods: {
strHashHSL, strHashHSL,
strtr_unscramble, strtr_unscramble
set_practitioner(patient, practitioner) {
this.practitioner[patient] = practitioner;
state.practitioner = this.practitioner;
}
}, },
created() { created() {
this.debounced_params = debounce(async function(value) { this.appointments = value.selection.length > 0 ? (await this.client.SDEC_CLINLET(value.selection.join('|') + '|', strfdate_vista(value.date_begin), strfdate_vista(value.date_end))).sort((a, b) => (new Date(a.ApptDate)) - (new Date(b.ApptDate))) : []; }, 500); this.debounced_params = debounce(async function(value) { this.appointments = value.selection.length > 0 ? (await this.client.SDEC_CLINLET(value.selection.join('|') + '|', strfdate_vista(value.date_begin), strfdate_vista(value.date_end))).sort((a, b) => (new Date(a.ApptDate)) - (new Date(b.ApptDate))) : []; }, 500);
}, },
async mounted() { async mounted() {
var practitioner = state.practitioner;
if(practitioner) this.practitioner = practitioner;
this.production = (await this.client.serverinfo()).result.production == '1'; this.production = (await this.client.serverinfo()).result.production == '1';
} }
}; };

View File

@ -2,19 +2,15 @@ import { reactive, watch } from 'vue';
import vista from './vista.mjs'; import vista from './vista.mjs';
import cookie from './cookie.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 { lab_parse, lab_reparse_results, measurement_parse, order_parse } from './reportparser.mjs';
import { TplFS, EncFS, randpassword as tplfs_randpassword } from './tplfs.mjs'; import { TplFS, EncFS, randpassword as tplfs_randpassword } from './tplfs.mjs';
export const state = reactive(cookie.get('state') ? JSON.parse(cookie.get('state')) : {}); export const localstate = 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'));
window.addEventListener('storage', function(evt) { 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); cookie.set('state', value = JSON.stringify(value), 45);
window.localStorage.setItem('state', value); window.localStorage.setItem('state', value);
}, { immediate: true, deep: true }); }, { immediate: true, deep: true });
@ -151,9 +147,27 @@ export function Client(cid, secret) {
this.userinfo = () => vista.userinfo(cid); this.userinfo = () => vista.userinfo(cid);
this.authenticate = (avcode=null) => vista.authenticate(cid, avcode); 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.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')); 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') { Client.fromCookie = async function(secret, defaulthost='vista.northport.med.va.gov:19209') {
if(!secret) secret = state.secret; if(!secret) secret = localstate.secret;
if(secret) { if(secret) {
var host = state.host; var host = localstate.host;
host = (host || defaulthost).split(':'); host = (host || defaulthost).split(':');
if(secret != state.secret) { if(secret != localstate.secret) {
console.log('Using new secret', secret); console.log('Using new secret', secret);
var client = await Client.fromScratch(secret, host[0], host[1]); var client = await Client.fromScratch(secret, host[0], host[1]);
if(client) { if(client) {
state.host = host.join(':'); localstate.host = host.join(':');
state.secret = secret; localstate.secret = secret;
state.cid = client.cid; localstate.cid = client.cid;
console.log('Established connection', client.cid); console.log('Established connection', client.cid);
return client; return client;
} else { } else {
delete state.secret; delete localstate.secret;
delete state.cid; delete localstate.cid;
console.log('Failed to connect'); console.log('Failed to connect');
return null; return null;
} }
} else if(!state.cid) { } else if(!localstate.cid) {
console.log('Using saved secret', secret); console.log('Using saved secret', secret);
var client = await Client.fromScratch(secret, host[0], host[1]); var client = await Client.fromScratch(secret, host[0], host[1]);
if(client) { if(client) {
state.host = host.join(':'); localstate.host = host.join(':');
state.secret = secret; localstate.secret = secret;
state.cid = client.cid; localstate.cid = client.cid;
console.log('Established connection', client.cid); console.log('Established connection', client.cid);
return client; return client;
} else { } else {
delete state.secret; delete localstate.secret;
delete state.cid; delete localstate.cid;
console.log('Failed connection'); console.log('Failed connection');
return null; return null;
} }
} else { } else {
console.log('Using saved secret and connection', secret); console.log('Using saved secret and connection', secret);
var cid = state.cid; var cid = localstate.cid;
var client = Client.fromID(cid, secret); var client = Client.fromID(cid, secret);
if((await vista.call(cid, 'XWB_IM_HERE')).result == '1') { if((await vista.call(cid, 'XWB_IM_HERE')).result == '1') {
var server = await client.serverinfo(); var server = await client.serverinfo();
if((host[0] == server.result.host) && (host[1] == server.result.port)) { if((host[0] == server.result.host) && (host[1] == server.result.port)) {
state.host = host.join(':'); localstate.host = host.join(':');
return client; return client;
} else console.log('Rejecting previous connection to different server', server); } else console.log('Rejecting previous connection to different server', server);
} }
delete state.cid; delete localstate.cid;
return await Client.fromCookie(secret, host.join(':')); return await Client.fromCookie(secret, host.join(':'));
} }
} }
}; };
export default window.vistax = { export default window.vistax = {
state, RPCError, Client, connect: Client.fromCookie localstate, RPCError, Client, connect: Client.fromCookie
}; };