Skip to content
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

Have loaders return resolved Promises in non-browser environments #22

Merged
merged 1 commit into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions addon/loaders/css.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import RSVP from 'rsvp';
import { createLoadElement } from './utilities';
import { createLoadElement, nodeLoader } from './utilities';

/**
* Default loader function for CSS assets. Loads them by inserting a link tag
Expand All @@ -13,7 +13,7 @@ import { createLoadElement } from './utilities';
* @param {String} uri
* @return {Promise}
*/
export default function css(uri) {
export default nodeLoader(function css(uri) {
return new RSVP.Promise((resolve, reject) => {
// Try using the default onload/onerror handlers...
const link = createLoadElement('link', resolve, reject);
Expand Down Expand Up @@ -45,4 +45,4 @@ export default function css(uri) {

setTimeout(checkSheetLoad);
});
}
});
6 changes: 3 additions & 3 deletions addon/loaders/js.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import RSVP from 'rsvp';
import { createLoadElement } from './utilities';
import { createLoadElement, nodeLoader } from './utilities';

/**
* Default loader function for JS assets. Loads them by inserting a script tag
Expand All @@ -9,12 +9,12 @@ import { createLoadElement } from './utilities';
* @param {String} uri
* @return {Promise}
*/
export default function js(uri) {
export default nodeLoader(function js(uri) {
return new RSVP.Promise((resolve, reject) => {
const script = createLoadElement('script', resolve, reject);

script.src = uri;

document.head.appendChild(script);
});
}
});
24 changes: 23 additions & 1 deletion addon/loaders/utilities.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import RSVP from 'rsvp';

const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';

/**
* Creates a DOM element with the specified onload and onerror handlers.
*
* @method createLoadElement
* @param {String} tag
* @param {Function} load
* @paeam {Function} error
* @param {Function} error
* @return {HTMLElement} el
*/
export function createLoadElement(tag, load, error) {
Expand All @@ -15,3 +19,21 @@ export function createLoadElement(tag, load, error) {

return el;
}

/**
* Creates a loader function that is compatible with Node environments (such as
* FastBoot). If we're in the browser, we'll use the passed in loader function,
* but when in Node, we'll just return a Promise that resolves (we assume assets
* will be pre-loaded).
*
* @method nodeLoader
* @param {Function} loader
* @return {Function}
*/
export function nodeLoader(loader) {
if (isBrowser) {
return loader;
} else {
return () => RSVP.resolve();
}
}