<commit_before>import React from 'react'
import ReactDOM from 'react-dom'
import { BinaryChart } from 'binary-charts'
import { logoutAllTokens } from 'binary-common-utils/lib/account'
import { observer as globalObserver } from 'binary-common-utils/lib/observer'
import {
  getTokenList, removeAllTokens,
  get as getStorage, set as setStorage, getToken,
} from 'binary-common-utils/lib/storageManager'
import { LiveApi } from 'binary-live-api'
import TradeInfo from './tradeInfo'
import _Blockly from './blockly'
import { translate } from '../../common/i18n'
import { SaveXml } from './react-components/SaveXml'
import { LimitsPanel } from './react-components/LimitsPanel'
import { getLanguage } from '../../common/lang'
import { symbolPromise, ticksService } from './shared'
import { logHandler } from './logger'
import { Tour } from './tour'
import OfficialVersionWarning from './react-components/OfficialVersionWarning'

let realityCheckTimeout

let chartData = []
let symbol = 'R_100'
let chartType = 'line'
const pipSize = 2
let dataType = 'ticks'
let granularity = 60
let contractForChart = null
let chartComponent
let listeners = {}

const api = new LiveApi({
  language: getStorage('lang') || 'en',
  appId: getStorage('appId') || 1,
})

api.events.on('balance', response => {
  const { balance: { balance, currency } } = response

  $('.topMenuBalance').text(`${balance} ${currency}`)
})

const addBalanceForToken = token => {
  api.authorize(token).then(() => {
    api.send({ forget_all: 'balance' }).then(() => {
      api.subscribeToBalance()
    })
  })
}

const stopTickListeners = () => {
  if (listeners.ohlc) {
    ticksService.stopMonitor(symbol, granularity, listeners.ohlc)
  }
  if (listeners.tick) {
    ticksService.stopMonitor(symbol, listeners.tick)
  }
  listeners = {}
}

const updateChart = () => {
  if (!$('#summaryPanel:visible').length) {
    return
  }

  const isMinHeight = $(window).height() <= 360

  if (chartComponent && dataType === 'ticks' && contractForChart) {
    const { chart } = chartComponent
    const { dataMax } = chart.xAxis[0].getExtremes()
    const { minRange } = chart.xAxis[0].options

    chart.xAxis[0].setExtremes(dataMax - minRange, dataMax)
  }

  chartComponent = ReactDOM.render(
    <BinaryChart
    className="trade-chart"
    id="trade-chart0"
    contract={dataType === 'ticks' ? contractForChart : false}
    pipSize={pipSize}
    shiftMode="dynamic"
    ticks={chartData}
    getData={getData} // eslint-disable-line no-use-before-define
    type={chartType}
    hideToolbar={isMinHeight}
    hideTimeFrame={isMinHeight}
    onTypeChange={(type) => (chartType = type)}
    />, $('#chart')[0])
}

const updateTickListeners = () => {
  listeners.ohlc = ticksService.monitor(symbol, granularity, response => {
    if (dataType === 'candles') {
      chartData = response
      updateChart()
    }
  })

  listeners.tick = ticksService.monitor(symbol, response => {
    if (dataType === 'ticks') {
      chartData = response
      updateChart()
    }
  })
}

const getData = (start, end, newDataType, newGranularity) => {
  stopTickListeners()
  dataType = newDataType
  granularity = newGranularity
  updateTickListeners()
  return ticksService.getHistory(symbol, dataType === 'ticks' ? undefined : newGranularity)
}

const showRealityCheck = () => {
  $('.blocker').show()
  $('.reality-check').show()
}

const hideRealityCheck = () => {
  $('#rc-err').hide()
  $('.blocker').hide()
  $('.reality-check').hide()
}

const stopRealityCheck = () => {
  clearInterval(realityCheckTimeout)
  realityCheckTimeout = null
}

