<commit_before>var sp = require('serialport'),
    _ = require('lodash'),
    async = require('async'),
    EventEmitter2 = require('eventemitter2').EventEmitter2,
    globalSerialDeviceLoader,
    subscribers = [],
    lookupIntervalId = null,
    isRunning = false;

module.exports = globalSerialDeviceLoader = {

  /**
   * Creates a deviceloader.
   * @param  {Object}   Device The constructor function of the device.
   * @param  {Function} filter The filter function that will filter the needed devices.
   * @return {Object}          Represents a SerialDeviceLoader.
   */
  create: function(Device, filter) {
    var sub = new EventEmitter2({
      wildcard: true,
      delimiter: ':',
      maxListeners: 1000 // default would be 10!
    });
    sub.Device = Device;
    sub.filter = filter;
    sub.oldDevices = [];
    sub.newDevices = [];

    /**
     * Calls the callback with an array of devices.
     * @param  {Array}    ports    When called within this file this are the listed system ports. [optional]
     * @param  {Function} callback The function, that will be called when finished lookup.
     *                             `function(err, devices){}` devices is an array of Device objects.
     */
    sub.lookup = function(ports, callback) {
      if (!callback) {
        callback = ports;
        ports = null;
      }

      if (this.newDevices.length > 0) {
        if (callback) { callback(null, this.newDevices); }
        return;
      } else {
        var self = this;
        globalSerialDeviceLoader.lookup(function(err) {
          if (callback) { callback(err, self.newDevices); }
        });
      }
    };

    /**
     * Calls lookup function with optional callback
     * and emits 'plug' for new attached devices
     * and 'unplug' for removed devices.
     * @param  {Function} callback The function, that will be called when finished triggering. [optional]
     *                             `function(err, devices){}` devices is an array of Device objects.
     */
    sub.trigger = function(callback) {
      var self = this;
      globalSerialDeviceLoader.trigger(function(err) {
        if (callback) { callback(err, self.newDevices); }
      });
    };

    /**
     * Starts to lookup.
     * @param  {Number}   interval The interval milliseconds. [optional]
     * @param  {Function} callback The function, that will be called when trigger has started. [optional]
     *                             `function(err, devices){}` devices is an array of Device objects.
     */
    sub.startLookup = function(interval, callback) {
      if (!callback && _.isFunction(interval)) {
        callback = interval;
        interval = null;
      }

      subscribers.push(this);

      var self = this;
      if (isRunning) {
        if (callback) { callback(null, self.newDevices); }
        return;
      } else {
        globalSerialDeviceLoader.startLookup(interval, function(err) {
          if (callback) { callback(err, self.newDevices); }
        });
      }
    };

    /**
     * Removes itself as subscriber.
     * If last stops the interval that calls trigger function.
     */
    sub.stopLookup = function() {
      var self = this;
      if (!isRunning) {
        return;
      } else {
        subscribers = _.reject(function(s) {
          return s === self;
        });
        if (subscribers.length === 0) {
          globalSerialDeviceLoader.stopLookup();
        }
        return;
      }
    };

    return sub;
  },

  /**
   * Calls the callback when finished.
   * @param  {Function} callback The function, that will be called when finished lookup.
   *                             `function(err){}`
   */
  lookup: function(callback) {
    sp.list(function(err, ports) {
      if (err && callback) { return callback(err); }

      async.forEach(subscribers, function(s, callback) {
        if (s) {
          var resPorts = s.filter(ports);

          var devices = _.map(resPorts, function(p) {
            var found = _.find(s.oldDevices, function(dev) {
              return dev.get('portName') === p.comName;
            });
            if (found) {
              return found;
            } else {
              return new s.Device(p.comName);
            }
          }) || [];

          s.newDevices = devices;
        }

        callback(null);
      }, function(err) {
        if (callback) { return callback(err); }
      });
    });
  },

  /**
   * Calls lookup function with optional callback
   * and emits 'plug' for new attached devices
   * and 'unplug' for removed devices.
   * @param  {Function} callback The function, that will be called when finished triggering. [optional]
   *                             `function(err){}`
   */
  trigger: function(callback) {
    globalSerialDeviceLoader.lookup(function(err) {
      if (err && callback) { return callback(err); }

      async.forEach(subscribers, function(s, callback) {
        if (s.oldDevices.length !== s.newDevices.length) {
          var devs = [];
          if (s.oldDevices.length > s.newDevices.length) {
            devs = _.difference(s.oldDevices, s.newDevices);
            _.each(devs, function(d) {
              if (s.log) s.log('unplug device with id ' + device.id);
              s.emit('unplug', d);
            });
          } else {
            devs = _.difference(s.newDevices, s.oldDevices);
            _.each(devs, function(d) {
              if (s.log) s.log('plug device with id ' + device.id);
              s.emit('plug', d);
            });
          }

          s.oldDevices = s.newDevices;
        }
        
        callback(null);
      }, function(err) {
        if (callback) { return callback(err); }
      });

    });
  },

  /**
   * Starts to lookup.
   * @param  {Number}   interval The interval milliseconds. [optional]
   * @param  {Function} callback The function, that will be called when trigger has started. [optional]
   *                             `function(err){}`
   */
  startLookup: function(interval, callback) {
    if (lookupIntervalId) {
      return;
    }

    isRunning = true;

    if (!callback && _.isFunction(interval)) {
      callback = interval;
      interval = null;
    }

    interval = interval || 500;
<commit_msg>little fix<commit_after>var sp = require('serialport'),
    _ = require('lodash'),
    async = require('async'),
    EventEmitter2 = require('eventemitter2').EventEmitter2,
    globalSerialDeviceLoader,
    subscribers = [],
    lookupIntervalId = null,
    isRunning = false;

module.exports = globalSerialDeviceLoader = {

  /**
   * Creates a deviceloader.
   * @param  {Object}   Device The constructor function of the device.
   * @param  {Function} filter The filter function that will filter the needed devices.
   * @return {Object}          Represents a SerialDeviceLoader.
   */
  create: function(Device, filter) {
    var sub = new EventEmitter2({
      wildcard: true,
      delimiter: ':',
      maxListeners: 1000 // default would be 10!
    });
    sub.Device = Device;
    sub.filter = filter;
    sub.oldDevices = [];
    sub.newDevices = [];

    /**
     * Calls the callback with an array of devices.
     * @param  {Array}    ports    When called within this file this are the listed system ports. [optional]
     * @param  {Function} callback The function, that will be called when finished lookup.
     *                             `function(err, devices){}` devices is an array of Device objects.
     */
    sub.lookup = function(ports, callback) {
      if (!callback) {
        callback = ports;
        ports = null;
      }

      if (this.newDevices.length > 0) {
        if (callback) { callback(null, this.newDevices); }
        return;
      } else {
        var self = this;
        globalSerialDeviceLoader.lookup(function(err) {
          if (callback) { callback(err, self.newDevices); }
        });
      }
    };

    /**
     * Calls lookup function with optional callback
     * and emits 'plug' for new attached devices
     * and 'unplug' for removed devices.
     * @param  {Function} callback The function, that will be called when finished triggering. [optional]
     *                             `function(err, devices){}` devices is an array of Device objects.
     */
    sub.trigger = function(callback) {
      var self = this;
      globalSerialDeviceLoader.trigger(function(err) {
        if (callback) { callback(err, self.newDevices); }
      });
    };

    /**
     * Starts to lookup.
     * @param  {Number}   interval The interval milliseconds. [optional]
     * @param  {Function} callback The function, that will be called when trigger has started. [optional]
     *                             `function(err, devices){}` devices is an array of Device objects.
     */
    sub.startLookup = function(interval, callback) {
      if (!callback && _.isFunction(interval)) {
        callback = interval;
        interval = null;
      }

      subscribers.push(this);

      var self = this;
      if (isRunning) {
        if (callback) { callback(null, self.newDevices); }
        return;
      } else {
        globalSerialDeviceLoader.startLookup(interval, function(err) {
          if (callback) { callback(err, self.newDevices); }
        });
      }
    };

    /**
     * Removes itself as subscriber.
     * If last stops the interval that calls trigger function.
     */
    sub.stopLookup = function() {
      var self = this;
      if (!isRunning) {
        return;
      } else {
        subscribers = _.reject(function(s) {
          return s === self;
        });
        if (subscribers.length === 0) {
          globalSerialDeviceLoader.stopLookup();
        }
        return;
      }
    };

    return sub;
  },

  /**
   * Calls the callback when finished.
   * @param  {Function} callback The function, that will be called when finished lookup.
   *                             `function(err){}`
   */
  lookup: function(callback) {
    sp.list(function(err, ports) {
      if (err && callback) { return callback(err); }

      async.forEach(subscribers, function(s, callback) {
        if (s) {
          var resPorts = s.filter(ports);

          var devices = _.map(resPorts, function(p) {
            var found = _.find(s.oldDevices, function(dev) {
              return dev.get('portName') === p.comName;
            });
            if (found) {
              return found;
            } else {
              return new s.Device(p.comName);
            }
          }) || [];

          s.newDevices = devices;
        }

        callback(null);
      }, function(err) {
        if (callback) { return callback(err); }
      });
    });
  },

  /**
   * Calls lookup function with optional callback
   * and emits 'plug' for new attached devices
   * and 'unplug' for removed devices.
   * @param  {Function} callback The function, that will be called when finished triggering. [optional]
   *                             `function(err){}`
   */
  trigger: function(callback) {
    globalSerialDeviceLoader.lookup(function(err) {
      if (err && callback) { return callback(err); }

      async.forEach(subscribers, function(s, callback) {
        if (s && s.oldDevices.length !== s.newDevices.length) {
          var devs = [];
          if (s.oldDevices.length > s.newDevices.length) {
            devs = _.difference(s.oldDevices, s.newDevices);
            _.each(devs, function(d) {
              if (s.log) s.log('unplug device with id ' + device.id);
              s.emit('unplug', d);
            });
          } else {
            devs = _.difference(s.newDevices, s.oldDevices);
            _.each(devs, function(d) {
              if (s.log) s.log('plug device with id ' + device.id);
              s.emit('plug', d);
            });
          }

          s.oldDevices = s.newDevices;
        }

        callback(null);
      }, function(err) {
        if (callback) { return callback(err); }
      });

    });
  },

  /**
   * Starts to lookup.
   * @param  {Number}   interval The interval milliseconds. [optional]
   * @param  {Function} callback The function, that will be called when trigger has started. [optional]
   *                             `function(err){}`
   */
  startLookup: function(interval, callback) {
    if (lookupIntervalId) {
      return;
    }

    isRunning = true;

    if (!callback && _.isFunction(interval)) {
      callback = interval;
      interval = null;
    }

    interval = interval || 500;

{'fraction_non_alphanumeric': 0.12078922040423484, 'fraction_numerical': 0.002406159769008662, 'mean_word_length': 1.9380301602262018, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2623380', 'n_tokens_mistral': 3552, 'n_tokens_neox': 3483, 'n_words': 952}