Remove cache store
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
import { observable, action, runInAction } from 'mobx';
|
import { observable, action, runInAction } from 'mobx';
|
||||||
import { client, cacheResponse } from 'utils/ApiClient';
|
import { client } from 'utils/ApiClient';
|
||||||
|
|
||||||
class DashboardStore {
|
class DashboardStore {
|
||||||
@observable collections;
|
@observable collections;
|
||||||
@ -18,7 +18,6 @@ class DashboardStore {
|
|||||||
runInAction('fetchCollections', () => {
|
runInAction('fetchCollections', () => {
|
||||||
this.collections = data;
|
this.collections = data;
|
||||||
this.pagination = pagination;
|
this.pagination = pagination;
|
||||||
data.forEach(collection => cacheResponse(collection.recentDocuments));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// If only one collection, visit it automatically
|
// If only one collection, visit it automatically
|
||||||
|
@ -20,12 +20,11 @@ import styles from './DocumentScene.scss';
|
|||||||
// const cx = classNames.bind(styles);
|
// const cx = classNames.bind(styles);
|
||||||
|
|
||||||
@keydown(['cmd+/', 'ctrl+/', 'c', 'e'])
|
@keydown(['cmd+/', 'ctrl+/', 'c', 'e'])
|
||||||
@inject('ui', 'cache')
|
@inject('ui')
|
||||||
@observer
|
@observer
|
||||||
class DocumentScene extends React.Component {
|
class DocumentScene extends React.Component {
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
ui: PropTypes.object.isRequired,
|
ui: PropTypes.object.isRequired,
|
||||||
cache: PropTypes.object.isRequired,
|
|
||||||
routeParams: PropTypes.object,
|
routeParams: PropTypes.object,
|
||||||
params: PropTypes.object.isRequired,
|
params: PropTypes.object.isRequired,
|
||||||
location: PropTypes.object.isRequired,
|
location: PropTypes.object.isRequired,
|
||||||
@ -34,10 +33,7 @@ class DocumentScene extends React.Component {
|
|||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.store = new DocumentSceneStore(
|
this.store = new DocumentSceneStore(
|
||||||
JSON.parse(localStorage[DOCUMENT_PREFERENCES] || '{}'),
|
JSON.parse(localStorage[DOCUMENT_PREFERENCES] || '{}')
|
||||||
{
|
|
||||||
cache: this.props.cache,
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,8 +13,6 @@ import { browserHistory } from 'react-router';
|
|||||||
const DOCUMENT_PREFERENCES = 'DOCUMENT_PREFERENCES';
|
const DOCUMENT_PREFERENCES = 'DOCUMENT_PREFERENCES';
|
||||||
|
|
||||||
class DocumentSceneStore {
|
class DocumentSceneStore {
|
||||||
static cache;
|
|
||||||
|
|
||||||
@observable document;
|
@observable document;
|
||||||
@observable collapsedNodes = [];
|
@observable collapsedNodes = [];
|
||||||
|
|
||||||
@ -53,18 +51,12 @@ class DocumentSceneStore {
|
|||||||
replaceUrl: true,
|
replaceUrl: true,
|
||||||
...options,
|
...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.isFetching = !options.softLoad;
|
||||||
this.updatingContent = options.softLoad && !cacheHit;
|
this.updatingContent = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await client.get('/documents.info', { id }, { cache: true });
|
const res = await client.get('/documents.info', { id });
|
||||||
const { data } = res;
|
const { data } = res;
|
||||||
runInAction('fetchDocument', () => {
|
runInAction('fetchDocument', () => {
|
||||||
this.document = data;
|
this.document = data;
|
||||||
@ -131,7 +123,6 @@ class DocumentSceneStore {
|
|||||||
constructor(settings, options) {
|
constructor(settings, options) {
|
||||||
// Rehydrate settings
|
// Rehydrate settings
|
||||||
this.collapsedNodes = settings.collapsedNodes || [];
|
this.collapsedNodes = settings.collapsedNodes || [];
|
||||||
this.cache = options.cache;
|
|
||||||
|
|
||||||
// Persist settings to localStorage
|
// Persist settings to localStorage
|
||||||
// TODO: This could be done more selectively
|
// TODO: This could be done more selectively
|
||||||
|
@ -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 };
|
|
@ -1,12 +1,10 @@
|
|||||||
import UserStore, { USER_STORE } from './UserStore';
|
import UserStore, { USER_STORE } from './UserStore';
|
||||||
import UiStore, { UI_STORE } from './UiStore';
|
import UiStore, { UI_STORE } from './UiStore';
|
||||||
import CacheStore from './CacheStore';
|
|
||||||
import { autorunAsync } from 'mobx';
|
import { autorunAsync } from 'mobx';
|
||||||
|
|
||||||
const stores = {
|
const stores = {
|
||||||
user: new UserStore(),
|
user: new UserStore(),
|
||||||
ui: new UiStore(),
|
ui: new UiStore(),
|
||||||
cache: new CacheStore(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Persist stores to localStorage
|
// Persist stores to localStorage
|
||||||
|
@ -3,17 +3,6 @@ import { browserHistory } from 'react-router';
|
|||||||
import constants from '../constants';
|
import constants from '../constants';
|
||||||
import stores from 'stores';
|
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 {
|
class ApiClient {
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
this.baseUrl = options.baseUrl || constants.API_BASE_URL;
|
this.baseUrl = options.baseUrl || constants.API_BASE_URL;
|
||||||
@ -76,10 +65,6 @@ class ApiClient {
|
|||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then(json => {
|
.then(json => {
|
||||||
// Cache responses
|
|
||||||
if (options.cache) {
|
|
||||||
cacheResponse(json.data);
|
|
||||||
}
|
|
||||||
resolve(json);
|
resolve(json);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
@ -112,4 +97,4 @@ export default ApiClient;
|
|||||||
|
|
||||||
// In case you don't want to always initiate, just import with `import { client } ...`
|
// In case you don't want to always initiate, just import with `import { client } ...`
|
||||||
const client = new ApiClient();
|
const client = new ApiClient();
|
||||||
export { client, cacheResponse };
|
export { client };
|
||||||
|
Reference in New Issue
Block a user