|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>Core-ajax handling race condition</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + <script src="../platform/platform.js" debug></script> |
| 9 | + <link rel="import" href="core-ajax.html"> |
| 10 | + <polymer-element name="race-condition"> |
| 11 | + <template> |
| 12 | + <style>section {margin-top: 20px;}</style> |
| 13 | + <core-ajax |
| 14 | + id='ajax' |
| 15 | + url="http://httpbin.org/delay/{{delay}}" |
| 16 | + handleas="json" auto |
| 17 | + response="{{response}}" |
| 18 | + on-core-response="{{handleResponse}}"></core-ajax> |
| 19 | + <div>Response url: {{response.url}}</div> |
| 20 | + <div>Result: {{testResult}}</div> |
| 21 | + |
| 22 | + <section> |
| 23 | + <div>Requests</div> |
| 24 | + <ul> |
| 25 | + <template repeat='{{request in requests}}'> |
| 26 | + <li> |
| 27 | + {{request.delay}} second delay, Status: {{request.statusText}} |
| 28 | + </li> |
| 29 | + </template> |
| 30 | + </ul> |
| 31 | + </section> |
| 32 | + </template> |
| 33 | + <script> |
| 34 | + Polymer({ |
| 35 | + delay: 1, |
| 36 | + response: null, |
| 37 | + testResult: 'pending...', |
| 38 | + passed: false, |
| 39 | + requests: [], |
| 40 | + observe: { |
| 41 | + '$.ajax.activeRequest': 'requestChanged' |
| 42 | + }, |
| 43 | + domReady: function() { |
| 44 | + setTimeout(function() { |
| 45 | + if (this.response != null) { |
| 46 | + console.error('HTTP request returned too quick!') |
| 47 | + this.testResult = 'indeterminate'; |
| 48 | + return; |
| 49 | + } |
| 50 | + this.delay = 3; |
| 51 | + }.bind(this), 100); |
| 52 | + }, |
| 53 | + responseChanged: function() { |
| 54 | + if (this.response.url != this.$.ajax.url) { |
| 55 | + this.testResult = 'FAIL'; |
| 56 | + console.error('Race condition in response attribute. Expected ' |
| 57 | + + this.$.ajax.url + ' got ' + this.response.url); |
| 58 | + return; |
| 59 | + } |
| 60 | + this.passed = true; |
| 61 | + }, |
| 62 | + passedChanged: function() { |
| 63 | + if (this.passed && this.testResult == 'pending...') { |
| 64 | + this.testResult = 'PASS'; |
| 65 | + } |
| 66 | + }, |
| 67 | + requestChanged: function(o, n) { |
| 68 | + this.requests.push({ |
| 69 | + 'statusText': 'pending', |
| 70 | + xhr: n, |
| 71 | + delay: this.delay |
| 72 | + }); |
| 73 | + }, |
| 74 | + handleResponse: function(resp) { |
| 75 | + var xhr = resp.detail.xhr; |
| 76 | + for (var i = 0; i < this.requests.length; i++) { |
| 77 | + if (this.requests[i].xhr === xhr) { |
| 78 | + this.requests[i].statusText = xhr.statusText; |
| 79 | + } |
| 80 | + } |
| 81 | + }, |
| 82 | + }); |
| 83 | + </script> |
| 84 | + </polymer-element> |
| 85 | + |
| 86 | + <race-condition></race-condition> |
| 87 | +</body> |
| 88 | +</html> |
0 commit comments