-
Notifications
You must be signed in to change notification settings - Fork 27
/
utilities.js
40 lines (35 loc) · 959 Bytes
/
utilities.js
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
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
* @param {Function} error
* @return {HTMLElement} el
*/
export function createLoadElement(tag, load, error) {
const el = document.createElement(tag);
el.onload = load;
el.onerror = 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();
}
}