From 5a1e10dee0c6459393a428f3be14c43e49aa5a08 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 6 May 2018 17:35:22 -0700 Subject: [PATCH] refactor some stuff (#274) --- .../_store/computations/autosuggestComputations.js | 13 ++++++++----- routes/_store/computations/timelineComputations.js | 9 +++++---- routes/_store/mixins/timelineMixins.js | 3 ++- routes/_store/observers/autosuggestObservers.js | 9 ++++----- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/routes/_store/computations/autosuggestComputations.js b/routes/_store/computations/autosuggestComputations.js index d1b2910..7fd719e 100644 --- a/routes/_store/computations/autosuggestComputations.js +++ b/routes/_store/computations/autosuggestComputations.js @@ -1,3 +1,5 @@ +import get from 'lodash-es/get' + const MIN_PREFIX_LENGTH = 1 const ACCOUNT_SEARCH_REGEX = new RegExp(`(?:\\s|^)(@\\S{${MIN_PREFIX_LENGTH},})$`) const EMOJI_SEARCH_REGEX = new RegExp(`(?:\\s|^)(:[^:]{${MIN_PREFIX_LENGTH},})$`) @@ -5,10 +7,10 @@ const EMOJI_SEARCH_REGEX = new RegExp(`(?:\\s|^)(:[^:]{${MIN_PREFIX_LENGTH},})$` function computeForAutosuggest (store, key, defaultValue) { store.compute(key, ['currentInstance', 'currentComposeRealm', `autosuggestData_${key}`], - (currentInstance, currentComposeRealm, root) => { - let instanceData = root && root[currentInstance] - return (currentComposeRealm && instanceData && currentComposeRealm in instanceData) ? instanceData[currentComposeRealm] : defaultValue - }) + (currentInstance, currentComposeRealm, root) => ( + get(root, [currentInstance, currentComposeRealm], defaultValue) + ) + ) } export function autosuggestComputations (store) { @@ -22,7 +24,8 @@ export function autosuggestComputations (store) { 'currentComposeText', ['currentComposeData', 'currentComposeRealm'], (currentComposeData, currentComposeRealm) => ( - currentComposeData[currentComposeRealm] && currentComposeData[currentComposeRealm].text) || '' + get(currentComposeData, [currentComposeRealm, 'text'], '') + ) ) store.compute( diff --git a/routes/_store/computations/timelineComputations.js b/routes/_store/computations/timelineComputations.js index 3edf635..0194308 100644 --- a/routes/_store/computations/timelineComputations.js +++ b/routes/_store/computations/timelineComputations.js @@ -1,11 +1,12 @@ +import get from 'lodash-es/get' function computeForTimeline (store, key, defaultValue) { store.compute(key, ['currentInstance', 'currentTimeline', `timelineData_${key}`], - (currentInstance, currentTimeline, root) => { - let instanceData = root && root[currentInstance] - return (currentTimeline && instanceData && currentTimeline in instanceData) ? instanceData[currentTimeline] : defaultValue - }) + (currentInstance, currentTimeline, root) => ( + get(root, [currentInstance, currentTimeline], defaultValue) + ) + ) } export function timelineComputations (store) { diff --git a/routes/_store/mixins/timelineMixins.js b/routes/_store/mixins/timelineMixins.js index 75fc809..159283d 100644 --- a/routes/_store/mixins/timelineMixins.js +++ b/routes/_store/mixins/timelineMixins.js @@ -1,4 +1,5 @@ import pickBy from 'lodash-es/pickBy' +import get from 'lodash-es/get' export function timelineMixins (Store) { Store.prototype.setForTimeline = function (instanceName, timelineName, obj) { @@ -17,7 +18,7 @@ export function timelineMixins (Store) { Store.prototype.getForTimeline = function (instanceName, timelineName, key) { let rootKey = `timelineData_${key}` let root = this.get()[rootKey] - return root && root[instanceName] && root[instanceName][timelineName] + return get(root, [instanceName, timelineName]) } Store.prototype.getForCurrentTimeline = function (key) { diff --git a/routes/_store/observers/autosuggestObservers.js b/routes/_store/observers/autosuggestObservers.js index 1598974..b272927 100644 --- a/routes/_store/observers/autosuggestObservers.js +++ b/routes/_store/observers/autosuggestObservers.js @@ -28,15 +28,14 @@ export function autosuggestObservers (store) { if (!composeFocused || !autosuggestSearchText) { return } - let type = autosuggestSearchText.startsWith('@') ? 'account' : 'emoji' - let results = (type === 'account') + let autosuggestType = autosuggestSearchText.startsWith('@') ? 'account' : 'emoji' + let results = (autosuggestType === 'account') ? await searchAccounts(store, autosuggestSearchText) : await searchEmoji(store, autosuggestSearchText) store.setForCurrentAutosuggest({ + autosuggestType, autosuggestSelected: 0, - autosuggestSearchText: autosuggestSearchText, - autosuggestSearchResults: results, - autosuggestType: type + autosuggestSearchResults: results }) }) }