<commit_before>/*global _:true, App:true, Backbone:true */
/*jshint forin:false, plusplus:false, sub:true */
'use strict';

define([
  'zepto',
  'currency-data',
  'collections/currencies',
  'collections/denominations',
  'models/currency',
  'models/denomination'
], function($, CurrencyData, CurrenciesCollection, DenominationsCollection, Currency, Denomination) {
  if (!window._faceValueDataCache) {
    window._faceValueDataCache = {}
  }

  var GLOBALS = {
    DEFAULT_FOREIGN_CURRENCY: 'EUR',
    DEFAULT_HOME_CURRENCY: 'USD',
    HAS: {
      nativeScroll: (function() {
        return 'WebkitOverflowScrolling' in window.document.createElement('div').style
      })()
    },
    TIME_TO_UPDATE: 3600 * 24 // Update currencies/denominations every day
  }

  var dataCache = window._faceValueDataCache

  function initialize(callback) {
    if (GLOBALS.HAS.nativeScroll) {
      $('body').addClass('native-scroll')
    }

    // TODO: Improve this.
    // This is a hacky cache-bust thing while I figure out a better way...
    cacheReady()

    // OMG, this is how we cache bust for now.
    window.applicationCache.addEventListener('cached', function() {
      cacheReady()
      window.location.reload()
    })

    window.applicationCache.addEventListener('updateready', function() {
      cacheReady()
      window.location.reload()
    })

    if (get('cacheReady')) {
      $('body').removeClass('cache-loading')
    } else {
      $('#loading-text').animate({opacity: 1})
    }

    CurrenciesCollection.fetch({
      success: function() {
        DenominationsCollection.fetch({
          success: function() {
            if (!get('foreignCurrency')) {
              set('foreignCurrency', GLOBALS.DEFAULT_FOREIGN_CURRENCY)
            }

            if (!get('homeCurrency')) {
              set('homeCurrency', GLOBALS.DEFAULT_HOME_CURRENCY)
            }

            // Update currency/denomination info every hour.
            if (!get('lastTimeCurrencyDataUpdated') ||
                timestamp() - get('lastTimeCurrencyDataUpdated') > GLOBALS.TIME_TO_UPDATE) {
              CurrencyData.update(function(worth) {
                set('worth', worth)

                fetchCurrencyData(callback)
              })
            } else {
              callback()
            }
          }
        })
      }
    })
  }

  function cacheReady() {
    $('body').removeClass('cache-loading')
    set('cacheReady', true)
  }

  function get(key, fallback) {
    if (dataCache[key] !== undefined) {
      // console.info('cache hit', '### ' + key + ' ###', dataCache[key])
      return dataCache[key]
    } else if (window.localStorage[key] !== undefined) {
      dataCache[key] = JSON.parse(window.localStorage[key])
      // console.info('cache miss', '### ' + key + ' ###', dataCache[key])
      return dataCache[key]
    } else {
      // console.info('not found')
      return fallback
    }
  }

  function fetchCurrencyData(callback) {
    $.ajax({
      url: 'denominations.json',
      dataType: 'json',
      success: function(results) {
        updateCurrencyData(results)

        callback()
      },
      error: function() {
        console.log('failed')
      }
    })
  }

  function set(key, value) {
    window.localStorage[key] = JSON.stringify(value)
    return dataCache[key] = value
  }

  function timestamp(date) {
    if (!date) {
      date = new Date()
    }

    return Math.round(date.getTime() / 1000)
  }

  function updateCurrencyData(currencyData) {
    for (var i in currencyData) {
      var currency = CurrenciesCollection.where({code: i})[0];

      // This currency doesn't exist yet, so we'll create it.
      if (!currency) {
        currency = new Currency({code: i})
        CurrenciesCollection.add(currency)
      }

      currency.set(_.pick(currencyData[i],
        'fraction',<commit_msg>Launch app even when currency update fails<commit_after>/*global _:true, App:true, Backbone:true */
/*jshint forin:false, plusplus:false, sub:true */
'use strict';

define([
  'zepto',
  'currency-data',
  'collections/currencies',
  'collections/denominations',
  'models/currency',
  'models/denomination'
], function($, CurrencyData, CurrenciesCollection, DenominationsCollection, Currency, Denomination) {
  if (!window._faceValueDataCache) {
    window._faceValueDataCache = {}
  }

  var GLOBALS = {
    DEFAULT_FOREIGN_CURRENCY: 'EUR',
    DEFAULT_HOME_CURRENCY: 'USD',
    HAS: {
      nativeScroll: (function() {
        return 'WebkitOverflowScrolling' in window.document.createElement('div').style
      })()
    },
    TIME_TO_UPDATE: 3600 * 24 // Update currencies/denominations every day
  }

  var dataCache = window._faceValueDataCache

  function initialize(callback) {
    if (GLOBALS.HAS.nativeScroll) {
      $('body').addClass('native-scroll')
    }

    // TODO: Improve this.
    // This is a hacky cache-bust thing while I figure out a better way...
    cacheReady()

    // OMG, this is how we cache bust for now.
    window.applicationCache.addEventListener('cached', function() {
      cacheReady()
      window.location.reload()
    })

    window.applicationCache.addEventListener('updateready', function() {
      cacheReady()
      window.location.reload()
    })

    if (get('cacheReady')) {
      $('body').removeClass('cache-loading')
    } else {
      $('#loading-text').animate({opacity: 1})
    }

    CurrenciesCollection.fetch({
      success: function() {
        DenominationsCollection.fetch({
          success: function() {
            if (!get('foreignCurrency')) {
              set('foreignCurrency', GLOBALS.DEFAULT_FOREIGN_CURRENCY)
            }

            if (!get('homeCurrency')) {
              set('homeCurrency', GLOBALS.DEFAULT_HOME_CURRENCY)
            }

            // Update currency/denomination info every hour.
            if (!get('lastTimeCurrencyDataUpdated') ||
                timestamp() - get('lastTimeCurrencyDataUpdated') > GLOBALS.TIME_TO_UPDATE) {
              CurrencyData.update(function(worth) {
                set('worth', worth)

                fetchCurrencyData(callback)
              })
            } else {
              callback()
            }
          }
        })
      }
    })
  }

  function cacheReady() {
    $('body').removeClass('cache-loading')
    set('cacheReady', true)
  }

  function get(key, fallback) {
    if (dataCache[key] !== undefined) {
      // console.info('cache hit', '### ' + key + ' ###', dataCache[key])
      return dataCache[key]
    } else if (window.localStorage[key] !== undefined) {
      dataCache[key] = JSON.parse(window.localStorage[key])
      // console.info('cache miss', '### ' + key + ' ###', dataCache[key])
      return dataCache[key]
    } else {
      // console.info('not found')
      return fallback
    }
  }

  function fetchCurrencyData(callback) {
    $.ajax({
      url: 'denominations.json',
      dataType: 'json',
      success: function(results) {
        updateCurrencyData(results)

        callback()
      },
      // We're likely offline, so we won't try to update the currency data,
      // and instead just load whatever data we currency have.
      error: function() {
        callback()
      }
    })
  }

  function set(key, value) {
    window.localStorage[key] = JSON.stringify(value)
    return dataCache[key] = value
  }

  function timestamp(date) {
    if (!date) {
      date = new Date()
    }

    return Math.round(date.getTime() / 1000)
  }

  function updateCurrencyData(currencyData) {
    for (var i in currencyData) {
      var currency = CurrenciesCollection.where({code: i})[0];

      // This currency doesn't exist yet, so we'll create it.
      if (!currency) {
        currency = new Currency({code: i})
        CurrenciesCollection.add(currency)
      }

      currency.set(_.pick(currencyData[i],
        'fraction',
{'fraction_non_alphanumeric': 0.13745704467353953, 'fraction_numerical': 0.0030546009927453228, 'mean_word_length': 2.483156028368794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '867727', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2298, 'n_words': 464}