<commit_before>
// ===== actions.jsx =====

var actions = Reflux.createActions({
  receivedPlanetUpdate: {},
  requestCursor: {},
  releaseCursor: {},
  loadSithLord: {asyncResult: true},
});

var activeRequests = {};

actions.loadSithLord.listen(function(cursor, url) {
  if(cursor in activeRequests)
    return;
  var promise = jQuery.get(url);
  promise.then(function(data) {
    delete activeRequests[cursor];
    actions.loadSithLord.completed(cursor, data);
  });
  activeRequests[cursor] = promise;
});

// Requirement: abort requests if a cursor is released and it is active
actions.releaseCursor.listen(function(cursor){
  if(activeRequests[cursor])
    activeRequests[cursor].abort();
});

// ===== store.jsx =====

var pendingRequests = {};
var requestedCursors = {};

//actions.loadSithLord.preEmit = function(cursor, url) {
//  if(url)
//    return undefined; // We know the URL of the Sith lord. Continue.
//  else {
//    if(cursor+1 in store.data.sithLords)
//      return [cursor, store.data.sithLords[cursor+1].master.url];
//    if(cursor-1 in store.data.sithLords)
//      return [cursor, store.data.sithLords[cursor-1].apprentice.url];
//    return undefined;
//  }
//};

actions.requestCursor.listen(function(cursor){
  if(!(cursor in requestedCursors))
    requestedCursors[cursor] = 0;
  requestedCursors[cursor] += 1;
  checkRequestedCursors();
});

actions.releaseCursor.preEmit = function(cursor) {
  requestedCursors[cursor] -= 1;
};

function checkRequestedCursors() {
  _.each(requestedCursors, function(value, cursor) {
    cursor = parseInt(cursor);
    if(cursor in store.data.sithLords)
      return; // Cursor already being loaded

    //if(cursor+1 in store.data.sithLords)
    //  actions.loadSithLord(cursor, store.data.sithLords[cursor+1].master.url);
    if(cursor-1 in store.data.sithLords && store.data.sithLords[cursor-1].loadState == 'done'){
      var url = store.data.sithLords[cursor-1].apprentice.url;
      if(url)
        actions.loadSithLord(cursor, url);
    }

    if(cursor+1 in store.data.sithLords && store.data.sithLords[cursor+1].loadState == 'done'){
      var url = store.data.sithLords[cursor+1].master.url;
      if(url)
        actions.loadSithLord(cursor, url);
    }
  });
};
//
//actions.loadSithLord.shouldEmit = function(cursor, url) {
//  if(activeRequests[cursor])
//    return false;
//  else{
//    activeRequests[cursor] = true;
//    return true;
//  }
//  //if(!url){
//  //  pendingRequests[cursor] = true;
//  //  return false;
//  //} else {
//  //  if(cursor in pendingRequests)
//  //    delete pendingRequests[cursor];
//  //  activeRequests[cursor] = true;
//  //  console.log(activeRequests);
//  //  return true;
//  //}
//};

var store = Reflux.createStore({
  init() {
    this.listenToMany(actions);
    this.data = {
      currentPlanet: {id: null, name: 'Unknown'},
      sithLords: {},
    };
  },<commit_msg>Minor cleanups<commit_after>
// ===== actions.jsx =====

var actions = Reflux.createActions({
  receivedPlanetUpdate: {},
  requestCursor: {},
  releaseCursor: {},
  loadSithLord: {asyncResult: true},
});

var activeRequests = {};

actions.loadSithLord.listen(function(cursor, url) {
  if(cursor in activeRequests)
    return;
  var promise = jQuery.get(url);
  promise.then(function(data) {
    delete activeRequests[cursor];
    actions.loadSithLord.completed(cursor, data);
  });
  activeRequests[cursor] = promise;
});

// Requirement: abort requests if a cursor is released and it is active
actions.releaseCursor.listen(function(cursor){
  if(activeRequests[cursor])
    activeRequests[cursor].abort();
});

// ===== store.jsx =====

var requestedCursors = {};

actions.requestCursor.listen(function(cursor){
  if(!(cursor in requestedCursors))
    requestedCursors[cursor] = 0;
  requestedCursors[cursor] += 1;
  checkRequestedCursors();
});

actions.releaseCursor.preEmit = function(cursor) {
  requestedCursors[cursor] -= 1;
};

function checkRequestedCursors() {
  _.each(requestedCursors, function(value, cursor) {
    cursor = parseInt(cursor);
    if(cursor in store.data.sithLords)
      return; // Cursor already being loaded

    //if(cursor+1 in store.data.sithLords)
    //  actions.loadSithLord(cursor, store.data.sithLords[cursor+1].master.url);
    if(cursor-1 in store.data.sithLords && store.data.sithLords[cursor-1].loadState == 'done'){
      var url = store.data.sithLords[cursor-1].apprentice.url;
      if(url)
        actions.loadSithLord(cursor, url);
    }

    if(cursor+1 in store.data.sithLords && store.data.sithLords[cursor+1].loadState == 'done'){
      var url = store.data.sithLords[cursor+1].master.url;
      if(url)
        actions.loadSithLord(cursor, url);
    }
  });
}

var store = Reflux.createStore({
  init() {
    this.listenToMany(actions);
    this.data = {
      currentPlanet: {id: null, name: 'Unknown'},
      sithLords: {},
    };
  },
{'fraction_non_alphanumeric': 0.16339468302658486, 'fraction_numerical': 0.005316973415132925, 'mean_word_length': 3.5540037243947857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1974951', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1682, 'n_words': 288}