<commit_before>var { ToggleButton } = require('sdk/ui/button/toggle');
var panels = require("sdk/panel");
var Request = require("sdk/request").Request;
var { setTimeout } = require("sdk/timers");
var prefs = require('sdk/simple-prefs');

var exchange_rate        = false;
var exchange_rate_ts     = false;
var exchange_rate_expiry = 120*1000; // 2 min
var units = {
  "BTC": 1,
  "mBTC": 1000
};

var button = ToggleButton({
  id: "btc-button",
  label: "my button",
  icon: {
    "64": "./bitcoin-logo.svg"
  },
  onChange: handleChange
});

var panel = panels.Panel({
  contentURL: "./main.html",
  contentScriptFile: "./ui.js",
  contentStyleFile: "./main.css",
  width: 104,
  height: 100,
  onShow: handleShow,
  onHide: handleHide
});

function handleShow() {
  console.info("handleShow called, calling fetch_exchange_rate...");
  panel.port.emit("show", prefs.prefs['unit']);
	fetch_exchange_rate(false);
}

function handleChange(state) {
  if (state.checked) {
    panel.show({
      position: button
    });
  }
}

function handleHide() {
  button.state('window', {checked: false});
}

prefs.on("unit", function(prefName){
	exchange_rate        = false;
	exchange_rate_ts     = false;
});

var doConvert = function(btc) {
  console.info("doConvert called with btc=%s", btc);
  var usd = (exchange_rate*btc).toFixed(2);
  console.info("%s %s at exchange_rate %s = %s", btc, prefs.prefs['unit'], exchange_rate, usd);
  console.info("emitting newConversion...");
  //setTimeout(function(){
  panel.port.emit("newConversion", usd);
  //}, 1000);
};

panel.port.on("pleaseConvert", function(btc) {
  console.info("received pleaseConvert with btc=%s, calling fetch_exchange_rate...", btc);
  fetch_exchange_rate(doConvert, btc);
});

function fetch_exchange_rate(callback, btc) {
  console.info("fetch_exchange_rate called with btc=%s, checking exchange_rate_ts (%s) ...", btc, exchange_rate_ts);
  console.log("exchange_rate_ts+exchange_rate_expiry: ", exchange_rate_ts+exchange_rate_expiry);
  if (exchange_rate_ts && (exchange_rate_ts < exchange_rate_ts+exchange_rate_expiry)) {
  	console.info("exchange_rate_ts ok, checking if we should call callback...");
  	if (callback) {
  		console.info("calling callback...");
    	return callback(btc);
    }
    return;
  }
  console.info("exchange_rate_ts not ok, fetching new exchange rate...");
  Request({
  	url: "https://www.bitstamp.net/api/ticker/",
  	onComplete: function(response){
  	  exchange_rate    = response.json.last/units[prefs.prefs['unit']];
  		console.info("exchange_rate fetched, set to %s...", exchange_rate);
  	  exchange_rate_ts = Date.now();
  		panel.port.emit("newRate", exchange_rate);
  		console.info("checking if we should call callback...");
  		if (callback) {
  			console.info("calling callback...");
  			callback(btc);
  		}
  	}
  }).get();
}
<commit_msg>Fixed mBTC exchange rate becoming too long.<commit_after>var { ToggleButton } = require('sdk/ui/button/toggle');
var panels = require("sdk/panel");
var Request = require("sdk/request").Request;
var { setTimeout } = require("sdk/timers");
var prefs = require('sdk/simple-prefs');

var exchange_rate        = false;
var exchange_rate_ts     = false;
var exchange_rate_expiry = 120*1000; // 2 min
var units = {
  "BTC": 1,
  "mBTC": 1000
};

var button = ToggleButton({
  id: "btc-button",
  label: "my button",
  icon: {
    "64": "./bitcoin-logo.svg"
  },
  onChange: handleChange
});

var panel = panels.Panel({
  contentURL: "./main.html",
  contentScriptFile: "./ui.js",
  contentStyleFile: "./main.css",
  width: 104,
  height: 100,
  onShow: handleShow,
  onHide: handleHide
});

function handleShow() {
  console.info("handleShow called, calling fetch_exchange_rate...");
  panel.port.emit("show", prefs.prefs['unit']);
	fetch_exchange_rate(false);
}

function handleChange(state) {
  if (state.checked) {
    panel.show({
      position: button
    });
  }
}

function handleHide() {
  button.state('window', {checked: false});
}

prefs.on("unit", function(prefName){
	exchange_rate        = false;
	exchange_rate_ts     = false;
});

var doConvert = function(btc) {
  console.info("doConvert called with btc=%s", btc);
  var usd = (exchange_rate*btc).toFixed(2);
  console.info("%s %s at exchange_rate %s = %s", btc, prefs.prefs['unit'], exchange_rate, usd);
  console.info("emitting newConversion...");
  //setTimeout(function(){
  panel.port.emit("newConversion", usd);
  //}, 1000);
};

panel.port.on("pleaseConvert", function(btc) {
  console.info("received pleaseConvert with btc=%s, calling fetch_exchange_rate...", btc);
  fetch_exchange_rate(doConvert, btc);
});

function fetch_exchange_rate(callback, btc) {
  console.info("fetch_exchange_rate called with btc=%s, checking exchange_rate_ts (%s) ...", btc, exchange_rate_ts);
  console.log("exchange_rate_ts+exchange_rate_expiry: ", exchange_rate_ts+exchange_rate_expiry);
  if (exchange_rate_ts && (exchange_rate_ts < exchange_rate_ts+exchange_rate_expiry)) {
  	console.info("exchange_rate_ts ok, checking if we should call callback...");
  	if (callback) {
  		console.info("calling callback...");
    	return callback(btc);
    }
    return;
  }
  console.info("exchange_rate_ts not ok, fetching new exchange rate...");
  Request({
  	url: "https://www.bitstamp.net/api/ticker/",
  	onComplete: function(response){
  	  exchange_rate    = response.json.last/units[prefs.prefs['unit']];
  	  if ("mBTC" == prefs.prefs['unit']) {
  	  	exchange_rate = exchange_rate.toFixed(5);
  	  }
  		console.info("exchange_rate fetched, set to %s...", exchange_rate);
  	  exchange_rate_ts = Date.now();
  		panel.port.emit("newRate", exchange_rate);
  		console.info("checking if we should call callback...");
  		if (callback) {
  			console.info("calling callback...");
  			callback(btc);
  		}
  	}
  }).get();
}

{'fraction_non_alphanumeric': 0.16537867078825347, 'fraction_numerical': 0.009101837540786537, 'mean_word_length': 4.113257243195786, 'pattern_counts': {'":': 6, '<': 5, '<?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': '3890480', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2013, 'n_words': 403}