-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.js
More file actions
191 lines (161 loc) · 4.14 KB
/
bundle.js
File metadata and controls
191 lines (161 loc) · 4.14 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))})
(['./track', './FauxXHR', './scaffold'], function (io, FauxXHR, scaffold) {
'use strict';
// bundle I/O requests
function bundle (options, prep, level) {
if (options.wait || !io.bundle.optIn(options)) {
return null;
}
var waitTime = io.bundle.waitTime,
isBundling = io.bundle.isStarted();
if (isBundling || waitTime > 0) {
if (!isBundling && waitTime > 0) {
setTimeout(io.bundle.commit, waitTime);
io.bundle.start();
}
io.bundle.pending[prep.key] = {options: options, prep: prep, level: level};
var deferred = io.track.deferred[prep.key];
return deferred.promise || deferred;
}
return null;
}
var delay = false;
function start () {
delay = true;
}
function isStarted () {
return delay;
}
function commit () {
var bundle = Object.keys(io.bundle.pending).map(function (key) {
return io.bundle.pending[key];
}),
bundleSize = Math.max(io.bundle.maxSize, 2);
io.bundle.pending = {};
delay = false;
if (bundle.length <= bundleSize) {
// send a single bundle
sendBundle(bundle);
} else {
// send several bundles
for (var i = 0; i < bundle.length; i += bundleSize) {
sendBundle(bundle.slice(i, i + bundleSize));
}
}
}
function sendBundle (bundle) {
if (bundle.length < Math.max(Math.min(io.bundle.minSize, io.bundle.maxSize), 1)) {
// small bundle => send each item separately
return bundle.forEach(sendRequest);
}
// send a bundle
io({
url: io.bundle.url,
method: 'PUT',
bundle: false,
data: bundle.map(function (item) {
return flattenOptions(item.options);
})
});
}
function sendRequest (item) {
var key = item.prep.key, deferred = io.track.deferred[key];
io.request(item.options, item.prep, item.level - 1).then(
function (value) { deferred.resolve(value, true); },
function (value) { deferred.reject (value, true); }
);
}
function flattenOptions (options) {
var newOptions = {};
for (var key in options) {
newOptions[key] = options[key];
}
return newOptions;
}
// processing bundles
function unbundle (data) {
var bundle = io.bundle.detect(data);
if (bundle) {
bundle.forEach(function (result) {
var key = io.makeKey(result.options),
xhr = new FauxXHR(result.response),
deferred = io.track.deferred[key];
if (deferred) {
deferred.resolve(new io.Result(xhr, result.options, null), true);
} else {
io.cache && io.cache.saveByKey(key, xhr);
}
});
}
}
function detect (data) {
return data && typeof data == 'object' && data.bundle === 'bundle' &&
data.results instanceof Array ? data.results : null;
}
function attachProcessSuccess (previousProcessSuccess) {
return function (result) {
var data = previousProcessSuccess(result);
if (io.bundle.isActive) {
io.bundle.unbundle(data);
}
return data;
};
}
// convenience functions
function fly (bundle) {
bundle.forEach(io.track.fly);
}
function submit (bundle) {
if (io.bundle.isStarted()) {
bundle.forEach(io);
} else {
io.bundle.start();
bundle.forEach(io);
io.bundle.commit();
}
}
function submitWithRelated (options, bundle) {
var promise;
if (io.bundle.isStarted()) {
bundle.forEach(io);
promise = io(options);
} else {
io.bundle.start();
bundle.forEach(io);
promise = io(options);
io.bundle.commit();
}
return promise;
}
// export
function attach () {
io.track.attach();
io.processSuccess = attachProcessSuccess(io.processSuccess);
io.attach({
name: 'bundle',
priority: 10,
callback: bundle
});
io.bundle.isActive = true;
}
io.bundle = {
attach: attach,
// start/commit bundles
start: start,
commit: commit,
isStarted: isStarted,
waitTime: 20, // in ms
// server-side bundle settings
url: '/bundle',
minSize: 2,
maxSize: 20,
detect: detect,
// advanced utilities
unbundle: unbundle,
submit: submit,
submitWithRelated: submitWithRelated,
fly: fly,
pending: {}
};
return scaffold(io, 'bundle', 10, bundle);
});