diff --git a/src/components/pages/Messages.jsx b/src/components/pages/Messages.jsx index 046f5079..fba08ba0 100644 --- a/src/components/pages/Messages.jsx +++ b/src/components/pages/Messages.jsx @@ -48,7 +48,6 @@ class Messages extends React.Component { searchContacts: null, notifyErrors: 0, }; - this.preDecoded = {}; this.cachedProfileImages = {}; this.windowFocused = true; this.newMessages = 0; @@ -307,9 +306,9 @@ class Messages extends React.Component { const updateData = async () => { const newContacts = contacts.size ? - await normalizeContacts(contacts, accounts, currentUser, this.preDecoded, this.cachedProfileImages) : + await normalizeContacts(contacts, accounts, currentUser, this.cachedProfileImages) : this.state.contacts - const decoded = await normalizeMessages(messages, accounts, currentUser, prevProps.to, this.preDecoded) + const decoded = await normalizeMessages(messages, accounts, currentUser, prevProps.to) this.setState({ contacts: newContacts, messages: decoded, diff --git a/src/redux/FetchDataSaga.js b/src/redux/FetchDataSaga.js index 9fa6af7c..bf1a1166 100644 --- a/src/redux/FetchDataSaga.js +++ b/src/redux/FetchDataSaga.js @@ -3,6 +3,7 @@ import golos, { api, auth } from 'golos-lib-js' import tt from 'counterpart' import g from 'app/redux/GlobalReducer' +import { getSpaceInCache, saveToCache } from 'app/utils/Normalizators' export function* fetchDataWatches () { yield fork(watchLocationChange) @@ -63,15 +64,18 @@ export function* fetchState(location_change_action) { const posting = yield select(state => state.user.getIn(['current', 'private_keys', 'posting_private'])) console.time('prof: getContactsAsync') + const conCache = getSpaceInCache(null, 'contacts') const con = yield call([auth, 'withNodeLogin'], { account, keys: { posting }, call: async (loginData) => { return await api.getContactsAsync({ ...loginData, owner: account, limit: 100, + cache: Object.keys(conCache), }) } }) //alert(JSON.stringify(con)) + console.log('procc:' + con._dec_processed) state.contacts = con.contacts if (hasErr) return console.timeEnd('prof: getContactsAsync') @@ -106,9 +110,11 @@ export function* fetchState(location_change_action) { state.the_group = the_group console.timeEnd('prof: getGroupsAsync') + const space = getSpaceInCache({ group: the_group.name }) console.time('prof: getThreadAsync') let query = { group: path, + cache: Object.keys(space), } const getThread = async (loginData) => { query = {...query, ...loginData} @@ -123,6 +129,7 @@ export function* fetchState(location_change_action) { } else { thRes = yield call(getThread) } + console.log('proc:' + thRes._dec_processed) if (the_group && thRes.error) { the_group.error = thRes.error } diff --git a/src/utils/Normalizators.js b/src/utils/Normalizators.js index 8eeef7aa..7b3694de 100644 --- a/src/utils/Normalizators.js +++ b/src/utils/Normalizators.js @@ -16,10 +16,26 @@ function getProfileImageLazy(account, cachedProfileImages) { return image; } +const getCache = () => { + if (!window.preDecoded) window.preDecoded = {} + return window.preDecoded +} + +export const getSpaceInCache = (msg, spaceKey = '') => { + const preDecoded = getCache() + const key = spaceKey || (msg.group ? msg.group : '') + if (!preDecoded[key]) preDecoded[key] = {} + const space = preDecoded[key] + return space +} + +export const getContactsSpace = (msg) => { + return getSpaceInCache(msg, 'contacts') +} + const cacheKey = (msg) => { let key = [msg.nonce] if (msg.group) { - key.push(msg.group) key.push(msg.receive_date) key.push(msg.from) key.push(msg.to) @@ -30,25 +46,39 @@ const cacheKey = (msg) => { return key } -const saveToCache = (preDecoded, msg) => { +export const saveToCache = (msg, contact = false) => { if (!msg.message) return false if (msg.group && msg.decrypt_date !== msg.receive_date) return false - let key = cacheKey(msg) - preDecoded[key] = { message: msg.message } + const space = getSpaceInCache(msg) + const key = cacheKey(msg) + space[key] = { message: msg.message } + if (contact) { + const cont = getContactsSpace(msg) + cont[key] = { message: msg.message } + } return true } -const loadFromCache = (preDecoded, msg) => { - let key = cacheKey(msg) - let pd = preDecoded[key]; +const loadFromCache = (msg, contact = false) => { + const space = getSpaceInCache(msg) + const key = cacheKey(msg) + const pd = space[key] if (pd) { msg.message = pd.message return true } + if (contact) { + const cont = getContactsSpace(msg) + const pdc = cont[key] + if (pdc) { + msg.message = pdc.message + return true + } + } return false } -export async function normalizeContacts(contacts, accounts, currentUser, preDecoded, cachedProfileImages) { +export async function normalizeContacts(contacts, accounts, currentUser, cachedProfileImages) { if (!currentUser || !accounts) return []; @@ -87,13 +117,13 @@ export async function normalizeContacts(contacts, accounts, currentUser, preDeco } } - if (loadFromCache(preDecoded, msg)) { + if (loadFromCache(msg, true)) { return true } return false; }, for_each: (msg) => { - saveToCache(preDecoded, msg) + saveToCache(msg, true) }, on_error: (msg, idx, exception) => { msg.message = { body: tt_invalid_message, invalid: true, }; @@ -108,7 +138,7 @@ export async function normalizeContacts(contacts, accounts, currentUser, preDeco return contactsCopy } -export async function normalizeMessages(messages, accounts, currentUser, to, preDecoded) { +export async function normalizeMessages(messages, accounts, currentUser, to) { let isGroup = false if (to) { if (to[0] !== '@') isGroup = true @@ -130,7 +160,7 @@ export async function normalizeMessages(messages, accounts, currentUser, to, pre const posting = currentUser.getIn(['private_keys', 'posting_private']) const privateMemo = currentUser.getIn(['private_keys', 'memo_private']); - console.time('dddm') + console.log('ttt', Date.now()) const decoded = await decodeMsgs({ msgs: messagesCopy, private_memo: !isGroup && privateMemo, login: { @@ -154,14 +184,14 @@ export async function normalizeMessages(messages, accounts, currentUser, to, pre } msg.decrypt_date = null - if (loadFromCache(preDecoded, msg)) { + if (loadFromCache(msg)) { results.push(msg) return true } return false; }, for_each: (msg, i) => { - saveToCache(preDecoded, msg) + saveToCache(msg) }, on_error: (msg, i, err) => { console.error(err, msg) @@ -170,7 +200,7 @@ export async function normalizeMessages(messages, accounts, currentUser, to, pre begin_idx: messagesCopy.length - 1, end_idx: -1, }) - console.timeEnd('dddm') + console.log('ttte', Date.now()) return decoded } catch (ex) { console.log(ex);