AsyncPreloader: assets preloader using ES2017 async/await and fetch.

It exports an instance of itself as default so you can:

import Preloader from "async-preloader";

await Preloader.loadItems([]);

to use directly as a singleton or

import { AsyncPreloader as Preloader } from "async-preloader";

const preloader = new Preloader();
await preloader.loadItems([]);

if you need more than one instance.

Constructors

Properties

defaultBodyMethod: BodyMethod = "blob"

Default body method to be called on the Response from fetch if no body option is specified on the LoadItem

defaultLoader: LoaderKey = LoaderKey.Text

Default loader to use if no loader key is specified in the LoadItem or if the extension doesn't match any of the AsyncPreloader.loaders extensions

items: Map<string, LoadedValue> = ...

Object that contains the loaded items

domParser: DOMParser = ...

DOMParser instance for the XML loader

loaders: Map<LoaderKey, LoaderValue> = ...

Loader types and the extensions they handle

Allows the omission of the loader key in a LoadItem.loader for some generic extensions

Methods

  • Load an item and parse the Response as arrayBuffer

    Parameters

    Returns Promise<ArrayBuffer>

    Fulfilled value of parsed Response

  • Load an item in one of the following cases:

    • item's "loader" option set as "Audio"
    • item's "src" option extensions matching the loaders Map
    • direct call of the method

    Parameters

    Returns Promise<LoadedValue>

    Fulfilled value of parsed Response according to the "body" option. Defaults to an HTMLAudioElement with a blob as srcObject or src.

  • Load an item and parse the Response as blob

    Parameters

    Returns Promise<Blob>

    Fulfilled value of parsed Response

  • Load a font via FontFace or check a font is loaded via FontFaceObserver instance

    Parameters

    • item: LoadItem

      Item to load (id correspond to the font family name).

    Returns Promise<string | FontFace>

    Fulfilled value with FontFace instance or initial id if no src provided.

  • Load an item and parse the Response as formData

    Parameters

    Returns Promise<FormData>

    Fulfilled value of parsed Response

  • Load an item in one of the following cases:

    • item's "loader" option set as "Image"
    • item's "src" option extensions matching the loaders Map
    • direct call of the method

    Parameters

    Returns Promise<LoadedValue>

    Fulfilled value with a decoded HTMLImageElement instance of or a parsed Response according to the "body" option. Defaults to a decoded HTMLImageElement.

  • Load the specified manifest (array of items)

    Parameters

    • items: string[] | LoadItem[]

      Items to load

    Returns Promise<LoadedValue[]>

    Resolve when all items are loaded, reject for any error

  • Load an item and parse the Response as json

    Parameters

    Returns Promise<JSON>

    Fulfilled value of parsed Response

  • Load a manifest of items

    Parameters

    • src: string

      Manifest src url

    • key: string = "items"

      Manifest key in the JSON object containing the array of LoadItem.

    Returns Promise<LoadedValue[]>

  • Load an item and parse the Response as text

    Parameters

    Returns Promise<string>

    Fulfilled value of parsed Response

  • Load an item in one of the following cases:

    • item's "loader" option set as "Video"
    • item's "src" option extensions matching the loaders Map
    • direct call of the method

    Parameters

    Returns Promise<LoadedValue>

    Fulfilled value of parsed Response according to the "body" option. Defaults to an HTMLVideoElement with a blob as srcObject or src.

  • Load an item in one of the following cases:

    • item's "loader" option set as "Xml"
    • item's "src" option extensions matching the loaders Map
    • direct call of the method

    Parameters

    • item: LoadItem

      Item to load (need a mimeType specified or default to "application/xml")

    Returns Promise<LoadedXMLValue>

    Result of Response parsed as a document.

  • Fetch wrapper for LoadItem

    Parameters

    Returns Promise<Response>

    Fetch response

  • Get file base name

    Parameters

    • path: string

    Returns string

  • Get file extension

    Parameters

    • path: string

    Returns string

  • Get file name

    Parameters

    • path: string

    Returns string

  • Retrieve loader key from extension (when the loader option isn't specified in the LoadItem)

    Parameters

    • extension: string

    Returns LoaderKey

  • Retrieve mime type from extension

    Parameters

    Returns DOMParserSupportedType

  • Get an object property by its path in the form 'a[0].b.c' or ['a', '0', 'b', 'c']. Similar to lodash.get.

    Parameters

    • object: unknown

      Object with nested properties

    • path: string | string[]

      Path to the desired property

    Returns any

    The returned object property