-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
52 lines (48 loc) · 1.17 KB
/
api.js
File metadata and controls
52 lines (48 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const callbacks = {};
let request_id = 0;
export const socket = new WebSocket("wss://microstudio.dev");
socket.onmessage = function(event) {
const data = JSON.parse(event.data);
const id = data.request_id;
delete data.request_id;
if (callbacks[id]) {
callbacks[id](data);
delete callbacks[id];
}
}
export async function send(data) {
data.request_id = request_id++;
socket.send(JSON.stringify(data));
const result = await new Promise(res => {
callbacks[data.request_id] = res;
});
return result;
}
export async function get_projects(user, callback) {
let all_projects = [];
let position = 0;
let offset = 0;
while (true) {
let result = await send({
name: "get_public_projects",
ranking: "top",
type: "all",
tags: [],
search: user.toLowerCase(),
position,
offset,
});
position += 25;
offset = result.offset;
let projects = result.list.filter(project => {
return project.owner == user;
})
all_projects = all_projects.concat(projects);
if (result.list.length < 25) {
callback(projects, true);
break;
}
callback(projects, false);
}
return all_projects;
}