loada 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bower.json +1 -1
- data/lib/loada.js +86 -68
- data/package.json +1 -1
- data/spec/loada_spec.coffee +104 -44
- data/spec/support/test1_1.js +1 -0
- data/spec/support/test1_2.js +1 -0
- data/spec/support/test2_1.js +1 -0
- data/spec/support/text +1 -0
- data/src/loada.coffee +69 -54
- metadata +5 -2
- data/spec/support/test.js +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e46e048066887349e82593374bd8e01be9939074
|
4
|
+
data.tar.gz: c2e315a44219740081edad7d28b5527454638597
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d80029578b98744380e519d132ad776c5a458dc70fe7db6630d735d5aee1ea9eaabebc837fcc42d1c0141ac4e73a4dc44326c14915afa588dda0b12d28ddca48
|
7
|
+
data.tar.gz: 41c455b41631f59aa046009ef4eccef8a19f8443551324618f73212b02ba40a27bb19efd6452f819bc5cbd23bbea6665cac9334fe0b8a6a734551586cd5aebae
|
data/bower.json
CHANGED
data/lib/loada.js
CHANGED
@@ -71,15 +71,49 @@
|
|
71
71
|
if (typeof this.storage === 'string') {
|
72
72
|
return this.storage = JSON.parse(localStorage[this.key]);
|
73
73
|
}
|
74
|
+
} else {
|
75
|
+
return this.storage = {};
|
76
|
+
}
|
77
|
+
};
|
78
|
+
|
79
|
+
Loada.prototype.expire = function() {
|
80
|
+
var byDate, byExistance, byRevision, key, library, now, _ref, _results,
|
81
|
+
_this = this;
|
82
|
+
if (this.options.localStorage) {
|
83
|
+
now = new Date;
|
84
|
+
byDate = function(library) {
|
85
|
+
return library.expirationDate && new Date(library.expirationDate) <= now;
|
86
|
+
};
|
87
|
+
byExistance = function(library) {
|
88
|
+
return !_this.requires.set[key];
|
89
|
+
};
|
90
|
+
byRevision = function(library) {
|
91
|
+
return _this.requires.set[key].revision !== library.revision;
|
92
|
+
};
|
93
|
+
_ref = this.storage;
|
94
|
+
_results = [];
|
95
|
+
for (key in _ref) {
|
96
|
+
library = _ref[key];
|
97
|
+
if (byDate(library) || byExistance(library) || byRevision(library)) {
|
98
|
+
_results.push(delete this.storage[key]);
|
99
|
+
} else {
|
100
|
+
_results.push(void 0);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
return _results;
|
74
104
|
}
|
75
105
|
};
|
76
106
|
|
77
107
|
Loada.prototype.save = function() {
|
78
|
-
|
108
|
+
if (this.options.localStorage) {
|
109
|
+
return localStorage[this.key] = JSON.stringify(this.storage);
|
110
|
+
}
|
79
111
|
};
|
80
112
|
|
81
113
|
Loada.prototype.clear = function() {
|
82
|
-
|
114
|
+
if (this.options.localStorage) {
|
115
|
+
return delete localStorage[this.key];
|
116
|
+
}
|
83
117
|
};
|
84
118
|
|
85
119
|
Loada.prototype.get = function(key) {
|
@@ -87,32 +121,6 @@
|
|
87
121
|
return (_ref = this.storage[key]) != null ? _ref.source : void 0;
|
88
122
|
};
|
89
123
|
|
90
|
-
Loada.prototype.expire = function() {
|
91
|
-
var byDate, byExistance, byRevision, key, library, now, _ref, _results,
|
92
|
-
_this = this;
|
93
|
-
now = new Date;
|
94
|
-
byDate = function(library) {
|
95
|
-
return library.expirationDate && new Date(library.expirationDate) <= now;
|
96
|
-
};
|
97
|
-
byExistance = function(library) {
|
98
|
-
return !_this.requires.set[key];
|
99
|
-
};
|
100
|
-
byRevision = function(library) {
|
101
|
-
return _this.requires.set[key].revision !== library.revision;
|
102
|
-
};
|
103
|
-
_ref = this.storage;
|
104
|
-
_results = [];
|
105
|
-
for (key in _ref) {
|
106
|
-
library = _ref[key];
|
107
|
-
if (byDate(library) || byExistance(library) || byRevision(library)) {
|
108
|
-
_results.push(delete this.storage[key]);
|
109
|
-
} else {
|
110
|
-
_results.push(void 0);
|
111
|
-
}
|
112
|
-
}
|
113
|
-
return _results;
|
114
|
-
};
|
115
|
-
|
116
124
|
Loada.prototype.require = function() {
|
117
125
|
var libraries, library, now, _i, _len, _ref;
|
118
126
|
libraries = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
@@ -120,8 +128,8 @@
|
|
120
128
|
library = libraries[_i];
|
121
129
|
library.key || (library.key = library.url);
|
122
130
|
library.type || (library.type = (_ref = library.url) != null ? _ref.split('.').pop() : void 0);
|
123
|
-
if (library.
|
124
|
-
library.
|
131
|
+
if (library.cache == null) {
|
132
|
+
library.cache = true;
|
125
133
|
}
|
126
134
|
if (library.require == null) {
|
127
135
|
library.require = true;
|
@@ -144,9 +152,7 @@
|
|
144
152
|
var loaders, progress,
|
145
153
|
_this = this;
|
146
154
|
callbacks || (callbacks = {});
|
147
|
-
|
148
|
-
this.expire();
|
149
|
-
}
|
155
|
+
this.expire();
|
150
156
|
progress = new this.Progress(this.requires.length, callbacks.progress);
|
151
157
|
loaders = 0;
|
152
158
|
return this._ensureSizes(callbacks.progress != null, function() {
|
@@ -206,31 +212,30 @@
|
|
206
212
|
};
|
207
213
|
|
208
214
|
Loada.prototype._loadGroup = function(group, progress, callback) {
|
209
|
-
var library, method,
|
215
|
+
var library, method, results, _i, _len,
|
210
216
|
_this = this;
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
progress.set(library.key, 100);
|
218
|
-
}
|
219
|
-
if (this.storage[library.key].require) {
|
220
|
-
this._inject(this.storage[library.key]);
|
221
|
-
}
|
222
|
-
return this._loadGroup(group, progress, callback);
|
223
|
-
} else {
|
224
|
-
method = this.options.localStorage ? "_loadAJAX" : "_loadInline";
|
225
|
-
return this[method](library, progress, function() {
|
226
|
-
if (_this.options.localStorage) {
|
227
|
-
_this.storage[library.key] = library;
|
228
|
-
}
|
229
|
-
if (library.require) {
|
230
|
-
_this._inject(library);
|
217
|
+
results = [];
|
218
|
+
for (_i = 0, _len = group.length; _i < _len; _i++) {
|
219
|
+
library = group[_i];
|
220
|
+
if (this.storage[library.key] && library.cache) {
|
221
|
+
if (progress != null) {
|
222
|
+
progress.set(library.key, 100);
|
231
223
|
}
|
232
|
-
|
233
|
-
}
|
224
|
+
results.push(this.storage[library.key]);
|
225
|
+
} else {
|
226
|
+
method = this.options.localStorage || !library.require ? "_loadAJAX" : "_loadInline";
|
227
|
+
this[method](library, progress, function() {
|
228
|
+
results.push(library);
|
229
|
+
if (results.length === group.length) {
|
230
|
+
_this._inject(results);
|
231
|
+
return callback();
|
232
|
+
}
|
233
|
+
});
|
234
|
+
}
|
235
|
+
}
|
236
|
+
if (results.length === group.length) {
|
237
|
+
this._inject(results);
|
238
|
+
return callback();
|
234
239
|
}
|
235
240
|
};
|
236
241
|
|
@@ -238,6 +243,9 @@
|
|
238
243
|
var poller, xhr,
|
239
244
|
_this = this;
|
240
245
|
xhr = this._ajax('GET', library.url, function(xhr) {
|
246
|
+
if (library.cache) {
|
247
|
+
_this.storage[library.key] = library;
|
248
|
+
}
|
241
249
|
library.source = xhr.responseText;
|
242
250
|
clearInterval(poller);
|
243
251
|
if (progress != null) {
|
@@ -283,18 +291,28 @@
|
|
283
291
|
return $head.appendChild(script);
|
284
292
|
};
|
285
293
|
|
286
|
-
Loada.prototype._inject = function(
|
287
|
-
var script, style;
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
294
|
+
Loada.prototype._inject = function(libraries) {
|
295
|
+
var library, script, style, _i, _len, _results;
|
296
|
+
libraries = [].concat(libraries);
|
297
|
+
_results = [];
|
298
|
+
for (_i = 0, _len = libraries.length; _i < _len; _i++) {
|
299
|
+
library = libraries[_i];
|
300
|
+
if (library.require && library.source) {
|
301
|
+
if (library.type === 'js') {
|
302
|
+
script = document.createElement("script");
|
303
|
+
script.defer = true;
|
304
|
+
script.text = library.source;
|
305
|
+
_results.push($head.appendChild(script));
|
306
|
+
} else if (library.type === 'css') {
|
307
|
+
style = document.createElement("style");
|
308
|
+
style.innerHTML = library.source;
|
309
|
+
_results.push($head.appendChild(style));
|
310
|
+
} else {
|
311
|
+
_results.push(void 0);
|
312
|
+
}
|
313
|
+
}
|
297
314
|
}
|
315
|
+
return _results;
|
298
316
|
};
|
299
317
|
|
300
318
|
Loada.prototype._ajax = function(method, url, callback) {
|
@@ -304,12 +322,12 @@
|
|
304
322
|
} else {
|
305
323
|
xhr = new ActiveXObject('Microsoft.XMLHTTP');
|
306
324
|
}
|
307
|
-
xhr.open(method, url, 1);
|
308
325
|
xhr.onreadystatechange = function() {
|
309
326
|
if (xhr.readyState > 3) {
|
310
327
|
return typeof callback === "function" ? callback(xhr) : void 0;
|
311
328
|
}
|
312
329
|
};
|
330
|
+
xhr.open(method, url, 1);
|
313
331
|
xhr.send();
|
314
332
|
return xhr;
|
315
333
|
};
|
data/package.json
CHANGED
data/spec/loada_spec.coffee
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
describe "Loada", ->
|
2
2
|
|
3
|
+
beforeEach ->
|
4
|
+
Loada.debug = false
|
5
|
+
window.sandbox = {}
|
6
|
+
|
7
|
+
afterEach ->
|
8
|
+
delete window.sandbox
|
9
|
+
|
3
10
|
it "initializes properly", ->
|
4
11
|
set = Loada.set(undefined, localStorage: false)
|
5
12
|
expect(set.set).toEqual '*'
|
6
13
|
expect(set.options.localStorage).toBeFalsy()
|
7
|
-
expect(set.storage).
|
14
|
+
expect(set.storage).toEqual {}
|
8
15
|
|
9
16
|
localStorage['loada.foo'] = JSON.stringify('foo': 'bar')
|
10
17
|
|
@@ -74,6 +81,10 @@ describe "Loada", ->
|
|
74
81
|
@set.require url: 'foo.js', size: 100
|
75
82
|
@set.require url: 'bar.js'
|
76
83
|
@set.require url: 'baz.js'
|
84
|
+
@server = sinon.fakeServer.create()
|
85
|
+
|
86
|
+
afterEach ->
|
87
|
+
@server.restore()
|
77
88
|
|
78
89
|
it "zerofills with no progress tracking", ->
|
79
90
|
callback = sinon.spy()
|
@@ -83,18 +94,16 @@ describe "Loada", ->
|
|
83
94
|
|
84
95
|
it "gets sizes with progress tracking", ->
|
85
96
|
callback = sinon.spy()
|
86
|
-
server = sinon.fakeServer.create()
|
87
97
|
@set._ensureSizes true, callback
|
88
98
|
|
89
99
|
waits 0
|
90
100
|
|
91
101
|
runs ->
|
92
|
-
expect(server.requests[0].url).toEqual 'bar.js'
|
93
|
-
expect(server.requests[1].url).toEqual 'baz.js'
|
102
|
+
expect(@server.requests[0].url).toEqual 'bar.js'
|
103
|
+
expect(@server.requests[1].url).toEqual 'baz.js'
|
94
104
|
|
95
|
-
server.requests[0].respond 200, {'Content-Length': '100'}, ''
|
96
|
-
server.requests[1].respond 200, {}, ''
|
97
|
-
server.restore()
|
105
|
+
@server.requests[0].respond 200, {'Content-Length': '100'}, ''
|
106
|
+
@server.requests[1].respond 200, {}, ''
|
98
107
|
|
99
108
|
waits 0
|
100
109
|
|
@@ -111,7 +120,6 @@ describe "Loada", ->
|
|
111
120
|
sinon.stub @set, '_inject'
|
112
121
|
|
113
122
|
afterEach ->
|
114
|
-
@set._inject.restore()
|
115
123
|
@server.restore()
|
116
124
|
|
117
125
|
it "gets single from cache", ->
|
@@ -130,7 +138,7 @@ describe "Loada", ->
|
|
130
138
|
expect(@server.requests.length).toEqual 0
|
131
139
|
expect(callback.callCount).toEqual 1
|
132
140
|
expect(@set._inject.callCount).toEqual 1
|
133
|
-
expect(@set._inject.args[0][0]).toEqual {require: true}
|
141
|
+
expect(@set._inject.args[0][0]).toEqual [{require: true}]
|
134
142
|
|
135
143
|
it "gets single from net", ->
|
136
144
|
callback = sinon.spy()
|
@@ -151,14 +159,14 @@ describe "Loada", ->
|
|
151
159
|
key: 'foo.js'
|
152
160
|
type: 'js'
|
153
161
|
source: 'foobar'
|
154
|
-
|
162
|
+
cache: true
|
155
163
|
require: true
|
156
164
|
|
157
165
|
expect(@server.requests.length).toEqual 1
|
158
166
|
expect(@set.storage['foo.js']).toEqual library
|
159
167
|
expect(callback.callCount).toEqual 1
|
160
168
|
expect(@set._inject.callCount).toEqual 1
|
161
|
-
expect(@set._inject.args[0][0]).toEqual library
|
169
|
+
expect(@set._inject.args[0][0]).toEqual [library]
|
162
170
|
|
163
171
|
it "orders properly", ->
|
164
172
|
@set.require(
|
@@ -166,23 +174,32 @@ describe "Loada", ->
|
|
166
174
|
{ url: 'bar.js' }
|
167
175
|
)
|
168
176
|
@set.require url: 'baz.js'
|
177
|
+
@set._inject.restore()
|
169
178
|
|
170
|
-
@set._loadGroup @set.requires.input[0], null,
|
171
|
-
@set._loadGroup @set.requires.input[1], null,
|
179
|
+
@set._loadGroup @set.requires.input[0], null, firstGroup = sinon.spy()
|
180
|
+
@set._loadGroup @set.requires.input[1], null, secondGroup = sinon.spy()
|
172
181
|
|
173
182
|
waits 0
|
174
183
|
|
175
184
|
runs ->
|
176
|
-
@server.requests[0].respond 200, {}, '
|
177
|
-
@server.requests[
|
178
|
-
@server.requests[2].respond 200, {}, 'foobar'
|
185
|
+
@server.requests[0].respond 200, {}, 'window.sandbox.TEST1 = 1'
|
186
|
+
@server.requests[2].respond 200, {}, 'window.sandbox.TEST2 = 1'
|
179
187
|
|
180
188
|
waits 0
|
181
189
|
|
182
190
|
runs ->
|
183
|
-
expect(
|
184
|
-
expect(
|
185
|
-
expect(
|
191
|
+
expect(firstGroup.callCount).toEqual 0
|
192
|
+
expect(secondGroup.callCount).toEqual 1
|
193
|
+
expect(window.sandbox.TEST1).toBeUndefined()
|
194
|
+
expect(window.sandbox.TEST2).toEqual 1
|
195
|
+
@server.requests[1].respond 200, {}, 'window.sandbox.TEST1 = 2'
|
196
|
+
|
197
|
+
waits 0
|
198
|
+
|
199
|
+
runs ->
|
200
|
+
expect(firstGroup.callCount).toEqual 1
|
201
|
+
expect(secondGroup.callCount).toEqual 1
|
202
|
+
expect(window.sandbox.TEST1).toEqual 2
|
186
203
|
|
187
204
|
describe "progress", ->
|
188
205
|
it "tracks with cache", ->
|
@@ -227,30 +244,23 @@ describe "Loada", ->
|
|
227
244
|
expect(progress.set.args[0]).toEqual ['foo.js', 100]
|
228
245
|
expect(progress.set.args[1]).toEqual ['bar.js', 100]
|
229
246
|
|
230
|
-
it "
|
231
|
-
window.TEST = 0
|
232
|
-
|
247
|
+
it "inlines", ->
|
233
248
|
set = Loada.set()
|
234
|
-
server = sinon.fakeServer.create()
|
235
249
|
callback = sinon.spy()
|
236
250
|
|
237
|
-
set._loadInline {url: 'spec/support/
|
251
|
+
set._loadInline {url: 'spec/support/test1_1.js', key: 'test.js', type: 'js'}, null, callback
|
238
252
|
|
239
253
|
waits 100
|
240
254
|
|
241
255
|
runs ->
|
242
256
|
expect(callback.callCount).toEqual 1
|
243
|
-
expect(window.TEST).toEqual 1
|
244
|
-
delete window.TEST
|
257
|
+
expect(window.sandbox.TEST).toEqual 1
|
245
258
|
|
246
259
|
it "injects", ->
|
247
|
-
window.TEST = 0
|
248
|
-
|
249
260
|
set = Loada.set()
|
250
|
-
set._inject source: 'window.TEST = 1', type: 'js'
|
261
|
+
set._inject source: 'window.sandbox.TEST = 1', type: 'js', require: true
|
251
262
|
|
252
|
-
expect(window.TEST).toEqual 1
|
253
|
-
delete window.TEST
|
263
|
+
expect(window.sandbox.TEST).toEqual 1
|
254
264
|
|
255
265
|
it "caches", ->
|
256
266
|
set = Loada.set()
|
@@ -281,13 +291,11 @@ describe "Loada", ->
|
|
281
291
|
set._inject.restore()
|
282
292
|
|
283
293
|
it "loads", ->
|
284
|
-
window.TEST = 0
|
285
|
-
|
286
294
|
progress = sinon.spy()
|
287
295
|
success = sinon.spy()
|
288
296
|
|
289
297
|
set = Loada.set()
|
290
|
-
set.require url: 'spec/support/
|
298
|
+
set.require url: 'spec/support/test1_1.js'
|
291
299
|
set.load
|
292
300
|
progress: progress
|
293
301
|
success: success
|
@@ -298,21 +306,73 @@ describe "Loada", ->
|
|
298
306
|
expect(progress.callCount).toEqual 1
|
299
307
|
expect(progress.args[0][0]).toEqual 100
|
300
308
|
expect(success.callCount).toEqual 1
|
301
|
-
expect(window.TEST).toEqual 1
|
302
|
-
|
303
|
-
delete window.TEST
|
309
|
+
expect(window.sandbox.TEST).toEqual 1
|
304
310
|
|
305
311
|
it "loads text", ->
|
306
312
|
set = Loada.set()
|
307
|
-
|
308
|
-
set.require url: 'foo', type: 'text'
|
309
|
-
|
313
|
+
set.require url: 'spec/support/text', key: 'foo', type: 'text'
|
310
314
|
set.load()
|
311
315
|
|
312
|
-
waits
|
316
|
+
waits 100
|
313
317
|
|
314
318
|
runs ->
|
315
|
-
expect(
|
316
|
-
|
319
|
+
expect(set.get 'foo').toEqual 'foobar'
|
320
|
+
|
321
|
+
describe "collisions", ->
|
322
|
+
|
323
|
+
it "loads in correct order with localStorage", ->
|
324
|
+
progress = sinon.spy()
|
325
|
+
success = sinon.spy()
|
326
|
+
|
327
|
+
set = Loada.set()
|
328
|
+
set.require {url: 'spec/support/test1_1.js'}, {url: 'spec/support/test1_2.js'}
|
329
|
+
set.load
|
330
|
+
progress: progress
|
331
|
+
success: success
|
332
|
+
|
333
|
+
waits 100
|
334
|
+
|
335
|
+
runs ->
|
336
|
+
expect(progress.callCount).toEqual 2
|
337
|
+
expect(progress.args[0][0]).toEqual 50
|
338
|
+
expect(progress.args[1][0]).toEqual 100
|
339
|
+
expect(success.callCount).toEqual 1
|
340
|
+
expect(window.sandbox.TEST).toEqual 2
|
341
|
+
|
342
|
+
it "loads in correct order with inlining", ->
|
343
|
+
progress = sinon.spy()
|
344
|
+
success = sinon.spy()
|
345
|
+
|
346
|
+
set = Loada.set('*', localStorage: false)
|
347
|
+
set.require {url: 'spec/support/test1_1.js'}, {url: 'spec/support/test1_2.js'}
|
348
|
+
set.load
|
349
|
+
progress: progress
|
350
|
+
success: success
|
351
|
+
|
352
|
+
waits 100
|
353
|
+
|
354
|
+
runs ->
|
355
|
+
expect(progress.callCount).toEqual 2
|
356
|
+
expect(progress.args[0][0]).toEqual 50
|
357
|
+
expect(progress.args[1][0]).toEqual 100
|
358
|
+
expect(success.callCount).toEqual 1
|
359
|
+
expect(window.sandbox.TEST).toEqual 2
|
360
|
+
|
361
|
+
it "loads in correct order when mixed", ->
|
362
|
+
progress = sinon.spy()
|
363
|
+
success = sinon.spy()
|
364
|
+
|
365
|
+
set = Loada.set('*')
|
366
|
+
set.require {url: 'spec/support/test1_1.js'}, {url: 'spec/support/test1_2.js', cache: false}
|
367
|
+
set.load
|
368
|
+
progress: progress
|
369
|
+
success: success
|
317
370
|
|
318
|
-
|
371
|
+
waits 100
|
372
|
+
|
373
|
+
runs ->
|
374
|
+
expect(progress.callCount).toEqual 2
|
375
|
+
expect(progress.args[0][0]).toEqual 50
|
376
|
+
expect(progress.args[1][0]).toEqual 100
|
377
|
+
expect(success.callCount).toEqual 1
|
378
|
+
expect(window.sandbox.TEST).toEqual 2
|
@@ -0,0 +1 @@
|
|
1
|
+
window.sandbox.TEST = 1
|
@@ -0,0 +1 @@
|
|
1
|
+
window.sandbox.TEST = 2
|
@@ -0,0 +1 @@
|
|
1
|
+
window.sandbox.TEST2 = 1
|
data/spec/support/text
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
foobar
|
data/src/loada.coffee
CHANGED
@@ -55,17 +55,48 @@ class @Loada
|
|
55
55
|
|
56
56
|
if typeof(@storage) == 'string'
|
57
57
|
@storage = JSON.parse(localStorage[@key])
|
58
|
+
else
|
59
|
+
@storage = {}
|
60
|
+
|
61
|
+
#
|
62
|
+
# Cleans up current state
|
63
|
+
#
|
64
|
+
# Removes entries that are:
|
65
|
+
# * expired by date (`expires` option)
|
66
|
+
# * got new revision (`revision` option)
|
67
|
+
# * not found in the current requires list
|
68
|
+
#
|
69
|
+
expire: ->
|
70
|
+
if @options.localStorage
|
71
|
+
now = new Date
|
72
|
+
|
73
|
+
byDate = (library) =>
|
74
|
+
library.expirationDate && new Date(library.expirationDate) <= now
|
75
|
+
|
76
|
+
byExistance = (library) =>
|
77
|
+
!@requires.set[key]
|
78
|
+
|
79
|
+
byRevision = (library) =>
|
80
|
+
@requires.set[key].revision != library.revision
|
81
|
+
|
82
|
+
for key, library of @storage
|
83
|
+
|
84
|
+
if byDate(library) || byExistance(library) || byRevision(library)
|
85
|
+
delete @storage[key]
|
58
86
|
|
59
87
|
#
|
60
88
|
# Saves current set state to localStorage
|
61
89
|
#
|
62
90
|
save: ->
|
63
|
-
|
91
|
+
if @options.localStorage
|
92
|
+
localStorage[@key] = JSON.stringify @storage
|
64
93
|
|
65
94
|
#
|
66
95
|
# Removes localStorage entry bound to current set
|
67
96
|
#
|
68
|
-
clear: ->
|
97
|
+
clear: ->
|
98
|
+
if @options.localStorage
|
99
|
+
delete localStorage[@key]
|
69
100
|
|
70
101
|
#
|
71
102
|
# Gets raw source of an asset
|
@@ -75,30 +106,7 @@ class @Loada
|
|
75
106
|
get: (key) ->
|
76
107
|
@storage[key]?.source
|
77
108
|
|
78
|
-
#
|
79
|
-
# Cleans up current state
|
80
|
-
#
|
81
|
-
# Removes entries that are:
|
82
|
-
# * expired by date (`expires` option)
|
83
|
-
# * got new revision (`revision` option)
|
84
|
-
# * not found in the current requires list
|
85
|
-
#
|
86
|
-
expire: ->
|
87
|
-
now = new Date
|
88
|
-
|
89
|
-
byDate = (library) =>
|
90
|
-
library.expirationDate && new Date(library.expirationDate) <= now
|
91
|
-
|
92
|
-
byExistance = (library) =>
|
93
|
-
!@requires.set[key]
|
94
|
-
|
95
|
-
byRevision = (library) =>
|
96
|
-
@requires.set[key].revision != library.revision
|
97
109
|
|
98
|
-
for key, library of @storage
|
99
|
-
|
100
|
-
if byDate(library) || byExistance(library) || byRevision(library)
|
101
|
-
delete @storage[key]
|
102
110
|
|
103
111
|
#
|
104
112
|
# Adds library that should be loaded
|
@@ -121,7 +129,7 @@ class @Loada
|
|
121
129
|
for library in libraries
|
122
130
|
library.key ||= library.url
|
123
131
|
library.type ||= library.url?.split('.').pop()
|
124
|
-
library.
|
132
|
+
library.cache = true unless library.cache?
|
125
133
|
library.require = true unless library.require?
|
126
134
|
|
127
135
|
if library.expires
|
@@ -147,7 +155,7 @@ class @Loada
|
|
147
155
|
#
|
148
156
|
load: (callbacks) ->
|
149
157
|
callbacks ||= {}
|
150
|
-
@expire()
|
158
|
+
@expire()
|
151
159
|
|
152
160
|
progress = new @Progress(@requires.length, callbacks.progress)
|
153
161
|
loaders = 0
|
@@ -195,24 +203,27 @@ class @Loada
|
|
195
203
|
# @private
|
196
204
|
#
|
197
205
|
_loadGroup: (group, progress, callback) =>
|
198
|
-
|
199
|
-
|
200
|
-
return callback() unless library
|
206
|
+
results = []
|
201
207
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
else
|
207
|
-
method = if @options.localStorage
|
208
|
-
"_loadAJAX"
|
208
|
+
for library in group
|
209
|
+
if @storage[library.key] && library.cache
|
210
|
+
progress?.set library.key, 100
|
211
|
+
results.push @storage[library.key]
|
209
212
|
else
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
@
|
213
|
+
method = if @options.localStorage || !library.require
|
214
|
+
"_loadAJAX"
|
215
|
+
else
|
216
|
+
"_loadInline"
|
217
|
+
|
218
|
+
@[method] library, progress, =>
|
219
|
+
results.push library
|
220
|
+
if results.length == group.length
|
221
|
+
@_inject results
|
222
|
+
callback()
|
223
|
+
|
224
|
+
if results.length == group.length
|
225
|
+
@_inject results
|
226
|
+
callback()
|
216
227
|
|
217
228
|
#
|
218
229
|
# Loads one asset by AJAX query and stores it into instance
|
@@ -221,6 +232,7 @@ class @Loada
|
|
221
232
|
#
|
222
233
|
_loadAJAX: (library, progress, callback) ->
|
223
234
|
xhr = @_ajax 'GET', library.url, (xhr) =>
|
235
|
+
@storage[library.key] = library if library.cache
|
224
236
|
library.source = xhr.responseText
|
225
237
|
clearInterval poller
|
226
238
|
progress?.set library.key, 100
|
@@ -265,16 +277,19 @@ class @Loada
|
|
265
277
|
#
|
266
278
|
# @private
|
267
279
|
#
|
268
|
-
_inject: (
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
280
|
+
_inject: (libraries) ->
|
281
|
+
libraries = [].concat(libraries)
|
282
|
+
|
283
|
+
for library in libraries when library.require && library.source
|
284
|
+
if library.type == 'js'
|
285
|
+
script = document.createElement "script"
|
286
|
+
script.defer = true
|
287
|
+
script.text = library.source
|
288
|
+
$head.appendChild script
|
289
|
+
else if library.type == 'css'
|
290
|
+
style = document.createElement "style"
|
291
|
+
style.innerHTML = library.source
|
292
|
+
$head.appendChild style
|
278
293
|
|
279
294
|
#
|
280
295
|
# Starring custom XHR wrapper!
|
@@ -287,8 +302,8 @@ class @Loada
|
|
287
302
|
else
|
288
303
|
xhr = new ActiveXObject 'Microsoft.XMLHTTP'
|
289
304
|
|
290
|
-
xhr.open method, url, 1
|
291
305
|
xhr.onreadystatechange = -> callback?(xhr) if xhr.readyState > 3
|
306
|
+
xhr.open method, url, 1
|
292
307
|
xhr.send()
|
293
308
|
|
294
309
|
xhr
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loada
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Boris Staal
|
@@ -29,7 +29,10 @@ files:
|
|
29
29
|
- package.json
|
30
30
|
- spec/helpers/spec_helper.coffee
|
31
31
|
- spec/loada_spec.coffee
|
32
|
-
- spec/support/
|
32
|
+
- spec/support/test1_1.js
|
33
|
+
- spec/support/test1_2.js
|
34
|
+
- spec/support/test2_1.js
|
35
|
+
- spec/support/text
|
33
36
|
- src/loada.coffee
|
34
37
|
homepage: http://github.com/joosy/loada
|
35
38
|
licenses: []
|
data/spec/support/test.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
window.TEST = 1
|