<commit_before>define [
  "underscore"
  "backbone"
  "config"
], (
  _
  Backbone
  Config
) ->
  class CommandStore
    commands: null
    fileEntry: null
    constructor: ->
      _.extend this, Backbone.Events
      @requestFileSystem(
        window.PERSISTENT
        10 * 1024 * 1024
        @onLoad.bind this
        @onLoadError.bind this
      )

      @on "initialised", @sync.bind this

    onLoad: (fs) ->
      @fs = fs
      @trigger "loaded"

    onLoadError: console.log.bind(console, "error")

    readFile: (file) ->
      reader = new FileReader
      reader.readAsText file
      self = this
      reader.onloadend = ->
        self.commands = JSON.parse(@result) if @result
        self.trigger "initialised"

    storeCommands: ->
      @fileEntry.createWriter ((fileWriter) =>
        blob = new Blob [JSON.stringify @commands], type: "application/json"

        fileWriter.write blob
        fileWriter.onwriteend = console.log.bind console, "Successfully wrote file"
        fileWriter.onerror = console.log.bind console, "Error writing file"
      ), console.log.bind(console, "Error creating writer")

    init: ->
      return @on "loaded", @init.bind(this, arguments) unless @fs
      @fs.root.getFile "commands.json", { create: true }, ((fileEntry) =>
        @fileEntry = fileEntry
        fileEntry.file @readFile.bind(this), @onLoadError.bind(this)
      ), console.log.bind(console, "error loading file")

    sync: ->
      return _.defer @trigger.bind(this, "synced") if @commands

      $.getJSON "#{Config.API_URL}/commands", (response) =>
        @commands = response
        @trigger "synced"
        @storeCommands()

    requestFileSystem: (
      window.requestFileSystem ||
      window.webkitRequestFileSystem
    ).bind(window)

  new CommandStore<commit_msg>Updated command fetching logic<commit_after>define [
  "underscore"
  "backbone"
  "config"
], (
  _
  Backbone
  Config
) ->
  class CommandStore
    commands: []
    lastSync: null
    fileEntry: null

    constructor: ->
      _.extend this, Backbone.Events
      @requestFileSystem(
        window.PERSISTENT
        10 * 1024 * 1024
        @onLoad.bind this
        @onLoadError.bind this
      )

      @on "initialised", @sync.bind this

    onLoad: (fs) ->
      @fs = fs
      @trigger "loaded"

    onLoadError: console.log.bind(console, "error")

    readFile: (file) ->
      reader = new FileReader
      reader.readAsText file
      self = this
      reader.onloadend = ->
        self.parseFile @result
        self.trigger "initialised"

    parseFile: (content) ->
      return unless content

      try
        json = JSON.parse content
      catch e
        console.log "Error parsing file"
        return

      @commands = json.commands
      @lastSync = json.lastSync

    storeCommands: ->
      @fileEntry.createWriter ((fileWriter) =>
        json = JSON.stringify {
          commands: @commands
          lastSync: @lastSync
        }
        blob = new Blob [json], type: "application/json"

        fileWriter.write blob
        fileWriter.onwriteend = \
          console.log.bind console, "Successfully wrote file"
        fileWriter.onerror = console.log.bind console, "Error writing file"
      ), console.log.bind(console, "Error creating writer")

    init: ->
      return @on "loaded", @init.bind(this, arguments) unless @fs
      @fs.root.getFile "commands.json", { create: true }, ((fileEntry) =>
        @fileEntry = fileEntry
        fileEntry.file @readFile.bind(this), @onLoadError.bind(this)
      ), console.log.bind(console, "Error loading file")

    sync: ->
      params = {}
      params["updatedSince"] = @lastSync if @lastSync

      $.getJSON("#{Config.API_URL}/commands", params)
        .success((response) =>
          if response.length
            @commands = response.concat @commands
            @lastSync = _.last(response).updatedAt # Response is sorted by
                                                   # ascending updatedAt
            @storeCommands()

          @trigger "synced"
        )
        .error(console.log.bind(console, "Error fetching commands"))

    requestFileSystem: (
      window.requestFileSystem ||
      window.webkitRequestFileSystem
    ).bind(window)

  new CommandStore
{'fraction_non_alphanumeric': 0.11332240693046125, 'fraction_numerical': 0.004682744088035589, 'mean_word_length': 2.231467473524962, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1540943', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1211, 'n_words': 290}