Skip to content

shaiis/resource-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Manage resource with promise

Resources

License

Stars

Watchers

Forks

Packages

No packages published