@nteract/epics
Version:
Redux-Observable epics for nteract apps
131 lines • 6.01 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeCellEpic = exports.executeFocusedCellEpic = exports.executeAllCellsEpic = void 0;
/**
* The epics in this file are responsible for mapping UI-based
* execution action, like EXECUTE_CELL or EXECUTE_FOCUSED_CELL, to
* a single SEND_EXECUTE_REQUEST action that the core execution epics
* listen to.
*/
const immutable_1 = __importDefault(require("immutable"));
const redux_observable_1 = require("redux-observable");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const actions = __importStar(require("@nteract/actions"));
const selectors = __importStar(require("@nteract/selectors"));
const types_1 = require("@nteract/types");
/**
* Maps ExecuteAllCells, ExecuteAllCellsAbove, and ExecuteAllCellsBelow actions to
* ExecuteCell actions. These ExecuteCell actions are mapped
* to SendExecuteRequest actions by another epic.
*
* @param action$ The stream of actions dispatched to Redux
* @param state$ The stream of state changes
*/
function executeAllCellsEpic(action$, state$) {
return action$.pipe(redux_observable_1.ofType(actions.EXECUTE_ALL_CELLS, actions.EXECUTE_ALL_CELLS_ABOVE, actions.EXECUTE_ALL_CELLS_BELOW), operators_1.concatMap((action) => {
const state = state$.value;
const contentRef = action.payload.contentRef;
const model = selectors.model(state, { contentRef });
// If it's not a notebook, we shouldn't be here
if (!model || model.type !== "notebook") {
return rxjs_1.of(actions.executeFailed({
error: new Error("Cannot send execute requests from non-notebook files."),
code: types_1.errors.EXEC_NOT_A_NOTEBOOK,
contentRef
}));
}
let codeCellIds = immutable_1.default.List();
if (action.type === actions.EXECUTE_ALL_CELLS) {
codeCellIds = selectors.notebook.codeCellIds(model);
}
else if (action.type === actions.EXECUTE_ALL_CELLS_ABOVE) {
codeCellIds = selectors.notebook.codeCellIdsAbove(model, action.payload.id);
}
else if (action.type === actions.EXECUTE_ALL_CELLS_BELOW) {
codeCellIds = selectors.notebook.codeCellIdsBelow(model, action.payload.id);
}
return rxjs_1.of(...codeCellIds.map((id) => actions.executeCell({ id, contentRef: action.payload.contentRef })));
}));
}
exports.executeAllCellsEpic = executeAllCellsEpic;
/**
* Maps an ExecuteFocusedCell action to an ExecuteCell action
* by extracting the currently focused cell from the state.
*
* @param action$
* @param state$
*/
function executeFocusedCellEpic(action$, state$) {
return action$.pipe(redux_observable_1.ofType(actions.EXECUTE_FOCUSED_CELL), operators_1.mergeMap((action) => {
const contentRef = action.payload.contentRef;
const state = state$.value;
const model = selectors.model(state, { contentRef });
// If it's not a notebook, we shouldn't be here
if (!model || model.type !== "notebook") {
return rxjs_1.of(actions.executeFailed({
error: new Error("Cannot send execute requests from non-notebook files."),
code: types_1.errors.EXEC_NOT_A_NOTEBOOK,
contentRef
}));
}
const id = model.cellFocused;
if (!id) {
return rxjs_1.of(actions.executeFailed({
error: new Error("There is currently no focused cell to execute."),
code: types_1.errors.EXEC_NO_CELL_WITH_ID,
contentRef: action.payload.contentRef
}));
}
return rxjs_1.of(actions.executeCell({ id, contentRef: action.payload.contentRef }));
}));
}
exports.executeFocusedCellEpic = executeFocusedCellEpic;
/**
* Maps an ExecuteCell action to a SendExecuteRequest or EnqueueAction action.
*
* If the kernel is ready to execute, emit the SendExecuteRequest action.
*
* F f the kernel is not ready, push the execute request to the message queue
* by emitting the EnqueueAction action.
*/
function executeCellEpic(action$, state$) {
return action$.pipe(redux_observable_1.ofType(actions.EXECUTE_CELL), operators_1.mergeMap((action) => {
const state = state$.value;
const contentRef = action.payload.contentRef;
const kernel = selectors.kernelByContentRef(state, { contentRef });
if (kernel &&
kernel.channels &&
kernel.status !== types_1.KernelStatus.NotConnected &&
kernel.status !== types_1.KernelStatus.ShuttingDown) {
return rxjs_1.of(actions.sendExecuteRequest(action.payload));
}
else {
return rxjs_1.of(actions.updateCellStatus(Object.assign(Object.assign({}, action.payload), { status: "queued" })), actions.enqueueAction(action.payload));
}
}));
}
exports.executeCellEpic = executeCellEpic;
//# sourceMappingURL=map-to-execute.js.map