Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com>
  • Loading branch information
abbyhu2000 committed Jan 4, 2023
1 parent 78baaf7 commit 896dd66
Showing 1 changed file with 65 additions and 61 deletions.
126 changes: 65 additions & 61 deletions src/plugins/data/public/query/state_sync/connect_to_query_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,77 +60,81 @@ export const connectStorageToQueryState = (
query: boolean;
}
) => {
const syncKeys: Array<keyof QueryStateChange> = [];
if (syncConfig.query) {
syncKeys.push('query');
}
if (syncConfig.filters === FilterStateStore.APP_STATE) {
syncKeys.push('appFilters');
}

const initialStateFromURL: QueryState = OsdUrlStateStorage.get('_q') ?? {
query: queryString.getDefaultQuery(),
filters: filterManager.getAppFilters(),
};
try {
const syncKeys: Array<keyof QueryStateChange> = [];
if (syncConfig.query) {
syncKeys.push('query');
}
if (syncConfig.filters === FilterStateStore.APP_STATE) {
syncKeys.push('appFilters');
}

// set up initial '_q' flag in the URL to sync query and filter changes
if (!OsdUrlStateStorage.get('_q')) {
OsdUrlStateStorage.set('_q', initialStateFromURL, {
replace: true,
});
}
const initialStateFromURL: QueryState = OsdUrlStateStorage.get('_q') ?? {
query: queryString.getDefaultQuery(),
filters: filterManager.getAppFilters(),
};

if (syncConfig.query && !_.isEqual(initialStateFromURL.query, queryString.getQuery())) {
if (initialStateFromURL.query) {
queryString.setQuery(_.cloneDeep(initialStateFromURL.query));
// set up initial '_q' flag in the URL to sync query and filter changes
if (!OsdUrlStateStorage.get('_q')) {
OsdUrlStateStorage.set('_q', initialStateFromURL, {
replace: true,
});
}
}

if (syncConfig.filters === FilterStateStore.APP_STATE) {
if (
!initialStateFromURL.filters ||
!compareFilters(initialStateFromURL.filters, filterManager.getAppFilters(), {
...COMPARE_ALL_OPTIONS,
state: false,
})
) {
if (initialStateFromURL.filters) {
filterManager.setAppFilters(_.cloneDeep(initialStateFromURL.filters));
if (syncConfig.query && !_.isEqual(initialStateFromURL.query, queryString.getQuery())) {
if (initialStateFromURL.query) {
queryString.setQuery(_.cloneDeep(initialStateFromURL.query));
}
}

const subs: Subscription[] = [
state$
.pipe(
filter(({ changes }) => {
return syncKeys.some((syncKey) => changes[syncKey]);
}),
map(({ changes, state }) => {
const newState: QueryState = {
query: state.query,
filters: state.filters,
};
if (syncConfig.query && changes.query) {
newState.query = queryString.getQuery();
}
if (syncConfig.filters === FilterStateStore.APP_STATE) {
if (
!initialStateFromURL.filters ||
!compareFilters(initialStateFromURL.filters, filterManager.getAppFilters(), {
...COMPARE_ALL_OPTIONS,
state: false,
})
) {
if (initialStateFromURL.filters) {
filterManager.setAppFilters(_.cloneDeep(initialStateFromURL.filters));
}
}

if (syncConfig.filters === FilterStateStore.APP_STATE && changes.appFilters) {
newState.filters = filterManager.getAppFilters();
}
const subs: Subscription[] = [
state$
.pipe(
filter(({ changes }) => {
return syncKeys.some((syncKey) => changes[syncKey]);
}),
map(({ changes, state }) => {
const newState: QueryState = {
query: state.query,
filters: state.filters,
};
if (syncConfig.query && changes.query) {
newState.query = queryString.getQuery();
}

return newState;
})
)
.subscribe((newState) => {
OsdUrlStateStorage.set('_q', newState, {
replace: true,
});
}),
];
if (syncConfig.filters === FilterStateStore.APP_STATE && changes.appFilters) {
newState.filters = filterManager.getAppFilters();
}

return () => {
subs.forEach((s) => s.unsubscribe());
};
return newState;
})
)
.subscribe((newState) => {
OsdUrlStateStorage.set('_q', newState, {
replace: true,
});
}),
];

return () => {
subs.forEach((s) => s.unsubscribe());
};
}
} catch (err) {
return;
}
};

Expand Down

0 comments on commit 896dd66

Please sign in to comment.