Options
All
  • Public
  • Public/Protected
  • All
Menu

Class AsyncPreloader

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.

Hierarchy

  • AsyncPreloader

Index

Properties

defaultBodyMethod

defaultBodyMethod: BodyMethod = "blob"

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

defaultLoader

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

items: Map<string, LoadedValue> = new Map()

Object that contains the loaded items

Static Private domParser

domParser: DOMParser = new DOMParser()

DOMParser instance for the XML loader

Static Private loaders

loaders: Map<LoaderKey, LoaderValue> = new Map().set(LoaderKey.Text, { extensions: ["txt"] }).set(LoaderKey.Json, { extensions: ["json"] }).set(LoaderKey.Image, { extensions: ["jpeg", "jpg", "gif", "png", "webp"] }).set(LoaderKey.Video, { extensions: ["webm", "ogg", "mp4"] }).set(LoaderKey.Audio, { extensions: ["webm", "ogg", "mp3", "wav", "flac"] }).set(LoaderKey.Xml, {extensions: ["xml", "svg", "html"],mimeType: {xml: "text/xml",svg: "image/svg+xml",html: "text/html"},defaultMimeType: "text/xml"}).set(LoaderKey.Font, {extensions: ["woff2", "woff", "ttf", "otf", "eot"]})

Loader types and the extensions they handle

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

Methods

loadArrayBuffer

  • Load an item and parse the Response as arrayBuffer

    Parameters

    Returns Promise<LoadedValue>

    Fulfilled value of parsed Response

loadAudio

  • 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.

loadBlob

  • Load an item and parse the Response as blob

    Parameters

    Returns Promise<LoadedValue>

    Fulfilled value of parsed Response

loadFont

  • loadFont(item: LoadItem): Promise<string>
  • Load a font via a FontFaceObserver instance

    Parameters

    • item: LoadItem

      Item to load (id correspond to the fontName).

    Returns Promise<string>

    Fulfilled value with fontName initial id.

loadFormData

  • Load an item and parse the Response as formData

    Parameters

    Returns Promise<LoadedValue>

    Fulfilled value of parsed Response

loadImage

  • 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 of parsed Response according to the "body" option. Defaults to an HTMLImageElement with a blob as srcObject or src.

loadItem

  • Load a single item

    Parameters

    Returns Promise<LoadedValue>

    Resolve when item is loaded, reject for any error

loadItems

  • Load the specified manifest (array of items)

    Parameters

    Returns Promise<LoadedValue[]>

    Resolve when all items are loaded, reject for any error

loadJson

  • Load an item and parse the Response as json

    Parameters

    Returns Promise<LoadedValue>

    Fulfilled value of parsed Response

loadManifest

  • loadManifest(src: string, key?: string): Promise<LoadedValue[]>
  • Load a manifest of items

    Parameters

    • src: string

      Manifest src url

    • Default value key: string = "items"

    Returns Promise<LoadedValue[]>

loadText

  • Load an item and parse the Response as text

    Parameters

    Returns Promise<LoadedValue>

    Fulfilled value of parsed Response

loadVideo

  • 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 src.

loadXml

  • 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.

Static Private fetchItem

  • fetchItem(item: LoadItem): Promise<Response>
  • Fetch wrapper for LoadItem

    Parameters

    Returns Promise<Response>

    Fetch response

Static Private getFileExtension

  • getFileExtension(path: RequestInfo | string): string
  • Get file extension from path

    Parameters

    • path: RequestInfo | string

    Returns string

Static Private getLoaderKey

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

    Parameters

    • extension: string

    Returns LoaderKey

Static Private getMimeType

  • getMimeType(loaderKey: LoaderKey, extension: string): SupportedType
  • Retrieve mime type from extension

    Parameters

    Returns SupportedType

Static Private getProp

  • getProp(object: any, path: string | string[]): any
  • 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: any

      Object with nested properties

    • path: string | string[]

      Path to the desired property

    Returns any

    The returned object property

Generated using TypeDoc