OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var code = 404; | 5 var sawSyncEvent = false; |
| 6 var fetchCount = 0; |
| 7 |
| 8 // onfetch is a state machine: |
| 9 // 1st call returns 404 and calls requestSyncEvents() |
| 10 // .. sync doesn't fire .. |
| 11 // 2nd call returns 404 and calls requestSyncEvents(true) |
| 12 // .. sync fires .. |
| 13 // 3rd call returns 200 and calls requestSyncEvents(false) |
| 14 // .. sync doesn't fire .. |
| 15 // 4th call returns 404 |
6 | 16 |
7 this.onfetch = function(event) { | 17 this.onfetch = function(event) { |
| 18 var code = sawSyncEvent ? 200 : 404; |
8 response = new Response({ | 19 response = new Response({ |
9 statusCode: code, | 20 statusCode: code, |
10 statusText: 'OK', | 21 statusText: 'OK', |
11 method: 'GET', | 22 method: 'GET', |
12 headers: { | 23 headers: { |
13 'Content-Language': 'fi', | 24 'Content-Language': 'fi', |
14 'Content-Type': 'text/html; charset=UTF-8' | 25 'Content-Type': 'text/html; charset=UTF-8' |
15 } | 26 } |
16 }); | 27 }); |
17 | 28 |
18 event.respondWith(new Promise(function(r) { | 29 event.respondWith(new Promise(function(r) { |
19 setTimeout(function() { r(response); }, 5); | 30 setTimeout(function() { r(response); }, 5); |
20 })); | 31 })); |
| 32 |
| 33 if (fetchCount == 0) { |
| 34 requestSyncEvents(); |
| 35 } else if (fetchCount == 1) { |
| 36 requestSyncEvents(true); |
| 37 } else if (fetchCount == 2) { |
| 38 requestSyncEvents(false); |
| 39 } |
| 40 |
| 41 fetchCount += 1; |
| 42 sawSyncEvent = false; |
21 }; | 43 }; |
22 | 44 |
23 this.onsync = function(event) { | 45 this.onsync = function(event) { |
24 code = 200; | 46 sawSyncEvent = true; |
25 }; | 47 }; |
OLD | NEW |