-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the script tag after-render #48
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import RSVP from 'rsvp'; | ||
import { createLoadElement, nodeLoader } from './utilities'; | ||
import { scheduleWork } from './scheduler'; | ||
|
||
/** | ||
* Default loader function for JS assets. Loads them by inserting a script tag | ||
|
@@ -15,11 +16,29 @@ export default nodeLoader(function js(uri) { | |
return resolve(); | ||
} | ||
|
||
const script = createLoadElement('script', resolve, reject); | ||
// DOM mutation should be batched, this indirection enables this batching. | ||
// By default, it will schedule work on the next afterRender queue. But can | ||
// be configured for further control via. | ||
// | ||
// ```js | ||
// import { setScheduler } from 'ember-asset-loader/scheduler'; | ||
// | ||
// setScheduler(function(work /* work is a callback */) { | ||
// someScheduler.scheduleWork(work); | ||
// }); | ||
// ``` | ||
// | ||
scheduleWork(() => { | ||
try { | ||
const script = createLoadElement('script', resolve, reject); | ||
|
||
script.src = uri; | ||
script.async = false; | ||
script.src = uri; | ||
script.async = false; | ||
|
||
document.head.appendChild(script); | ||
document.head.appendChild(script); | ||
} catch(e) { | ||
reject(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What error would we expect to happen here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. none, this is to handle the exceptional cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also maintains existing behavior. Currently if any of those lines error, the promise constructor will catch them. As this PR moves them to be async, we must manage that ourselves. |
||
} | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Ember from 'ember'; | ||
export const defaultScheduler = (work) => Ember.run.schedule('afterRender', work); | ||
|
||
let scheduler = defaultScheduler; | ||
|
||
export function setScheduler(_scheduler) { | ||
scheduler = _scheduler; | ||
} | ||
|
||
export function scheduleWork(work) { | ||
Ember.run.join(scheduler, work); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why move this declaration outside the block where it is initialized?