@nteract/epics
Version:
Redux-Observable epics for nteract apps
93 lines • 4.35 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.lazyLaunchKernelEpic = exports.executeCellAfterKernelLaunchEpic = void 0;
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");
const kernel_lifecycle_1 = require("../kernel-lifecycle");
/**
* Executes all requests in the message queue then clears the queue after
* the kernel is launched successfully and is ready to execute.
*/
exports.executeCellAfterKernelLaunchEpic = (action$, state$) => action$.pipe(redux_observable_1.ofType(actions.LAUNCH_KERNEL_SUCCESSFUL), operators_1.withLatestFrom(state$), operators_1.filter(([action, state]) => {
if (selectors.messageQueue(state).size === 0) {
return false;
}
const contentRef = action.payload.contentRef;
const kernel = selectors.kernelByContentRef(state, { contentRef });
return !!(kernel &&
kernel.channels &&
kernel.status !== types_1.KernelStatus.NotConnected &&
kernel.status !== types_1.KernelStatus.ShuttingDown);
}), operators_1.switchMap(([_action, state]) => rxjs_1.merge(rxjs_1.of(...selectors
.messageQueue(state)
.map((queuedAction) => actions.executeCell(queuedAction.payload))), rxjs_1.of(actions.clearMessageQueue()))));
/**
* Launches the kernel when user tries to execute a cell.
* The distinct operator prevents the LaunchKernelByName action from
* being emitted more than once within the same notebook.
*/
function lazyLaunchKernelEpic(action$, state$) {
return action$.pipe(redux_observable_1.ofType(actions.EXECUTE_CELL), operators_1.filter((action) => {
const state = state$.value;
const contentRef = action.payload.contentRef;
return !selectors.kernelByContentRef(state, { contentRef });
}), operators_1.switchMap(action => {
const state = state$.value;
const contentRef = action.payload.contentRef;
if (!contentRef) {
return rxjs_1.of(actions.launchKernelFailed({
error: new Error("Launch kernel did not receive a ContentRef."),
code: types_1.errors.LAUNCH_NO_CONTENT_REF,
contentRef
}));
}
const content = selectors.content(state, { contentRef });
const kernelRef = selectors.kernelRefByContentRef(state, {
contentRef
});
if (!kernelRef ||
!content ||
content.type !== "notebook" ||
content.model.type !== "notebook") {
return rxjs_1.of(actions.launchKernelFailed({
error: new Error("Launch kernel failed because the source content is not a notebook"),
code: types_1.errors.LAUNCH_NOT_A_NOTEBOOK
}));
}
const filepath = content.filepath;
const notebook = content.model.notebook;
const { cwd, kernelSpecName } = kernel_lifecycle_1.extractNewKernel(filepath, notebook);
return rxjs_1.of(actions.launchKernelByName({
kernelSpecName,
cwd,
kernelRef,
selectNextKernel: true,
contentRef
}));
}), operators_1.distinct((action) => action.payload.contentRef));
}
exports.lazyLaunchKernelEpic = lazyLaunchKernelEpic;
//# sourceMappingURL=lazy-launch.js.map