<commit_before>import { bothSlotsSelected } from '../utils'

import {
  BRICK,
  SELECTABLE_PIPE
} from '../utils/componentNames'

import {
  unique
} from '../utils'

import {
  doesAllInputsHaveValues,
  elementInputValueIds
} from '../utils/evalUtils'

export const ADD_BRICK = 'ADD_BRICK'
export const ADD_PIPE = 'ADD_PIPE'
export const ADD_PRIMITIVE = 'ADD_PRIMITIVE'
export const ADD_UNIT_TEST = 'ADD_UNIT_TEST'
export const CHANGE_PRIMITIVE_VALUE = 'CHANGE_PRIMITIVE_VALUE'
export const CHANGE_TEST_NODE_TYPE = 'CHANGE_TEST_NODE_TYPE'
export const CHANGE_TEST_NODE_VALUE = 'CHANGE_TEST_NODE_VALUE'
export const CLEAR_SLOT_SELECTION = 'CLEAR_SLOT_SELECTION'
export const EVALUATE = 'EVALUATE'
export const INIT_WORKSPACE = 'INIT_WORKSPACE'
export const LINK_SLOTS = 'LINK_SLOTS'
export const MOVE_ELEMENT = 'MOVE_ELEMENT'
export const REMOVE_ELEMENT = 'REMOVE_ELEMENT'
export const REMOVE_SELECTED_ELEMENT = 'REMOVE_SELECTED_ELEMENT'
export const REMOVE_UNIT_TEST = 'REMOVE_UNIT_TEST'
export const START_DRAG = 'START_DRAG'
export const STOP_DRAG = 'STOP_DRAG'
export const SELECT_ELEMENT = 'SELECT_ELEMENT'
export const SELECT_SLOT = 'SELECT_SLOT'
export const UNEVALUATE = 'UNEVALUATE'
export const UNLINK_SLOTS = 'UNLINK_SLOTS'

export const initWorkspace = (type) => {
  return {
    type: INIT_WORKSPACE,
    payload: type
  }
}

export const addBrick = (brick) => {
  return (dispatch, getState) => {
    dispatch(removeSelectedElement())
    dispatch(_addBrick(brick))
  }
}

const _addBrick = (brick) => {
  return {
    type: ADD_BRICK,
    payload: brick
  }
}

export const addPrimitive = (type) => {
  return (dispatch, getState) => {
    dispatch(removeSelectedElement())
    dispatch(_addPrimitive(type))
  }
}

const _addPrimitive = (type) => {
  return {
    type: ADD_PRIMITIVE,
    payload: type
  }
}

export const startDrag = (elementId, mousePosition, elementPosition) => {
  return {
    type: START_DRAG,
    payload: {
      elementId,
      mousePosition,
      elementPosition
    }
  }
}

export const selectElementOrStopDrag = (mousePosition) => {
  return (dispatch, getState) => {
    const { element } = getState().workspace.selectionState

    dispatch(stopDrag())

    if(element.mouseDownPosition.x === mousePosition.x &&
      element.mouseDownPosition.y === mousePosition.y ) {
      dispatch(selectElement(element.id, mousePosition))
    } else {
      dispatch(removeSelectedElement())
    }
  }
}

export const stopDrag = () => {
  return {
    type: STOP_DRAG
  }
}

export const moveElement = (currentMousePosition) => {
  return {
    type: MOVE_ELEMENT,
    payload: {
      currentMousePosition
    }
  }
}

export const addPipeOrSelectSlot = (type, elementId, slotId) => {
  return (dispatch, getState) => {
    dispatch(removeSelectedElement())
    dispatch(selectSlot(type, elementId, slotId))
    dispatch(_addPipeIfBothSlotsSelected())
  }
}

// type: 'input' or 'output' slot
export const selectSlot = (type, elementId, slotId) => {
  return {
    type: SELECT_SLOT,
    payload: {
      elementId,
      slotId,
      type
    }
  }
}

const _addPipeIfBothSlotsSelected = () => {
  return (dispatch, getState) => {
    const { workspace } = getState()
    const { pipe } = workspace.selectionState

    if(bothSlotsSelected(pipe)) {
      dispatch(_addPipe(pipe))
      dispatch(_clearSlotSelection())
      dispatch(_linkSlots(pipe))
      dispatch(_evalAllWorkspacesIfNeeded(output.elementId))
    }
  }
}

const _evalAllWorkspacesIfNeeded = (elementId) => {
  return (dispatch, getState) => {
    const { workspace } = getState()

    if(_shouldEval(elementId, workspace)) {
      return dispatch(_evalWorkspaces(elementId))
    }
  }
}

