@okiba/resource-loader

0.1.25 • Public • Published

Okiba / ResourceLoader

Manages loading of resources trough fetch to boost caching. Transparently relies on a WebWorker if possible to load on a separate thread.

__

import ResourceLoader from '@okiba/resource-loader'

const resLoader = new ResourceLoader()

// if `window.Worker` is available
// `fetch` happens on a separate thread!

urls.forEach(
  url => resLoader.load(url)
    .then(console.log('Loaded! 📦'))
    .catch(console.log('Fail! ☹️'))
)

Installation

npm i --save @okiba/resource-loader

Or import it directly in the browser

<script type="module" src="https://unpkg.com/@okiba/resource-loader/index.js"></script>

Usage

import ResourceLoader from '@okiba/resource-loader'

Untranspiled code 🛑

Okiba Core packages are not transpiled, so don't forget to transpile them with your favourite bundler. For example, using Babel with Webpack, you should prevent imports from okiba to be excluded from transpilation, like follows:

{
  test: /\.js$/,
  exclude: /node_modules\/(?!(@okiba)\/).*/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['@babel/preset-env']
    }
  }
}

Todo:

  • Handle abortion
  • Fetch is not on IE11
  • Clone response and pass it as promise reslult

load(url)

Initiates loading of a resource at a given URL

Arguments

+ url: String

Resource URL

Returns

Promise A promise which will be resolved if the resource is loaded and rejected if not.

Package Sidebar

Install

npm i @okiba/resource-loader

Weekly Downloads

0

Version

0.1.25

License

MIT

Unpacked Size

8.54 kB

Total Files

5

Last publish

Collaborators

  • fiad
  • ghzmdr
  • lavolpecheprogramma