<commit_before># The amount by which we increase the limit of returned results
LIMIT_INCREASE_STEP = 10

# List of all session variables that activate views with catalogs (used to determine infinite scrolling)
globals = @
globals.catalogActiveVariables = []

# Subscribe the client to catalog's documents
Template.catalog.created = ->
  variables = @data.variables

  # Make sure to access the global catalogActiveVariables variable
  globals.catalogActiveVariables = _.union globals.catalogActiveVariables, [variables.active]

  # We need a reset signal that will rerun the search
  # when the ready variable is set to false from the router
  reset = new Variable false
  wasReady = new Variable false

  @_resetSignalHandle?.stop()
  @_resetSignalHandle = Deps.autorun =>
    # Detect when ready is turned to false
    ready = Session.get(variables.ready)
    if wasReady() and not ready
      reset.set true
      wasReady.set false

  @_searchParametersHandle?.stop()
  @_searchParametersHandle = Deps.autorun =>
    # Every time filter or sort is changed, we reset counts
    # (We don't want to reset counts on currentLimit change)
    Session.get variables.filter
    Session.get variables.sort
    Session.set variables.ready, false
    Session.set variables.limit, INITIAL_CATALOG_LIMIT
    Session.set variables.limitIncreasing, false

  subscriptionHandle = null

  @_subscriptionAutorunHandle?.stop()
  @_subscriptionAutorunHandle = Deps.autorun =>
    # Listen for the reset signal, so the search is
    # rerun when ready is set to false from the outside
    reset()
    reset.set false
    if Session.get(variables.active) and Session.get(variables.limit)
      Session.set variables.loading, true
      # Make sure there is only one subscription being executed at once
      subscriptionHandle.stop() if subscriptionHandle
      subscriptionHandle = Meteor.subscribe @data.subscription, Session.get(variables.limit), Session.get(variables.filter), Session.get(variables.sort),
        onReady: =>
          Session.set variables.ready, true
          wasReady.set true

          Session.set variables.loading, false
        onError: ->
          # TODO: Should we display some error?
          Session.set variables.loading, false
    else
      Session.set variables.loading, false

  @_searchResultHandle?.stop()
  @_searchResultHandle = Deps.autorun =>
    fields = {}
    fields["count#{@data.documentClass.name}s"] = 1

    searchResultCursor = SearchResult.documents.find
      name: @data.subscription
      query: [Session.get(variables.filter), Session.get(variables.sort)]
    ,
      fields: fields

    # Store how many results there are
    searchResultCursor.observe
      added: (document) =>
        Session.set variables.count, document["count#{@data.documentClass.name}s"]
      changed: (newDocument, oldDocument) =>
        Session.set variables.count, newDocument["count#{@data.documentClass.name}s"]

Template.catalog.destroyed = ->
  @_resetSignalHandle?.stop()
  @_resetSignalHandle = null
  @_searchParametersHandle?.stop()
  @_searchParametersHandle = null
  @_subscriptionAutorunHandle?.stop()
  @_subscriptionAutorunHandle = null
  @_searchResultHandle?.stop()
  @_searchResultHandle = null

Template.catalogFilter.documentsName = ->
  @documentClass.verboseNamePlural()

Template.catalogFilter.filter = ->
  Session.get(@variables.filter) or ''

Template.catalogSort.field = ->
  index = Session.get @variables.sort
  @documentClass.PUBLISH_CATALOG_SORT[index].name
<commit_msg>Don't use class names directly.<commit_after># The amount by which we increase the limit of returned results
LIMIT_INCREASE_STEP = 10

# List of all session variables that activate views with catalogs (used to determine infinite scrolling)
globals = @
globals.catalogActiveVariables = []

# Subscribe the client to catalog's documents
Template.catalog.created = ->
  variables = @data.variables

  # Make sure to access the global catalogActiveVariables variable
  globals.catalogActiveVariables = _.union globals.catalogActiveVariables, [variables.active]

  # We need a reset signal that will rerun the search
  # when the ready variable is set to false from the router
  reset = new Variable false
  wasReady = new Variable false

  @_resetSignalHandle?.stop()
  @_resetSignalHandle = Deps.autorun =>
    # Detect when ready is turned to false
    ready = Session.get(variables.ready)
    if wasReady() and not ready
      reset.set true
      wasReady.set false

  @_searchParametersHandle?.stop()
  @_searchParametersHandle = Deps.autorun =>
    # Every time filter or sort is changed, we reset counts
    # (We don't want to reset counts on currentLimit change)
    Session.get variables.filter
    Session.get variables.sort
    Session.set variables.ready, false
    Session.set variables.limit, INITIAL_CATALOG_LIMIT
    Session.set variables.limitIncreasing, false

  subscriptionHandle = null

  @_subscriptionAutorunHandle?.stop()
  @_subscriptionAutorunHandle = Deps.autorun =>
    # Listen for the reset signal, so the search is
    # rerun when ready is set to false from the outside
    reset()
    reset.set false
    if Session.get(variables.active) and Session.get(variables.limit)
      Session.set variables.loading, true
      # Make sure there is only one subscription being executed at once
      subscriptionHandle.stop() if subscriptionHandle
      subscriptionHandle = Meteor.subscribe @data.subscription, Session.get(variables.limit), Session.get(variables.filter), Session.get(variables.sort),
        onReady: =>
          Session.set variables.ready, true
          wasReady.set true

          Session.set variables.loading, false
        onError: ->
          # TODO: Should we display some error?
          Session.set variables.loading, false
    else
      Session.set variables.loading, false

  @_searchResultHandle?.stop()
  @_searchResultHandle = Deps.autorun =>
    fields = {}
    fields["count#{ @data.documentClass.Meta.collection._name }"] = 1

    searchResultCursor = SearchResult.documents.find
      name: @data.subscription
      query: [Session.get(variables.filter), Session.get(variables.sort)]
    ,
      fields: fields

    # Store how many results there are
    searchResultCursor.observe
      added: (document) =>
        Session.set variables.count, document["count#{ @data.documentClass.Meta.collection._name }"]
      changed: (newDocument, oldDocument) =>
        Session.set variables.count, newDocument["count#{ @data.documentClass.Meta.collection._name }"]

Template.catalog.destroyed = ->
  @_resetSignalHandle?.stop()
  @_resetSignalHandle = null
  @_searchParametersHandle?.stop()
  @_searchParametersHandle = null
  @_subscriptionAutorunHandle?.stop()
  @_subscriptionAutorunHandle = null
  @_searchResultHandle?.stop()
  @_searchResultHandle = null

Template.catalogFilter.documentsName = ->
  @documentClass.verboseNamePlural()

Template.catalogFilter.filter = ->
  Session.get(@variables.filter) or ''

Template.catalogSort.field = ->
  index = Session.get @variables.sort
  @documentClass.PUBLISH_CATALOG_SORT[index].name

{'fraction_non_alphanumeric': 0.08848314606741572, 'fraction_numerical': 0.0008426966292134832, 'mean_word_length': 3.753671562082777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '5029839', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1932, 'n_words': 547}