Have ideas to improve npm?Join in the discussion! ¬Ľ

    resource-manager

    1.0.2¬†‚Äʬ†Public¬†‚Äʬ†Published

    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

    Install

    npm i resource-manager

    DownloadsWeekly Downloads

    4

    Version

    1.0.2

    License

    none

    Last publish

    Collaborators

    • avatar