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

45 lines
1.7 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 createCachedSelector from "re-reselect";
import { getEvaluatedDataTree } from "utils/DynamicBindingUtils";
import { extraLibraries } from "jsExecution/JSExecutionManagerSingleton";
import { DataTree, DataTreeFactory } from "entities/DataTree/dataTreeFactory";
export const getUnevaluatedDataTree = (state: AppState): DataTree =>
DataTreeFactory.create(state);
export const getParsedDataTree = createSelector(
getUnevaluatedDataTree,
(dataTree: DataTree) => {
return getEvaluatedDataTree(dataTree, true);
},
);
// For autocomplete. Use actions cached responses if
// there isn't a response already
export const getDataTreeForAutocomplete = createCachedSelector(
getParsedDataTree,
getActions,
(dataTree: DataTree, actions: ActionDataState) => {
const cachedResponses: Record<string, any> = {};
if (actions && actions.length) {
actions.forEach(action => {
if (!(action.config.name in dataTree) && action.config.cacheResponse) {
try {
cachedResponses[action.config.name] = JSON.parse(
action.config.cacheResponse,
);
} catch (e) {
cachedResponses[action.config.name] = action.config.cacheResponse;
}
}
});
}
const libs: Record<string, any> = {};
2020-02-25 08:50:40 +00:00
extraLibraries.forEach(config => (libs[config.accessor] = config.lib));
2020-02-18 10:41:52 +00:00
return { ...dataTree, ...cachedResponses, ...libs };
},
)((state: AppState) => state.entities.actions.length);