Sha256: faf1d2d9ad96fa06b6c633c6b5ebcb0e3d2bf2ae78b70e4c84919eeee3a570e8

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

import { isArray, pick, isEmpty, keys } from 'lodash';
import { loadCSS, loadJS } from './loader';
import { logger } from './util';

class AssetLoader {
    constructor(urls, cb) {
        let finished = 0;
        const completed = {};
        const onComplete = (url, success, error = false) => {
            finished += 1;
            completed[url] = { success: true === success, error };
            if (finished === urls.length) { cb(completed); }
        };

        Array.from(urls).forEach((baseUrl) => {
            const url = `http://localhost:8889${baseUrl}?${parseInt(Math.random() * 100000, 10)}`;
            if (/.css($|\?)/.test(url)) {
                loadCSS(url, onComplete);
            } else {
                loadJS(url, onComplete);
            }
        });
    }
}

export default function RequestAssets(...urlArgs) {
    const urls = ((1 === urlArgs.length) && isArray(urlArgs[0])) ? urlArgs[0] : urlArgs;
    return new Promise((resolve, reject) =>
        new AssetLoader(urls, (completed) => {
            const failures = pick(completed, status => !status.success);
            if (isEmpty(failures)) {
                return resolve(completed);
            }
            logger.warn(`${keys(failures).join(',')} failed to load`);
            return reject(failures);
        }),
    );
}

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hippo-fw-0.9.7 client/hippo/lib/request-assets.js
hippo-fw-0.9.6 client/hippo/lib/request-assets.js
hippo-fw-0.9.5 client/hippo/lib/request-assets.js
hippo-fw-0.9.4 client/hippo/lib/request-assets.js
hippo-fw-0.9.3 client/hippo/lib/request-assets.js
hippo-fw-0.9.2 client/hippo/lib/request-assets.js
hippo-fw-0.9.1 client/hippo/lib/request-assets.js