-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
batched-xhr-impl.js
108 lines (97 loc) · 2.83 KB
/
batched-xhr-impl.js
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
/**
* Copyright 2017 The AMP HTML Authors. 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.
*/
import {Xhr} from './xhr-impl';
import {getService, registerServiceBuilder} from '../service';
import {getSourceOrigin, removeFragment, resolveRelativeUrl} from '../url';
import {map} from '../utils/object';
/**
* A wrapper around the Xhr service which batches the result of GET requests
*
* @package Visible for type.
* @visibleForTesting
*/
export class BatchedXhr extends Xhr {
/**
* @param {!Window} win
*/
constructor(win) {
super(win);
/** @const {!Object<!Promise<!Response>>} */
this.fetchPromises_ = map();
}
/**
* Fetch and batch the requests if possible.
*
* @param {string} input URL
* @param {?FetchInitDef=} opt_init Fetch options object.
* @return {!Promise<!Response>}
* @override
*/
fetch(input, opt_init) {
const accept =
(opt_init && opt_init.headers && opt_init.headers['Accept']) || '';
const isBatchable =
!opt_init || !opt_init.method || opt_init.method === 'GET';
const key = this.getMapKey_(input, accept);
const isBatched = !!this.fetchPromises_[key];
if (isBatchable && isBatched) {
return this.fetchPromises_[key].then((response) => response.clone());
}
const fetchPromise = super.fetch(input, opt_init);
if (isBatchable) {
this.fetchPromises_[key] = fetchPromise.then(
(response) => {
delete this.fetchPromises_[key];
return response.clone();
},
(err) => {
delete this.fetchPromises_[key];
throw err;
}
);
}
return fetchPromise;
}
/**
* Creates a map key for a fetch.
*
* @param {string} input URL
* @param {string} responseType
* @return {string}
* @private
*/
getMapKey_(input, responseType) {
const absoluteUrl = resolveRelativeUrl(
input,
getSourceOrigin(this.win.location)
);
return removeFragment(absoluteUrl) + responseType;
}
}
/**
* @param {!Window} window
* @return {!BatchedXhr}
*/
export function batchedXhrServiceForTesting(window) {
installBatchedXhrService(window);
return getService(window, 'batched-xhr');
}
/**
* @param {!Window} window
*/
export function installBatchedXhrService(window) {
registerServiceBuilder(window, 'batched-xhr', BatchedXhr);
}