<commit_before>import motionInput from 'motion-input';
import Service from '../core/Service';
import serviceManager from '../core/serviceManager';

const SERVICE_ID = 'service:motion-input';


/**
 * Interface of the client `'motion-input'` service.
 *
 * This service provides a wrapper for the
 * [`motionInput`]{@link https://github.com/collective-soundworks/motion-input}
 * external module.
 *
 * @memberof module:soundworks/client
 * @example
 * // in the experince constructor
 * this.motionInput = this.require('motioninput', { descriptors: ['energy'] });
 * // when the experience has started
 * if (this.motionInput.isAvailable('energy')) {
 *   this.motionInput.addListener('energy', (data) => {
 *     // digest motion data
 *   });
 * } else {
 *   // handle error
 * }
 */
class MotionInput extends Service {
  /** _<span class="warning">__WARNING__</span> This class should never be instanciated manually_ */
  constructor() {
    super(SERVICE_ID, false);

    const defaults = {
      descriptors: [],
      // @todo - how to handle if only some descriptors are invalid ?
      // showError: false,
    };

    this.configure(defaults);
    // @todo - should be handled directly inside the motionInput
    this._descriptorsValidity = {}
  }

  /**
   * Override default configure to add descriptors from multiple calls.
   * @private
   * @param {Object} options - The options to apply to the service.
   */
  configure(options) {
    if (this.options.descriptors)
      options.descriptors = this.options.descriptors.concat(options.descriptors);

    super.configure(options);
  }

  /** @private */
  start() {
    super.start();

    motionInput
      .init(...this.options.descriptors)
      .then((modules) => {
        this.options.descriptors.forEach((name, index) => {
          this._descriptorsValidity[name] = modules[index].isValid;
        });

        this.ready();
      });
  }

  /** @private */
  stop() {
    super.stop();
  }

  /**
   * Define if a given descriptor is available or not
   * @param {String} name - Descriptor name.
   * @returns {Boolean} - Returns `true` if available, `false` otherwise.
   */
  isAvailable(name) {
    return this._descriptorsValidity[name];
  }<commit_msg>Get motion input descriptor periods<commit_after>import motionInput from 'motion-input';
import Service from '../core/Service';
import serviceManager from '../core/serviceManager';

const SERVICE_ID = 'service:motion-input';


/**
 * Interface of the client `'motion-input'` service.
 *
 * This service provides a wrapper for the
 * [`motionInput`]{@link https://github.com/collective-soundworks/motion-input}
 * external module.
 *
 * @memberof module:soundworks/client
 * @example
 * // in the experince constructor
 * this.motionInput = this.require('motioninput', { descriptors: ['energy'] });
 * // when the experience has started
 * if (this.motionInput.isAvailable('energy')) {
 *   this.motionInput.addListener('energy', (data) => {
 *     // digest motion data
 *   });
 * } else {
 *   // handle error
 * }
 */
class MotionInput extends Service {
  /** _<span class="warning">__WARNING__</span> This class should never be instanciated manually_ */
  constructor() {
    super(SERVICE_ID, false);

    const defaults = {
      descriptors: [],
      // @todo - how to handle if only some descriptors are invalid ?
      // showError: false,
    };

    this.configure(defaults);
    // @todo - should be handled directly inside the motionInput
    this._descriptorsValidity = {};
    this._descriptorsPeriod = {};
  }

  /**
   * Override default configure to add descriptors from multiple calls.
   * @private
   * @param {Object} options - The options to apply to the service.
   */
  configure(options) {
    if (this.options.descriptors)
      options.descriptors = this.options.descriptors.concat(options.descriptors);

    super.configure(options);
  }

  /** @private */
  start() {
    super.start();

    motionInput
      .init(...this.options.descriptors)
      .then((modules) => {
        this.options.descriptors.forEach((name, index) => {
          this._descriptorsValidity[name] = modules[index].isValid;
          this._descriptorsPeriod[name] = modules[index].period;
        });

        this.ready();
      });
  }

  /** @private */
  stop() {
    super.stop();
  }

  /**
   * Define if a given descriptor is available or not
   * @param {String} name - Descriptor name.
   * @returns {Boolean} - Returns `true` if available, `false` otherwise.
   */
  isAvailable(name) {
    return this._descriptorsValidity[name];
  }

  /**
   * Returns the event period of a descriptor
   * @param {String} name - Descriptor name.
   * @returns {Number} - Event period (in seconds).
   */
  getPeriod(name) {
    return this._descriptorsPeriod[name];
  }
{'fraction_non_alphanumeric': 0.1398834304746045, 'fraction_numerical': 0.0, 'mean_word_length': 3.004166666666667, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '2118242', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1387, 'n_words': 356}