import * as path from "path" ; import { ImmutableNotebook , monocellNotebook , toJS } from "@nteract/commutable" ; import { actions , AppState , selectors } from "@nteract/core" ; import { ofType , StateObservable } from "redux-observable" ; import { empty , Observable , of } from "rxjs" ; import { map , mergeMap } from "rxjs/operators" ; const notebookMediaType = "application/x-ipynb+json" ; /**
 * Determines the right kernel to launch based on a notebook
 */ export const extractNewKernel = ( filepath , notebook ) => { const cwd = ( filepath !== null && path . dirname ( path . resolve ( filepath ) ) ) || process . cwd ( ) ; const kernelSpecName = notebook . getIn ( [ "metadata" , "kernelspec" , "name" ] ) || notebook . getIn ( [ "metadata" , "language_info" , "name" ] ) || "python3" ; return { cwd , kernelSpecName } ; } ; export const launchKernelWhenNotebookSetEpic = ( action$ , state$ ) => action$ . pipe ( ofType ( actions . FETCH_CONTENT_FULFILLED ) , mergeMap ( ( action ) => { const contentRef = action . payload . contentRef ; const content = selectors . content ( state$ . value , { contentRef } ) ; if ( ! content || content . type !== "notebook" || content . model . type !== "notebook" ) { return empty ( ) ; } const filepath = content . filepath ; const notebook = content . model . notebook ; const { cwd , kernelSpecName } = extractNewKernel ( filepath , notebook ) ; return of ( actions . launchKernelByName ( { kernelSpecName , cwd , kernelRef : action . payload . kernelRef , selectNextKernel : true , contentRef : action . payload . contentRef } ) ) ; } ) ) ; param { ActionObservable } ActionObservable export const newNotebookEpic = ( action$ ) => action$ . pipe ( ofType ( actions . NEW_NOTEBOOK ) , map ( ( action ) => { const { payload : { kernelSpec : { name , spec } } } = action ; let notebook = monocellNotebook ; if ( name ) { notebook = notebook . setIn ( [ "metadata" , "kernel_info" , "name" ] , name ) . setIn ( [ "metadata" , "language_info" , "name" ] , name ) ; } if ( spec ) { notebook = notebook . setIn ( [ "metadata" , "kernelspec" ] , spec ) . setIn ( [ "metadata" , "kernelspec" , "name" ] , name ) ; } const timestamp = new Date ( ) ; const filepath = ( action . payload . filepath !== null && path . resolve ( action . payload . filepath ) ) || path . join ( action . payload . cwd , "Untitled.ipynb" ) ; return actions . fetchContentFulfilled ( { filepath , model : { type : "notebook" , mimetype : notebookMediaType , format : "json" , content : toJS ( notebook ) , writable : true , name : path . basename ( filepath ) , path : filepath , created : timestamp . toString ( ) , last_modified : timestamp . toString ( ) } , kernelRef : action . payload . kernelRef , contentRef : action . payload . contentRef } ) ; } ) ) ;