<commit_before>const { Howl } = require('howler');
const { isObjectWithValues } = require('./utils');

module.exports = {
  removeId(id) {
    Object.keys(this.playing).forEach((key) => this.playing[key].delete(id));
  },

  initialize(soundsData, dispatch) {
    // { String: Set[Integer] } Map of currently playing ids for each unique sound name
    // Can also use `new Map()`
    this.playing = Object.create(null);
    const soundNames = Object.getOwnPropertyNames(soundsData);
    this.sounds = soundNames.reduce((memo, name) => {
      let soundOptions = soundsData[name];

      // Allow strings instead of objects, for when all that is needed is a URL
      if (typeof soundOptions === 'string') {
        soundOptions = { src: [soundOptions] };
      }

      const sprites = soundOptions.sprite;
      if (isObjectWithValues(sprites)) {
        Object.keys(sprites).forEach((spriteName) => {
          this.playing[name + spriteName] = new Set();
        });
      } else {
        this.playing[name] = new Set();
      }
      return {
        ...memo,
        [name]: new Howl({
          ...soundOptions,
          onplay: (id) => {
            if (soundOptions.onplay) soundOptions.onplay(id, dispatch);
          },
          onend: (id) => {
            if (soundOptions.onend) soundOptions.onend(id, dispatch);
            this.removeId(id);
          },
          onstop: (id) => {
            if (soundOptions.onstop) soundOptions.onstop(id, dispatch);
            this.removeId(id);
          }
        })
      };
    }, {});

    return this.sounds;
  },

  add(soundsData, dispatch) {
    if (!isObjectWithValues(this.sounds)) return this.initialize(soundsData);
    const soundNames = Object.getOwnPropertyNames(soundsData);

    this.sounds = soundNames.reduce((memo, name) => {
      let soundOptions = soundsData[name];

      // Allow strings instead of objects, for when all that is needed is a URL
      if (typeof soundOptions === 'string') {
        soundOptions = { src: [soundOptions] };
      }

      const sprites = soundOptions.sprite;
      if (isObjectWithValues(sprites)) {
        Object.keys(sprites).forEach((spriteName) => {
          this.playing[name + spriteName] = new Set();
        });
      } else {
        this.playing[name] = new Set();
      }
      const { onplay, onstop, onend } = soundOptions;
      const result = {
        ...memo,
        [name]: new Howl({
          ...soundOptions,
          onplay: (id) => {
            if (onplay) onplay(id, dispatch);
          },
          onend: (id) => {
            if (onend) onend(id, dispatch);
            this.removeId(id);
            if (this.playlistIds[id]) this.playlistIds[id](id);
          },
          onstop: (id) => {
            if (onstop) onstop(id);
            this.removeId(id);
          }
        })
      };
      // Delete to keep the action serializable
      delete soundOptions.onplay;
      delete soundOptions.onend;
      delete soundOptions.onstop;
      return result;
    }, this.sounds);

    console.log(soundsData);
    console.log(this.playing);
    return this.sounds;
  },

  proxy(soundName, spriteName, name, ...args) {
    const sound = this.sounds[soundName];
    if (typeof sound === 'undefined') {
      return console.warn(`
      The sound '${soundName}' was requested, but redux-sounds doesn't have anything registered under that name.
      See https://github.com/joshwcomeau/redux-sounds#unregistered-sound
    `);
    }
    if (spriteName && typeof sound._sprite[spriteName] === 'undefined') {
      const validSprites = Object.keys(sound._sprite).join(', ');
      return console.warn(`
      The sound '${soundName}' was found, but it does not have a sprite specified for '${spriteName}'.
      It only has access to the following sprites: ${validSprites}.<commit_msg>remove debug console.log print outs<commit_after>const { Howl } = require('howler');
const { isObjectWithValues } = require('./utils');

module.exports = {
  removeId(id) {
    Object.keys(this.playing).forEach((key) => this.playing[key].delete(id));
  },

  initialize(soundsData, dispatch) {
    // { String: Set[Integer] } Map of currently playing ids for each unique sound name
    // Can also use `new Map()`
    this.playing = Object.create(null);
    const soundNames = Object.getOwnPropertyNames(soundsData);
    this.sounds = soundNames.reduce((memo, name) => {
      let soundOptions = soundsData[name];

      // Allow strings instead of objects, for when all that is needed is a URL
      if (typeof soundOptions === 'string') {
        soundOptions = { src: [soundOptions] };
      }

      const sprites = soundOptions.sprite;
      if (isObjectWithValues(sprites)) {
        Object.keys(sprites).forEach((spriteName) => {
          this.playing[name + spriteName] = new Set();
        });
      } else {
        this.playing[name] = new Set();
      }
      return {
        ...memo,
        [name]: new Howl({
          ...soundOptions,
          onplay: (id) => {
            if (soundOptions.onplay) soundOptions.onplay(id, dispatch);
          },
          onend: (id) => {
            if (soundOptions.onend) soundOptions.onend(id, dispatch);
            this.removeId(id);
          },
          onstop: (id) => {
            if (soundOptions.onstop) soundOptions.onstop(id, dispatch);
            this.removeId(id);
          }
        })
      };
    }, {});

    return this.sounds;
  },

  add(soundsData, dispatch) {
    if (!isObjectWithValues(this.sounds)) return this.initialize(soundsData);
    const soundNames = Object.getOwnPropertyNames(soundsData);

    this.sounds = soundNames.reduce((memo, name) => {
      let soundOptions = soundsData[name];

      // Allow strings instead of objects, for when all that is needed is a URL
      if (typeof soundOptions === 'string') {
        soundOptions = { src: [soundOptions] };
      }

      const sprites = soundOptions.sprite;
      if (isObjectWithValues(sprites)) {
        Object.keys(sprites).forEach((spriteName) => {
          this.playing[name + spriteName] = new Set();
        });
      } else {
        this.playing[name] = new Set();
      }
      const { onplay, onstop, onend } = soundOptions;
      const result = {
        ...memo,
        [name]: new Howl({
          ...soundOptions,
          onplay: (id) => {
            if (onplay) onplay(id, dispatch);
          },
          onend: (id) => {
            if (onend) onend(id, dispatch);
            this.removeId(id);
            if (this.playlistIds[id]) this.playlistIds[id](id);
          },
          onstop: (id) => {
            if (onstop) onstop(id);
            this.removeId(id);
          }
        })
      };
      // Delete to keep the action serializable
      delete soundOptions.onplay;
      delete soundOptions.onend;
      delete soundOptions.onstop;
      return result;
    }, this.sounds);

    return this.sounds;
  },

  proxy(soundName, spriteName, name, ...args) {
    const sound = this.sounds[soundName];
    if (typeof sound === 'undefined') {
      return console.warn(`
      The sound '${soundName}' was requested, but redux-sounds doesn't have anything registered under that name.
      See https://github.com/joshwcomeau/redux-sounds#unregistered-sound
    `);
    }
    if (spriteName && typeof sound._sprite[spriteName] === 'undefined') {
      const validSprites = Object.keys(sound._sprite).join(', ');
      return console.warn(`
      The sound '${soundName}' was found, but it does not have a sprite specified for '${spriteName}'.
      It only has access to the following sprites: ${validSprites}.
{'fraction_non_alphanumeric': 0.13637555730396014, 'fraction_numerical': 0.0, 'mean_word_length': 2.252452025586354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '3796840', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2236, 'n_words': 539}