Add timeouted, s/for/reduce

This commit is contained in:
Ilya Ig. Petrov 2017-02-06 18:37:23 +00:00
parent a460ed85d2
commit a6b5b7ab57
4 changed files with 28 additions and 19 deletions

View File

@ -48,6 +48,12 @@
}, },
timeouted(cb = self.mandatory) {
return (...args) => setTimeout(cb.bind(null, ...args), 0)
},
chromified(cb = self.mandatory(), ...replaceArgs) { chromified(cb = self.mandatory(), ...replaceArgs) {
// Take error first callback and convert it to chrome API callback. // Take error first callback and convert it to chrome API callback.

View File

@ -2,7 +2,7 @@
{ // Private namespace { // Private namespace
const chromified = window.utils.chromified; const timeouted = window.utils.timeouted;
function errorJsonReplacer(key, value) { function errorJsonReplacer(key, value) {
@ -204,10 +204,10 @@
chrome.proxy.settings.get( chrome.proxy.settings.get(
{}, {},
chromified((err, details) => handlers.isControllable(details)) timeouted( handlers.isControllable.bind(handlers) )
); );
chrome.notifications.onClicked.addListener( chromified( (_, notId) => { chrome.notifications.onClicked.addListener( timeouted( (notId) => {
chrome.notifications.clear(notId); chrome.notifications.clear(notId);
if(notId === 'no-control') { if(notId === 'no-control') {
@ -221,7 +221,7 @@
handlers.installListenersOn(window, 'BG'); handlers.installListenersOn(window, 'BG');
chrome.proxy.onProxyError.addListener( chromified( (_, details) => { chrome.proxy.onProxyError.addListener( timeouted( (details) => {
if (!handlers.ifControlled) { if (!handlers.ifControlled) {
return; return;
@ -241,7 +241,7 @@
})); }));
chrome.proxy.settings.onChange.addListener( chromified((_, details) => { chrome.proxy.settings.onChange.addListener( timeouted( (details) => {
console.log('Proxy settings changed.', details); console.log('Proxy settings changed.', details);
const noCon = 'no-control'; const noCon = 'no-control';

View File

@ -5,6 +5,7 @@
const mandatory = window.utils.mandatory; const mandatory = window.utils.mandatory;
const throwIfError = window.utils.throwIfError; const throwIfError = window.utils.throwIfError;
const chromified = window.utils.chromified; const chromified = window.utils.chromified;
const timeouted = window.utils.timeouted;
const kitchenStartsMark = '\n\n//%#@@@@@@ PAC_KITCHEN_STARTS @@@@@@#%'; const kitchenStartsMark = '\n\n//%#@@@@@@ PAC_KITCHEN_STARTS @@@@@@#%';
const kitchenState = window.utils.createStorage('pac-kitchen-'); const kitchenState = window.utils.createStorage('pac-kitchen-');
@ -256,7 +257,7 @@
details details
? resolve(details) ? resolve(details)
: chrome.proxy.settings.get({}, chromified( (_, res) => resolve(res))) : chrome.proxy.settings.get({}, timeouted(resolve) )
).then( (details) => { ).then( (details) => {
@ -365,8 +366,8 @@
pacKitchen.checkIncontinence(); pacKitchen.checkIncontinence();
chrome.proxy.settings.onChange.addListener( chrome.proxy.settings.onChange.addListener(
chromified( timeouted(
(_, details) => pacKitchen.checkIncontinence(details) pacKitchen.checkIncontinence.bind(pacKitchen)
) )
); );

View File

@ -24,6 +24,7 @@
const mandatory = window.utils.mandatory; const mandatory = window.utils.mandatory;
const throwIfError = window.utils.throwIfError; const throwIfError = window.utils.throwIfError;
const chromified = window.utils.chromified; const chromified = window.utils.chromified;
const timeouted = window.utils.timeouted;
const clarifyThen = window.apis.errorsLib.clarifyThen; const clarifyThen = window.apis.errorsLib.clarifyThen;
const Warning = window.apis.errorsLib.Warning; const Warning = window.apis.errorsLib.Warning;
@ -64,7 +65,7 @@
if (err) { if (err) {
return cb(err); return cb(err);
} }
chrome.proxy.settings.get({}, chromified((_, details) => { chrome.proxy.settings.get({}, timeouted( (details) => {
if ( !window.utils.areSettingsControlledFor( details ) ) { if ( !window.utils.areSettingsControlledFor( details ) ) {
@ -118,21 +119,20 @@
} }
// Employ all urls, the latter are fallbacks for the former. // Employ all urls, the latter are fallbacks for the former.
let pacDataPromise = Promise.reject(); const pacDataPromise = provider.pacUrls.reduce(
for(const url of provider.pacUrls) { (promise, url) => promise.catch(
() => new Promise(
pacDataPromise = pacDataPromise.catch(
(err) => new Promise(
(resolve, reject) => httpLib.get( (resolve, reject) => httpLib.get(
url, url,
(newErr, pacData) => newErr ? reject(newErr) : resolve(pacData) (newErr, pacData) => newErr ? reject(newErr) : resolve(pacData)
) )
) )
); ),
Promise.reject()
} );
pacDataPromise.then( pacDataPromise.then(
(pacData) => { (pacData) => {
setPacAsync( setPacAsync(
@ -144,11 +144,13 @@
); );
}, },
clarifyThen( clarifyThen(
'Не удалось скачать PAC-скрипт с адресов: [ ' 'Не удалось скачать PAC-скрипт с адресов: [ '
+ provider.pacUrls.join(' , ') + ' ].', + provider.pacUrls.join(' , ') + ' ].',
cb cb
) )
); );
}); });
@ -274,7 +276,7 @@
chrome.storage.local.clear( chrome.storage.local.clear(
() => chrome.storage.local.set( () => chrome.storage.local.set(
onlySettable, onlySettable,
chromified(cb, onlySettable) chromified(cb)
) )
); );
@ -427,7 +429,7 @@
const antiCensorRu = window.apis.antiCensorRu; const antiCensorRu = window.apis.antiCensorRu;
chrome.alarms.onAlarm.addListener( chrome.alarms.onAlarm.addListener(
chromified((_, alarm) => { timeouted( (alarm) => {
if (alarm.name === antiCensorRu._periodicUpdateAlarmReason) { if (alarm.name === antiCensorRu._periodicUpdateAlarmReason) {
console.log( console.log(