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) {
// I believe logging objects precludes them from being GCed.
@ -18,6 +18,10 @@
});
}
const privates = {
requestToResponder: {},
};
const self = window.utils = {
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) {
console.log('Keep cooked now...', pacMods);
if (typeof(pacMods) === 'function') {
cb = pacMods;
pacMods = getCurrentConfigs();
@ -307,10 +306,11 @@
}
kitchenState(modsKey, pacMods);
}
console.log('Keep cooked now...', pacMods);
this._tryNowAsync(
(err, res, ...warns) => {
console.log('Try now cb...', err);
console.log('Try now err:', err);
if (err) {
return cb(err, res, ...warns);
}
@ -321,8 +321,7 @@
}
const hosts = par.map( (ps) => ps.split(/\s+/)[1] );
window.utils.fireEvent('ip-to-host-replace-all', hosts, throwIfError);
cb(null, null, ...warns);
window.utils.fireRequest('ip-to-host-replace-all', hosts, (err, res, ...moreWarns) => cb( err, res, ...warns.concat(moreWarns) ));
}
);

View File

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

View File

@ -23,6 +23,7 @@
],
"background": {
${persistent}
"scripts": [
"00-init-apis.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) {
return false;
}
pacKitchen.resetToDefaults();
backgroundPage.utils.fireEvent('ip-to-host-reset-to-defaults');
window.close();
conduct(
'Сбрасываем...',
(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 = {
persist() {
persistData() {
console.log('Persisting ipToHost...', privates);
const ipToHost = {};
@ -299,7 +299,7 @@
this._updateAllAsync((err, ...args) => {
if (!err) {
this.persist();
this.persistData();
}
cb(err, ...args);
@ -320,7 +320,7 @@
this._replaceAllAsync(hostArr, (allErr, ...args) => {
if (!allErr) {
this.persist();
this.persistData();
}
cb(allErr, ...args);
@ -337,14 +337,17 @@
};
window.utils.addEventHandler(
window.utils.addRequestResponder(
'ip-to-host-update-all', (...args) => self.updateAllAsync(...args)
);
window.utils.addEventHandler(
window.utils.addRequestResponder(
'ip-to-host-replace-all', (...args) => self.replaceAllAsync(...args)
);
window.utils.addEventHandler(
'ip-to-host-reset-to-defaults', () => self.resetToDefaults()
window.utils.addRequestResponder(
'ip-to-host-reset-to-defaults', (cb) => {
self.resetToDefaults();
cb();
}
);
}

View File

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