const _shouldEval = (elementId, workspace) => {
  const element = workspace.entities[elementId]

  if(element.componentName != BRICK) {
    return false
  }
<commit_msg>Fix bug when trying to evaluate workspace introduced in beedb7a<commit_after>import { bothSlotsSelected } from '../utils'

import {
  BRICK,
  SELECTABLE_PIPE
} from '../utils/componentNames'

import {
  unique
} from '../utils'

import {
  doesAllInputsHaveValues,
  elementInputValueIds
} from '../utils/evalUtils'

export const ADD_BRICK = 'ADD_BRICK'
export const ADD_PIPE = 'ADD_PIPE'
export const ADD_PRIMITIVE = 'ADD_PRIMITIVE'
export const ADD_UNIT_TEST = 'ADD_UNIT_TEST'
export const CHANGE_PRIMITIVE_VALUE = 'CHANGE_PRIMITIVE_VALUE'
export const CHANGE_TEST_NODE_TYPE = 'CHANGE_TEST_NODE_TYPE'
export const CHANGE_TEST_NODE_VALUE = 'CHANGE_TEST_NODE_VALUE'
export const CLEAR_SLOT_SELECTION = 'CLEAR_SLOT_SELECTION'
export const EVALUATE = 'EVALUATE'
export const INIT_WORKSPACE = 'INIT_WORKSPACE'
export const LINK_SLOTS = 'LINK_SLOTS'
export const MOVE_ELEMENT = 'MOVE_ELEMENT'
export const REMOVE_ELEMENT = 'REMOVE_ELEMENT'
export const REMOVE_SELECTED_ELEMENT = 'REMOVE_SELECTED_ELEMENT'
export const REMOVE_UNIT_TEST = 'REMOVE_UNIT_TEST'
export const START_DRAG = 'START_DRAG'
export const STOP_DRAG = 'STOP_DRAG'
export const SELECT_ELEMENT = 'SELECT_ELEMENT'
export const SELECT_SLOT = 'SELECT_SLOT'
export const UNEVALUATE = 'UNEVALUATE'
export const UNLINK_SLOTS = 'UNLINK_SLOTS'

export const initWorkspace = (type) => {
  return {
    type: INIT_WORKSPACE,
    payload: type
  }
}

export const addBrick = (brick) => {
  return (dispatch, getState) => {
    dispatch(removeSelectedElement())
    dispatch(_addBrick(brick))
  }
}

const _addBrick = (brick) => {
  return {
    type: ADD_BRICK,
    payload: brick
  }
}

export const addPrimitive = (type) => {
  return (dispatch, getState) => {
    dispatch(removeSelectedElement())
    dispatch(_addPrimitive(type))
  }
}

const _addPrimitive = (type) => {
  return {
    type: ADD_PRIMITIVE,
    payload: type
  }
}

export const startDrag = (elementId, mousePosition, elementPosition) => {
  return {
    type: START_DRAG,
    payload: {
      elementId,
      mousePosition,
      elementPosition
    }
  }
}

export const selectElementOrStopDrag = (mousePosition) => {
  return (dispatch, getState) => {
    const { element } = getState().workspace.selectionState

    dispatch(stopDrag())

    if(element.mouseDownPosition.x === mousePosition.x &&
      element.mouseDownPosition.y === mousePosition.y ) {
      dispatch(selectElement(element.id, mousePosition))
    } else {
      dispatch(removeSelectedElement())
    }
  }
}

export const stopDrag = () => {
  return {
    type: STOP_DRAG
  }
}

export const moveElement = (currentMousePosition) => {
  return {
    type: MOVE_ELEMENT,
    payload: {
      currentMousePosition
    }
  }
}

export const addPipeOrSelectSlot = (type, elementId, slotId) => {
  return (dispatch, getState) => {
    dispatch(removeSelectedElement())
    dispatch(selectSlot(type, elementId, slotId))
    dispatch(_addPipeIfBothSlotsSelected())
  }
}

// type: 'input' or 'output' slot
export const selectSlot = (type, elementId, slotId) => {
  return {
    type: SELECT_SLOT,
    payload: {
      elementId,
      slotId,
      type
    }
  }
}

const _addPipeIfBothSlotsSelected = () => {
  return (dispatch, getState) => {
    const { workspace } = getState()
    const { pipe } = workspace.selectionState

    if(bothSlotsSelected(pipe)) {
      dispatch(_addPipe(pipe))
      dispatch(_clearSlotSelection())
      dispatch(_linkSlots(pipe))
      dispatch(_evalAllWorkspacesIfNeeded(pipe.output.elementId))
    }
  }
}

const _evalAllWorkspacesIfNeeded = (elementId) => {
  return (dispatch, getState) => {
    const { workspace } = getState()

    if(_shouldEval(elementId, workspace)) {
      return dispatch(_evalWorkspaces(elementId))
    }
  }
}

const _shouldEval = (elementId, workspace) => {
  const element = workspace.entities[elementId]

  if(element.componentName != BRICK) {
    return false
  }

{'fraction_non_alphanumeric': 0.10899987161381436, 'fraction_numerical': 0.00012838618564642445, 'mean_word_length': 4.016097875080489, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '4530903', 'n_tokens_mistral': 2594, 'n_tokens_neox': 2589, 'n_words': 537}