Skip to content

Commit

Permalink
Fix AppState synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
weltenwort committed Dec 15, 2016
1 parent 72e7faf commit 366962a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
49 changes: 32 additions & 17 deletions src/core_plugins/kibana/public/context/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
} from './dispatch';
import {
QueryParameterActionCreatorsProvider,
QUERY_PARAMETER_KEYS,
selectPredecessorCount,
selectQueryParameters,
selectSuccessorCount,
updateQueryParameters,
} from './query_parameters';
Expand Down Expand Up @@ -41,25 +43,20 @@ module.directive('contextApp', function ContextApp() {
anchorUid: '=',
columns: '=',
indexPattern: '=',
size: '=',
predecessorCount: '=',
successorCount: '=',
sort: '=',
},
template: contextAppTemplate,
};
});

function ContextAppController(Private) {
function ContextAppController($scope, Private) {
const createDispatch = Private(createDispatchProvider);
const queryParameterActionCreators = Private(QueryParameterActionCreatorsProvider);
const queryActionCreators = Private(QueryActionCreatorsProvider);

this.state = createInitialState(
this.anchorUid,
this.columns,
this.indexPattern,
this.size,
this.sort,
);
this.state = createInitialState();

this.update = createPipeline(
createScopedUpdater('queryParameters', updateQueryParameters),
Expand Down Expand Up @@ -91,18 +88,36 @@ function ContextAppController(Private) {
),
};

this.actions.fetchAllRows();
/**
* Sync query parameters to arguments
*/
$scope.$watchCollection(
() => _.pick(this, QUERY_PARAMETER_KEYS),
(newValues) => {
// break the watch cycle
if (!_.isEqual(newValues, selectQueryParameters(this.state))) {
this.dispatch(queryActionCreators.fetchAllRowsWithNewQueryParameters(newValues));
}
},
);

$scope.$watchCollection(
() => selectQueryParameters(this.state),
(newValues) => {
_.assign(this, newValues);
},
);
}

function createInitialState(anchorUid, columns, indexPattern, size, sort) {
function createInitialState() {
return {
queryParameters: {
anchorUid,
columns,
indexPattern,
predecessorCount: size,
successorCount: size,
sort,
anchorUid: null,
columns: [],
indexPattern: null,
predecessorCount: 0,
successorCount: 0,
sort: [],
},
rows: {
anchor: null,
Expand Down
9 changes: 6 additions & 3 deletions src/core_plugins/kibana/public/context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ uiRoutes
anchor-uid="contextAppRoute.anchorUid"
columns="contextAppRoute.state.columns"
index-pattern="contextAppRoute.indexPattern"
size="contextAppRoute.state.size"
predecessor-count="contextAppRoute.state.predecessorCount"
successor-count="contextAppRoute.state.successorCount"
sort="[contextAppRoute.indexPattern.timeFieldName, 'desc']"
>`
),
Expand All @@ -30,7 +31,8 @@ function ContextAppRouteController($routeParams, $scope, AppState, config, index

$scope.$watchGroup([
'contextAppRoute.state.columns',
'contextAppRoute.state.size',
'contextAppRoute.state.predecessorCount',
'contextAppRoute.state.successorCount',
], () => this.state.save());
this.anchorUid = getDocumentUid($routeParams.type, $routeParams.id);
this.indexPattern = indexPattern;
Expand All @@ -39,6 +41,7 @@ function ContextAppRouteController($routeParams, $scope, AppState, config, index
function createDefaultAppState(config) {
return {
columns: ['_source'],
size: parseInt(config.get('context:defaultSize'), 10),
predecessorCount: parseInt(config.get('context:defaultSize'), 10),
successorCount: parseInt(config.get('context:defaultSize'), 10),
};
}
9 changes: 9 additions & 0 deletions src/core_plugins/kibana/public/context/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ function QueryActionCreatorsProvider($q, es, Private) {
increasePredecessorCount,
increaseSuccessorCount,
setPredecessorCount,
setQueryParameters,
setSuccessorCount,
} = Private(QueryParameterActionCreatorsProvider);

return {
fetchAllRows,
fetchAllRowsWithNewQueryParameters,
fetchAnchorRow,
fetchGivenPredecessorRows,
fetchGivenSuccessorRows,
Expand All @@ -40,6 +42,13 @@ function QueryActionCreatorsProvider($q, es, Private) {
});
}

function fetchAllRowsWithNewQueryParameters(queryParameters) {
return (dispatch) => {
dispatch(setQueryParameters(queryParameters));
return dispatch(fetchAllRows());
};
}

function fetchAnchorRow() {
return (dispatch, getState) => {
const { queryParameters: { indexPattern, anchorUid, sort } } = getState();
Expand Down
27 changes: 27 additions & 0 deletions src/core_plugins/kibana/public/context/query_parameters.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import _ from 'lodash';


const MIN_CONTEXT_SIZE = 0;
const QUERY_PARAMETER_KEYS = [
'anchorUid',
'columns',
'indexPattern',
'predecessorCount',
'successorCount',
'sort',
];

function QueryParameterActionCreatorsProvider(config) {
const defaultSizeStep = parseInt(config.get('context:step'), 10);
Expand All @@ -7,6 +18,7 @@ function QueryParameterActionCreatorsProvider(config) {
increasePredecessorCount,
increaseSuccessorCount,
setPredecessorCount,
setQueryParameters,
setSuccessorCount,
};

Expand Down Expand Up @@ -37,12 +49,23 @@ function QueryParameterActionCreatorsProvider(config) {
payload: value,
};
}

function setQueryParameters(queryParameters) {
return {
type: 'context/set_query_parameters',
payload: queryParameters,
};
}
}

function selectPredecessorCount(state) {
return state.queryParameters.predecessorCount;
}

function selectQueryParameters(state) {
return state.queryParameters;
}

function selectSuccessorCount(state) {
return state.queryParameters.successorCount;
}
Expand All @@ -57,6 +80,8 @@ function updateQueryParameters(state, action) {
return { ...state, predecessorCount: Math.max(MIN_CONTEXT_SIZE, action.payload) };
case 'context/set_successor_count':
return { ...state, successorCount: Math.max(MIN_CONTEXT_SIZE, action.payload) };
case 'context/set_query_parameters':
return { ...state, ...(_.pick(action.payload, QUERY_PARAMETER_KEYS)) };
default:
return state;
}
Expand All @@ -65,7 +90,9 @@ function updateQueryParameters(state, action) {

export {
QueryParameterActionCreatorsProvider,
QUERY_PARAMETER_KEYS,
selectPredecessorCount,
selectQueryParameters,
selectSuccessorCount,
updateQueryParameters,
};

0 comments on commit 366962a

Please sign in to comment.