PromucFlow_constructor/app/client/src/selectors/dataTreeSelectors.ts

46 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-02-18 10:41:52 +00:00
import { AppState } from "reducers";
import { createSelector } from "reselect";
import { getActions } from "./entitiesSelector";
import { ActionDataState } from "reducers/entityReducers/actionsReducer";
import { getEvaluatedDataTree } from "utils/DynamicBindingUtils";
import { extraLibraries } from "jsExecution/JSExecutionManagerSingleton";
import { DataTree, DataTreeFactory } from "entities/DataTree/dataTreeFactory";
import _ from "lodash";
2020-02-18 10:41:52 +00:00
export const getUnevaluatedDataTree = (state: AppState): DataTree =>
DataTreeFactory.create(state);
export const evaluateDataTree = createSelector(
2020-02-18 10:41:52 +00:00
getUnevaluatedDataTree,
(dataTree: DataTree): DataTree => {
return getEvaluatedDataTree(dataTree);
2020-02-18 10:41:52 +00:00
},
);
// For autocomplete. Use actions cached responses if
// there isn't a response already
export const getDataTreeForAutocomplete = createSelector(
evaluateDataTree,
2020-02-18 10:41:52 +00:00
getActions,
(tree: DataTree, actions: ActionDataState) => {
2020-02-18 10:41:52 +00:00
const cachedResponses: Record<string, any> = {};
if (actions && actions.length) {
actions.forEach(action => {
if (!(action.config.name in tree) && action.config.cacheResponse) {
2020-02-18 10:41:52 +00:00
try {
cachedResponses[action.config.name] = JSON.parse(
action.config.cacheResponse,
);
} catch (e) {
cachedResponses[action.config.name] = action.config.cacheResponse;
}
}
});
}
_.omit(tree, ["MainContainer", "actionPaths"]);
2020-02-18 10:41:52 +00:00
const libs: Record<string, any> = {};
2020-02-25 08:50:40 +00:00
extraLibraries.forEach(config => (libs[config.accessor] = config.lib));
return { ...tree, ...cachedResponses, ...libs };
2020-02-18 10:41:52 +00:00
},
);