<commit_before>"use strict"

//module initialization
module.exports = function (logClass) {

  //Constants
  const STATUS_QUERY_TIME = 30000; // 30 seconds

  //Includes
  var Playlist              = require("./playlist")(logClass);
  var DefaultMediaReceiver  = require('castv2-client').DefaultMediaReceiver;;
  var EventEmitter          = require('events').EventEmitter;
  var util                  = require("util");

  var log = logClass ? logClass : require("./dummyLogClass")("PersistentPlayer");

  //PersistentPlayer class
  class PersistentPlayer extends EventEmitter {

    constructor (name, client) {

      super();
      let that = this;

      that._name = name;
      that._client = client;

      //List of events triggered by PersistentPlayer:
      that.EVENT_CONNECTED    = "playerConnected";
      that.EVENT_DISCONNECTED = "playerDisconnected";
      that.EVENT_PLAYING      = "playerPlaying";
      that.EVENT_STOPPED      = "playerStopped";
      that.EVENT_STATUS       = "playerStatus";


      //Register for client events
      that._client.on(that._client.EVENT_STATUS,       that._clientStatus.bind(that));
      that._client.on(that._client.EVENT_DISCONNECTED, that._clientDisconnected.bind(this));

      //Create playlist
      that._playlist = new Playlist(that._name, that);
    }


    //Play URL -> playlist.addUrl(arguments)
    playUrl (url, options) {

      let that = this;

      log.info ("%s - Try to play url - %s", that._name, url);

      if (that._playlist)
        delete that._playlist;
      that._playlist = new Playlist(that._name, that);

      return that._playlist.addUrlPromise(url, options)
      .catch (function(err) {
        log.error("%s - Error playing playlist - %s", that._name, err.stack);
        return Promise.reject(err);
      });
    }


    //pause
    pausePromise () {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("pause", player.pause.bind(player));});
    }

    //play
    playPromise () {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("play", player.play.bind(player));})
      .then(function () {return that.getStatusPromise(); });
    }

    //stop
    stopPromise () {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("stop", player.stop.bind(player));})
      .then(function () {return that.getStatusPromise(); });
    }

    //seek
    seekPromise (currentTime) {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("seek", player.seek.bind(player, currentTime));})
      .then(function () { return that.getStatusPromise(true)});
    }

    //getStatusPromise
    getStatusPromise (wait = false) {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("getStatus", player.getStatus.bind(player));})
      .then (function () {
        if (wait)
          return new Promise(function (resolve, reject) {
            that.once(that.EVENT_STATUS, function (status){
              return resolve(status);
            });
          });
        else
          return that.getStatus();
      });
    }

    //getStatus
    getStatus () {
      let that = this;
      return that._status;
    }

    //getPreviousStatus
    getPreviousStatus () {
      let that = this;
      return that._previousStatus;
    }

    //PLAYLIST
    //get current playlist index
    getCurrentPlaylistIndex() {
      let that = this;
      if (that._playlist)
        return that._playlist.getIndexForId(that.getCurrentPlaylistId());
      else
        return undefined;
    }

    //get current playlist itemID
    getCurrentPlaylistId() {
      let that = this;
      return that._status.currentItemId;
    }

    //get full playlist
    getPlaylist(id) {
      let that = this;
      if (that._playlist)
        return that._playlis.getAll;
      else
        return undefined;
    }

    //get playlist item with itemID
    getPlaylistItemWithId(id) {
      let that = this;
      if (that._playlist)
        return that._playlist.getItemWithId(id);
      else
        return undefined;
    }

    //get playlist item with playlist index
    getPlaylistItemWithIndex(index) {
      let that = this;
      if (that._playlist)
        return that._playlist.getItemWithIndex(id);
      else
        return undefined;
    }
