Skip to content

Commit

Permalink
Define getAppState TS correctly, to avoid filter assignment into an u…
Browse files Browse the repository at this point in the history
…ndefined state
  • Loading branch information
Liza Katz committed Jul 3, 2019
1 parent 1e1153e commit bea845d
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import _ from 'lodash';
import { State } from 'ui/state_management/state';
import { FilterManager } from './filter_manager';

type GetAppStateFunc = () => State | undefined | null;

/**
* FilterStateManager is responsible for watching for filter changes
* and syncing with FilterManager, as well as syncing FilterManager changes
Expand All @@ -31,12 +33,12 @@ import { FilterManager } from './filter_manager';
export class FilterStateManager {
filterManager: FilterManager;
globalState: State;
getAppState: () => State;
getAppState: GetAppStateFunc;
prevGlobalFilters: Filter[] | undefined;
prevAppFilters: Filter[] | undefined;
interval: NodeJS.Timeout | undefined;

constructor(globalState: State, getAppState: () => State, filterManager: FilterManager) {
constructor(globalState: State, getAppState: GetAppStateFunc, filterManager: FilterManager) {
this.getAppState = getAppState;
this.globalState = globalState;
this.filterManager = filterManager;
Expand All @@ -63,7 +65,7 @@ export class FilterStateManager {
if (stateUndefined) return;

const globalFilters = this.globalState.filters || [];
const appFilters = appState.filters || [];
const appFilters = (appState && appState.filters) || [];

const globalFilterChanged = !(
this.prevGlobalFilters && _.isEqual(this.prevGlobalFilters, globalFilters)
Expand Down Expand Up @@ -96,7 +98,9 @@ export class FilterStateManager {
// Update Angular state before saving State objects (which save it to URL)
const partitionedFilters = this.filterManager.getPartitionedFilters();
const appState = this.getAppState();
appState.filters = partitionedFilters.appFilters;
if (appState) {
appState.filters = partitionedFilters.appFilters;
}
this.globalState.filters = partitionedFilters.globalFilters;
this.saveState();
}
Expand Down

0 comments on commit bea845d

Please sign in to comment.