mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-12 16:43:17 -04:00
webui: requesthandling improved
- shutdown-detecting - waiting for password - requestFail available - thennables for requests and cache - req.request method selectable, default POST
This commit is contained in:
parent
2f94f416ba
commit
408f379db8
4 changed files with 85 additions and 42 deletions
|
@ -8,15 +8,26 @@ var currentpasswd = null;
|
||||||
var accountMap = new Map();
|
var accountMap = new Map();
|
||||||
|
|
||||||
function login(){
|
function login(){
|
||||||
//alert("login:" + curraccount.location + "; passwort: " + currentpasswd);
|
|
||||||
rs.request("control/login", {id: curraccount.id}, function(){
|
rs.request("control/login", {id: curraccount.id}, function(){
|
||||||
//alert("login send");
|
console.log("login sent");
|
||||||
rs.request("control/password", {password: currentpasswd}, function(){
|
waitForPassword(currentpasswd);
|
||||||
m.redraw();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function waitForPassword(password){
|
||||||
|
rs.request("control/password",null,function(data){
|
||||||
|
if (data.want_password) {
|
||||||
|
console.log("sending pwd for " + data.key_name + "...");
|
||||||
|
rs.request("control/password", {password: password}, function(){
|
||||||
|
m.redraw();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log("waiting for pwd ...");
|
||||||
|
setTimeout(50, waitForPassword(password));
|
||||||
|
}
|
||||||
|
}, { method: "GET"})
|
||||||
|
}
|
||||||
|
|
||||||
function cancel(){
|
function cancel(){
|
||||||
curraccount=null;
|
curraccount=null;
|
||||||
m.redraw();
|
m.redraw();
|
||||||
|
@ -33,8 +44,7 @@ function setPasswd(password) {
|
||||||
|
|
||||||
module.exports = {view: function(){
|
module.exports = {view: function(){
|
||||||
var accounts = rs("control/locations");
|
var accounts = rs("control/locations");
|
||||||
console.log("accounts: " + accounts);
|
if(accounts === undefined || accounts == null){
|
||||||
if(accounts === undefined){
|
|
||||||
return m("div", "accounts: waiting_server");
|
return m("div", "accounts: waiting_server");
|
||||||
}
|
}
|
||||||
if (curraccount == null) {
|
if (curraccount == null) {
|
||||||
|
@ -43,7 +53,6 @@ module.exports = {view: function(){
|
||||||
m("h2","accounts:"),
|
m("h2","accounts:"),
|
||||||
m("hr"),
|
m("hr"),
|
||||||
accounts.map(function(account){
|
accounts.map(function(account){
|
||||||
console.log("adding: " + account.id);
|
|
||||||
accountMap.set(account.id,account);
|
accountMap.set(account.id,account);
|
||||||
return [
|
return [
|
||||||
m("div", {onclick: m.withAttr("account", selAccount), account:account.id }, account.location + " (" + account.name + ")"),
|
m("div", {onclick: m.withAttr("account", selAccount), account:account.id }, account.location + " (" + account.name + ")"),
|
||||||
|
|
|
@ -6,15 +6,18 @@ var menu =require("menu");
|
||||||
|
|
||||||
module.exports = {view: function(){
|
module.exports = {view: function(){
|
||||||
var runstate = rs("control/runstate");
|
var runstate = rs("control/runstate");
|
||||||
console.log("runstate: " + runstate);
|
console.log("runstate: " + (runstate === undefined ? runstate : runstate.runstate));
|
||||||
if(runstate === undefined){
|
if(runstate === undefined){
|
||||||
return m("div", "waiting_server");
|
return m("div", "waiting_server ... ");
|
||||||
|
} else if (runstate.runstate == null){
|
||||||
|
return m("div", "server down");
|
||||||
} else {
|
} else {
|
||||||
if (rs.content === undefined) {
|
if (rs.content === undefined) {
|
||||||
rs.content = null;
|
rs.content = null;
|
||||||
}
|
}
|
||||||
|
if (runstate.runstate == "waiting_init") {
|
||||||
if(runstate.runstate =="waiting_account_select") {
|
return m("div","server starting ...")
|
||||||
|
} else if(runstate.runstate =="waiting_account_select") {
|
||||||
return m("div", [
|
return m("div", [
|
||||||
m("div", menu.view()),
|
m("div", menu.view()),
|
||||||
m("hr"),
|
m("hr"),
|
||||||
|
|
|
@ -4,7 +4,11 @@ var m = require("mithril");
|
||||||
var rs = require("retroshare");
|
var rs = require("retroshare");
|
||||||
|
|
||||||
function shutdown(){
|
function shutdown(){
|
||||||
rs("control/shutdown",{args:[]});
|
rs.request("control/shutdown",null,function(){
|
||||||
|
rs("control/runstate").runstate=null;
|
||||||
|
m.redraw();
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function goback(){
|
function goback(){
|
||||||
|
|
|
@ -40,13 +40,14 @@ var last_update_ts = 0;
|
||||||
|
|
||||||
function check_for_changes(){
|
function check_for_changes(){
|
||||||
var tokens = [];
|
var tokens = [];
|
||||||
|
// console.log("start-check");
|
||||||
for_key_in_obj(cache, function(path){
|
for_key_in_obj(cache, function(path){
|
||||||
var token = cache[path].statetoken;
|
var token = cache[path].statetoken;
|
||||||
if(token !== undefined){
|
if(token !== undefined){
|
||||||
tokens.push(token);
|
tokens.push(token);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// console.log("tokens found: " + tokens);
|
||||||
var req = m.request({
|
var req = m.request({
|
||||||
method: "POST",
|
method: "POST",
|
||||||
url: api_url + "statetokenservice",
|
url: api_url + "statetokenservice",
|
||||||
|
@ -55,6 +56,7 @@ function check_for_changes(){
|
||||||
});
|
});
|
||||||
|
|
||||||
req.then(function handle_statetoken_response(response){
|
req.then(function handle_statetoken_response(response){
|
||||||
|
// console.log("checking result " + response);
|
||||||
var paths_to_fetch = [];
|
var paths_to_fetch = [];
|
||||||
for_key_in_obj(cache, function(path){
|
for_key_in_obj(cache, function(path){
|
||||||
var found = false;
|
var found = false;
|
||||||
|
@ -67,6 +69,7 @@ function check_for_changes(){
|
||||||
paths_to_fetch.push(path);
|
paths_to_fetch.push(path);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// console.log("generating Results for " + paths_to_fetch.length + " paths");
|
||||||
var requests = [];
|
var requests = [];
|
||||||
paths_to_fetch.map(function request_it(path){
|
paths_to_fetch.map(function request_it(path){
|
||||||
var req2 = m.request({
|
var req2 = m.request({
|
||||||
|
@ -81,6 +84,7 @@ function check_for_changes(){
|
||||||
requests.push(req2);
|
requests.push(req2);
|
||||||
});
|
});
|
||||||
if(requests.length > 0){
|
if(requests.length > 0){
|
||||||
|
// console.log("requesting " + requests.length + " requests");
|
||||||
m.sync(requests).then(function trigger_render(){
|
m.sync(requests).then(function trigger_render(){
|
||||||
m.startComputation();
|
m.startComputation();
|
||||||
m.endComputation();
|
m.endComputation();
|
||||||
|
@ -88,8 +92,12 @@ function check_for_changes(){
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
// console.log("no requests");
|
||||||
setTimeout(check_for_changes, 500);
|
setTimeout(check_for_changes, 500);
|
||||||
}
|
}
|
||||||
|
}, function errhandling(value){
|
||||||
|
// console.log("server disconnected " + value);
|
||||||
|
setTimeout(check_for_changes, 500);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,10 +122,24 @@ function schedule_request_missing(){
|
||||||
url: api_url + path,
|
url: api_url + path,
|
||||||
background: true,
|
background: true,
|
||||||
});
|
});
|
||||||
req = req.then(function fill_data(response){
|
|
||||||
|
req.then(function fill_data(response){
|
||||||
// TODO: add errorhandling
|
// TODO: add errorhandling
|
||||||
cache[path].data = response.data;
|
cache[path].data = response.data;
|
||||||
cache[path].statetoken = response.statetoken;
|
cache[path].statetoken = response.statetoken;
|
||||||
|
if (cache[path].then != undefined && cache[path].then != null) {
|
||||||
|
try {
|
||||||
|
cache[path].then(response);
|
||||||
|
} catch (ex) {
|
||||||
|
if (cache[path].errorCallback != undefined && cache[path].errorCallback != null) {
|
||||||
|
cache[path].errorCallback(ex);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, function errhandling(value){
|
||||||
|
if (cache[path].errorCallback != undefined && cache[path].errorCallback != null) {
|
||||||
|
cache[path].errorCallback(value);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
requests.push(req);
|
requests.push(req);
|
||||||
}
|
}
|
||||||
|
@ -130,53 +152,58 @@ function schedule_request_missing(){
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// called every time, rs or rs.request failed, only response or value is set
|
||||||
|
function requestFail(path, response, value) {
|
||||||
|
rs.error = "error on " + path;
|
||||||
|
console.log("Error on " + path + (response == null ? ", value " + value : ", response " + response));
|
||||||
|
}
|
||||||
|
|
||||||
function rs(path, args, callback){
|
function rs(path, args, callback){
|
||||||
if(cache[path] === undefined){
|
if(cache[path] === undefined){
|
||||||
cache[path] = {
|
var req = {
|
||||||
data: args,
|
data: args,
|
||||||
statetoken: undefined,
|
statetoken: undefined,
|
||||||
requested: false,
|
requested: false,
|
||||||
then: function(){
|
then: function(response){
|
||||||
if (callback != undefined) {
|
console.log(path + ": response: " + response.returncode);
|
||||||
callback();
|
if (response.returncode != "ok") {
|
||||||
|
requestFail(path, response, null);
|
||||||
|
} else if (callback != undefined && callback != null) {
|
||||||
|
callback(response.data, response.statetoken);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
errorCallback: function(value){
|
||||||
|
requestFail(path, null, value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
cache[path] = req;
|
||||||
schedule_request_missing();
|
schedule_request_missing();
|
||||||
}
|
}
|
||||||
return cache[path].data;
|
return cache[path].data;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
} else{
|
|
||||||
m.request({
|
|
||||||
method: "POST",
|
|
||||||
url: api_url + path,
|
|
||||||
data: optionen.data,
|
|
||||||
background: true,
|
|
||||||
}).then(function(){
|
|
||||||
// i'm not happy about this.
|
|
||||||
// wouldn't it be nicer to return the thennable to the caller?
|
|
||||||
// but it is not nice to return different things from the rs function
|
|
||||||
// maybe make anotehr function which returns thennables
|
|
||||||
if(optionen.callback)
|
|
||||||
optionen.callback();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = rs;
|
module.exports = rs;
|
||||||
|
|
||||||
rs.request=function(path, args, callback){
|
rs.request=function(path, args, callback, options){
|
||||||
m.request({
|
if (options === undefined) {
|
||||||
method: "POST",
|
options = {};
|
||||||
|
}
|
||||||
|
var req = m.request({
|
||||||
|
method: options.method === undefined ? "POST" : options.method,
|
||||||
url: api_url + path,
|
url: api_url + path,
|
||||||
data: args,
|
data: args,
|
||||||
background: true
|
background: true
|
||||||
}).then(function(response){
|
});
|
||||||
if (callback != undefined) {
|
req.then(function checkResponseAndCallback(response){
|
||||||
|
console.log(path + ": response: " + response.returncode);
|
||||||
|
if (response.returncode != "ok") {
|
||||||
|
requestFail(path, response, null);
|
||||||
|
} else if (callback != undefined && callback != null) {
|
||||||
callback(response.data, response.statetoken);
|
callback(response.data, response.statetoken);
|
||||||
}
|
}
|
||||||
|
}, function errhandling(value){
|
||||||
|
requestFail(path, null, value);
|
||||||
});
|
});
|
||||||
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue