lint common
This commit is contained in:
@@ -10,7 +10,9 @@ export function asyncResourceFactory () {
|
||||
};
|
||||
}
|
||||
|
||||
export function loadAsyncResource ({store, path, url, deserialize, forceLoad = false}) {
|
||||
export function loadAsyncResource ({
|
||||
store, path, url, deserialize, forceLoad = false,
|
||||
}) {
|
||||
if (!store) throw new Error('"store" is required and must be the application store.');
|
||||
if (!path) throw new Error('The path to the resource in the application state is required.');
|
||||
if (!url) throw new Error('The resource\'s url on the server is required.');
|
||||
@@ -18,22 +20,21 @@ export function loadAsyncResource ({store, path, url, deserialize, forceLoad = f
|
||||
|
||||
const resource = get(store.state, path);
|
||||
if (!resource) throw new Error(`No resouce found at path "${path}".`);
|
||||
const loadingStatus = resource.loadingStatus;
|
||||
const { loadingStatus } = resource;
|
||||
|
||||
if (loadingStatus === 'LOADED' && !forceLoad) {
|
||||
return Promise.resolve(resource);
|
||||
} else if (loadingStatus === 'LOADING') {
|
||||
} if (loadingStatus === 'LOADING') {
|
||||
return new Promise((resolve, reject) => {
|
||||
const resourceWatcher = store.watch(state => get(state, `${path}.loadingStatus`), (newLoadingStatus) => {
|
||||
const resourceWatcher = store.watch(state => get(state, `${path}.loadingStatus`), newLoadingStatus => {
|
||||
resourceWatcher(); // remove the watcher
|
||||
if (newLoadingStatus === 'LOADED') {
|
||||
return resolve(resource);
|
||||
} else {
|
||||
return reject(); // TODO add reason?
|
||||
}
|
||||
return reject(); // TODO add reason?
|
||||
});
|
||||
});
|
||||
} else if (loadingStatus === 'NOT_LOADED' || forceLoad) {
|
||||
} if (loadingStatus === 'NOT_LOADED' || forceLoad) {
|
||||
return axios.get(url).then(response => { // TODO support more params
|
||||
resource.loadingStatus = 'LOADED';
|
||||
// deserialize can be a promise
|
||||
@@ -42,7 +43,6 @@ export function loadAsyncResource ({store, path, url, deserialize, forceLoad = f
|
||||
return resource;
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return Promise.reject(new Error(`Invalid loading status "${loadingStatus} for resource at "${path}".`));
|
||||
}
|
||||
return Promise.reject(new Error(`Invalid loading status "${loadingStatus} for resource at "${path}".`));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user