API Reference 1.1.0stream_pluginAssetLoader

AssetLoader abstract class

A skeletal implementation of ResourceLoader that utilizes AssetCache and supports all HTTP requirements such as partial content, multi-part loading, ETag and more.

To implement your own loader, you can do the follows:

  1. Implements Asset to load your asset.
  2. Extends AssetLoader and override getAsset to return the Asset object you implemented for the given path.

Note: rootDir is not used in AssetLoader. Whether

it is meaningful depends on how getAsset is implemented in derives.

abstract class AssetLoader implements ResourceLoader {
 AssetLoader([this.rootDir]) {
   _cache = new _AssetCache(this);
 }

 @override
 final String rootDir;

 /** The total cache size (unit: bytes). Default: 3 * 1024 * 1024.
  * Note: [cacheSize] must be larger than [cacheThreshold].
  * Otherwise, result is unpredictable.
  */
 int cacheSize = 3 * 1024 * 1024;
 /** The thread hold (unit: bytes) to cache. Only resources less than this size
  * will be cached. Default: 128 * 1024.
  */
 int cacheThreshold = 128 * 1024;

 /** The cache.
  */
 AssetCache get cache => _cache;
 AssetCache _cache;

 ///Whether to generate the ETag header. Default: true.
 bool useETag = true;
 ///Whether to generate the Expires and max-age headers. Default: true.
 bool useExpires = true;

 /** Returns the value of the ETag header.
  * If null is returned, ETag header won't be generated.
  *
  * Default: a string combining [lastModified] and [assetSize]
  * if [useETag] is true.
  * You can override this method if necessary.
  */
 String getETag(Asset asset, DateTime lastModified, int assetSize)
 => useETag ? _getETag(lastModified, assetSize): null;
 /** Returns the duration for the Expires and max-age headers.
  * If null is returned, the Expires and max-age headers won't be generated.
  *
  * Default: 30 days if [useExpres] is true.
  * You can override this method if necessary.
  */
 Duration getExpires(Asset asset) => useExpires ? const Duration(days: 30): null;

 /** Whether the given asset shall be cached.
  *
  * Default: it returns true if [assetSize] is not greater than [cacheThreshold].
  * You override this method if necessary.
  *
  * * [assetSize] - the size of [asset] in bytes.
  */
 bool shallCache(Asset asset, int assetSize) => assetSize <= cacheThreshold;

 /** Returns the [Asset] instance representing [path].
  *
  * The derives must override this method.
  */
 Asset getAsset(String path);

 @override
 Future load(HttpConnect connect, String uri)
 => loadAsset(connect, getAsset(uri), _cache);
}

Subclasses

FileLoader

Implements

ResourceLoader

Constructors

new AssetLoader([String rootDir]) #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
AssetLoader([this.rootDir]) {
 _cache = new _AssetCache(this);
}

Properties

final AssetCache cache #

The cache.

AssetCache get cache => _cache;

int cacheSize #

The total cache size (unit: bytes). Default: 3 * 1024 * 1024. Note: cacheSize must be larger than cacheThreshold. Otherwise, result is unpredictable.

int cacheSize = 3 * 1024 * 1024

int cacheThreshold #

The thread hold (unit: bytes) to cache. Only resources less than this size will be cached. Default: 128 * 1024.

int cacheThreshold = 128 * 1024

final String rootDir #

The root directory.

docs inherited from ResourceLoader
final String rootDir

bool useETag #

Whether to generate the ETag header. Default: true.

bool useETag = true

bool useExpires #

Whether to generate the Expires and max-age headers. Default: true.

bool useExpires = true

Methods

abstract Asset getAsset(String path) #

Returns the Asset instance representing path.

The derives must override this method.

String getETag(Asset asset, DateTime lastModified, int assetSize) #

Returns the value of the ETag header. If null is returned, ETag header won't be generated.

Default: a string combining lastModified and assetSize if useETag is true. You can override this method if necessary.

String getETag(Asset asset, DateTime lastModified, int assetSize)
=> useETag ? _getETag(lastModified, assetSize): null;

Duration getExpires(Asset asset) #

Returns the duration for the Expires and max-age headers. If null is returned, the Expires and max-age headers won't be generated.

Default: 30 days if useExpres is true. You can override this method if necessary.

Duration getExpires(Asset asset) => useExpires ? const Duration(days: 30): null;

Future load(HttpConnect connect, String uri) #

Loads the asset of the given URI to the given response.

docs inherited from ResourceLoader
@override
Future load(HttpConnect connect, String uri)
=> loadAsset(connect, getAsset(uri), _cache);

bool shallCache(Asset asset, int assetSize) #

Whether the given asset shall be cached.

Default: it returns true if assetSize is not greater than cacheThreshold. You override this method if necessary.

  • assetSize - the size of asset in bytes.
bool shallCache(Asset asset, int assetSize) => assetSize <= cacheThreshold;