mirror of
https://gitlab.com/octtspacc/Friendiiverse
synced 2025-02-08 07:38:49 +01:00
Async request fix
This commit is contained in:
parent
b96b82847a
commit
42dc0771d2
@ -16,6 +16,7 @@ var UseFakeApi = true;
|
|||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<main>
|
||||||
<div id="CoverView">
|
<div id="CoverView">
|
||||||
LOGO
|
LOGO
|
||||||
Loading...
|
Loading...
|
||||||
@ -26,6 +27,13 @@ var UseFakeApi = true;
|
|||||||
<div id="PlazasView"></div>
|
<div id="PlazasView"></div>
|
||||||
<div id="TimelineView"></div>
|
<div id="TimelineView"></div>
|
||||||
<xmp id="DataView"></xmp>
|
<xmp id="DataView"></xmp>
|
||||||
|
</main>
|
||||||
|
<footer>
|
||||||
|
<a href="https://gitlab.com/octtspacc/Friendiiverse">Source Code</a>
|
||||||
|
</footer>
|
||||||
|
<script>NoscriptView.remove();</script>
|
||||||
|
<script src="./Utils.js"></script>
|
||||||
|
<script src="./Strings.js"></script>
|
||||||
<script src="./Translations.js"></script>
|
<script src="./Translations.js"></script>
|
||||||
<script src="./Main.js"></script>
|
<script src="./Main.js"></script>
|
||||||
<script>(function(){
|
<script>(function(){
|
||||||
|
87
Main.js
87
Main.js
@ -1,12 +1,8 @@
|
|||||||
NoscriptView.remove();
|
var CurrTasks = {};
|
||||||
|
|
||||||
var CurrentTimeline = [];
|
var CurrentTimeline = [];
|
||||||
var CurrentTimelineExp = 0;
|
var CurrentTimelineExp = 0;
|
||||||
|
|
||||||
var FakeApi = {};
|
|
||||||
FakeApi.F = {};
|
|
||||||
FakeApi.My = {};
|
|
||||||
|
|
||||||
function LogDebug(Data, Status) {
|
function LogDebug(Data, Status) {
|
||||||
if (Debug) {
|
if (Debug) {
|
||||||
for (var i=0; i<Data.length; i++) {
|
for (var i=0; i<Data.length; i++) {
|
||||||
@ -18,29 +14,55 @@ function LogDebug(Data, Status) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
function ApiCall(Data) {
|
function WaitTasks(First, Then, Data) {
|
||||||
|
var Run = RndId();
|
||||||
|
CurrTasks[Run] = {Remains: 0};
|
||||||
|
ForceList(First).forEach(function(Fun, Data){
|
||||||
|
var Task = RndId();
|
||||||
|
var Proc = [Run, Task];
|
||||||
|
CurrTasks[Run].Remains += 1;
|
||||||
|
Data ? Fun(Proc, Data) : Fun(Proc);
|
||||||
|
});
|
||||||
|
CurrTasks[Run].Interval = setInterval(function(Run, Then){
|
||||||
|
if (CurrTasks[Run].Remains === 0) {
|
||||||
|
clearInterval(CurrTasks[Run].Interval);
|
||||||
|
ForceList(Then).forEach(function(Fun){
|
||||||
|
Fun(CurrTasks[Run].Result);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}, 50, Run, Then);
|
||||||
|
};
|
||||||
|
|
||||||
|
function HttpCodeGood(Code) {
|
||||||
|
var Unit = String(Code)[0];
|
||||||
|
if (['1', '2', '3'].includes(Unit)) {
|
||||||
|
return true;
|
||||||
|
} else
|
||||||
|
if (['4', '5'].includes(Unit)) {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
function ApiCall(Data, Proc) {
|
||||||
// Data = {Target: "Friendica", Method: "...", Data: {}, Call: (), CallFine: (), CallFail: ()}
|
// Data = {Target: "Friendica", Method: "...", Data: {}, Call: (), CallFine: (), CallFail: ()}
|
||||||
var Req = new XMLHttpRequest();
|
var Req = new XMLHttpRequest();
|
||||||
//Req.onreadystatechange = function(){
|
Req.Proc = Proc;
|
||||||
Req.onloadend = function(){
|
Req.onloadend = function(){
|
||||||
//if (this.readyState == 4) {
|
var Status = String(this.status);
|
||||||
var Status = String(this.status);
|
if (Data.Call) {
|
||||||
if (Data.Call) {
|
Data.Call(this);
|
||||||
return Data.Call(this);
|
}
|
||||||
}
|
if (HttpCodeGood(this.status)) {
|
||||||
if (['1', '2', '3'].includes(Status[0])) {
|
if (Data.CallFine) {
|
||||||
if (Data.CallFine) {
|
Data.CallFine(this);
|
||||||
return Data.CallFine(this);
|
|
||||||
};
|
|
||||||
LogDebug([Status, this.responseText], 'l');
|
|
||||||
} else
|
|
||||||
if (['4', '5'].includes(Status[0])) {
|
|
||||||
if (Data.CallFail) {
|
|
||||||
return Data.CallFail(this);
|
|
||||||
};
|
|
||||||
LogDebug([Status, this.responseText], 'e');
|
|
||||||
};
|
};
|
||||||
//};
|
LogDebug([this.status, this.responseText], 'l');
|
||||||
|
} else {
|
||||||
|
if (Data.CallFail) {
|
||||||
|
Data.CallFail(this);
|
||||||
|
};
|
||||||
|
LogDebug([this.status, this.responseText], 'e');
|
||||||
|
};
|
||||||
};
|
};
|
||||||
if (Data.Target == 'Mastodon') {
|
if (Data.Target == 'Mastodon') {
|
||||||
Req.open('GET', `${MastodonUrl}/api/v1/${Data.Method}`, true);
|
Req.open('GET', `${MastodonUrl}/api/v1/${Data.Method}`, true);
|
||||||
@ -93,16 +115,20 @@ function DisplayFriendicaTimeline(Timeline) {
|
|||||||
}});
|
}});
|
||||||
};
|
};
|
||||||
|
|
||||||
function FetchMastodon() {
|
function FetchMastodon(Proc) {
|
||||||
return ApiCall({Target: "Mastodon", Method: "timelines/public", CallFine: function(Res){
|
return ApiCall({Target: "Mastodon", Method: "timelines/public", CallFine: function(Res){
|
||||||
//console.log(JSON.parse(Res.responseText)[0])
|
//console.log(JSON.parse(Res.responseText)[0])
|
||||||
console.log([TransParsers.Mastodon.Status(JSON.parse(Res.responseText)[0])])
|
var New = [ TransParsers.Mastodon.Status( JSON.parse(Res.responseText)[0] ) ];
|
||||||
|
console.log(New)
|
||||||
//return [
|
//return [
|
||||||
// TransParsers.Mastodon.Status(JSON.parse(Res.responseText)[0])
|
// TransParsers.Mastodon.Status(JSON.parse(Res.responseText)[0])
|
||||||
//];
|
//];
|
||||||
CurrentTimeline = CurrentTimeline.concat([TransParsers.Mastodon.Status(JSON.parse(Res.responseText)[0])]);
|
//CurrentTimeline = CurrentTimeline.concat([TransParsers.Mastodon.Status(JSON.parse(Res.responseText)[0])]);
|
||||||
CurrentTimelineExp -= 1;
|
//CurrentTimelineExp -= 1;
|
||||||
}});
|
CurrTasks[Res.Proc[0]].Remains -= 1;
|
||||||
|
// TODO: store data in global object
|
||||||
|
CurrTasks[Res.Proc[0]].Result = New;
|
||||||
|
}}, Proc);
|
||||||
};
|
};
|
||||||
|
|
||||||
function FillTimeline(Notes) {
|
function FillTimeline(Notes) {
|
||||||
@ -122,7 +148,7 @@ PlazasView.innerHTML = `
|
|||||||
<ul>
|
<ul>
|
||||||
<li onclick="DisplayFriendicaTimeline('statuses/networkpublic_timeline');">Federation</li>
|
<li onclick="DisplayFriendicaTimeline('statuses/networkpublic_timeline');">Federation</li>
|
||||||
<li onclick="DisplayFriendicaTimeline('statuses/public_timeline');">${FriendicaUrl}</li>
|
<li onclick="DisplayFriendicaTimeline('statuses/public_timeline');">${FriendicaUrl}</li>
|
||||||
<li onclick="var CurrentTimelineExp = 1; FetchMastodon(); /*FillTimeline();*//*DisplayMastodonTimeline('timelines/public');*/">${MastodonUrl}</li>
|
<li onclick="WaitTasks(FetchMastodon, FillTimeline);">${MastodonUrl}</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
@ -133,4 +159,3 @@ PlazasView.innerHTML = `
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
3
Strings.js
Normal file
3
Strings.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
var Strings = {
|
||||||
|
|
||||||
|
};
|
15
Style.css
15
Style.css
@ -2,6 +2,20 @@ img, video {
|
|||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
main {
|
||||||
|
margin-bottom: 2em;
|
||||||
|
}
|
||||||
|
|
||||||
|
footer {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
position: fixed;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
border: 2px solid black;
|
||||||
|
margin-top: 4em;
|
||||||
|
}
|
||||||
|
|
||||||
#DataView {
|
#DataView {
|
||||||
white-space: break-spaces;
|
white-space: break-spaces;
|
||||||
}
|
}
|
||||||
@ -10,4 +24,3 @@ img, video {
|
|||||||
border: 4px solid purple;
|
border: 4px solid purple;
|
||||||
margin: 12px;
|
margin: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ var TransSchemas = {
|
|||||||
},
|
},
|
||||||
Status: {
|
Status: {
|
||||||
"__": "Note",
|
"__": "Note",
|
||||||
"account": {"__": "Account"},
|
//"account": {"__": "Account"},
|
||||||
"content": "Content",
|
"content": "Content",
|
||||||
"created_at": "Time",
|
"created_at": "Time",
|
||||||
"url": "Url",
|
"url": "Url",
|
||||||
@ -25,11 +25,11 @@ var TransParsers = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
function JsonTranslate(Old, Schema) {
|
function JsonTranslate(TreeOld, Schema) {
|
||||||
var New = {};
|
var TreeNew = {};
|
||||||
Object.keys(Old).forEach(function(OldKey){
|
Object.keys(TreeOld).forEach(function(KeyOld){
|
||||||
var Content = Old[OldKey];
|
var Content = TreeOld[KeyOld];
|
||||||
var NewKey = (OldKey in Schema ? Schema[OldKey]: OldKey);
|
var KeyNew = ((typeof(Schema) == 'object' && KeyOld in Schema) ? Schema[KeyOld] : KeyOld);
|
||||||
if (typeof(Content) == 'object' && Content !== null) {
|
if (typeof(Content) == 'object' && Content !== null) {
|
||||||
if (Array.isArray(Content)) {
|
if (Array.isArray(Content)) {
|
||||||
// Lists
|
// Lists
|
||||||
@ -37,14 +37,15 @@ function JsonTranslate(Old, Schema) {
|
|||||||
//Content.forEach(function());
|
//Content.forEach(function());
|
||||||
} else {
|
} else {
|
||||||
// Dicts
|
// Dicts
|
||||||
NewKey.__ ||= OldKey;
|
if (!KeyNew.__) {
|
||||||
New[NewKey.__] = JsonTranslate(Content, NewKey);
|
KeyNew.__ = KeyOld;
|
||||||
|
};
|
||||||
|
TreeNew[KeyNew.__] = JsonTranslate(Content, KeyNew);
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
// Values
|
// Values
|
||||||
New[NewKey] = Content;
|
TreeNew[KeyNew] = Content;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
return New;
|
return TreeNew;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user