const realityCheckInterval = () => {
  realityCheckTimeout = setInterval(() => {
    const now = parseInt((new Date().getTime()) / 1000, 10)
    const checkTime = +getStorage('realityCheckTime')
    if (checkTime && now >= checkTime) {
      showRealityCheck()
      stopRealityCheck()
    }
  }, 1000)
}
<commit_msg>Fix redundant call of getHistory in view<commit_after>import React from 'react'
import ReactDOM from 'react-dom'
import { BinaryChart } from 'binary-charts'
import { logoutAllTokens } from 'binary-common-utils/lib/account'
import { observer as globalObserver } from 'binary-common-utils/lib/observer'
import {
  getTokenList, removeAllTokens,
  get as getStorage, set as setStorage, getToken,
} from 'binary-common-utils/lib/storageManager'
import { LiveApi } from 'binary-live-api'
import TradeInfo from './tradeInfo'
import _Blockly from './blockly'
import { translate } from '../../common/i18n'
import { SaveXml } from './react-components/SaveXml'
import { LimitsPanel } from './react-components/LimitsPanel'
import { getLanguage } from '../../common/lang'
import { symbolPromise, ticksService } from './shared'
import { logHandler } from './logger'
import { Tour } from './tour'
import OfficialVersionWarning from './react-components/OfficialVersionWarning'

let realityCheckTimeout

let chartData = []
let symbol = 'R_100'
let chartType = 'line'
const pipSize = 2
let dataType = 'ticks'
let granularity = 60
let contractForChart = null
let chartComponent
let listeners = {}

const api = new LiveApi({
  language: getStorage('lang') || 'en',
  appId: getStorage('appId') || 1,
})

api.events.on('balance', response => {
  const { balance: { balance, currency } } = response

  $('.topMenuBalance').text(`${balance} ${currency}`)
})

const addBalanceForToken = token => {
  api.authorize(token).then(() => {
    api.send({ forget_all: 'balance' }).then(() => {
      api.subscribeToBalance()
    })
  })
}

const stopTickListeners = () => {
  if (listeners.ohlc) {
    ticksService.stopMonitor(symbol, granularity, listeners.ohlc)
  }
  if (listeners.tick) {
    ticksService.stopMonitor(symbol, listeners.tick)
  }
  listeners = {}
}

const updateChart = () => {
  if (!$('#summaryPanel:visible').length) {
    return
  }

  const isMinHeight = $(window).height() <= 360

  if (chartComponent && dataType === 'ticks' && contractForChart) {
    const { chart } = chartComponent
    const { dataMax } = chart.xAxis[0].getExtremes()
    const { minRange } = chart.xAxis[0].options

    chart.xAxis[0].setExtremes(dataMax - minRange, dataMax)
  }

  chartComponent = ReactDOM.render(
    <BinaryChart
    className="trade-chart"
    id="trade-chart0"
    contract={dataType === 'ticks' ? contractForChart : false}
    pipSize={pipSize}
    shiftMode="dynamic"
    ticks={chartData}
    getData={getData} // eslint-disable-line no-use-before-define
    type={chartType}
    hideToolbar={isMinHeight}
    hideTimeFrame={isMinHeight}
    onTypeChange={(type) => (chartType = type)}
    />, $('#chart')[0])
}

const updateTickListeners = () => new Promise(resolve => {
    listeners.ohlc = ticksService.monitor(symbol, granularity, response => {
      if (dataType === 'candles') {
        chartData = response
        resolve()
        updateChart()
      }
    })

    listeners.tick = ticksService.monitor(symbol, response => {
      if (dataType === 'ticks') {
        chartData = response
        resolve()
        updateChart()
      }
    })
  })

const getData = (start, end, newDataType, newGranularity) => {
  stopTickListeners()
  dataType = newDataType
  granularity = newGranularity
  return updateTickListeners()
}

const showRealityCheck = () => {
  $('.blocker').show()
  $('.reality-check').show()
}

const hideRealityCheck = () => {
  $('#rc-err').hide()
  $('.blocker').hide()
  $('.reality-check').hide()
}

const stopRealityCheck = () => {
  clearInterval(realityCheckTimeout)
  realityCheckTimeout = null
}

const realityCheckInterval = () => {
  realityCheckTimeout = setInterval(() => {
    const now = parseInt((new Date().getTime()) / 1000, 10)
    const checkTime = +getStorage('realityCheckTime')
    if (checkTime && now >= checkTime) {
      showRealityCheck()
      stopRealityCheck()
    }
  }, 1000)
}

{'fraction_non_alphanumeric': 0.15330610160837377, 'fraction_numerical': 0.0068930303803931585, 'mean_word_length': 3.83940704138357, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 40, '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': '3677872', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2459, 'n_words': 537}