0.1.0 • Published 3 years ago

@wault/web-loader v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Web Loader

Build npm version

WARNING: This project is under development. Current use is not recommended!

Modular asset loader for web applications.

Table of Contents

Installation

With NPM

$ npm install --save @wault/web-loader

API

import {
    load,
    Chain,
    XHRLoader,
    ImageElementLoader,
    WebAudioParser,
    TypeDetector
} from '@wault/web-loader'

load(Chain([
    TypeDetector,
    XHRLoader(),
    ImageElementLoader,
    WebAudioParser(audioContext)
]), [
    'assets/textures/background.jpg',
    'assets/sfx/theme.mp3'
], progress => console.log(`Loading ${100 * progress}%`))
.then(assets => {
    const image = assets[0].data
    const music = assets[1].data
})
.catch(error => console.error(error))

Modules

Loader is composed out of different modules, each providing it's own functionality.

Operators

ModuleExampleDescription
ChainChain([ ...modules ])For each block.
FilterFilter(module, material => true/false)If block.
CatchCatch(module, (error, material) => material)Try catch block.
ThrottleThrottle(16)(module)Limit the number of concurrent requests.
StoreStore(internal?)Cache. Internal resolve assumes that no additional requests should be made.
FallbackFallback()In case of failure will resort to fallbacks if supplied.
OrderOrder(module)Execute in strict order.

Loaders

ModuleExampleDescription
XHRLoaderXHRLoader()Load files using http request.
ImageElementLoaderImageElementLoaderLoad image into <img>.
AudioElementLoaderAudioElementLoaderLoad audio into <audio>.
ScriptElementLoaderScriptElementLoader()Load and execute javascript asynchronously into <script>.
StylesheetLoaderStylesheetLoader()Load CSS asynchronously into <link>.

Parsers

ModuleExampleDescription
TypeDetectorTypeDetector()Detect file extension.
JSONParserJSONParserParse JSON data.
XMLParserXMLParserParse XML data.
ScriptExecutorScriptExecutor()Execute script.
StylesheetParserStylesheetParser()Apply CSS.
SVGParserSVGParserConvert SVG to image.
WebAudioDecoderWebAudioDecoder(audioContext)Decode web audio buffer.
Base64UnpackerBase64Unpacker(material => material.data)Unpack base64 encoded assets from json bundle.

Custom

Any function or async function can be used as a loading module.

async function customLoader(material){
    const response = await fetch(material.path)
    const data = await response.json()
    return { ...material, data }
}