-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxhr.js
More file actions
144 lines (119 loc) · 3.6 KB
/
xhr.js
File metadata and controls
144 lines (119 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
const xhrTimeout = 15;
const jsonType = 'application/json';
/**
* Helper to safely dispatch events (i.e., for IE9-10).
* @param {!EventTarget} target to dispatch event on
* @param {string} name of event
*/
function dispatchEvent(target, name) {
let ev = document.createEvent('Event');
ev.initEvent(name, /* bubbles */ true, /* cancelable */ false);
target.dispatchEvent(ev);
}
const xe = class XhrElement extends HTMLElement {
connectedCallback() {
this._timeout = null;
this._result = undefined;
this._xhr = new XMLHttpRequest();
this.performRequest();
}
/**
* @param {string} attr
* @param {string} oldVal
* @param {string} newVal
*/
attributeChangedCallback(attr, oldVal, newVal) {
switch(attr) {
case 'url':
case 'credentials':
this.performRequest();
break;
}
}
/**
* @return {string} URL to fetch
*/
get url() {
return this.getAttribute('url');
}
/**
* @param {string} url to fetch
*/
set url(url) {
this.setAttribute('url', url);
}
/**
* @return {boolean} whether to pass credentials on cross-site requests
*/
get credentials() {
return this.hasAttribute('credentials');
}
/**
* @param {boolean} v whether to pass credentials on cross-site requests
*/
set credentials(v) {
if (v) {
this.setAttribute('credentials', '');
} else {
this.removeAttribute('credentials');
}
}
get response() {
return this._response;
}
_setResponse(response) {
this._response = response;
this.innerHTML = response;
dispatchEvent(this, 'response-change');
}
/**
* Requests that this element perform the request, even if the result is already up-to-date.
*/
performRequest() {
this._xhr.abort(); // always abort for safety
// TODO: move all this into a helper class
if (!this.url) {
this._setResult(undefined);
return;
}
window.clearTimeout(this._timeout);
this._timeout = window.setTimeout(_ => {
let x = new XMLHttpRequest();
this._xhr = x;
// Check for this._xhr != x below, to see whether we've been superceded by some other
// request.
x.open('GET', this.url);
x.onload = () => {
if (this._xhr != x) { return; }
const ct = x.getResponseHeader('Content-Type');
let response = x.response;
// look for 'type' or 'type;...'
if (ct == jsonType || ct.substr(0, jsonType.length + 1) == jsonType + ';') {
response = JSON.parse(response);
}
this._setResponse(response);
};
x.onerror = () => {
if (this._xhr != x) { return; }
this._setResponse(new Error(x.statusText ? x.statusText : '' + x.status));
};
x.send();
}, xhrTimeout);
}
};
window.customElements.define("my-xhr", xe)