<commit_before>async          = require 'async'
{EventEmitter} = require 'events'
_              = require 'lodash'
debug           = require('debug')('meshblu-connector-skype:index')

class Connector extends EventEmitter
  constructor: ({@Lync}) ->
    @Lync ?= require './lync-manager'

  start: (device, callback) =>
    @onConfig device, (error) =>
      return callback error if error
      setInterval @_refreshCurrentState, 5000
      @_refreshCurrentState null, callback

  close: (callback) =>
    return callback()

  onConfig: (device, callback=->) =>
    @_computeState (error, state) =>
      return callback error if error
      return @_emitNoClient {state}, callback unless state.hasClient

      desiredState = _.get device, 'desiredState', {}
      return callback() if _.isEmpty desiredState

      @_handleDesiredState desiredState, (error) =>
        if error
          console.error error.stack
          return callback error

        @_refreshCurrentState desiredState: {}, callback

  _refreshCurrentState: (update=null, callback=->) =>
    @_computeState (error, state) =>
      return callback error if error?
      @_emitUpdate _.defaults({state}, update), callback

  _emitNoClient: ({state}, callback) =>
    @emit 'error', new Error('Cannot find running Lync Process')
    @_emitUpdate {state}, callback

  _emitUpdate: (update, callback) =>
    return callback() if _.isEqual update, @_previousUpdate<commit_msg>Let's call it 'Last Job', I guess<commit_after>async          = require 'async'
{EventEmitter} = require 'events'
_              = require 'lodash'
debug           = require('debug')('meshblu-connector-skype:index')

class Connector extends EventEmitter
  constructor: ({@Lync}) ->
    @Lync ?= require './lync-manager'
    @worker = async.queue @_handleDesiredState, 1

  start: (device, callback) =>
    @onConfig device, (error) =>
      return callback error if error
      setInterval @_refreshCurrentState, 5000
      @_refreshCurrentState null, callback

  close: (callback) =>
    return callback()

  onConfig: (device, callback=->) =>
    @_computeState (error, state) =>
      return callback error if error
      return @_emitNoClient {state}, callback unless state.hasClient

      desiredState = _.get device, 'desiredState', {}
      return callback() if _.isEmpty desiredState
      return callback() if _.isEqual @_lastJob, desiredState
      @_lastJob = desiredState

      @worker.push desiredState, (error) =>
        @_lastJob = null if _.isEqual desiredState, @_lastJob

        if error
          console.error error.stack
          return callback error

        @_refreshCurrentState desiredState: {}, callback

  _refreshCurrentState: (update=null, callback=->) =>
    @_computeState (error, state) =>
      return callback error if error?
      @_emitUpdate _.defaults({state}, update), callback

  _emitNoClient: ({state}, callback) =>
    @emit 'error', new Error('Cannot find running Lync Process')
    @_emitUpdate {state}, callback

  _emitUpdate: (update, callback) =>
    return callback() if _.isEqual update, @_previousUpdate
{'fraction_non_alphanumeric': 0.11336553945249597, 'fraction_numerical': 0.002898550724637681, 'mean_word_length': 3.2723521320495186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '2080668', 'n_tokens_mistral': 867, 'n_tokens_neox': 841, 'n_words': 257}