Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Accept messages from the test JavaScript to trigger worker based tests. | |
| 6 self.addEventListener('message', function handler (event) { | |
| 7 if (event.data.command == 'workerGet') { | |
| 8 self.workerGetBudget(); | |
| 9 } else if (event.data.command == 'workerReserve') { | |
| 10 self.workerReserve(); | |
|
Peter Beverloo
2016/09/27 15:57:47
nit: drop the |self| everywhere. That's the global
harkness
2016/09/28 12:29:21
Done.
| |
| 11 } else { | |
| 12 sendMessageToClients('message', 'error - unknown message request'); | |
| 13 return; | |
| 14 } | |
| 15 }); | |
| 16 | |
| 17 // Query for the budget and return the current total. | |
| 18 function workerGetBudget() { | |
|
Peter Beverloo
2016/09/27 15:57:47
Since we shipped async/await, you can use a much f
harkness
2016/09/28 12:29:21
Actually, when I tried to switch to async, the ser
| |
| 19 navigator.budget.getBudget().then(function(budget) { | |
| 20 sendMessageToClients('message', | |
| 21 "ok - budget returned value of " + budget[0].budgetAt); | |
| 22 }, function() { | |
| 23 sendMessageToClients('message', "failed - unable to get budget values"); | |
| 24 }); | |
| 25 } | |
| 26 | |
| 27 // Request a reservation for a silent push. | |
| 28 function workerReserve() { | |
| 29 navigator.budget.reserve('silent-push').then(function(reserved) { | |
| 30 if (reserved) | |
| 31 sendMessageToClients('message', "ok - reserved budget"); | |
| 32 else | |
| 33 sendMessageToClients('message', "failed - not able to reserve budget"); | |
| 34 }, function() { | |
| 35 sendMessageToClients('message', | |
| 36 "failed - error while trying to reserve budget"); | |
| 37 }); | |
| 38 } | |
| 39 | |
| 40 function sendMessageToClients(type, data) { | |
| 41 var message = JSON.stringify({ | |
|
Peter Beverloo
2016/09/27 15:57:47
nit: use destructoring, don't use var.
const mess
harkness
2016/09/28 12:29:21
Done.
Peter Beverloo
2016/09/28 13:04:26
This hasn't been done.
| |
| 42 'type': type, | |
| 43 'data': data | |
| 44 }); | |
| 45 clients.matchAll().then(function(clients) { | |
| 46 clients.forEach(function(client) { | |
| 47 client.postMessage(message); | |
| 48 }); | |
| 49 }, function(error) { | |
| 50 console.log(error); | |
| 51 }); | |
|
Peter Beverloo
2016/09/27 15:57:47
If this were an async function, you could replace
harkness
2016/09/28 12:29:21
Same issues as above.
| |
| 52 } | |
| OLD | NEW |