UNPKG

bonsai-analyzer

Version:
185 lines (162 loc) 4.62 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.DiscoveredDataPaths = DiscoveredDataPaths; exports.PickDataPath = PickDataPath; exports.DroppedDataFile = DroppedDataFile; exports.RequestedDataAtPath = RequestedDataAtPath; exports.LoadedStatsAtPath = LoadedStatsAtPath; exports.ErroredAtPath = ErroredAtPath; exports.fetchApiList = fetchApiList; exports.fetchDataFile = fetchDataFile; exports.SortedTable = SortedTable; exports.FilteredTable = FilteredTable; exports.PickedChild = PickedChild; exports.PickedChunk = PickedChunk; exports.RemovedModule = RemovedModule; exports.IncludedModule = IncludedModule; exports.ChangeExpandMode = ChangeExpandMode; exports.ExpandRecords = ExpandRecords; exports.CollapseRecords = CollapseRecords; exports.FocusElement = FocusElement; var _fetchJSON = _interopRequireDefault(require("./fetchJSON")); var _getRawStats = _interopRequireDefault(require("../types/getRawStats")); var _invariant = _interopRequireDefault(require("invariant")); function DiscoveredDataPaths(dispatch) { return paths => dispatch({ type: 'discoveredDataPaths', paths }); } function PickDataPath(dispatch) { return path => dispatch({ type: 'pickDataPath', path }); } function DroppedDataFile(dispatch) { return (path, fileText) => { try { const json = JSON.parse(fileText); return LoadedStatsAtPath(dispatch)(path, json); } catch (error) { alert(`JSON parse error. Unable to load stats file.\n\n${String(error)}\n\nCheck the console for full details.`); return ErroredAtPath(dispatch)(path, error); } }; } function RequestedDataAtPath(dispatch) { return path => dispatch({ type: 'requestedDataAtPath', path }); } function LoadedStatsAtPath(dispatch) { return (path, json) => { try { const children = (0, _getRawStats.default)(json); return dispatch({ type: 'loadedStatsAtPath', path, children }); } catch (error) { alert(`Invalid stats file.\n\n${String(error)}\n\nCheck the console for full details.`); return ErroredAtPath(dispatch)(path, error); } }; } function ErroredAtPath(dispatch) { return (path, error) => dispatch({ type: 'erroredAtPath', path, error }); } function fetchApiList(dispatch) { return endpoint => { (0, _fetchJSON.default)(endpoint).then(json => { (0, _invariant.default)(json.paths, `Missing field. Expected '{"paths": []}' key. Got: ${String(Object.keys(json))}`); (0, _invariant.default)(Array.isArray(json.paths), `Invalid type. Expected "paths" to be an array of web urls. Got: ${JSON.stringify(json.paths)}`); DiscoveredDataPaths(dispatch)(json.paths.map(String)); }).catch(error => { // eslint-disable-next-line no-console console.error(`Failed while fetching json from '${String(endpoint)}'.`, error); }); }; } function fetchDataFile(dispatch) { return endpoint => { PickDataPath(dispatch)(endpoint); RequestedDataAtPath(dispatch)(endpoint); (0, _fetchJSON.default)(endpoint).then(json => { LoadedStatsAtPath(dispatch)(endpoint, json); }).catch(error => { // eslint-disable-next-line no-console console.error(`Failed while fetching json from '${String(endpoint)}'.`, error); ErroredAtPath(dispatch)(endpoint, error); }); }; } function SortedTable(dispatch) { return field => dispatch({ type: 'onSorted', field }); } function FilteredTable(dispatch) { return changes => dispatch({ type: 'onFiltered', changes }); } function PickedChild(dispatch) { return childIndex => dispatch({ type: 'onPickedChild', childIndex }); } function PickedChunk(dispatch) { return chunkId => dispatch({ type: 'onPickedChunk', chunkId }); } function RemovedModule(dispatch) { return moduleID => dispatch({ type: 'onRemoveModule', moduleID }); } function IncludedModule(dispatch) { return moduleID => dispatch({ type: 'onIncludeModule', moduleID }); } function ChangeExpandMode(dispatch) { return mode => dispatch({ type: 'changeExpandRecordsMode', mode }); } function ExpandRecords(dispatch) { return moduleID => dispatch({ type: 'onExpandRecords', moduleID }); } function CollapseRecords(dispatch) { return moduleID => dispatch({ type: 'onCollapseRecords', moduleID }); } function FocusElement(dispatch) { return elementID => dispatch({ type: 'onFocusChanged', elementID }); }