<commit_before>import { getJSON } from '../api';

import { launchKernel } from '../api/kernel';

import {
  createExecuteRequest,
  msgSpecToNotebookFormat,
} from '../api/messaging';

import Immutable from 'immutable';

export function exit() {
  return {
    type: 'EXIT',
  };
}

export function killKernel() {
  return {
    type: 'KILL_KERNEL',
  };
}

export function newKernel(kernelSpecName) {
  return (subject) => {
    launchKernel(kernelSpecName)
      .then(kc => {
        const { channels, connectionFile, spawn } = kc;
        subject.next({
          type: 'NEW_KERNEL',
          channels,
          connectionFile,
          spawn,
        });
      })
      .catch((err) => console.error(err));
  };
}

export function save() {
  return (subject) => {
    subject.next({
      type: 'START_SAVING',
    });

    alert('can\'t actually save yet');
    console.error('we can\'t save yet and don\'t even have the state to do so');

    // when done saving
    subject.next({
      type: 'DONE_SAVING',
    });
  };
}

export function readJSON(filePath) {
  return (subject) => {
    getJSON(filePath)
      .then((data) => {
        subject.next({
          type: 'READ_JSON',
          data,
        });
        newKernel(data.metadata.kernelspec.name)(subject);
      });
  };
}

export function setSelected(ids, additive) {
  return {
    type: 'SET_SELECTED',
    ids,
    additive,
  };
}

export function updateCellSource(id, source) {
  return {
    type: 'UPDATE_CELL_SOURCE',
    id,
    source,
  };
}

export function updateCellOutputs(id, outputs) {
  return {
    type: 'UPDATE_CELL_OUTPUTS',
    id,
    outputs,
  };
}

export function moveCell(id, destinationId, above) {
  return {
    type: 'MOVE_CELL',
    id,
    destinationId,
    above,
  };
}

export function updateCellExecutionCount(id, count) {
  return {
    type: 'UPDATE_CELL_EXECUTION_COUNT',
    id,
    count,
  };
}

export function executeCell(id, source) {
  return (subject, dispatch, channels) => {
    const { iopub, shell } = channels;

    if(!iopub || !shell) {
      // TODO: propagate error about execution when kernel not connected
      // IDEA: Propagage error via subject!
      return;
    }

    const executeRequest = createExecuteRequest(source);

    // Limitation of the Subject implementation in enchannel
    // we must shell.subscribe in order to shell.next
    shell.subscribe(() => {});

    // Set the current outputs to an empty list
    dispatch(updateCellOutputs(id, new Immutable.List()));

    const childMessages = iopub.childOf(executeRequest)
                               .publish()
                               .refCount();<commit_msg>Problem: publish().refCount() can be simplified<commit_after>import { getJSON } from '../api';

import { launchKernel } from '../api/kernel';

import {
  createExecuteRequest,
  msgSpecToNotebookFormat,
} from '../api/messaging';

import Immutable from 'immutable';

export function exit() {
  return {
    type: 'EXIT',
  };
}

export function killKernel() {
  return {
    type: 'KILL_KERNEL',
  };
}

export function newKernel(kernelSpecName) {
  return (subject) => {
    launchKernel(kernelSpecName)
      .then(kc => {
        const { channels, connectionFile, spawn } = kc;
        subject.next({
          type: 'NEW_KERNEL',
          channels,
          connectionFile,
          spawn,
        });
      })
      .catch((err) => console.error(err));
  };
}

export function save() {
  return (subject) => {
    subject.next({
      type: 'START_SAVING',
    });

    alert('can\'t actually save yet');
    console.error('we can\'t save yet and don\'t even have the state to do so');

    // when done saving
    subject.next({
      type: 'DONE_SAVING',
    });
  };
}

export function readJSON(filePath) {
  return (subject) => {
    getJSON(filePath)
      .then((data) => {
        subject.next({
          type: 'READ_JSON',
          data,
        });
        newKernel(data.metadata.kernelspec.name)(subject);
      });
  };
}

export function setSelected(ids, additive) {
  return {
    type: 'SET_SELECTED',
    ids,
    additive,
  };
}

export function updateCellSource(id, source) {
  return {
    type: 'UPDATE_CELL_SOURCE',
    id,
    source,
  };
}

export function updateCellOutputs(id, outputs) {
  return {
    type: 'UPDATE_CELL_OUTPUTS',
    id,
    outputs,
  };
}

export function moveCell(id, destinationId, above) {
  return {
    type: 'MOVE_CELL',
    id,
    destinationId,
    above,
  };
}

export function updateCellExecutionCount(id, count) {
  return {
    type: 'UPDATE_CELL_EXECUTION_COUNT',
    id,
    count,
  };
}

export function executeCell(id, source) {
  return (subject, dispatch, channels) => {
    const { iopub, shell } = channels;

    if(!iopub || !shell) {
      // TODO: propagate error about execution when kernel not connected
      // IDEA: Propagage error via subject!
      return;
    }

    const executeRequest = createExecuteRequest(source);

    // Limitation of the Subject implementation in enchannel
    // we must shell.subscribe in order to shell.next
    shell.subscribe(() => {});

    // Set the current outputs to an empty list
    dispatch(updateCellOutputs(id, new Immutable.List()));

    const childMessages = iopub.childOf(executeRequest)
                               .share();
{'fraction_non_alphanumeric': 0.12942498595242555, 'fraction_numerical': 0.0, 'mean_word_length': 2.5154707044107965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3473475', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1634, 'n_words': 406}