Remove cache store

This commit is contained in:
Jori Lallo
2017-04-29 14:08:15 -07:00
parent 70dad7aa47
commit 4907bd5d75
6 changed files with 6 additions and 77 deletions

View File

@ -1,5 +1,5 @@
import { observable, action, runInAction } from 'mobx';
import { client, cacheResponse } from 'utils/ApiClient';
import { client } from 'utils/ApiClient';
class DashboardStore {
@observable collections;
@ -18,7 +18,6 @@ class DashboardStore {
runInAction('fetchCollections', () => {
this.collections = data;
this.pagination = pagination;
data.forEach(collection => cacheResponse(collection.recentDocuments));
});
// If only one collection, visit it automatically

View File

@ -20,12 +20,11 @@ import styles from './DocumentScene.scss';
// const cx = classNames.bind(styles);
@keydown(['cmd+/', 'ctrl+/', 'c', 'e'])
@inject('ui', 'cache')
@inject('ui')
@observer
class DocumentScene extends React.Component {
static propTypes = {
ui: PropTypes.object.isRequired,
cache: PropTypes.object.isRequired,
routeParams: PropTypes.object,
params: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
@ -34,10 +33,7 @@ class DocumentScene extends React.Component {
constructor(props) {
super(props);
this.store = new DocumentSceneStore(
JSON.parse(localStorage[DOCUMENT_PREFERENCES] || '{}'),
{
cache: this.props.cache,
}
JSON.parse(localStorage[DOCUMENT_PREFERENCES] || '{}')
);
}

View File

@ -13,8 +13,6 @@ import { browserHistory } from 'react-router';
const DOCUMENT_PREFERENCES = 'DOCUMENT_PREFERENCES';
class DocumentSceneStore {
static cache;
@observable document;
@observable collapsedNodes = [];
@ -53,18 +51,12 @@ class DocumentSceneStore {
replaceUrl: true,
...options,
};
let cacheHit = false;
runInAction('retrieve document from cache', () => {
const cachedValue = this.cache.fetchFromCache(id);
cacheHit = !!cachedValue;
if (cacheHit) this.document = cachedValue;
});
this.isFetching = !options.softLoad;
this.updatingContent = options.softLoad && !cacheHit;
this.updatingContent = true;
try {
const res = await client.get('/documents.info', { id }, { cache: true });
const res = await client.get('/documents.info', { id });
const { data } = res;
runInAction('fetchDocument', () => {
this.document = data;
@ -131,7 +123,6 @@ class DocumentSceneStore {
constructor(settings, options) {
// Rehydrate settings
this.collapsedNodes = settings.collapsedNodes || [];
this.cache = options.cache;
// Persist settings to localStorage
// TODO: This could be done more selectively

View File

@ -1,40 +0,0 @@
import _ from 'lodash';
import { action, toJS } from 'mobx';
const CACHE_STORE = 'CACHE_STORE';
class CacheStore {
cache = {};
/* Computed */
get asJson() {
return JSON.stringify({
cache: this.cache,
});
}
/* Actions */
@action cacheWithId = (id, data) => {
this.cache[id] = toJS(data);
_.defer(() => localStorage.setItem(CACHE_STORE, this.asJson));
};
@action cacheList = data => {
data.forEach(item => this.cacheWithId(item.id, item));
};
@action fetchFromCache = id => {
return this.cache[id];
};
constructor() {
// Rehydrate
const data = JSON.parse(localStorage.getItem(CACHE_STORE) || '{}');
this.cache = data.cache || {};
}
}
export default CacheStore;
export { CACHE_STORE };

View File

@ -1,12 +1,10 @@
import UserStore, { USER_STORE } from './UserStore';
import UiStore, { UI_STORE } from './UiStore';
import CacheStore from './CacheStore';
import { autorunAsync } from 'mobx';
const stores = {
user: new UserStore(),
ui: new UiStore(),
cache: new CacheStore(),
};
// Persist stores to localStorage

View File

@ -3,17 +3,6 @@ import { browserHistory } from 'react-router';
import constants from '../constants';
import stores from 'stores';
const isIterable = object =>
object != null && typeof object[Symbol.iterator] === 'function';
const cacheResponse = data => {
if (isIterable(data)) {
stores.cache.cacheList(data);
} else {
stores.cache.cacheWithId(data.id, data);
}
};
class ApiClient {
constructor(options = {}) {
this.baseUrl = options.baseUrl || constants.API_BASE_URL;
@ -76,10 +65,6 @@ class ApiClient {
return response.json();
})
.then(json => {
// Cache responses
if (options.cache) {
cacheResponse(json.data);
}
resolve(json);
})
.catch(error => {
@ -112,4 +97,4 @@ export default ApiClient;
// In case you don't want to always initiate, just import with `import { client } ...`
const client = new ApiClient();
export { client, cacheResponse };
export { client };