Changes on promises

keepass importer ready and using promises
This commit is contained in:
Marcos Zuriaga 2016-09-26 17:05:47 +02:00
parent 349043c2c3
commit 8db3f31367
4 changed files with 53 additions and 44 deletions

View file

@ -82,7 +82,10 @@ angular.module('passmanApp')
$scope.startImport = function(){
if(file_data){
$window.PassmanImporter[$scope.selectedImporter.id].readFile(file_data, function(parseddata){
$window.PassmanImporter[$scope.selectedImporter.id]
.readFile(file_data)
.then(function(parseddata){
console.log("woof!");
parsed_data = parseddata;
_log('Parsed '+ parsed_data.length + ' credentials, starting to import');
$scope.current_import_length = parsed_data.length;
@ -91,9 +94,9 @@ angular.module('passmanApp')
} else {
// @TODO Show message no data found
}
}).progress(function(percentage){
console.log(percentage + '%');
});
}
}

View file

@ -26,7 +26,7 @@ angular.module('passmanApp')
});
},
generateRSAKeys: function(key_length, progress, callback){
var p = new C_Promise(function(promise){
var p = new C_Promise(function(){
var state = forge.pki.rsa.createKeyPairGenerationState(key_length, 0x10001);
var step = function() {
// run for 100 ms
@ -34,32 +34,32 @@ angular.module('passmanApp')
// console.log(state);
if (state.p !== null) {
// progress(50);
promise.call_progress(50);
this.call_progress(50);
}
else {
// progress(0);
promise.call_progress(50);
this.call_progress(0);
}
setTimeout(step, 1);
setTimeout(step.bind(this), 1);
}
else {
// callback(state.keys);
promise.call_then(state.keys);
this.call_then(state.keys);
}
};
setTimeout(step, 100);
setTimeout(step.bind(this), 100);
});
return p;
},
generateSharedKey: function(size){
size = size || 20;
return new C_Promise(function(promise){ /** prmise C_Promise **/
return new C_Promise(function(){
CRYPTO.PASSWORD.generate(size,
function(pass) {
promise.call_then(pass);
this.call_then(pass);
},
function(progress) {
promise.call_progress(progress);
this.call_progress(progress);
}
);
})

View file

@ -12,6 +12,7 @@ PassmanImporter.keepassCsv = {
};
PassmanImporter.keepassCsv.readFile = function (file_data, callback) {
var p = new C_Promise(function(){
var parsed_csv = PassmanImporter.readCsv(file_data);
var credential_list = [];
for (var i = 0; i < parsed_csv.length; i++) {
@ -34,6 +35,9 @@ PassmanImporter.keepassCsv.readFile = function (file_data, callback) {
}
_credential.tags = tags;
credential_list.push(_credential);
this.call_progress(i/parsed_csv.length*100);
}
callback(credential_list);
this.call_then(credential_list);
});
return p;
};

View file

@ -9,26 +9,28 @@
*/
function C_Promise(workload){
this.update = null; this.finally = null; this.error_function = null;
this.then = function(callback){
this.finally = callback;
return this;
}
};
this.progress = function(callback){
this.update = callback;
return this;
}
};
this.error = function (callback){
this.error_function = callback;
return this;
}
};
this.call_then = function(data){
if (this.finally !== undefined) this.finally(data);
}
if (this.finally !== null) this.finally(data);
};
this.call_progress = function(data){
if (this.progress !== undefined) this.progress(data);
}
if (this.update !== null) this.update(data);
};
this.call_error = function(data){
if(this.error_function !== undefined) this.error_function(data);
}
setTimeout(workload(this), 100);
if(this.error_function !== null) this.error_function(data);
};
setTimeout(workload.bind(this), 100);
}