Swap events for requests

This commit is contained in:
Ilya Ig. Petrov 2017-02-23 06:39:54 +00:00
parent 8c88d70398
commit f20da20e48
8 changed files with 58 additions and 22 deletions

View File

@ -0,0 +1 @@
grep -r addEvent ./ --exclude-dir=vendor --exclude-dir=node_modules

View File

@ -2,7 +2,7 @@
{ {
const IF_DEBUG = false; const IF_DEBUG = true;
if (!IF_DEBUG) { if (!IF_DEBUG) {
// I believe logging objects precludes them from being GCed. // I believe logging objects precludes them from being GCed.
@ -18,6 +18,10 @@
}); });
} }
const privates = {
requestToResponder: {},
};
const self = window.utils = { const self = window.utils = {
mandatory() { mandatory() {
@ -84,15 +88,34 @@
}, },
addEventHandler(type, handler) { assert(value) {
document.addEventListener(type, (event) => handler(...event.detail)); if(!value) {
console.assert(value);
throw new Error('Assert failed for:' + value);
}
}, },
fireEvent(type, ...args) { addRequestResponder(requestType, responder) {
document.dispatchEvent( new CustomEvent(type, {detail: args}) ); if( privates.requestToResponder[requestType] ) {
throw new TypeError(`Request ${requestType} already has responder!`);
}
privates.requestToResponder[requestType] = responder;
},
fireRequest(requestType, ...args) {
const cb = args.slice(-1)[0];
self.assert(typeof(cb) === 'function');
const responder = privates.requestToResponder[requestType];
if(responder) {
responder(...args);
} else {
cb();
}
}, },

View File

@ -295,7 +295,6 @@
keepCookedNowAsync(pacMods = mandatory(), cb = throwIfError) { keepCookedNowAsync(pacMods = mandatory(), cb = throwIfError) {
console.log('Keep cooked now...', pacMods);
if (typeof(pacMods) === 'function') { if (typeof(pacMods) === 'function') {
cb = pacMods; cb = pacMods;
pacMods = getCurrentConfigs(); pacMods = getCurrentConfigs();
@ -307,10 +306,11 @@
} }
kitchenState(modsKey, pacMods); kitchenState(modsKey, pacMods);
} }
console.log('Keep cooked now...', pacMods);
this._tryNowAsync( this._tryNowAsync(
(err, res, ...warns) => { (err, res, ...warns) => {
console.log('Try now cb...', err); console.log('Try now err:', err);
if (err) { if (err) {
return cb(err, res, ...warns); return cb(err, res, ...warns);
} }
@ -321,8 +321,7 @@
} }
const hosts = par.map( (ps) => ps.split(/\s+/)[1] ); const hosts = par.map( (ps) => ps.split(/\s+/)[1] );
window.utils.fireEvent('ip-to-host-replace-all', hosts, throwIfError); window.utils.fireRequest('ip-to-host-replace-all', hosts, (err, res, ...moreWarns) => cb( err, res, ...warns.concat(moreWarns) ));
cb(null, null, ...warns);
} }
); );

View File

@ -93,8 +93,7 @@
'Getting IPs for PAC hosts...', 'Getting IPs for PAC hosts...',
cb cb
); );
window.utils.fireEvent('ip-to-host-update-all', () => {/* Swallow. */}); window.utils.fireRequest('ip-to-host-update-all', cb);
cb();
}; };
@ -261,7 +260,7 @@
}, },
_periodicUpdateAlarmReason: 'Периодичное обновление PAC-скрипта Антизапрет', _periodicUpdateAlarmReason: 'Периодичное обновление PAC-скрипта',
pushToStorageAsync(cb = throwIfError) { pushToStorageAsync(cb = throwIfError) {

View File

@ -23,6 +23,7 @@
], ],
"background": { "background": {
${persistent}
"scripts": [ "scripts": [
"00-init-apis.js" "00-init-apis.js"
, "11-error-handlers-api.js" , "11-error-handlers-api.js"

View File

@ -599,9 +599,17 @@ HTTPS 11.22.33.44:8080;">${conf.value || localStorage.getItem(uiRaw) || ''}</tex
if (!ifSure) { if (!ifSure) {
return false; return false;
} }
pacKitchen.resetToDefaults(); conduct(
backgroundPage.utils.fireEvent('ip-to-host-reset-to-defaults'); 'Сбрасываем...',
window.close(); (cb) => {
pacKitchen.resetToDefaults();
backgroundPage.utils.fireRequest('ip-to-host-reset-to-defaults', cb);
},
'Откройте окно заново для отображения эффекта.',
() => window.close()
);
}; };

View File

@ -188,7 +188,7 @@
const self = window.apis.ipToHost = { const self = window.apis.ipToHost = {
persist() { persistData() {
console.log('Persisting ipToHost...', privates); console.log('Persisting ipToHost...', privates);
const ipToHost = {}; const ipToHost = {};
@ -299,7 +299,7 @@
this._updateAllAsync((err, ...args) => { this._updateAllAsync((err, ...args) => {
if (!err) { if (!err) {
this.persist(); this.persistData();
} }
cb(err, ...args); cb(err, ...args);
@ -320,7 +320,7 @@
this._replaceAllAsync(hostArr, (allErr, ...args) => { this._replaceAllAsync(hostArr, (allErr, ...args) => {
if (!allErr) { if (!allErr) {
this.persist(); this.persistData();
} }
cb(allErr, ...args); cb(allErr, ...args);
@ -337,14 +337,17 @@
}; };
window.utils.addEventHandler( window.utils.addRequestResponder(
'ip-to-host-update-all', (...args) => self.updateAllAsync(...args) 'ip-to-host-update-all', (...args) => self.updateAllAsync(...args)
); );
window.utils.addEventHandler( window.utils.addRequestResponder(
'ip-to-host-replace-all', (...args) => self.replaceAllAsync(...args) 'ip-to-host-replace-all', (...args) => self.replaceAllAsync(...args)
); );
window.utils.addEventHandler( window.utils.addRequestResponder(
'ip-to-host-reset-to-defaults', () => self.resetToDefaults() 'ip-to-host-reset-to-defaults', (cb) => {
self.resetToDefaults();
cb();
}
); );
} }

View File

@ -11,6 +11,7 @@ exports.contexts.full = Object.assign({}, commonContext, {
nameSuffixEn: '', nameSuffixEn: '',
nameSuffixRu: '', nameSuffixRu: '',
extra_permissions: ', "webRequest"', extra_permissions: ', "webRequest"',
persistent: '',
scripts_2x: ', "20-ip-to-host-api.js"', scripts_2x: ', "20-ip-to-host-api.js"',
scripts_7x: ', "70-block-informer.js"', scripts_7x: ', "70-block-informer.js"',
}); });
@ -20,6 +21,7 @@ exports.contexts.mini = Object.assign({}, commonContext, {
nameSuffixEn: ' MINI', nameSuffixEn: ' MINI',
nameSuffixRu: ' МИНИ', nameSuffixRu: ' МИНИ',
extra_permissions: '', extra_permissions: '',
persistent: '"persistent": false,',
scripts_2x: ', "20-for-mini-only.js"', scripts_2x: ', "20-for-mini-only.js"',
scripts_7x: '', scripts_7x: '',
}); });