<commit_before>// see https://www.discovermeteor.com/blog/template-level-subscriptions/

var debug = false;

/*

This template acts as the controller that sets and manages the reactive context 
for the embedded postsList template. It receives its parameters from a "caller" template.

The goal is to resubscribe when either one of the following events happens: 
- The external data context passed to the template from its parent changes
- The template's own internal rLimit ReactiveVar changes

In both cases, the template should resubscribe to the publication, and then once the subscription is ready
update the terms used by the template helper's Posts.find(). 

*/

Template.posts_list_controller.onCreated(function () {

  // 1. Initialization (*not* reactive!)
  var template = this;
  var terms = _.clone(template.data.terms);
  var limit = Settings.get('postsPerPage', 10);
  var postsSubscription;
  var usersSubscription;
  var subscriptionTerms;

  // initialize the reactive variables
  template.rTerms = new ReactiveVar(terms);
  template.rLimit = new ReactiveVar(limit);
  template.rReady = new ReactiveVar(false);

  // if caching is set to true, use Subs Manager. Else use template.subscribe. Default to false
  var enableCache = (typeof terms.enableCache === "undefined") ? false : terms.enableCache;
  var subscriber = enableCache ? Telescope.subsManager : template;

  // enable not subscribing to users on a per-controller basis
  var subscribeToUsers = (typeof terms.subscribeToUsers === "undefined") ? true : terms.subscribeToUsers;

  // Autorun 1: reruns when Template.currentData().terms changes (and new terms are different from old ones),
  // or when post limit changes. On first run, terms and limit won't have had time to change yet, so just
  // do nothing
  template.autorun(function (computation) {


    // add a dependency on data context to trigger the autorun
    var newTerms = Template.currentData().terms; // ⚡ reactive ⚡

    // get limit from local template variable
    var rLimit = template.rLimit.get(); // ⚡ reactive ⚡
        
    if (!computation.firstRun) {

      if (debug) {
        console.log('// ------ autorun 1 ------ //');
        console.log("terms: ", _.clone(terms));
        console.log("newTerms: ", _.clone(newTerms));
        console.log("isEqual?: ", _.isEqual(newTerms, terms));
        console.log("oldLimit: ", limit);
        console.log("newLimit: ", rLimit);
      }

      if (!_.isEqual(newTerms, terms)) {
        // Case 1: terms have changed
        // Note: if terms have changed, we reset the limit no matter its value

        // update terms
        terms = newTerms;

        // reset limit
        limit = Settings.get('postsPerPage', 10);

      } else if (rLimit !== limit) {
        // Case 2: limit has changed

        // update limit
        limit = rLimit;
      
      }

      // set template.rReady to false to trigger Autorun 2
      template.rReady.set(false);
    }

  });

  // Autorun 2: runs once on load, then reruns when template.rReady changes
  // On first run, we subscribe with the original terms andlimit
  template.autorun(function () {

    var ready = template.rReady.get(); // ⚡ reactive ⚡

    // if ready is false, (re)subscribe
    if (!ready) {
      
      subscriptionTerms = _.clone(terms);
      subscriptionTerms.limit = limit;

      if (debug) {<commit_msg>reset rLimit as well<commit_after>// see https://www.discovermeteor.com/blog/template-level-subscriptions/

var debug = false;

/*

This template acts as the controller that sets and manages the reactive context 
for the embedded postsList template. It receives its parameters from a "caller" template.

The goal is to resubscribe when either one of the following events happens: 
- The external data context passed to the template from its parent changes
- The template's own internal rLimit ReactiveVar changes

In both cases, the template should resubscribe to the publication, and then once the subscription is ready
update the terms used by the template helper's Posts.find(). 

*/

Template.posts_list_controller.onCreated(function () {

  // 1. Initialization (*not* reactive!)
  var template = this;
  var terms = _.clone(template.data.terms);
  var limit = Settings.get('postsPerPage', 10);
  var postsSubscription;
  var usersSubscription;
  var subscriptionTerms;

  // initialize the reactive variables
  template.rTerms = new ReactiveVar(terms);
  template.rLimit = new ReactiveVar(limit);
  template.rReady = new ReactiveVar(false);

  // if caching is set to true, use Subs Manager. Else use template.subscribe. Default to false
  var enableCache = (typeof terms.enableCache === "undefined") ? false : terms.enableCache;
  var subscriber = enableCache ? Telescope.subsManager : template;

  // enable not subscribing to users on a per-controller basis
  var subscribeToUsers = (typeof terms.subscribeToUsers === "undefined") ? true : terms.subscribeToUsers;

  // Autorun 1: reruns when Template.currentData().terms changes (and new terms are different from old ones),
  // or when post limit changes. On first run, terms and limit won't have had time to change yet, so just
  // do nothing
  template.autorun(function (computation) {


    // add a dependency on data context to trigger the autorun
    var newTerms = Template.currentData().terms; // ⚡ reactive ⚡

    // get limit from local template variable
    var rLimit = template.rLimit.get(); // ⚡ reactive ⚡
        
    if (!computation.firstRun) {

      if (debug) {
        console.log('// ------ autorun 1 ------ //');
        console.log("terms: ", _.clone(terms));
        console.log("newTerms: ", _.clone(newTerms));
        console.log("isEqual?: ", _.isEqual(newTerms, terms));
        console.log("oldLimit: ", limit);
        console.log("newLimit: ", rLimit);
      }

      if (!_.isEqual(newTerms, terms)) {
        // Case 1: terms have changed
        // Note: if terms have changed, we reset the limit no matter its value

        // update terms
        terms = newTerms;

        // reset limit
        limit = Settings.get('postsPerPage', 10);
        template.rLimit.set(limit);

      } else if (rLimit !== limit) {
        // Case 2: limit has changed

        // update limit
        limit = rLimit;
      
      }

      // set template.rReady to false to trigger Autorun 2
      template.rReady.set(false);
    }

  });

  // Autorun 2: runs once on load, then reruns when template.rReady changes
  // On first run, we subscribe with the original terms andlimit
  template.autorun(function () {

    var ready = template.rReady.get(); // ⚡ reactive ⚡

    // if ready is false, (re)subscribe
    if (!ready) {
      
      subscriptionTerms = _.clone(terms);
      subscriptionTerms.limit = limit;

      if (debug) {
{'fraction_non_alphanumeric': 0.10519346770634103, 'fraction_numerical': 0.0032367220832720317, 'mean_word_length': 2.900172117039587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1796706', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1861, 'n_words': 692}