-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker.js
59 lines (52 loc) · 1.96 KB
/
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
55
56
57
58
59
function weatherApiUrl(location) {
return 'https://query.yahooapis.com/v1/public/yql?q=' +
encodeURIComponent('select item.condition, wind from ' +
'weather.forecast where woeid in (select woeid from geo.places(1) ' +
`where text="${location}")`) + '&format=json&env=' +
encodeURIComponent('store://datatables.org/alltableswithkeys');
}
// Expose a service to the main thread.
self.services.register('speak', class {
// We have to list what methods are exposed.
static get exposed() { return ['concat']; }
// The constructor can take arguments.
constructor(prefix) {
this.prefix_ = prefix;
}
concat(message) {
return this.prefix_ + message;
}
});
self.services.register('weather', class {
static get exposed() { return ['query']; }
// If a method returns a Promise (ex. is async) then the system waits for it
// to resolve and returns the answer.
async query(location) {
// Make some network requests and do something "expensive".
let response = await fetch(weatherApiUrl(location));
let data = await response.json();
let channel = data.query.results.channel;
return {
temp: channel.item.condition.temp,
text: channel.item.condition.text,
wind: channel.wind.speed,
};
}
});
// We can also connect to services the main thread exposes to us.
(async function() {
let dom = await self.services.connect('dom');
// We can send multiple messages in parallel, since this is all inside one
// function they'll show up in the same order on the other thread.
dom.appendText('Inside the worker?');
dom.appendBox('A box!');
dom.appendBox('A styled box!', new Map([
['color', 'red'],
['border', '1px solid blue'],
['padding', '5px'],
]));
// For now we need to manually disconnect, eventually we might clean up the
// instances automatically and auto reconnect when a method is called if the
// end point has been cleaned up.
self.services.disconnect(dom);
})();