summaryrefslogtreecommitdiffstats
path: root/webapp/stores
diff options
context:
space:
mode:
authorHarrison Healey <harrisonmhealey@gmail.com>2016-03-23 13:03:12 -0400
committerHarrison Healey <harrisonmhealey@gmail.com>2016-03-23 13:03:12 -0400
commit1f46cca65ca0669c1fc1a6fec5113f835a1a3023 (patch)
tree5d2563bb88d8f71cbd8a50b921358378c04a2c5e /webapp/stores
parent6568e15023c009c96a46f022236f5fd4415445c0 (diff)
downloadchat-1f46cca65ca0669c1fc1a6fec5113f835a1a3023.tar.gz
chat-1f46cca65ca0669c1fc1a6fec5113f835a1a3023.tar.bz2
chat-1f46cca65ca0669c1fc1a6fec5113f835a1a3023.zip
Changed PreferenceStore to store data in memory
Diffstat (limited to 'webapp/stores')
-rw-r--r--webapp/stores/preference_store.jsx113
1 files changed, 25 insertions, 88 deletions
diff --git a/webapp/stores/preference_store.jsx b/webapp/stores/preference_store.jsx
index df77f0d51..fcfd1c426 100644
--- a/webapp/stores/preference_store.jsx
+++ b/webapp/stores/preference_store.jsx
@@ -4,143 +4,80 @@
import Constants from 'utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
-import BrowserStore from './browser_store.jsx';
import EventEmitter from 'events';
-import UserStore from 'stores/user_store.jsx';
const CHANGE_EVENT = 'change';
-function getPreferenceKey(category, name) {
- return `${category}-${name}`;
-}
-
-function getPreferenceKeyForModel(preference) {
- return `${preference.category}-${preference.name}`;
-}
-
class PreferenceStoreClass extends EventEmitter {
constructor() {
super();
- this.getAllPreferences = this.getAllPreferences.bind(this);
- this.get = this.get.bind(this);
- this.getBool = this.getBool.bind(this);
- this.getInt = this.getInt.bind(this);
- this.getPreference = this.getPreference.bind(this);
- this.getCategory = this.getCategory.bind(this);
- this.getPreferencesWhere = this.getPreferencesWhere.bind(this);
- this.setAllPreferences = this.setAllPreferences.bind(this);
- this.setPreference = this.setPreference.bind(this);
-
- this.emitChange = this.emitChange.bind(this);
- this.addChangeListener = this.addChangeListener.bind(this);
- this.removeChangeListener = this.removeChangeListener.bind(this);
-
this.handleEventPayload = this.handleEventPayload.bind(this);
this.dispatchToken = AppDispatcher.register(this.handleEventPayload);
+
+ this.preferences = new Map();
}
- getAllPreferences() {
- return new Map(BrowserStore.getItem('preferences', []));
+ getKey(category, name) {
+ return `${category}--${name}`;
}
get(category, name, defaultValue = '') {
- const preference = this.getAllPreferences().get(getPreferenceKey(category, name));
+ const key = this.getKey(category, name);
- if (!preference) {
+ if (!this.preferences.has(key)) {
return defaultValue;
}
- return preference.value || defaultValue;
+ return this.preferences.get(key);
}
getBool(category, name, defaultValue = false) {
- const preference = this.getAllPreferences().get(getPreferenceKey(category, name));
+ const key = this.getKey(category, name);
- if (!preference) {
+ if (!this.preferences.has(key)) {
return defaultValue;
}
- // prevent a non-false default value from being returned instead of an actual false value
- if (preference.value === 'false') {
- return false;
- }
-
- return (preference.value !== 'false') || defaultValue;
+ return this.preferences.get(key) !== 'false';
}
getInt(category, name, defaultValue = 0) {
- const preference = this.getAllPreferences().get(getPreferenceKey(category, name));
+ const key = this.getKey(category, name);
- if (!preference) {
+ if (!this.preferences.has(key)) {
return defaultValue;
}
- // prevent a non-zero default value from being returned instead of an actual 0 value
- if (preference.value === '0') {
- return 0;
- }
-
- return parseInt(preference.value, 10) || defaultValue;
- }
-
- getPreference(category, name, defaultValue = {}) {
- return this.getAllPreferences().get(getPreferenceKey(category, name)) || defaultValue;
+ return parseInt(this.preferences.get(key), 10);
}
getCategory(category) {
- return this.getPreferencesWhere((preference) => (preference.category === category));
- }
+ const prefix = category + '--';
- getPreferencesWhere(pred) {
- const all = this.getAllPreferences();
- const preferences = [];
+ const preferences = new Map();
- for (const [, preference] of all) {
- if (pred(preference)) {
- preferences.push(preference);
+ for (const [key, value] of this.preferences) {
+ if (key.startsWith(prefix)) {
+ preferences.set(key.substring(prefix.length), value);
}
}
return preferences;
}
- setAllPreferences(preferences) {
- // note that we store the preferences as an array of key-value pairs so that we can deserialize
- // it as a proper Map instead of an object
- BrowserStore.setItem('preferences', [...preferences]);
- }
-
setPreference(category, name, value) {
- const preferences = this.getAllPreferences();
-
- const key = getPreferenceKey(category, name);
- let preference = preferences.get(key);
-
- if (!preference) {
- preference = {
- user_id: UserStore.getCurrentId(),
- category,
- name
- };
- }
- preference.value = value;
-
- preferences.set(key, preference);
-
- this.setAllPreferences(preferences);
-
- return preference;
+ this.preferences.set(this.getKey(category, name), value);
}
- setPreferences(newPreferences) {
- const preferences = this.getAllPreferences();
-
+ setPreferencesFromServer(newPreferences) {
for (const preference of newPreferences) {
- preferences.set(getPreferenceKeyForModel(preference), preference);
+ this.setPreference(preference.category, preference.name, preference.value);
}
+ }
- this.setAllPreferences(preferences);
+ clear() {
+ this.preferences.clear();
}
emitChange() {
@@ -166,7 +103,7 @@ class PreferenceStoreClass extends EventEmitter {
break;
}
case ActionTypes.RECEIVED_PREFERENCES:
- this.setPreferences(action.preferences);
+ this.setPreferencesFromServer(action.preferences);
this.emitChange();
break;
}