added config manager (closes #18) and fixed crash at win
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
const assets=require('assets');
|
||||
|
||||
let watchers=Object.create(null);
|
||||
let lastWatchCode=1;
|
||||
|
||||
const toBoolean=v => {
|
||||
if(v=='false' || v==false) return false;
|
||||
return true;
|
||||
};
|
||||
|
||||
const get=key => {
|
||||
let confVal=localStorage.getItem('config.'+key);
|
||||
if(confVal===null) return assets.get('config')[key];
|
||||
return confVal;
|
||||
};
|
||||
const getB=key => toBoolean(get(key));
|
||||
const getN=key => +get(key);
|
||||
const getS=key => ''+get(key);
|
||||
|
||||
const set=(key, value) => {
|
||||
localStorage.setItem('config.'+key, value);
|
||||
let interested=watchers[key];
|
||||
if(interested) interested.forEach(watcher => watcher(key, value));
|
||||
};
|
||||
|
||||
const remove=key => {
|
||||
localStorage.removeItem('config.'+key, value);
|
||||
let interested=watchers[key];
|
||||
if(interested) interested.forEach(watcher => watcher(key, assets.get('config')[key]));
|
||||
};
|
||||
const clear=() =>
|
||||
Object
|
||||
.keys(assets.get('config'))
|
||||
.forEach(remove);
|
||||
|
||||
const watch=(key, fn) => {
|
||||
if(!watchers[key]) watchers[key]=[];
|
||||
const code='w'+lastWatchCode++;
|
||||
watchers[key][code]=fn;
|
||||
return code;
|
||||
};
|
||||
const watchB=(key, fn) => watch(key, (k, v) => fn(k, toBoolean(v)));
|
||||
const watchN=(key, fn) => watch(key, (k, v) => fn(k, +v));
|
||||
const watchS=(key, fn) => watch(key, (k, v) => fn(k, ''+v));
|
||||
|
||||
const unwatch=(key, code) => {
|
||||
if(!watchers[key]) return;
|
||||
delete watchers[key][code];
|
||||
};
|
||||
|
||||
const list=() =>
|
||||
Object
|
||||
.keys(assets.get('config'));
|
||||
const dict=() => {
|
||||
let dict=Object.create(null);
|
||||
Object
|
||||
.keys(assets.get('config'))
|
||||
.forEach(
|
||||
key => dict[key]={
|
||||
raw: get(key),
|
||||
b: getB(key),
|
||||
n: getN(key),
|
||||
s: getS(key)
|
||||
}
|
||||
);
|
||||
return dict;
|
||||
};
|
||||
|
||||
return module.exports={
|
||||
get, getB, getN, getS,
|
||||
set,
|
||||
remove, clear,
|
||||
watch, watchB, watchN, watchS,
|
||||
unwatch,
|
||||
list, dict
|
||||
};
|
||||
Reference in New Issue
Block a user