Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
cleanup new configuration style
Browse files Browse the repository at this point in the history
  • Loading branch information
amasad committed Sep 1, 2014
1 parent bc69d36 commit d915407
Showing 1 changed file with 76 additions and 29 deletions.
105 changes: 76 additions & 29 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
(function() {
'use strict';

/**
* Constants
*/

var FLO_CONFIG_KEY = 'flo-config';

/**
* Flo client controller.
*
Expand All @@ -37,18 +43,14 @@
}

/**
* Save and optionally set new config.
* Save current config to disk.
*
* @param {object} config
* @private
*/

FloClient.prototype.saveConfig = function() {
chrome.runtime.sendMessage({
name : 'localStorage:set',
key : 'flo-config',
data : JSON.stringify(this.config)
});
saveConfig(this.config);
};

/**
Expand Down Expand Up @@ -289,49 +291,94 @@
});
};

/**
* Save passed in config object to disk.
*
* @param {object} config
* @private
*/

function saveConfig(config) {
chrome.runtime.sendMessage({
name: 'localStorage:set',
key: FLO_CONFIG_KEY,
data: JSON.stringify(config)
});
}

/**
* Loads config from storage.
*
* @param {function} done
* @private
*/

function loadConfig(done) {
var config,
parseConfig = function (config) {
try {
config = JSON.parse(config);
}
catch (ex) {
config = {};
var configJSON = tryToLoadLegacyConfig();

if (configJSON) {
var config = parseConfig(configJSON);
// Persist new config to the new storage.
saveConfig(config);
setTimeout(done.bind(null, config), 0);
}
else {
chrome.runtime.sendMessage(
{
name : 'localStorage:get',
key : FLO_CONFIG_KEY
},
function (configJSON){
var config = parseConfig(configJSON);
done(config);
}
);
}
}

config.sites = config.sites || [];
config.port = config.port || 8888;
/**
* Parses config and sets sensible defaults.
*
* @param {string} config
* @param {}
* @private
*/

done(config);
};
function parseConfig(configJSON) {
var config;

if ((config = loadLegacyConfig())) {
window.setTimeout(parseConfig.bind(null, config), 0);
try {
config = JSON.parse(configJSON);
}
else {
chrome.runtime.sendMessage({
name : 'localStorage:get',
key : 'flo-config'
}, parseConfig);
catch (ex) {
config = {};
}

config.sites = config.sites || [];
config.port = config.port || 8888;

return config;
}

function loadLegacyConfig() {
/**
* Tries to load config from localstorage which was the old way of storing
* config and returns false if it fails.
*
* @return {string} config
* @private
*/

function tryToLoadLegacyConfig() {;
var config = null;

try {
if (window.localStorage && window.localStorage.hasOwnProperty('flo-config')) {
config = window.localStorage.getItem('flo-config');
window.localStorage.removeItem('flo-config');
var config = window.localStorage && localStorage.getItem(FLO_CONFIG_KEY);
if (config) {
localStorage.removeItem(FLO_CONFIG_KEY);
}
} catch (e) {
return false;
}
catch (ex) {}

return config;
}
Expand Down

0 comments on commit d915407

Please sign in to comment.