From 8d1d6537a4a006cc3b5102e2a0b9395961a62087 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 25 Dec 2022 17:12:12 +0100 Subject: [PATCH] ask password works! --- .../web/src/forms/FormPasswordFieldRaw.svelte | 6 ++++++ .../web/src/forms/FormTextFieldRaw.svelte | 6 ++++++ .../web/src/modals/DatabaseLoginModal.svelte | 12 +++++++----- packages/web/src/utility/api.ts | 16 +++++++++++++++- packages/web/src/utility/cache.ts | 19 ++++++++++++++----- packages/web/src/utility/connectionsPinger.js | 4 ++-- 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/packages/web/src/forms/FormPasswordFieldRaw.svelte b/packages/web/src/forms/FormPasswordFieldRaw.svelte index 5244373f..b2a8f0e3 100644 --- a/packages/web/src/forms/FormPasswordFieldRaw.svelte +++ b/packages/web/src/forms/FormPasswordFieldRaw.svelte @@ -8,6 +8,7 @@ export let name; export let disabled = false; + export let saveOnInput = false; const { values, setFieldValue } = getFormContext(); @@ -23,6 +24,11 @@ {disabled} value={isCrypted ? '' : value} on:change={e => setFieldValue(name, e.target['value'])} + on:input={e => { + if (saveOnInput) { + setFieldValue(name, e.target['value']); + } + }} placeholder={isCrypted ? '(Password is encrypted)' : undefined} type={isCrypted || showPassword ? 'text' : 'password'} /> diff --git a/packages/web/src/forms/FormTextFieldRaw.svelte b/packages/web/src/forms/FormTextFieldRaw.svelte index 215c0317..320dd7b2 100644 --- a/packages/web/src/forms/FormTextFieldRaw.svelte +++ b/packages/web/src/forms/FormTextFieldRaw.svelte @@ -4,6 +4,7 @@ export let name; export let defaultValue; + export let saveOnInput = false; const { values, setFieldValue } = getFormContext(); @@ -12,4 +13,9 @@ {...$$restProps} value={$values[name] ?? defaultValue} on:input={e => setFieldValue(name, e.target['value'])} + on:input={e => { + if (saveOnInput) { + setFieldValue(name, e.target['value']); + } + }} /> diff --git a/packages/web/src/modals/DatabaseLoginModal.svelte b/packages/web/src/modals/DatabaseLoginModal.svelte index d4991aea..53380fe4 100644 --- a/packages/web/src/modals/DatabaseLoginModal.svelte +++ b/packages/web/src/modals/DatabaseLoginModal.svelte @@ -18,7 +18,7 @@ import FormTextField from '../forms/FormTextField.svelte'; import FontIcon from '../icons/FontIcon.svelte'; import { apiCall, setVolatileConnectionRemapping } from '../utility/api'; - import { dispatchCacheChange } from '../utility/cache'; + import { batchDispatchCacheTriggers, dispatchCacheChange } from '../utility/cache'; import createRef from '../utility/createRef'; import { getConnectionInfo } from '../utility/metadataLoaders'; @@ -64,16 +64,16 @@ const testid = testIdRef.get(); const resp = await apiCall('connections/save-volatile', { conid, - user: ev.detail.user, - password: ev.detail.password, + user: $values['user'], + password: $values['password'], test: true, }); if (testIdRef.get() != testid) return; isTesting = false; if (resp.msgtype == 'connected') { setVolatileConnectionRemapping(conid, resp._id); - dispatchCacheChange(`database-list-changed-${conid}`); - dispatchCacheChange(`server-status-changed`); + dispatchCacheChange({ key: `server-status-changed` }); + batchDispatchCacheTriggers(x => x.conid == conid); closeCurrentModal(); } else { sqlConnectResult = resp; @@ -92,12 +92,14 @@ autocomplete="username" disabled={passwordMode == 'askPassword'} focused={passwordMode == 'askUser'} + saveOnInput /> {#if isTesting} diff --git a/packages/web/src/utility/api.ts b/packages/web/src/utility/api.ts index 45f3165f..c95f2c14 100644 --- a/packages/web/src/utility/api.ts +++ b/packages/web/src/utility/api.ts @@ -16,6 +16,7 @@ let apiDisabled = false; const disabledOnOauth = isOauthCallback(); const volatileConnectionMap = {}; +const volatileConnectionMapInv = {}; export function disableApi() { apiDisabled = true; @@ -27,12 +28,17 @@ export function enableApi() { export function setVolatileConnectionRemapping(existingConnectionId, volatileConnectionId) { volatileConnectionMap[existingConnectionId] = volatileConnectionId; + volatileConnectionMapInv[volatileConnectionId] = existingConnectionId; } export function getVolatileRemapping(conid) { return volatileConnectionMap[conid] || conid; } +export function getVolatileRemappingInv(conid) { + return volatileConnectionMapInv[conid] || conid; +} + function wantEventSource() { if (!eventSource) { eventSource = new EventSource(`${resolveApi()}/stream`); @@ -64,7 +70,7 @@ function processApiResponse(route, args, resp) { return resp; } -function transformApiArgs(args) { +export function transformApiArgs(args) { return _.mapValues(args, (v, k) => { if (k == 'conid' && v && volatileConnectionMap[v]) return volatileConnectionMap[v]; if (k == 'conidArray' && _.isArray(v)) return v.map(x => volatileConnectionMap[x] || x); @@ -72,6 +78,14 @@ function transformApiArgs(args) { }); } +export function transformApiArgsInv(args) { + return _.mapValues(args, (v, k) => { + if (k == 'conid' && v && volatileConnectionMapInv[v]) return volatileConnectionMapInv[v]; + if (k == 'conidArray' && _.isArray(v)) return v.map(x => volatileConnectionMapInv[x] || x); + return v; + }); +} + export async function apiCall(route: string, args: {} = undefined) { if (apiLogging) { console.log('>>> API CALL', route, args); diff --git a/packages/web/src/utility/cache.ts b/packages/web/src/utility/cache.ts index 8e4443ea..19a100d0 100644 --- a/packages/web/src/utility/cache.ts +++ b/packages/web/src/utility/cache.ts @@ -1,4 +1,4 @@ -import { apiOn } from './api'; +import { apiOn, transformApiArgsInv } from './api'; import getAsArray from './getAsArray'; import stableStringify from 'json-stable-stringify'; @@ -34,7 +34,7 @@ function cacheSet(cacheKey, value, reloadTrigger, generation) { function cacheClean(reloadTrigger) { cacheGeneration += 1; for (const item of getAsArray(reloadTrigger)) { - const itemString = stableStringify(item); + const itemString = stableStringify(transformApiArgsInv(item)); const keys = cachedKeysByReloadTrigger[itemString]; if (keys) { for (const key of keys) { @@ -80,7 +80,8 @@ export async function loadCachedValue(reloadTrigger, cacheKey, func) { } } catch (err) { console.error('Error when using cached promise', err); - cacheClean(cacheKey); + // cacheClean(cacheKey); + cacheClean(reloadTrigger); const res = await func(); cacheSet(cacheKey, res, reloadTrigger, generation); return res; @@ -113,11 +114,10 @@ export async function unsubscribeCacheChange(reloadTrigger, cacheKey, reloadHand } export function dispatchCacheChange(reloadTrigger) { - // console.log('CHANGE', reloadTrigger); cacheClean(reloadTrigger); for (const item of getAsArray(reloadTrigger)) { - const itemString = stableStringify(item); + const itemString = stableStringify(transformApiArgsInv(item)); if (subscriptionsByReloadTrigger[itemString]) { for (const handler of subscriptionsByReloadTrigger[itemString]) { handler(); @@ -126,4 +126,13 @@ export function dispatchCacheChange(reloadTrigger) { } } +export function batchDispatchCacheTriggers(predicate) { + for (const key in subscriptionsByReloadTrigger) { + const relaodTrigger = JSON.parse(key); + if (predicate(relaodTrigger)) { + dispatchCacheChange(relaodTrigger); + } + } +} + apiOn('changed-cache', reloadTrigger => dispatchCacheChange(reloadTrigger)); diff --git a/packages/web/src/utility/connectionsPinger.js b/packages/web/src/utility/connectionsPinger.js index 426a6a9f..f22ee3d2 100644 --- a/packages/web/src/utility/connectionsPinger.js +++ b/packages/web/src/utility/connectionsPinger.js @@ -29,12 +29,12 @@ export function subscribeConnectionPingers() { openedConnections.subscribe(value => { doServerPing(value); if (openedConnectionsHandle) window.clearInterval(openedConnectionsHandle); - openedConnectionsHandle = window.setInterval(() => doServerPing(value), 30 * 1000); + openedConnectionsHandle = window.setInterval(() => doServerPing(value), 20 * 1000); }); currentDatabase.subscribe(value => { doDatabasePing(value); if (currentDatabaseHandle) window.clearInterval(currentDatabaseHandle); - currentDatabaseHandle = window.setInterval(() => doDatabasePing(value), 30 * 1000); + currentDatabaseHandle = window.setInterval(() => doDatabasePing(value), 20 * 1000); }); }