@nteract/epics
Version:
Redux-Observable epics for nteract apps
131 lines • 5.34 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.publishToBookstoreAfterSave = exports.publishToBookstore = void 0;
const actions = __importStar(require("@nteract/actions"));
const commutable_1 = require("@nteract/commutable");
const selectors = __importStar(require("@nteract/selectors"));
const redux_observable_1 = require("redux-observable");
const rx_jupyter_1 = require("rx-jupyter");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
/**
* First step in publishing notebooks to bookstore.
* Saves notebook using the `content` API. Then,
* kicks off an action that saves the notebook
* to `Bookstore`.
*
* @param action$ Action type.
* @param state$ Application state.
*/
function publishToBookstore(action$, state$, dependencies) {
return action$.pipe(redux_observable_1.ofType(actions.PUBLISH_TO_BOOKSTORE), operators_1.switchMap(action => {
const state = state$.value;
const host = selectors.currentHost(state);
const serverConfig = selectors.serverConfig(host);
if (!action.payload) {
return rxjs_1.of({
type: "ERROR",
error: true,
payload: {
error: new Error("saving content to Bookstore needs a payload")
}
});
}
// Dismiss any usage that isn't targeting a jupyter server
if (host.type !== "jupyter") {
return rxjs_1.EMPTY;
}
const content = selectors
.contentByRef(state)
.get(action.payload.contentRef);
if (!content || content.type !== "notebook") {
return rxjs_1.of({
type: "ERROR",
error: true,
payload: {
error: new Error("Only Notebooks can be published to Bookstore")
}
});
}
const notebook = commutable_1.toJS(content.model.notebook);
// Save notebook first before sending to Bookstore
return dependencies.contentProvider
.save(serverConfig, content.filepath, {
content: notebook,
type: "notebook"
})
.pipe(operators_1.tap((xhr) => {
if (xhr.status !== 200) {
throw new Error(xhr.response);
}
}), operators_1.map((nb) => {
return actions.publishToBookstoreAfterSave({
contentRef: action.payload.contentRef,
model: {
name: content.filepath.split("/").pop(),
path: content.filepath,
type: content.type,
created: content && content.created && content.created.toString(),
last_modified: "",
content: notebook,
mimetype: content.mimetype,
format: content.format
}
});
}), operators_1.catchError((xhrError) => rxjs_1.of(actions.publishToBookstoreFailed({
error: xhrError,
contentRef: action.payload.contentRef
}))));
}));
}
exports.publishToBookstore = publishToBookstore;
/**
* Last step in publishing notebooks to bookstore.
* Saves notebook to `Bookstore`.
*
* @param action$ Action type.
* @param state$ Application state.
*/
function publishToBookstoreAfterSave(action$, state$) {
const state = state$.value;
const host = selectors.currentHost(state);
const serverConfig = selectors.serverConfig(host);
return action$.pipe(redux_observable_1.ofType(actions.PUBLISH_TO_BOOKSTORE_AFTER_SAVE), operators_1.switchMap(action => {
const targetPath = action.payload.model.path;
const model = action.payload.model;
// Publish notebook to Bookstore
return rx_jupyter_1.bookstore.publish(serverConfig, targetPath, model).pipe(operators_1.tap((xhr) => {
if (xhr.status !== 200) {
throw new Error(xhr.response);
}
}), operators_1.map(() => {
actions.publishToBookstoreSucceeded({
contentRef: action.payload.contentRef
});
}), operators_1.catchError((xhrError) => rxjs_1.of(actions.publishToBookstoreFailed({
error: xhrError,
contentRef: action.payload.contentRef
}))));
}));
}
exports.publishToBookstoreAfterSave = publishToBookstoreAfterSave;
//# sourceMappingURL=hosts.js.map