/
ldf-client-worker.js
54 lines (47 loc) · 1.69 KB
/
ldf-client-worker.js
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
// The active fragments client and the current results
var fragmentsClient, resultsIterator;
// Set up logging
var logger = new ldf.Logger();
ldf.Logger.setLevel('info');
logger._print = function (items) {
postMessage({ type: 'log', log: items.slice(2).join(' ').trim() + '\n' });
};
// Handlers of incoming messages
var handlers = {
// Execute the given query with the given options
query: function (config) {
// Create a client to fetch the fragments through HTTP
config.logger = logger;
config.fragmentsClient = fragmentsClient =
new ldf.FragmentsClient(config.datasources, config);
// Create an iterator to evaluate the query
try { resultsIterator = new ldf.SparqlIterator(config.query, config); }
catch (error) { return postMessage({ type: 'error', error: error }); }
// Post query metadata
postMessage({ type: 'queryInfo', queryType: resultsIterator.queryType });
// Post iterator events
resultsIterator.on('data', function (result) {
postMessage({ type: 'result', result: result });
});
resultsIterator.on('end', function () {
postMessage({ type: 'end' });
});
resultsIterator.on('error', function (error) {
error = { message: error.message || error.toString() };
postMessage({ type: 'error', error: error });
});
},
// Stop the execution of the current query
stop: function () {
if (resultsIterator) {
resultsIterator.removeAllListeners();
resultsIterator = null;
}
if (fragmentsClient) {
fragmentsClient.abortAll();
fragmentsClient = null;
}
},
};
// Send incoming message to the appropriate handler
self.onmessage = function (m) { handlers[m.data.type](m.data); };