Class: Loader

Tiny.loaders. Loader

继承自 Resource Loader 的 Loader 类。

const loader = new Tiny.loaders.Loader(); // 你也可以通过 `Loader` 类自己实例化一个
// 建议使用 Tiny.js 提供静态方法来获取 Loader 实例化对象
// const loader = Tiny.Loader;

// 声明资源文件
var resources = [
  'https://zos.alipayobjects.com/rmsportal/nJBojwdMJfUqpCWvwyoA.png',
  'https://zos.alipayobjects.com/rmsportal/kkroUtIawGrWrqOLRmjq.jpg',
  'https://zos.alipayobjects.com/rmsportal/jkgwjYSFHRkorsKaZbng.jpeg',
  'https://zos.alipayobjects.com/rmsportal/HAacythTETlcsPxEePbk.webp',
  'https://os.alipayobjects.com/rmsportal/atrIuwPurrBiNEyWNdQA.ogg'
];
//执行加载
loader.run({
  resources: resources,
  // 加载进度
  onProgress: function(per){
      console.log('percent:', per + '%');
  },
  // 单个资源加载完成后的回调
  onComplete: function(resourceLoader, resource){
      console.log(resource.url);
  },
  // 单个资源加载失败后的回调
  onError: function(errorMsg, resourceLoader, resource){
      console.log(errorMsg);
  },
  // 所有资源加载完成后的回调
  onAllComplete: function (resourceLoader, object) {
      console.log('all complete');
  }
});

new Tiny.loaders.Loader(baseUrl, concurrency)

Name Type Default Description
baseUrl string '' optional

The base url for all resources loaded by this loader.

concurrency number 10 optional

The number of resources to load concurrently.

Extends

Members

_afterMiddlewareArray.<function()>

The middleware to run after loading each resource.

_beforeMiddlewareArray.<function()>

The middleware to run before loading each resource.

_resourcesParsingArray.<Tiny.loaders.Resource>

The tracks the resources we are currently completing parsing for.

baseUrlstring

The base url for all resources loaded by this loader.

concurrencynumber

The number of resources to load concurrently.

Default Value:
  • 10

defaultQueryStringstring

A querystring to append to every URL added to the loader.

This should be a valid query string without the question-mark (?). The loader will also not escape values for you. Make sure to escape your parameters with encodeURIComponent before assigning this property.

Example
const loader = new Tiny.ResourceLoader();

loader.defaultQueryString = 'user=me&password=secret';

// This will request 'image.png?user=me&password=secret'
loader.add('image.png').load();

loader.reset();

// This will request 'image.png?v=1&user=me&password=secret'
loader.add('iamge.png?v=1').load();

loadingboolean

Loading state of the loader, true if it is currently loading resources.

onCompleteSignal

Dispatched when the queued resources all load.

The callback looks like Tiny.ResourceLoader.OnCompleteSignal.

onErrorSignal

Dispatched once per errored resource.

The callback looks like Tiny.ResourceLoader.OnErrorSignal.

onLoadSignal

Dispatched once per loaded resource.

The callback looks like Tiny.ResourceLoader.OnLoadSignal.

onProgressSignal

Dispatched once per loaded or errored resource.

The callback looks like Tiny.ResourceLoader.OnProgressSignal.

onStartSignal

Dispatched when the loader begins to process the queue.

The callback looks like Tiny.ResourceLoader.OnStartSignal.

progressnumber

The progress percent of the loader going through the queue.

resourcesobject.<string, Tiny.loaders.Resource>

All the resources for this loader keyed by name.

Methods

staticTiny.loaders.Loader.addTinyMiddleware(fn)

Adds a default middleware to the Tiny loader.

Name Type Description
fn function

The middleware to add.

staticTiny.loaders.Loader.addTinyPreMiddleware(fn)

Adds a default middleware pre the Tiny loader.

Name Type Description
fn function

The middleware to add pre.

Version:
  • 1.1.7

add(name, url, options, cb){Tiny.loaders.Loader}

Adds a resource (or multiple resources) to the loader queue.

