ligo/tools/webide/packages/client/src/redux/actions/evaluate-value.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Dispatch } from 'redux';
import { evaluateValue, getErrorMessage } from '../../services/api';
import { AppState } from '../app';
import { DoneLoadingAction, UpdateLoadingAction } from '../loading';
import { ChangeOutputAction } from '../result';
2020-02-28 00:22:43 +04:00
import { Command } from '../types';
import { CancellableAction } from './cancellable';
export class EvaluateValueAction extends CancellableAction {
getAction() {
return async (dispatch: Dispatch, getState: () => AppState) => {
const { editor, evaluateValue: evaluateValueState } = getState();
dispatch({
...new UpdateLoadingAction(
`Evaluating "${evaluateValueState.entrypoint}" entrypoint...`
)
});
try {
const result = await evaluateValue(
editor.language,
editor.code,
evaluateValueState.entrypoint
);
if (this.isCancelled()) {
return;
}
2020-02-28 00:22:43 +04:00
dispatch({
...new ChangeOutputAction(result.code, Command.EvaluateValue)
});
} catch (ex) {
if (this.isCancelled()) {
return;
}
dispatch({
2020-02-28 00:22:43 +04:00
...new ChangeOutputAction(
`Error: ${getErrorMessage(ex)}`,
Command.EvaluateValue
)
});
}
dispatch({ ...new DoneLoadingAction() });
};
}
}