OLD | NEW |
---|---|
(Empty) | |
1 // Allows a document to exercise the Budget API within a service worker by sendi ng commands. | |
2 | |
3 self.addEventListener('message', function(workerEvent) { | |
4 port = workerEvent.data; | |
5 | |
6 // Listen to incoming commands on the message port. | |
7 port.onmessage = function(event) { | |
8 if (typeof event.data != 'object' || !event.data.command) | |
9 return; | |
10 var options = event.data.options || {} | |
Peter Beverloo
2016/08/24 15:28:23
;
harkness
2016/08/24 16:22:50
Done.
| |
11 switch (event.data.command) { | |
12 | |
13 case 'getCost': | |
14 navigator.budget.getCost('silent-push').then(function(cost) { | |
15 port.postMessage({ command: event.data.command, | |
16 success: true, | |
17 cost: cost }); | |
Peter Beverloo
2016/08/24 15:28:23
The alignment of this (and lines 23+, 31+, 39+ and
harkness
2016/08/24 16:22:50
Done.
| |
18 }).catch(makeErrorHandler(event.data.command)); | |
19 break; | |
20 | |
21 case 'getCostInvalidType': | |
22 navigator.budget.getCost('frobinator').then(function(cost) { | |
23 port.postMessage({ command: event.data.command, | |
24 success: true, | |
25 cost: cost }); | |
26 }).catch(makeErrorHandler(event.data.command)); | |
27 break; | |
28 | |
29 case 'getBudget': | |
30 navigator.budget.getBudget().then(budget => { | |
31 port.postMessage({ command: event.data.command, | |
32 success: true, | |
33 budgetAt: budget[0].budgetAt, | |
34 time: budget[0].time }); | |
35 }).catch(makeErrorHandler(event.data.command)); | |
36 break; | |
37 | |
38 default: | |
39 port.postMessage({ command: 'error', | |
40 errorMessage: 'Invalid command: ' + event.data.command }); | |
41 break; | |
42 } | |
43 }; | |
44 | |
45 // Notify the controller that the worker is now available. | |
46 port.postMessage('ready'); | |
47 }); | |
48 | |
49 function makeErrorHandler(command) { | |
50 return function(error) { | |
51 var errorMessage = error ? error.message : 'unknown error'; | |
52 port.postMessage({ command: command, | |
53 success: false, | |
54 errorMessage: errorMessage }); | |
55 }; | |
56 } | |
OLD | NEW |