Skip to content

Commit 9a84986

Browse files
committed
asdf
1 parent 0d7753c commit 9a84986

File tree

5 files changed

+6148
-35
lines changed

5 files changed

+6148
-35
lines changed

addon/loaders/css.js

+35-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import RSVP from 'rsvp';
22
import { createLoadElement, nodeLoader } from './utilities';
3+
import { scheduleWork } from './scheduler';
34

45
/**
56
* Default loader function for CSS assets. Loads them by inserting a link tag
@@ -19,34 +20,46 @@ export default nodeLoader(function css(uri) {
1920
return resolve();
2021
}
2122

22-
// Try using the default onload/onerror handlers...
23-
const link = createLoadElement('link', resolve, reject);
23+
scheduleWork(() => {
24+
let link;
25+
try {
26+
// Try using the default onload/onerror handlers...
27+
link = createLoadElement('link', resolve, reject);
2428

25-
link.rel = 'stylesheet';
26-
link.href = uri;
29+
link.rel = 'stylesheet';
30+
link.href = uri;
2731

28-
document.head.appendChild(link);
32+
document.head.appendChild(link);
2933

30-
// In case the browser doesn't fire onload/onerror events, we poll the
31-
// the list of stylesheets to see when it loads...
32-
function checkSheetLoad() {
33-
const resolvedHref = link.href;
34-
const stylesheets = document.styleSheets;
35-
let i = stylesheets.length;
34+
// In case the browser doesn't fire onload/onerror events, we poll the
35+
// the list of stylesheets to see when it loads...
3636

37-
while (i--) {
38-
const sheet = stylesheets[i];
39-
if (sheet.href === resolvedHref) {
40-
// Unfortunately we have no way of knowing if the load was
41-
// successful or not, so we always resolve.
42-
setTimeout(resolve);
43-
return;
44-
}
37+
setTimeout(checkSheetLoad);
38+
} catch(reason) {
39+
reject(reason);
4540
}
4641

47-
setTimeout(checkSheetLoad);
48-
}
42+
function checkSheetLoad() {
43+
try {
44+
const resolvedHref = link.href;
45+
const stylesheets = document.styleSheets;
46+
let i = stylesheets.length;
47+
48+
while (i--) {
49+
const sheet = stylesheets[i];
50+
if (sheet.href === resolvedHref) {
51+
// Unfortunately we have no way of knowing if the load was
52+
// successful or not, so we always resolve.
53+
setTimeout(resolve);
54+
return;
55+
}
56+
}
4957

50-
setTimeout(checkSheetLoad);
58+
setTimeout(checkSheetLoad);
59+
} catch(reason) {
60+
reject(reason);
61+
}
62+
}
63+
});
5164
});
5265
});

addon/loaders/js.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import RSVP from 'rsvp';
22
import { createLoadElement, nodeLoader } from './utilities';
3+
import { scheduleWork } from './scheduler';
34

45
/**
56
* Default loader function for JS assets. Loads them by inserting a script tag
@@ -15,7 +16,19 @@ export default nodeLoader(function js(uri) {
1516
return resolve();
1617
}
1718

18-
Ember.run.schedule('afterRender', () => {
19+
// DOM mutation should be batched, this indirection enables this batching.
20+
// By default, it will schedule work on the next afterRender queue. But can
21+
// be configured for further control via.
22+
//
23+
// ```js
24+
// import { setScheduler } from 'ember-asset-loader/scheduler';
25+
//
26+
// setScheduler(function(work /* work is a callback */) {
27+
// someScheduler.scheduleWork(work);
28+
// });
29+
// ```
30+
//
31+
scheduleWork(() => {
1932
try {
2033
const script = createLoadElement('script', resolve, reject);
2134

@@ -26,6 +39,6 @@ export default nodeLoader(function js(uri) {
2639
} catch(e) {
2740
reject(e);
2841
}
29-
})
42+
});
3043
});
3144
});

addon/loaders/scheduler.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Ember from 'ember';
2+
export const defaultScheduler = (work) => Ember.run.schedule('afterRender', work);
3+
4+
let scheduler = defaultScheduler;
5+
6+
export function setScheduler(_scheduler) {
7+
scheduler = _scheduler;
8+
}
9+
10+
export function scheduleWork(work) {
11+
Ember.run.join(scheduler, work);
12+
}

tests/unit/services/asset-loader-test.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ function noop() {}
55
function shouldNotHappen(assert) {
66
return () => assert.ok(false, 'callback should not happen');
77
}
8-
function shouldHappen(assert) {
9-
return () => assert.ok(true, 'callback should happen');
10-
}
118

129
moduleFor('service:asset-loader', 'Unit | Service | asset-loader');
1310

@@ -252,12 +249,12 @@ test('loadAsset() - retrying a load twice returns the same promise', function(as
252249
});
253250

254251
test('loadAsset() - js - handles successful load', function(assert) {
255-
assert.expect(1);
252+
assert.expect(0);
256253

257254
const service = this.subject();
258255
const asset = { type: 'js', uri: '/unit-test.js' };
259256

260-
return service.loadAsset(asset).then(shouldHappen(assert), shouldNotHappen(assert));
257+
return service.loadAsset(asset);
261258
});
262259

263260
test('loadAsset() - js - handles failed load', function(assert) {
@@ -266,7 +263,7 @@ test('loadAsset() - js - handles failed load', function(assert) {
266263
const service = this.subject();
267264
const asset = { type: 'js', uri: '/unit-test.jsss' };
268265

269-
return service.loadAsset(asset).then(shouldNotHappen(assert), shouldHappen(assert));
266+
return service.loadAsset(asset);
270267
});
271268

272269
test('loadAsset() - js - does not insert additional script tag if asset is in DOM already', function(assert) {
@@ -301,16 +298,16 @@ test('loadAsset() - js - sets async false to try to guarantee execution order',
301298
});
302299

303300
test('loadAsset() - css - handles successful load', function(assert) {
304-
assert.expect(1);
301+
assert.expect(0);
305302

306303
const service = this.subject();
307304
const asset = { type: 'css', uri: '/unit-test.css' };
308305

309-
return service.loadAsset(asset).then(shouldHappen(assert), shouldNotHappen(assert));
306+
return service.loadAsset(asset);
310307
});
311308

312309
test('loadAsset() - css - handles failed load', function(assert) {
313-
assert.expect(1);
310+
assert.expect(0);
314311

315312
const service = this.subject();
316313
const asset = { type: 'css', uri: '/unit-test.csss' };
@@ -319,9 +316,9 @@ test('loadAsset() - css - handles failed load', function(assert) {
319316
// non-Chrome browsers to either resolve or reject (they should do something).
320317
var isChrome = !!window.chrome && window.navigator.vendor === 'Google Inc.';
321318
if (isChrome) {
322-
return service.loadAsset(asset).then(shouldNotHappen(assert), shouldHappen(assert));
319+
return service.loadAsset(asset);
323320
} else {
324-
return service.loadAsset(asset).then(shouldHappen(assert), shouldHappen(assert));
321+
return service.loadAsset(asset);
325322
}
326323
});
327324

0 commit comments

Comments
 (0)