Skip to content

Simple resource loader with able to retry and cache operations.

Notifications You must be signed in to change notification settings

RabotaRu/loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@rabota/loader

npm npm (scoped with tag)

Loader with ability to cache & retry operations.

Example

Script Loader

const loader = new ScriptLoader();

loader.retry('https://example.com/script.js').then(_ => {
  // ...
});

Image loader

const loader = new ImageLoader();

loader.retry('https://example.com/image.png').then(image => {
  // image loaded
});

Custom retry operation

function action () {
  return new Promise((resolve, reject) => {
    setTimeout(_ => {
      reject( new Error('Cannot load content') );
    }, 1000 * Math.random());
  });
}

const loader = new RetryOperation();

loader.retry( action, 10 ).then(content => {
  // ...
}).catch(error => {
  // content cannot be loaded after 10 attempts
});

Retry operation with caching

function action () {
  return new Promise(resolve => {
    setTimeout(_ => {
      resolve({
        content: 'some content'
      });
    }, 1000 * Math.random());
  });
}

// 100 is a max number of cache entities
// You can provide a LRU instance instead of number
const loader = new RetryOperationCached( 100 );

// `example_operation_cache_key` - is a key for caching success result
loader.retry( action, 10, 'example_operation_cache_key' ).then(result => {
  // `result` now is an object: { item: <your result>, cached: <true/false> }
}).catch(error => {
  // content cannot be loaded after 10 attempts
  // will not be cached
});

If you try to load content with same cache key you will get content from the cache.

About

Simple resource loader with able to retry and cache operations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published