Async request fix

This commit is contained in:
octospacc 2023-04-19 12:18:14 +02:00
parent b96b82847a
commit 42dc0771d2
6 changed files with 100 additions and 43 deletions

View File

@ -16,6 +16,7 @@ var UseFakeApi = true;
</script>
</head>
<body>
<main>
<div id="CoverView">
LOGO
Loading...
@ -26,6 +27,13 @@ var UseFakeApi = true;
<div id="PlazasView"></div>
<div id="TimelineView"></div>
<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="./Main.js"></script>
<script>(function(){

87
Main.js
View File

@ -1,12 +1,8 @@
NoscriptView.remove();
var CurrTasks = {};
var CurrentTimeline = [];
var CurrentTimelineExp = 0;
var FakeApi = {};
FakeApi.F = {};
FakeApi.My = {};
function LogDebug(Data, Status) {
if (Debug) {
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: ()}
var Req = new XMLHttpRequest();
//Req.onreadystatechange = function(){
Req.Proc = Proc;
Req.onloadend = function(){
//if (this.readyState == 4) {
var Status = String(this.status);
if (Data.Call) {
return Data.Call(this);
}
if (['1', '2', '3'].includes(Status[0])) {
if (Data.CallFine) {
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');
var Status = String(this.status);
if (Data.Call) {
Data.Call(this);
}
if (HttpCodeGood(this.status)) {
if (Data.CallFine) {
Data.CallFine(this);
};
//};
LogDebug([this.status, this.responseText], 'l');
} else {
if (Data.CallFail) {
Data.CallFail(this);
};
LogDebug([this.status, this.responseText], 'e');
};
};
if (Data.Target == 'Mastodon') {
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){
//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 [
// TransParsers.Mastodon.Status(JSON.parse(Res.responseText)[0])
//];
CurrentTimeline = CurrentTimeline.concat([TransParsers.Mastodon.Status(JSON.parse(Res.responseText)[0])]);
CurrentTimelineExp -= 1;
}});
//CurrentTimeline = CurrentTimeline.concat([TransParsers.Mastodon.Status(JSON.parse(Res.responseText)[0])]);
//CurrentTimelineExp -= 1;
CurrTasks[Res.Proc[0]].Remains -= 1;
// TODO: store data in global object
CurrTasks[Res.Proc[0]].Result = New;
}}, Proc);
};
function FillTimeline(Notes) {
@ -122,7 +148,7 @@ PlazasView.innerHTML = `
<ul>
<li onclick="DisplayFriendicaTimeline('statuses/networkpublic_timeline');">Federation</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>
</div>
<div>
@ -133,4 +159,3 @@ PlazasView.innerHTML = `
</ul>
</div>
`;

3
Strings.js Normal file
View File

@ -0,0 +1,3 @@
var Strings = {
};

View File

@ -2,6 +2,20 @@ img, video {
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 {
white-space: break-spaces;
}
@ -10,4 +24,3 @@ img, video {
border: 4px solid purple;
margin: 12px;
}

View File

@ -6,7 +6,7 @@ var TransSchemas = {
},
Status: {
"__": "Note",
"account": {"__": "Account"},
//"account": {"__": "Account"},
"content": "Content",
"created_at": "Time",
"url": "Url",
@ -25,11 +25,11 @@ var TransParsers = {
},
};
function JsonTranslate(Old, Schema) {
var New = {};
Object.keys(Old).forEach(function(OldKey){
var Content = Old[OldKey];
var NewKey = (OldKey in Schema ? Schema[OldKey]: OldKey);
function JsonTranslate(TreeOld, Schema) {
var TreeNew = {};
Object.keys(TreeOld).forEach(function(KeyOld){
var Content = TreeOld[KeyOld];
var KeyNew = ((typeof(Schema) == 'object' && KeyOld in Schema) ? Schema[KeyOld] : KeyOld);
if (typeof(Content) == 'object' && Content !== null) {
if (Array.isArray(Content)) {
// Lists
@ -37,14 +37,15 @@ function JsonTranslate(Old, Schema) {
//Content.forEach(function());
} else {
// Dicts
NewKey.__ ||= OldKey;
New[NewKey.__] = JsonTranslate(Content, NewKey);
if (!KeyNew.__) {
KeyNew.__ = KeyOld;
};
TreeNew[KeyNew.__] = JsonTranslate(Content, KeyNew);
};
} else {
// Values
New[NewKey] = Content;
TreeNew[KeyNew] = Content;
};
});
return New;
return TreeNew;
};

7
Utils.js Normal file
View File

@ -0,0 +1,7 @@
function ForceList(Item) {
return (Array.isArray(Item) ? Item : [Item]);
};
function RndId() {
return `${Date.now()}${Math.random()}`;
};