<commit_before>import getAndInitNewEpisodes from "../lib/getAndInitNewEpisodes"
import episodesToString from "../lib/episodesToString";
var ipc = nRequire("ipc");

export default {
  name: "fetchLoop",
  after: "store",
  initialize: function(container, application) {
    var store = container.lookup("store:main");
    var epController = container.lookup("controller:episodes");
    var apController = container.lookup("controller:application");
    
    var checkForEp = function(){
      if(apController.get("isUpdating")) { return; }

      apController.set("isUpdating", true);

      getAndInitNewEpisodes(epController.currentUser, store).then(function(episodes) {
        epController.get("episodes").unshiftObjects(episodes);

        episodes.forEach(function(episode) {
          episode.loading();
        });

        apController.set("isUpdating", false);

        if(episodes.get("length")){
          new Notification("New episodes", {
            body: episodesToString(episodes)
          });

          ipc.send("newBackgroundEpisodes", 1);
        }
      }, function(err) {
        apController.set("isUpdating", false);
      });
    }

    var checkForNewMagnets = function(){
      if(apController.get("isReloading")) { return; }

      apController.set("isReloading", true);
      store.find("episode", {
        seen: false, 
        removed: false,
        magnet: null
      }).then(function(episodes) {
        var promises = episodes.map(function(episode) {
          return new Promise(function(resolve, reject) {
            episode.loading(resolve, reject);
          });
        });

        Ember.RSVP.allSettled(promises).then(function(){
          // Remove all episodes which doesn't have a magnet link
          var filteredEps = episodes.reject(function(episode) {
            return ! episode.get("magnet");
          });

          if(filteredEps.length){
            new Notification("New magnet links", {
              body: episodesToString(filteredEps)
            });

            ipc.send("newBackgroundEpisodes", 1);
          }

          apController.set("isReloading", false);
        }, function() {
          apController.set("isReloading", false);
        });

      }, function(err) {
        apController.set("isReloading", false);
      });
    }

    var updateMagnets = function(){
      store.find("episode", {
        seen: false, 
        removed: false
      }).then(function(episodes) {
        episodes.forEach(function(episode) {
          episode.loading();
        });
      });
    }

    // Update releases every 60 min
    setInterval(checkForEp, 1 * 60 * 60 * 1000);
    // Find new magnets every 30 min
    setInterval(checkForNewMagnets, 1 * 30 * 60 * 1000);
    // Update magnets every 40 min
    setInterval(updateMagnets, 1 * 40 * 60 * 1000);

    var check = function() {
      checkForEp();
      checkForNewMagnets();
      updateMagnets();
    };

    // Check all on start up
    check();

    // Check when online
    window.addEventListener("online",  check);<commit_msg>Wait 5 sec on connectivity before checking for eps<commit_after>import getAndInitNewEpisodes from "../lib/getAndInitNewEpisodes"
import episodesToString from "../lib/episodesToString";
var ipc = nRequire("ipc");

export default {
  name: "fetchLoop",
  after: "store",
  initialize: function(container, application) {
    var store = container.lookup("store:main");
    var epController = container.lookup("controller:episodes");
    var apController = container.lookup("controller:application");
    
    var checkForEp = function(){
      if(apController.get("isUpdating")) { return; }

      apController.set("isUpdating", true);

      getAndInitNewEpisodes(epController.currentUser, store).then(function(episodes) {
        epController.get("episodes").unshiftObjects(episodes);

        episodes.forEach(function(episode) {
          episode.loading();
        });

        apController.set("isUpdating", false);

        if(episodes.get("length")){
          new Notification("New episodes", {
            body: episodesToString(episodes)
          });

          ipc.send("newBackgroundEpisodes", 1);
        }
      }, function(err) {
        apController.set("isUpdating", false);
      });
    }

    var checkForNewMagnets = function(){
      if(apController.get("isReloading")) { return; }

      apController.set("isReloading", true);
      store.find("episode", {
        seen: false, 
        removed: false,
        magnet: null
      }).then(function(episodes) {
        var promises = episodes.map(function(episode) {
          return new Promise(function(resolve, reject) {
            episode.loading(resolve, reject);
          });
        });

        Ember.RSVP.allSettled(promises).then(function(){
          // Remove all episodes which doesn't have a magnet link
          var filteredEps = episodes.reject(function(episode) {
            return ! episode.get("magnet");
          });

          if(filteredEps.length){
            new Notification("New magnet links", {
              body: episodesToString(filteredEps)
            });

            ipc.send("newBackgroundEpisodes", 1);
          }

          apController.set("isReloading", false);
        }, function() {
          apController.set("isReloading", false);
        });

      }, function(err) {
        apController.set("isReloading", false);
      });
    }

    var updateMagnets = function(){
      store.find("episode", {
        seen: false, 
        removed: false
      }).then(function(episodes) {
        episodes.forEach(function(episode) {
          episode.loading();
        });
      });
    }

    var minute = 1000 * 60;

    // Update releases every 60 min
    setInterval(checkForEp, 60 * minute);
    // Find new magnets every 30 min
    setInterval(checkForNewMagnets, * 30 * minute);
    // Update magnets every 40 min
    setInterval(updateMagnets, 40 * minute);

    var check = function() {
      checkForEp();
      checkForNewMagnets();
      updateMagnets();
    };

    // Check when online
    window.addEventListener("online",  function() {
      // Wait 5 sec to ensure connectivity
      setTimeout(check, 5000);
    });

    // Check all on start up
    check();
{'fraction_non_alphanumeric': 0.1303234069804675, 'fraction_numerical': 0.009766250400256164, 'mean_word_length': 2.409934497816594, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3600905', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1733, 'n_words': 351}