<commit_before>class CommentListViewController extends KDListViewController
  constructor:->
    super
    @startListeners()

  instantiateListItems:(items)->

    newItems = []

    for comment, i in items
      
      nextComment = items[i+1]

      skipComment = no
      if nextComment? and comment.deletedAt
        if Date.parse(nextComment.meta.createdAt) > Date.parse(comment.deletedAt)
          skipComment = yes
      
      if not nextComment and comment.deletedAt
        skipComment = yes

      unless skipComment
        commentView = @getListView().addItem comment
        newItems.push commentView
    
    return newItems

  startListeners:->
    listView = @getListView()

    listView.on 'ItemWasAdded', (view, index)=>
      view.on 'CommentIsDeleted', ->
        listView.emit "CommentIsDeleted"
    
    listView.on "AllCommentsLinkWasClicked", (commentHeader)=>

      # some problems when logged out server doesnt responds
      @utils.wait 5000, -> listView.emit "BackgroundActivityFinished"
      
      @fetchAllComments 0, (err, comments)=>
        @removeAllItems()
        @instantiateListItems comments

    listView.registerListener
      KDEventTypes  : "CommentSubmitted"
      listener      : @
      callback      : (pubInst, reply)->
        model = listView.getData()
        listView.emit "BackgroundActivityStarted"
        model.reply reply, (err, reply)->
          # listView.emit "AllCommentsLinkWasClicked"
          listView.addItem reply
          listView.emit "OwnCommentHasArrived"
          listView.emit "BackgroundActivityFinished"

  fetchCommentsByRange:(from,to,callback)=>
    [to,callback] = [callback,to] unless callback
    query = {from,to}
    message = @getListView().getData()
    message.commentsByRange query,(err,comments)=>
      @getListView().emit "BackgroundActivityFinished"
      callback err,comments
  
  fetchAllComments:(skipCount=3, callback = noop)=>
    
    listView = @getListView()
    listView.emit "BackgroundActivityStarted"
    message = @getListView().getData()
    message.restComments skipCount, (err, comments)=>
      listView.emit "BackgroundActivityFinished"
      listView.emit "AllCommentsWereAdded"
      callback err, comments
  <commit_msg> Fetch comments 10 by 10 asynchronously<commit_after>class CommentListViewController extends KDListViewController
  constructor:->
    super
    @startListeners()

  instantiateListItems:(items, keepDeletedComments = no)->

    newItems = []

    for comment, i in items

      nextComment = items[i+1]

      skipComment = no
      if nextComment? and comment.deletedAt
        if Date.parse(nextComment.meta.createdAt) > Date.parse(comment.deletedAt)
          skipComment = yes

      if not nextComment and comment.deletedAt
        skipComment = yes

      skipComment = no if keepDeletedComments

      unless skipComment
        commentView = @getListView().addItem comment
        newItems.push commentView

    return newItems

  startListeners:->
    listView = @getListView()

    listView.on 'ItemWasAdded', (view, index)=>
      view.on 'CommentIsDeleted', ->
        listView.emit "CommentIsDeleted"

    listView.on "AllCommentsLinkWasClicked", (commentHeader)=>

      # some problems when logged out server doesnt responds
      @utils.wait 5000, -> listView.emit "BackgroundActivityFinished"

      {repliesCount} = listView.getData()

      log "Total comment: ", repliesCount
      stack = []
      removedAlready = no

      stack.push (callback) =>
        listView.emit "BackgroundActivityStarted"
        callback()

      for xto in [0..repliesCount] by 10
        do (xto) =>
          stack.push (callback) =>
            @fetchCommentsByRange xto, xto+10, (err, comments) =>
              if not removedAlready
                @removeAllItems()
                removedAlready = yes
              @instantiateListItems comments, yes
              callback err, comments

      async.parallel stack, (error, result) =>
        listView.emit "BackgroundActivityFinished"
        if not error
          listView.emit "AllCommentsWereAdded"
        else
          log "Failed to get comments..."

    listView.registerListener
      KDEventTypes  : "CommentSubmitted"
      listener      : @
      callback      : (pubInst, reply)->
        model = listView.getData()
        listView.emit "BackgroundActivityStarted"
        model.reply reply, (err, reply)->
          # listView.emit "AllCommentsLinkWasClicked"
          listView.addItem reply
          listView.emit "OwnCommentHasArrived"
          listView.emit "BackgroundActivityFinished"

  fetchCommentsByRange:(from,to,callback)=>
    [to,callback] = [callback,to] unless callback
    query = {from,to}
    message = @getListView().getData()
    message.commentsByRange query,(err,comments)=>
      @getListView().emit "BackgroundActivityFinished"
      callback err,comments

  fetchAllComments:(skipCount=3, callback = noop)=>

    listView = @getListView()
    listView.emit "BackgroundActivityStarted"
    message = @getListView().getData()
    message.restComments skipCount, (err, comments)=>
      listView.emit "BackgroundActivityFinished"
      listView.emit "AllCommentsWereAdded"
      callback err, comments

{'fraction_non_alphanumeric': 0.09170638703527169, 'fraction_numerical': 0.004194470924690181, 'mean_word_length': 2.784992784992785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '1564145', 'n_tokens_mistral': 1489, 'n_tokens_neox': 1406, 'n_words': 331}