OLD | NEW |
1 // Allows a document to exercise the Push API within a service worker by sending
commands. | 1 // Allows a document to exercise the Push API within a service worker by sending
commands. |
2 | 2 |
3 // The port through which the document sends commands to the service worker. | 3 // The port through which the document sends commands to the service worker. |
4 var port = null; | 4 var port = null; |
5 | 5 |
6 // The most recently seen subscription. | 6 // The most recently seen subscription. |
7 var lastSeenSubscription = null; | 7 var lastSeenSubscription = null; |
8 | 8 |
9 self.addEventListener('message', function(workerEvent) { | 9 self.addEventListener('message', function(workerEvent) { |
10 port = workerEvent.data; | 10 port = workerEvent.data; |
(...skipping 10 matching lines...) Expand all Loading... |
21 success: true, | 21 success: true, |
22 permission: permissionStatus }); | 22 permission: permissionStatus }); |
23 }).catch(makeErrorHandler(event.data.command)); | 23 }).catch(makeErrorHandler(event.data.command)); |
24 break; | 24 break; |
25 | 25 |
26 case 'subscribe': | 26 case 'subscribe': |
27 self.registration.pushManager.subscribe(options).then(function(s
ubscription) { | 27 self.registration.pushManager.subscribe(options).then(function(s
ubscription) { |
28 lastSeenSubscription = subscription; | 28 lastSeenSubscription = subscription; |
29 port.postMessage({ command: event.data.command, | 29 port.postMessage({ command: event.data.command, |
30 success: true, | 30 success: true, |
31 subscriptionId: subscription.subscription
Id, | |
32 endpoint: subscription.endpoint }); | 31 endpoint: subscription.endpoint }); |
33 }).catch(makeErrorHandler(event.data.command)); | 32 }).catch(makeErrorHandler(event.data.command)); |
34 break; | 33 break; |
35 | 34 |
36 case 'getSubscription': | 35 case 'getSubscription': |
37 self.registration.pushManager.getSubscription().then(function(su
bscription) { | 36 self.registration.pushManager.getSubscription().then(function(su
bscription) { |
38 lastSeenSubscription = subscription; | 37 lastSeenSubscription = subscription; |
39 var subscriptionId = subscription ? subscription.subscriptio
nId : null; | 38 var endpoint = subscription ? subscription.endpoint : null; |
40 port.postMessage({ command: event.data.command, | 39 port.postMessage({ command: event.data.command, |
41 success: true, | 40 success: true, |
42 subscriptionId: subscriptionId }); | 41 endpoint: endpoint }); |
43 }).catch(makeErrorHandler(event.data.command)); | 42 }).catch(makeErrorHandler(event.data.command)); |
44 break; | 43 break; |
45 | 44 |
46 case 'unsubscribe': | 45 case 'unsubscribe': |
47 self.registration.pushManager.getSubscription() | 46 self.registration.pushManager.getSubscription() |
48 .then(function(subscription) { | 47 .then(function(subscription) { |
49 // We keep track of lastSeenSubscription so we can attem
pt to unsubscribe | 48 // We keep track of lastSeenSubscription so we can attem
pt to unsubscribe |
50 // more than once. | 49 // more than once. |
51 subscription = subscription || lastSeenSubscription; | 50 subscription = subscription || lastSeenSubscription; |
52 if (!subscription) | 51 if (!subscription) |
(...skipping 20 matching lines...) Expand all Loading... |
73 }); | 72 }); |
74 | 73 |
75 function makeErrorHandler(command) { | 74 function makeErrorHandler(command) { |
76 return function(error) { | 75 return function(error) { |
77 var errorMessage = error ? error.message : 'unknown error'; | 76 var errorMessage = error ? error.message : 'unknown error'; |
78 port.postMessage({ command: command, | 77 port.postMessage({ command: command, |
79 success: false, | 78 success: false, |
80 errorMessage: errorMessage }); | 79 errorMessage: errorMessage }); |
81 }; | 80 }; |
82 } | 81 } |
OLD | NEW |