This function can take a wide variety of different parameters. The only thing that is always required the url to load. All the following will work:

loader
    // normal param syntax
    .add('key', 'http://...', function () {})
    .add('http://...', function () {})
    .add('http://...')

    // object syntax
    .add({
        name: 'key1'
        url: 'data:image/png;base64,iV...Jggg=='
    }, function () {})
    .add({
        url: 'http://...'
    }, function () {})
    .add({
        name: 'key3',
        url: 'http://...'
        onComplete: function () {}
    })
    .add({
        url: 'https://...',
        onComplete: function () {},
        crossOrigin: true
    })

    // you can pass JSONObject
    .add({
        url: 'key4.json', // the name can be anything, but the extname must be .json
        metadata: {
          JSONObject: {"meta":{"image":"https://...","size":{"w":1730,"h":1158},"scale":"1"},"frames":{}},
          fallback: function () {}
        },
        xhrType: Tiny.loaders.Resource.XHR_RESPONSE_TYPE.JSONOBJECT,
    })

    // you can also pass an array of objects or urls or both
    .add([
        { name: 'key5', url: 'http://...', onComplete: function () {} },
        { url: 'http://...', onComplete: function () {} },
        'http://...',
        'data:image/png;base64,iV...Jggg=='
    ])

    // and you can use both params and options
    .add('key6', 'http://...', { crossOrigin: true }, function () {})
    .add('http://...', { crossOrigin: true }, function () {});
Name Type Description
name string optional

The name of the resource to load, if not passed the url is used.

url string optional

The url for this resource, relative to the baseUrl of this loader.

options object optional

The options for the load.

Name Type Default Description
crossOrigin boolean optional

Is this request cross-origin? Default is to determine automatically.

loadType Tiny.loaders.Resource.LOAD_TYPE XHR optional

How should this resource be loaded?「XHR, IMAGE, AUDIO, VIDEO

xhrType Tiny.loaders.Resource.XHR_RESPONSE_TYPE DEFAULT optional

How should the data being loaded be interpreted when using XH?「BLOB, BUFFER, DEFAULT, DOCUMENT, JSON, JSONOBJECT, TEXT

metadata object optional

Extra configuration for middleware and the Resource object.

Name Type Default Description
loadElement HTMLImageElement | HTMLAudioElement | HTMLVideoElement null optional

The element to use for loading, instead of creating one.

skipSource boolean false optional

Skips adding source(s) to the load element. This is useful if you want to pass in a loadElement that you already added load sources to.

JSONObject object optional

加载的资源如果是一个 JSON 对象,将对象在这里传入,同时传入 url 的格式为:*.jsonxhrType 的格式为:JSONOBJECT

fallback function optional

Spritesheet 解析识别时的兜底回调

cb function optional

Function to call when this specific resource completes loading.

Version:
  • 1.1.7
Returns:
Type Description
Tiny.loaders.Loader Returns itself.

Destroy the loader, removes references.

Version:
  • 1.2.0

Starts loading the queued resources.

Name Type Description
cb function optional

Optional callback that will be bound to the complete event.

Returns:
Type Description
Tiny.ResourceLoader Returns itself.

Sets up a middleware function that will run before the resource is loaded.

Name Type Description
fn function

The middleware function to register.

Returns:
Type Description
Tiny.ResourceLoader Returns itself.

Resets the queue of the loader to prepare for a new load.

Returns:
Type Description
Tiny.ResourceLoader Returns itself.

执行加载

Name Type Description
opts.resources array optional

资源集合

opts.onProgress function optional

加载进度回调函数

opts.onComplete function optional

单个资源加载完成回调函数

opts.onError function optional

单个资源加载失败回调函数

opts.onAllComplete function optional

所有资源加载完成回调函数

Sets up a middleware function that will run after the resource is loaded.

Name Type Description
fn function

The middleware function to register.

Returns:
Type Description
Tiny.ResourceLoader Returns itself.
Documentation generated by JSDoc 3.4.3 on Mon Dec 03 2018 17:48:52 GMT+0800 (CST)