rm

resource-manager

Manage resource with promise

Showing:

Popularity

Downloads/wk

3

GitHub Stars

4

Maintenance

Last Commit

7yrs ago

Contributors

2

Package

Dependencies

2

Size (min+gzip)

4.7KB

License

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Resource Manager

Resource Manager - manage cached resource with promise.

Motivation

Lets say you have a function that opens and retreive a conneciton, and you want bunch of stuff to work with the connection when it's ready. In normal cases you need to wait until the connection is ready and then continue with your flow, but with Resource Manager you can get the "resource" connection any time - it will be served to you when it's ready (with Q promise).

Installation

npm install resource-manager

API

####ResourceManager(resourceRetrieverFunction, timeout, retry)

  • resourceRetrieverFunction - function which retrieve the resource - MUST return a promise.
  • timeout - timeout to wait for the resource in milliseconds before rejecting the promise
  • retry - if set to false then no retry on next invocation - immediately reject the promise

ResourceManager.getResource

return a cached resource, if the resource is not ready add the waiting promise to a queue to be resolved when the resource is received. If the retrieve of the resource fail, all the waiting promises are rejected. Calling this function after 'error' state will cause another try of getting the resource if retry is true. Calling the function after reource is ready return the cached resource as fulfilled promise

Examlpe

function getDummyConnection() {
  var def = Q.defer();
  setTimeout(function() { def.resolve("Connection OK")}, 2000);
  //setTimeout(function() { def.reject(new Error('Failed To connect'))}, 2000);
  return def.promise;
}

var resourceManager = new ResourceManager(getDummyConnection,3000, false);

resourceManager.getResource().
  then(function(connection) {
    // do something with the conneciton when ready
  },
  function (err) {
    // handle error
  });

License

MIT

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100