рефакторинг

This commit is contained in:
vgoma 2017-11-30 12:30:47 +03:00
parent e6bfea656d
commit 1f684aa888
3 changed files with 705 additions and 728 deletions

1314
dist/crypto-pro.js vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -1,70 +1,59 @@
var bowser = require('bowser/bowser'),
browserInfo = bowser._detect(navigator.userAgent),
global = Function('return this')(),
canPromise = Boolean(global.Promise),
canAsync,
cadesplugin,
cryptoService,
_errorMsg = '',
_isLoaded = false,
_onLoadCbQueue = [];
global.allow_firefox_cadesplugin_async = browserInfo.firefox && browserInfo.version >= 52;
require('../vendor/cadesplugin_api'); require('../vendor/cadesplugin_api');
cadesplugin = global.cadesplugin; var global = Function('return this')(),
canPromise = Boolean(global.Promise),
cadesplugin = global.cadesplugin,
cryptoService = require('./api'),
errorMsg = '',
loadedPlugin = false,
onLoadCallbacs = [],
canAsync = Boolean(cadesplugin.CreateObjectAsync); execOnloadQueue = function execOnloadQueue() {
onLoadCallbacs.forEach(function (callback) {
cryptoService = require('./api'); callback();
function execOnloadQueue() {
_onLoadCbQueue.forEach(function (callback) {
callback();
});
}
function passToWaitOnLoad(callback) {
if (Object.prototype.toString.call(callback) === '[object Function]') {
_onLoadCbQueue.push(callback);
}
}
function callOnLoad(method) {
_isLoaded ? method() : passToWaitOnLoad(method);
}
function finishLoading() {
_isLoaded = true;
execOnloadQueue();
}
function call() {
var args = Array.prototype.slice.call(arguments),
methodName = args.shift();
return new Promise(function (resolve, reject) {
callOnLoad(function () {
var method;
if (_errorMsg) {
reject(_errorMsg);
return;
}
method = cryptoService[methodName];
if (!method) {
reject('Метод "' + methodName + '" не доступен');
return;
}
method.apply(null, args).then(resolve, reject);
}); });
}); },
}
passToWaitOnLoad = function passToWaitOnLoad(callback) {
if (Object.prototype.toString.call(callback) === '[object Function]') {
onLoadCallbacs.push(callback);
}
},
callOnLoad = function callOnLoad(method) {
loadedPlugin ? method() : passToWaitOnLoad(method);
},
finishLoading = function finishLoading() {
loadedPlugin = true;
execOnloadQueue();
},
call = function call() {
var args = Array.prototype.slice.call(arguments),
methodName = args.shift();
return new Promise(function (resolve, reject) {
callOnLoad(function () {
var method;
if (errorMsg) {
reject(errorMsg);
return;
}
method = cryptoService[methodName];
if (!method) {
reject('Метод "' + methodName + '" не доступен');
return;
}
method.apply(null, args).then(resolve, reject);
});
});
};
if (cadesplugin) { if (cadesplugin) {
// Уровень отладки (LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_ERROR) // Уровень отладки (LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_ERROR)
@ -72,7 +61,7 @@ if (cadesplugin) {
if (canPromise) { if (canPromise) {
cadesplugin.then(finishLoading, function () { cadesplugin.then(finishLoading, function () {
_errorMsg = 'КриптоПРО ЭЦП Browser Plug-In не доступен'; errorMsg = 'КриптоПРО ЭЦП Browser Plug-In не доступен';
finishLoading(); finishLoading();
}); });
} else { } else {