rswebui: add eventqueue, switch login to eventqueue

This commit is contained in:
Zener 2020-12-23 17:59:44 +01:00
parent cee5576a22
commit bc2eab69dc
2 changed files with 66 additions and 23 deletions

View File

@ -14,26 +14,10 @@ const verifyLogin = async function(uname, passwd, url, displayAuthError = true)
return;
}
rs.setKeys('', '', url, false);
rs.rsJsonApiRequest(
'/rsPeers/GetRetroshareInvite',
{},
(data, successful) => {
if (successful) {
rs.setKeys(uname, passwd, url);
m.route.set('/home');
} else if (data.status == 401 && !displayAuthError) {
// skip - Autologon failed
} else if (data.status == 401) {
displayErrorMessage('Incorrect login/password.');
} else if (data.status == 0) {
displayErrorMessage(['Retroshare-jsonapi not available.',m('br'),'Please fix host and/or port.']);
} else {
displayErrorMessage('Login failed: HTTP ' + data.status + ' ' + data.statusText);
}
},
true,
loginHeader
);
rs.startEventQueue('login', loginHeader, displayAuthError ? displayErrorMessage : () => {}, displayErrorMessage, () => {
rs.setKeys(uname, passwd, url);
m.route.set('/home');
});
};
function loginComponent() {

View File

@ -23,7 +23,8 @@ function rsJsonApiRequest(
async = true,
headers = {},
handleDeserialize = JSON.parse,
handleSerialize = JSON.stringify
handleSerialize = JSON.stringify,
config = null
) {
headers['Accept'] = 'application/json';
if (loginKey.isVerified) {
@ -49,7 +50,8 @@ function rsJsonApiRequest(
},
serialize: handleSerialize,
headers: headers,
body: data
body: data,
config: config
})
.then(result => {
if (result.status === 200) {
@ -87,9 +89,66 @@ function setBackgroundTask(task, interval, taskInScope) {
}, interval);
return taskId;
}
/*
path,
data = {},
callback = () => {},
async = true,
headers = {},
handleDeserialize = JSON.parse,
handleSerialize = JSON.stringify
config
*/
function startEventQueue(info, loginHeader = {}, displayAuthError = () => {}, displayErrorMessage = () => {}, successful = () => {}){
return rsJsonApiRequest('/rsEvents/registerEventsHandler', {},
(data, success) => {
if (success) {
// unused
} else if (data.status == 401) {
displayAuthError('Incorrect login/password.');
} else if (data.status == 0) {
displayErrorMessage(['Retroshare-jsonapi not available.',m('br'),'Please fix host and/or port.']);
} else {
displayErrorMessage('Login failed: HTTP ' + data.status + ' ' + data.statusText);
}
},
true, loginHeader, JSON.parse, JSON.stringify,
(xhr, args, url) => {
let lastIndex = 0;
xhr.onprogress = ev => {
let currIndex = xhr.responseText.length;
let registered = false;
if (currIndex > lastIndex) {
let parts = xhr.responseText.substring(lastIndex,currIndex);
lastIndex=currIndex;
for(data of parts.trim().split('\n\n').filter(e=> e.startsWith('data: {')).map(e=> e.substr(6)).map(JSON.parse)){
if (data.hasOwnProperty('retval')) {
console.info(info + ' [' + data.retval.errorCategory + '] ' + data.retval.errorMessage);
if (data.retval.errorNumber === 0) {
successful();
} else {
displayErrorMessage(info + ' failed: [' + data.retval.errorCategory + '] ' + data.retval.errorMessage);
}
registered = true;
} else if(data.hasOwnProperty('event')) {
data.event.queueSize = currIndex;
console.info(data.event);
}
}
if (currIndex > 1e6) { // max 1 MB eventQueue
startEventQueue('restart queue');
xhr.abort();
}
}
}
return xhr;
}
);
}
module.exports = {
rsJsonApiRequest,
setKeys,
setBackgroundTask
setBackgroundTask,
startEventQueue
};