mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-12-29 01:16:20 -05:00
initial commit of retroshare backend for mithril webui
This commit is contained in:
parent
bb51e62387
commit
ddd91e228a
@ -11,7 +11,7 @@
|
||||
<script type="text/javascript">
|
||||
function load_ui(){
|
||||
var m = require("mithril");
|
||||
var ui = require("chat");
|
||||
var ui = require("peers");
|
||||
var main = document.getElementById("main");
|
||||
m.mount(main, ui);
|
||||
}
|
||||
|
16
libresapi/src/webui-src/app/peers.js
Normal file
16
libresapi/src/webui-src/app/peers.js
Normal file
@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
var m = require("mithril");
|
||||
var rs = require("retroshare");
|
||||
|
||||
module.exports = {view: function(){
|
||||
var peers = rs("peers");
|
||||
console.log(peers);
|
||||
if(peers === undefined){
|
||||
return m("div", "waiting_server");
|
||||
}
|
||||
return m("div", peers.map(function(peer){
|
||||
return m("div",peer.name)
|
||||
}));
|
||||
}
|
||||
};
|
160
libresapi/src/webui-src/app/retroshare.js
Normal file
160
libresapi/src/webui-src/app/retroshare.js
Normal file
@ -0,0 +1,160 @@
|
||||
/*
|
||||
var rs = requires("rs");
|
||||
var m = require("mithril");
|
||||
|
||||
function main(){
|
||||
var state = rs("runstate");
|
||||
if(state=== undefined){
|
||||
return m("div", "waiting for server");
|
||||
}
|
||||
if(state === "waiting_login"){
|
||||
return require("login")();
|
||||
}
|
||||
if(state === "running_ok"){
|
||||
return require("mainwindow")();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
idea: statetokenservice could just send the date instead of the token
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var m = require("mithril");
|
||||
|
||||
var api_url = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port + "/api/v2/";
|
||||
var filestreamer_url = window.location.protocol + "//" +window.location.hostname + ":" + window.location.port + "/fstream/";
|
||||
var upload_url = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port + "/upload/";
|
||||
|
||||
function for_key_in_obj(obj, callback){
|
||||
var key;
|
||||
for(key in obj){
|
||||
callback(key);
|
||||
}
|
||||
}
|
||||
|
||||
var cache = {};
|
||||
var last_update_ts = 0;
|
||||
|
||||
function check_for_changes(){
|
||||
var tokens = [];
|
||||
for_key_in_obj(cache, function(path){
|
||||
var token = cache[path].token;
|
||||
if(token !== undefined){
|
||||
tokens.push(token);
|
||||
}
|
||||
});
|
||||
|
||||
var req = m.request({
|
||||
method: "POST",
|
||||
url: api_url + "statetokenservice",
|
||||
background: true,
|
||||
data: tokens,
|
||||
});
|
||||
|
||||
req.then(function handle_statetoken_response(response){
|
||||
var paths_to_fetch = [];
|
||||
for_key_in_obj(cache, function(path){
|
||||
var found = false;
|
||||
for(var i = 0; i > response.data.length; i++){
|
||||
if(response.data[i] === cache[path].statetoken){
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if(found){
|
||||
paths_to_fetch.push(path);
|
||||
}
|
||||
});
|
||||
var requests = [];
|
||||
paths_to_fetch.map(function request_it(path){
|
||||
var req = m.request({
|
||||
|
||||
method: "GET",
|
||||
|
||||
url: api_url + path,
|
||||
|
||||
background: true,
|
||||
|
||||
});
|
||||
req.then(function fill_in_result(response){
|
||||
cache[path].data = response.data;
|
||||
});
|
||||
requests.push(req);
|
||||
});
|
||||
m.sync(requests, function trigger_render(){
|
||||
m.startComputation();
|
||||
m.endComputation();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
check_for_changes();
|
||||
|
||||
var update_scheduled = false;
|
||||
function schedule_request_missing(){
|
||||
if(update_scheduled)
|
||||
return;
|
||||
update_scheduled = true;
|
||||
// place update logic outside of render loop, this way we can fetch multiple things at once
|
||||
// (because after the render loop everything we should fetch is in the list)
|
||||
// if we fetch multiple things at once, we can delay a re-rende runtil everything is done
|
||||
// so we need only one re-render for multiple updates
|
||||
setTimeout(function request_missing(){
|
||||
update_scheduled = false;
|
||||
var requests = [];
|
||||
for_key_in_obj(cache, function(path){
|
||||
if(!cache[path].requested){
|
||||
var req = m.request({
|
||||
method: "GET",
|
||||
url: api_url + path,
|
||||
background: true,
|
||||
});
|
||||
req = req.then(function fill_data(response){
|
||||
// TODO: add errorhandling
|
||||
cache[path].data = response.data;
|
||||
cache[path].statetoken = response.statetoken;
|
||||
});
|
||||
requests.push(req);
|
||||
}
|
||||
cache[path].requested = true;
|
||||
});
|
||||
m.sync(requests).then(function trigger_render(){
|
||||
m.startComputation();
|
||||
m.endComputation();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function rs(path, optionen){
|
||||
if(optionen === undefined) {
|
||||
if(cache[path] === undefined){
|
||||
cache[path] = {
|
||||
data: undefined,
|
||||
statetoken: undefined,
|
||||
requested: false,
|
||||
};
|
||||
schedule_request_missing();
|
||||
}
|
||||
return cache[path].data;
|
||||
} else if (optionen.args != undefined) {
|
||||
var req = m.request({
|
||||
method: "GET",
|
||||
|
||||
url: api_url + path,
|
||||
|
||||
args: optionen.args,
|
||||
|
||||
background: false,
|
||||
|
||||
callback: {
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = rs;
|
Loading…
Reference in New Issue
Block a user