# preloader

> A library for loading common web assets

Latest version **4.0.3** (published 2019-03-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install preloader
pnpm add preloader
yarn add preloader
bun add preloader
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.3 |
| Published | 2019-03-21 |
| First published | 2015-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 37.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 69 |
| Author | Nick Poisson |
| Maintainers | alemesa, chelorope, colin-j3, craighillwood, djkramnik, donghyukjang, intricus, iranreyes, jam3dev, jmckinnell, n0wak, namniak, neoli, nickfish, njam3, tostegroo |
| Keywords | preload, load, json, image, binary, loader, preloader |

## Links

- npm: https://www.npmjs.com/package/preloader
- Repository: https://github.com/Jam3/preloader
- Issues: https://github.com/Jam3/preloader/issues
- npm.io page: https://npm.io/package/preloader

## Dependencies (2)

- [js-oop](https://npm.io/package/js-oop.md) ^1.0.0
- [base64mime](https://npm.io/package/base64mime.md) 0.0.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 4.0.3 (latest) — 2019-03-21
- 4.0.2 — 2017-06-22
- 4.0.1 — 2017-04-17
- 4.0.0 — 2017-04-17
- 3.0.0 — 2017-02-23
- 2.2.1 — 2017-02-17
- 2.2.0 — 2017-01-19
- 2.1.0 — 2016-11-15
- 2.0.1 — 2016-07-06
- 2.0.0 — 2016-07-06
- 1.1.0 — 2016-05-12
- 1.0.4 — 2016-05-11
- 1.0.3 — 2016-05-11
- 1.0.2 — 2015-09-14
- 1.0.1 — 2015-03-05
- … 1 more at https://npm.io/package/preloader/versions

## README

# Preloader

[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)

A library for loading common web assets

## Usage

[![NPM](https://nodei.co/npm/preloader.png)](https://www.npmjs.com/package/preloader)

# preloader

The preloader is capable of loading almost all types of files, if it does not understand a file type, it will attempt to load it as a basic xhr request. It extends the [nodejs event emitter](https://nodejs.org/api/events.html) and uses the following events.

```progress```: `Event` Sends updates on loading progress to other part of application (loading ui)
```complete```: `Event` Notifies loading completion to other part of application

Here is a common usage of the preloader.

```js
var preloader = require('preloader');
var loader = preloader({
  xhrImages: false
});
loader.on('progress',function(progress) {
  console.log(progress);
});
loader.on('complete',function() {
  var data = loader.get('site_data.json');
  console.log('all content loaded!');
});
loader.add('video1.mp4');
loader.add('test_image.jpg',{
  onComplete: function(content) {
    document.body.appendChild(loader.get('test_image.jpg'));
  }
});
loader.add('site_data.json');
loader.load();
```

### new Preloader(options) / preloader(options)

This creates a new instance of the preloader on which on you use the following api. It is not a singleton and must be instantiated to use. The options object contains the following properties.

```xhrImages``` Loads images via XHR and converts to a Blob instead of the image tag, default: false
```onComplete``` A function to attach to the complete event
```onProgress``` A function to attach to the progress event
```throttle``` A integer specifying maximum amount of connections at a time, 0 = infinite

### add(url, options)

Generic asset loader function - determines loader to be used based on file-extension

```url```: `String` URL of asset
```options```: `Object` Custom options to override the global options created at instantiation, can also pass in `onComplete` and `onProgress` to listen to the events on this particular item.

### addImage(url ,options)

Load image - uses the LoaderImage loader

```url```: `String` URL of asset
```options```: `Object` Custom options to override the global options created at instantiation, can also pass in `onComplete` and `onProgress` to listen to the events on this particular item.

### addJSON(url, options)

Load JSON - uses the LoaderJSON loader

```url```: `String` URL of asset
```options```: `Object` Custom options to override the global options created at instantiation, can also pass in `onComplete` and `onProgress` to listen to the events on this particular item.

### addText(url, options)

Load text - uses the LoaderText loader

```url```: `String` URL of asset
```options```: `Object` Custom options to override the global options created at instantiation, can also pass in `onComplete` and `onProgress` to listen to the events on this particular item.

### addVideo(url, options)

Load video - uses the LoaderVideo loader

```url```: `String` URL of asset
```options```: `Object` Custom options to override the global options created at instantiation, can also pass in `onComplete` and `onProgress` to listen to the events on this particular item.

### addAudio(url, options)

Load audio - uses the LoaderAudio loader

```url```: `String` URL of asset
```options```: `Object` Custom options to override the global options created at instantiation, can also pass in `onComplete` and `onProgress` to listen to the events on this particular item.

### addImage(url, options)

Load image - uses the LoaderImage loader

```url```: `String` URL of asset
```options```: `Object` Custom options to override the global options created at instantiation, can also pass in `onComplete` and `onProgress` to listen to the events on this particular item.

### addFromLoaderType(url, loaderType, options)

Load asset using custom loader

```url```: `String` URL of asset
```loaderType```: `function` Custom loader function
```options```: `Object` Custom options to override the global options created at instantiation, can also pass in `onComplete` and `onProgress` to listen to the events on this particular item.

### setPercentage(url, percentageOfLoad)

Sets percentage of total load for a given asset

```url```: `String` URL of asset
```percentageOfLoad```: `Number`  representing percentage of total load

### load()

Begins loading process

### stopLoad()

Stops loading process

### get(url)

Retrieves loaded asset from loader

```url```: `String` URL of asset
```Returns```: asset instance

### reset()

Resets loading so you can reuse the preloader. does not remove cached loads so `get()` continues to function for all assets.

## License

MIT, see [LICENSE.md](https://github.com/Jam3/preloader/blob/master/LICENSE.md) for details.

---
_Source: https://npm.io/package/preloader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