<commit_msg>Fix incorrect reference to that._playlist (#12)<commit_after>"use strict"

//module initialization
module.exports = function (logClass) {

  //Constants
  const STATUS_QUERY_TIME = 30000; // 30 seconds

  //Includes
  var Playlist              = require("./playlist")(logClass);
  var DefaultMediaReceiver  = require('castv2-client').DefaultMediaReceiver;;
  var EventEmitter          = require('events').EventEmitter;
  var util                  = require("util");

  var log = logClass ? logClass : require("./dummyLogClass")("PersistentPlayer");

  //PersistentPlayer class
  class PersistentPlayer extends EventEmitter {

    constructor (name, client) {

      super();
      let that = this;

      that._name = name;
      that._client = client;

      //List of events triggered by PersistentPlayer:
      that.EVENT_CONNECTED    = "playerConnected";
      that.EVENT_DISCONNECTED = "playerDisconnected";
      that.EVENT_PLAYING      = "playerPlaying";
      that.EVENT_STOPPED      = "playerStopped";
      that.EVENT_STATUS       = "playerStatus";


      //Register for client events
      that._client.on(that._client.EVENT_STATUS,       that._clientStatus.bind(that));
      that._client.on(that._client.EVENT_DISCONNECTED, that._clientDisconnected.bind(this));

      //Create playlist
      that._playlist = new Playlist(that._name, that);
    }


    //Play URL -> playlist.addUrl(arguments)
    playUrl (url, options) {

      let that = this;

      log.info ("%s - Try to play url - %s", that._name, url);

      if (that._playlist)
        delete that._playlist;
      that._playlist = new Playlist(that._name, that);

      return that._playlist.addUrlPromise(url, options)
      .catch (function(err) {
        log.error("%s - Error playing playlist - %s", that._name, err.stack);
        return Promise.reject(err);
      });
    }


    //pause
    pausePromise () {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("pause", player.pause.bind(player));});
    }

    //play
    playPromise () {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("play", player.play.bind(player));})
      .then(function () {return that.getStatusPromise(); });
    }

    //stop
    stopPromise () {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("stop", player.stop.bind(player));})
      .then(function () {return that.getStatusPromise(); });
    }

    //seek
    seekPromise (currentTime) {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("seek", player.seek.bind(player, currentTime));})
      .then(function () { return that.getStatusPromise(true)});
    }

    //getStatusPromise
    getStatusPromise (wait = false) {
      let that = this;

      return that._getConnectedPlayerPromise()
      .then (function (player) {return that._playerActionPromise ("getStatus", player.getStatus.bind(player));})
      .then (function () {
        if (wait)
          return new Promise(function (resolve, reject) {
            that.once(that.EVENT_STATUS, function (status){
              return resolve(status);
            });
          });
        else
          return that.getStatus();
      });
    }

    //getStatus
    getStatus () {
      let that = this;
      return that._status;
    }

    //getPreviousStatus
    getPreviousStatus () {
      let that = this;
      return that._previousStatus;
    }

    //PLAYLIST
    //get current playlist index
    getCurrentPlaylistIndex() {
      let that = this;
      if (that._playlist)
        return that._playlist.getIndexForId(that.getCurrentPlaylistId());
      else
        return undefined;
    }

    //get current playlist itemID
    getCurrentPlaylistId() {
      let that = this;
      return that._status.currentItemId;
    }

    //get full playlist
    getPlaylist(id) {
      let that = this;
      if (that._playlist)
        return that._playlist.getAll();
      else
        return undefined;
    }

    //get playlist item with itemID
    getPlaylistItemWithId(id) {
      let that = this;
      if (that._playlist)
        return that._playlist.getItemWithId(id);
      else
        return undefined;
    }

    //get playlist item with playlist index
    getPlaylistItemWithIndex(index) {
      let that = this;
      if (that._playlist)
        return that._playlist.getItemWithIndex(id);
      else
        return undefined;
    }

{'fraction_non_alphanumeric': 0.1163065677738364, 'fraction_numerical': 0.0019348597226701064, 'mean_word_length': 2.416819684171869, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6779218', 'n_tokens_mistral': 2690, 'n_tokens_neox': 2613, 'n_words': 615}