summaryrefslogtreecommitdiff
path: root/content/repo_query.js
blob: b8c8ed9bb340251169433b8e935028e0e42c8061 (about) (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * part of Hachette
 * Getting available content for site from remote repositories.
 *
 * Copyright (C) 2021 Wojtek Kosior
 * Redistribution terms are gathered in the `copyright' file.
 */

/*
 * IMPORTS_START
 * IMPORT make_ajax_request
 * IMPORTS_END
 */

var query_started = false;

var url = undefined;
var repos = undefined;
var callback = undefined;

async function query(repo)
{
    const [repo_url, data] = repo;

    let response = "Query failed";
    const query_url = `${repo_url}/query?n=${encodeURIComponent(url)}`;

    try {
	let xhttp = await make_ajax_request("GET", query_url);
	if (xhttp.status === 200)
	    response = xhttp.responseText;
	console.log(xhttp);
    } catch (e) {
	console.log(e);
    }

    callback([repo_url, response]);
}

function start_query()
{
    if (query_started || !url || !repos || !callback)
	return;

    query_started = true;

    console.log(`about to query ${url} from ${repos}`);

    for (const repo of repos)
	query(repo);
}

function set_repo_query_url(_url)
{
    url = _url;

    start_query();
}

function set_repo_query_repos(_repos)
{
    repos = _repos;

    start_query();
}

function set_repo_query_callback(_callback)
{
    callback = _callback;

    start_query();
}

/*
 * EXPORTS_START
 * EXPORT set_repo_query_url
 * EXPORT set_repo_query_repos
 * EXPORT set_repo_query_callback
 * EXPORTS_END
 */