bonsai-analyzer
Version:
Trim your dependency tree.
236 lines (205 loc) • 7.62 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = reducer;
exports.INITIAL_STATE = void 0;
var _getEntryChunks = _interopRequireDefault(require("../stats/getEntryChunks"));
var _fullModuleData = _interopRequireDefault(require("../stats/fullModuleData"));
var _getCollapsableParentOf = _interopRequireDefault(require("../stats/getCollapsableParentOf"));
var _getModulesById = _interopRequireDefault(require("../stats/getModulesById"));
var _withTimings = _interopRequireDefault(require("./withTimings"));
const INITIAL_STATE = {
appState: 'empty',
dataPaths: {},
selectedFilename: null,
childrenIndexes: [],
selectedChildIndex: null,
selectedChunkId: null,
blacklistedModuleIds: [],
jsonChildren: {},
sort: {
field: 'cumulativeSize',
direction: 'DESC'
},
filters: {
moduleName: '',
cumulativeSizeMin: '',
cumulativeSizeMax: '',
requiredByCountMin: '',
requiredByCountMax: '',
requirementsCountMin: '',
requirementsCountMax: ''
},
expandMode: 'collapse-all',
expandedRecords: new Set(),
currentlyFocusedElementID: null,
calculatedFullModuleData: null
};
exports.INITIAL_STATE = INITIAL_STATE;
function getDefaultChildIndex(children) {
return children.length === 1 ? 0 : null;
}
function getDefaultChunkId(children, childIndex) {
if (childIndex === null || childIndex === undefined || !children[childIndex]) {
return null;
}
const entryChunks = (0, _getEntryChunks.default)(children[childIndex]);
return entryChunks.length === 1 ? entryChunks[0].id : null;
}
function handleAction(state, action) {
switch (action.type) {
case 'discoveredDataPaths':
return { ...state,
dataPaths: { ...action.paths.reduce((map, path) => {
map[path] = map[path] || 'unknown';
return map;
}, {}),
...state.dataPaths
}
};
case 'pickDataPath':
{
const childIndex = getDefaultChildIndex(state.jsonChildren[action.path] || []);
return { ...state,
dataPaths: {
[action.path]: state.dataPaths[action.path] || 'unknown',
...state.dataPaths
},
selectedFilename: action.path,
selectedChildIndex: childIndex,
selectedChunkId: getDefaultChunkId(state.jsonChildren[action.path] || [], childIndex),
blacklistedModuleIds: [],
expandMode: 'collapse-all',
expandedRecords: new Set(),
currentlyFocusedElementID: null
};
}
case 'requestedDataAtPath':
return { ...state,
dataPaths: { ...state.dataPaths,
[action.path]: state.dataPaths[action.path] === 'ready' ? 'ready' : 'loading'
}
};
case 'loadedStatsAtPath':
{
const childIndex = getDefaultChildIndex(action.children);
return { ...state,
dataPaths: { ...state.dataPaths,
[action.path]: 'ready'
},
jsonChildren: { ...state.jsonChildren,
[action.path]: action.children
},
selectedChildIndex: childIndex,
selectedChunkId: getDefaultChunkId(action.children, childIndex)
};
}
case 'erroredAtPath':
return { ...state,
dataPaths: { ...state.dataPaths,
[action.path]: 'error'
}
};
case 'onSorted':
{
const isSameField = state.sort.field === action.field;
const invertedDir = state.sort.direction === 'ASC' ? 'DESC' : 'ASC';
const nextDirection = isSameField ? invertedDir : 'DESC';
return { ...state,
sort: {
field: action.field,
direction: nextDirection
}
};
}
case 'onFiltered':
return { ...state,
filters: { ...state.filters,
...action.changes
}
};
case 'onPickedChild':
return { ...state,
selectedChildIndex: action.childIndex,
selectedChunkId: getDefaultChunkId(state.jsonChildren && state.jsonChildren[String(state.selectedFilename)] || [], action.childIndex),
blacklistedModuleIds: [],
expandMode: 'collapse-all',
expandedRecords: new Set(),
currentlyFocusedElementID: null
};
case 'onPickedChunk':
return { ...state,
selectedChunkId: String(action.chunkId),
blacklistedModuleIds: [],
expandMode: 'collapse-all',
expandedRecords: new Set(),
currentlyFocusedElementID: null
};
case 'onRemoveModule':
return { ...state,
blacklistedModuleIds: [...state.blacklistedModuleIds, action.moduleID]
};
case 'onIncludeModule':
{
const moduleID = action.moduleID;
return { ...state,
blacklistedModuleIds: state.blacklistedModuleIds.filter(id => String(id) !== String(moduleID))
};
}
case 'changeExpandRecordsMode':
return { ...state,
expandMode: action.mode
};
case 'onExpandRecords':
return { ...state,
expandMode: 'manual',
expandedRecords: new Set(state.expandedRecords.add(action.moduleID))
};
case 'onCollapseRecords':
{
const expandedRecords = new Set(state.expandedRecords);
expandedRecords.delete(action.moduleID);
return { ...state,
expandMode: state.expandedRecords.size === 0 ? 'collapse-all' : 'manual',
expandedRecords: expandedRecords
};
}
case 'onFocusChanged':
{
if (action.elementID && state.calculatedFullModuleData && state.calculatedFullModuleData.extendedModules) {
const moduleId = action.elementID;
const modulesById = (0, _getModulesById.default)(state.calculatedFullModuleData.extendedModules);
const collapseableParent = (0, _getCollapsableParentOf.default)(modulesById, moduleId);
return { ...state,
expandMode: collapseableParent ? 'manual' : state.expandMode,
expandedRecords: collapseableParent ? new Set(state.expandedRecords.add(collapseableParent.id)) : state.expandedRecords,
currentlyFocusedElementID: action.elementID
};
} else {
return { ...state,
currentlyFocusedElementID: action.elementID
};
}
}
default:
return state;
}
}
function calculateFullModuleData(oldState, newState) {
if (!newState.jsonChildren || !newState.selectedFilename || newState.selectedChildIndex === null || newState.selectedChildIndex === undefined || !newState.jsonChildren[newState.selectedFilename] || !newState.jsonChildren[newState.selectedFilename][newState.selectedChildIndex]) {
return { ...newState,
calculatedFullModuleData: null
};
}
if (oldState.jsonChildren === newState.jsonChildren && oldState.selectedFilename === newState.selectedFilename && oldState.selectedChildIndex === newState.selectedChildIndex && oldState.selectedChunkId === newState.selectedChunkId && oldState.blacklistedModuleIds === newState.blacklistedModuleIds) {
return newState;
}
return { ...newState,
calculatedFullModuleData: (0, _fullModuleData.default)(newState.jsonChildren[newState.selectedFilename][newState.selectedChildIndex], newState.selectedChunkId, newState.blacklistedModuleIds)
};
}
function reducer(state = INITIAL_STATE, action) {
return (0, _withTimings.default)('Reducer', action.type)(() => calculateFullModuleData(state, handleAction(state, action)));
}