<commit_before>EventEmitter2 = require 'eventemitter2'

{APIHandler} = require 'shared'
routes = require './routes'
ErrorMonitoring = require 'shared/src/helpers/error-monitoring'

coachAPIHandler = null
channel = new EventEmitter2 wildcard: true, maxListeners: routes.length * 2
isPending = -> false
# HACK - WORKAROUND
# MediaBodyView.prototype.initializeConceptCoach calls this multiple times
# (triggered by back-button and most perhaps search)
IS_INITIALIZED = false

updateHeadersWithToken = (token) ->
  coachAPIHandler?.updateXHR(
    headers:
      Authorization: "Bearer #{token}"
  )

getAPIOptions = (baseURL) ->
  coachAPIOptions =
    xhr:
      baseURL: baseURL
    # on set.access_token event, set headers with token
    events: [['set.access_token', updateHeadersWithToken]]
    handlers:
      onFail: (args...) ->
        # broadcast out error for final error handling by notification modal
        coachAPIHandler?.channel.emit('error', args...)
    isLocal: window.__karma__

initialize = (baseUrl) ->
  coachAPIHandler = new APIHandler(getAPIOptions(baseUrl), routes, channel) unless IS_INITIALIZED
  IS_INITIALIZED = true

  # export coach api handler things for each access
  module.exports.isPending = coachAPIHandler.records.isPending
  module.exports.channel = coachAPIHandler.channel
  ErrorMonitoring.start()

  coachAPIHandler

destroy = ->
  coachAPIHandler.destroy()
  IS_INITIALIZED = false

module.exports = {initialize, destroy, channel, isPending}<commit_msg>call stop() on coach destroy<commit_after>EventEmitter2 = require 'eventemitter2'

{APIHandler} = require 'shared'
routes = require './routes'
ErrorMonitoring = require 'shared/src/helpers/error-monitoring'

coachAPIHandler = null
channel = new EventEmitter2 wildcard: true, maxListeners: routes.length * 2
isPending = -> false
# HACK - WORKAROUND
# MediaBodyView.prototype.initializeConceptCoach calls this multiple times
# (triggered by back-button and most perhaps search)
IS_INITIALIZED = false

updateHeadersWithToken = (token) ->
  coachAPIHandler?.updateXHR(
    headers:
      Authorization: "Bearer #{token}"
  )

getAPIOptions = (baseURL) ->
  coachAPIOptions =
    xhr:
      baseURL: baseURL
    # on set.access_token event, set headers with token
    events: [['set.access_token', updateHeadersWithToken]]
    handlers:
      onFail: (args...) ->
        # broadcast out error for final error handling by notification modal
        coachAPIHandler?.channel.emit('error', args...)
    isLocal: window.__karma__

initialize = (baseUrl) ->
  coachAPIHandler = new APIHandler(getAPIOptions(baseUrl), routes, channel) unless IS_INITIALIZED
  IS_INITIALIZED = true

  # export coach api handler things for each access
  module.exports.isPending = coachAPIHandler.records.isPending
  module.exports.channel = coachAPIHandler.channel
  ErrorMonitoring.start()

  coachAPIHandler

destroy = ->
  coachAPIHandler.destroy()
  ErrorMonitoring.stop()
  IS_INITIALIZED = false

module.exports = {initialize, destroy, channel, isPending}
{'fraction_non_alphanumeric': 0.09537953795379538, 'fraction_numerical': 0.0026402640264026403, 'mean_word_length': 4.383658969804618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '5255370', 'n_tokens_mistral': 882, 'n_tokens_neox': 904, 'n_words': 232}