Skip to content

Commit

Permalink
Simplify loading data #490
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Oct 16, 2024
1 parent 4bf054c commit b5ebe6b
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/StacBrowser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default {
// A better way would be to combine the language code and URL as the index in the browser database
// This needs a database refactor though: https://github.com/radiantearth/stac-browser/issues/231
this.$store.commit('resetCatalog', true);
await this.$store.dispatch("load", { url, loadApi: true, show: true });
await this.$store.dispatch("load", { url, show: true });
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/components/StacHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ export default {
if (this.url) {
this.addAction(() => this.$store.dispatch("load", {
url: this.url,
loadApi: true,
show: true,
force: true,
noRetry: true
Expand Down
4 changes: 2 additions & 2 deletions src/components/Tree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export default {
if (this.expanded && !this.pagination) {
this.loading = true;
let url = this.item instanceof STAC ? this.item.getAbsoluteUrl() : this.item.href;
await this.$store.dispatch("load", { url, loadApi: true });
await this.$store.dispatch("load", { url });
this.loading = false;
}
}
Expand Down Expand Up @@ -251,4 +251,4 @@ export default {
margin-left: 1.5em;
}
}
</style>
</style>
157 changes: 82 additions & 75 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,9 @@ function getStore(config, router) {
Vue.delete(state.stateQueryParameters[type], idx);
}
},
updateLoading(state, { url, show, loadApi }) {
updateLoading(state, { url, show }) {
let data = state.database[url];
Vue.set(data, 'show', show || data.show);
Vue.set(data, 'loadApi', loadApi || data.loadApi);
},
loading(state, { url, loading }) {
Vue.set(state.database, url, loading);
Expand Down Expand Up @@ -686,7 +685,7 @@ function getStore(config, router) {
break;
}
let url = Utils.toAbsolute(parentLink.href, stac.getAbsoluteUrl());
await cx.dispatch('load', { url, loadApi: true });
await cx.dispatch('load', { url });
let parentStac = cx.getters.getStac(url, true);
if (parentStac instanceof Error) {
cx.commit('parents', parentStac);
Expand All @@ -709,102 +708,110 @@ function getStore(config, router) {
await cx.dispatch('auth/requestLogin');
},
async load(cx, args) {
let { url, show, loadApi, loadRoot, force, noRetry } = args;
let { url, show, force, noRetry } = args;

let path = cx.getters.toBrowserPath(url);
const path = cx.getters.toBrowserPath(url);
url = Utils.toAbsolute(url, cx.state.url);

// Make sure we have all authentication details
await cx.dispatch("auth/waitForAuth");

// Load the root catalog data if not available (e.g. after page refresh or external access)
if (!loadRoot && path !== '/' && cx.state.catalogUrl && !cx.getters.getStac(cx.state.catalogUrl)) {
await cx.dispatch("load", { url: cx.state.catalogUrl, loadApi: true, loadRoot: true });
}

if (force) {
cx.commit('clear', url);
}

let loading = new Loading(show, loadApi);
let loading = new Loading(show);
let data = cx.state.database[url];
if (data instanceof Loading) {
cx.commit('updateLoading', { url, show, loadApi });
cx.commit('updateLoading', { url, show });
return;
}
else if (data instanceof STAC && !data.isPotentiallyIncomplete()) {
if (show) {
cx.commit('showPage', { url });
}
return;
}
else if (!data || data instanceof Error || (data instanceof STAC && data.isPotentiallyIncomplete())) {
cx.commit('loading', { url, loading });
try {
let response = await stacRequest(cx, url);
if (!Utils.isObject(response.data)) {
throw new BrowserError(i18n.t('errors.invalidJsonObject'));
}
data = new STAC(response.data, url, path);
cx.commit('loaded', { url, data });

if (show) {
// If we prefer another language abort redirect to the new language
let localeLink = data.getLocaleLink(cx.state.dataLanguage);
if (localeLink) {
router.replace(cx.getters.toBrowserPath(localeLink.href));
return;
}
cx.commit('loading', { url, loading });
try {
const response = await stacRequest(cx, url);
if (!Utils.isObject(response.data)) {
throw new BrowserError(i18n.t('errors.invalidJsonObject'));
}
data = new STAC(response.data, url, path);
cx.commit('loaded', { url, data });

if (show) {
// If we prefer another language abort redirect to the new language
let localeLink = data.getLocaleLink(cx.state.dataLanguage);
if (localeLink) {
router.replace(cx.getters.toBrowserPath(localeLink.href));
return;
}
}

if (!cx.getters.root) {
let root = data.getLinkWithRel('root');
if (root) {
await cx.dispatch('config', { catalogUrl: Utils.toAbsolute(root.href, url) });
}
}
// Handle conformance classes
let conformanceLink = data.getStacLinkWithRel('conformance');
if (Array.isArray(data.conformsTo) && data.conformsTo.length > 0) {
cx.commit('setConformanceClasses', data.conformsTo);
}
else if (conformanceLink) {
await cx.dispatch('loadOgcApiConformance', conformanceLink);
}
} catch (error) {
if (!noRetry && cx.state.authConfig && isAuthenticationError(error)) {
await cx.dispatch('tryLogin', {
url,
action: () => cx.dispatch('load', Object.assign({noRetry: true, force: true, show: true}, args))
});
return;
}
console.error(error);
cx.commit('errored', { url, error });
}

let conformanceLink = data.getStacLinkWithRel('conformance');
if (Array.isArray(data.conformsTo) && data.conformsTo.length > 0) {
cx.commit('setConformanceClasses', data.conformsTo);
}
else if (conformanceLink) {
await cx.dispatch('loadOgcApiConformance', conformanceLink);
}
// Load API Collections
if (data.getApiCollectionsLink()) {
let args = { stac: data, show: loading.show };
try {
await cx.dispatch('loadNextApiCollections', args);
} catch (error) {
if (!noRetry && cx.state.authConfig && isAuthenticationError(error)) {
await cx.dispatch('tryLogin', {
url,
action: () => cx.dispatch('load', Object.assign({noRetry: true, force: true, show: true}, args))
});
return;
}
console.error(error);
cx.commit('errored', { url, error });
}
}

if (loading.loadApi && data instanceof STAC) {
// Load API Collections
if (data.getApiCollectionsLink()) {
let args = { stac: data, show: loading.show };
try {
await cx.dispatch('loadNextApiCollections', args);
} catch (error) {
cx.commit('showGlobalError', {
message: i18n.t('errors.loadApiCollectionsFailed'),
error
});
}
cx.commit('showGlobalError', {
message: i18n.t('errors.loadApiCollectionsFailed'),
error
});
}
}
// Load API Items
if (data.getApiItemsLink()) {
let args = { stac: data, show: loading.show };
try {
await cx.dispatch('loadApiItems', args);
} catch (error) {
cx.commit('showGlobalError', {
message: i18n.t('errors.loadApiItemsFailed'),
error
});
}
// Load API Items
if (data.getApiItemsLink()) {
let args = { stac: data, show: loading.show };
try {
await cx.dispatch('loadApiItems', args);
} catch (error) {
cx.commit('showGlobalError', {
message: i18n.t('errors.loadApiItemsFailed'),
error
});
}

// Load the root catalog data if not available (e.g. after page refresh or external access)
if (!cx.getters.root) {
let catalogUrl = cx.state.catalogUrl;
if (!catalogUrl) {
const root = data.getLinkWithRel('root');
if (root) {
catalogUrl = Utils.toAbsolute(root.href, url);
await cx.dispatch('config', { catalogUrl });
}
}
if (catalogUrl) {
await cx.dispatch("load", { url: catalogUrl });
}
}

// All tasks finished, show the page if requested
if (loading.show) {
cx.commit('showPage', { url });
}
Expand Down
3 changes: 1 addition & 2 deletions src/store/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import Utils from "../utils";

export class Loading {

constructor(show = false, loadApi = false) {
constructor(show = false) {
this.show = Boolean(show);
this.loadApi = Boolean(loadApi);
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/views/BrowseMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default {
}

let url = this.fromBrowserPath(path || '/');
this.$store.dispatch("load", { url, show: true, loadApi: true });
this.$store.dispatch("load", { url, show: true });
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/views/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export default {
this.parent = this.root;
}
if (!this.parent) {
await this.$store.dispatch('load', { url, loadApi: true });
await this.$store.dispatch('load', { url });
if (!this.root) {
await this.$store.dispatch("config", { catalogUrl: url });
}
Expand Down

0 comments on commit b5ebe6b

Please sign in to comment.