Comparing version 0.1.2 to 0.1.3
70
index.js
@@ -61,2 +61,3 @@ var Promise = require('bluebird'); | ||
this.pendingAcquires = []; | ||
this.destroyed = false; | ||
@@ -106,3 +107,6 @@ var self = this; | ||
this.used.splice(i, 1); | ||
this.free.push(new FreeResource(used.resource)); | ||
used.freed.resolve(); | ||
this._tryAcquireNext(0); | ||
@@ -138,3 +142,27 @@ return true; | ||
Tarn.prototype.destroy = function() { | ||
var self = this; | ||
clearInterval(this.interval); | ||
this.destroyed = true; | ||
// First wait for all the pending creates get ready. | ||
return Promise.all(this.pendingCreates.map(function (create) { | ||
create.abort(); | ||
return create.promise.reflect(); | ||
})).then(function () { | ||
// Now we can destroy all the freed resources. | ||
self.free.map(function (free) { | ||
self._destroy(free.resource); | ||
}); | ||
// Now we can destroy all the freed resources. | ||
self.used.map(function (used) { | ||
self._destroy(used.resource); | ||
}); | ||
// And abort all pending acquires. | ||
self.pendingAcquires.map(function (acquire) { | ||
acquire.abort(); | ||
}); | ||
}).reflect(); | ||
}; | ||
@@ -145,3 +173,3 @@ | ||
if (this.used.length >= this.max || this.pendingAcquires.length === 0 || recursion > RECURSION_LIMIT) { | ||
if (this.destroyed || this.used.length >= this.max || this.pendingAcquires.length === 0 || recursion > RECURSION_LIMIT) { | ||
// Nothing to do. | ||
@@ -184,3 +212,5 @@ return; | ||
this.used.push(new UsedResource(free.resource)); | ||
pendingAcquire._resolve(free.resource); | ||
free.used.resolve(); | ||
pendingAcquire.ready.resolve(free.resource); | ||
} | ||
@@ -209,3 +239,3 @@ | ||
self.free.push(new FreeResource(resource)); | ||
pendingCreate._resolve(resource); | ||
pendingCreate.ready.resolve(resource); | ||
} | ||
@@ -215,3 +245,3 @@ }).catch(function (err) { | ||
pendingCreate._reject(err); | ||
pendingCreate.ready.reject(err); | ||
}); | ||
@@ -233,2 +263,3 @@ | ||
this.timestamp = now(); | ||
this.used = defer(); | ||
} | ||
@@ -239,2 +270,3 @@ | ||
this.timestamp = now(); | ||
this.freed = defer(); | ||
} | ||
@@ -247,18 +279,12 @@ | ||
this._resolve = null; | ||
this._reject = null; | ||
var self = this; | ||
this.promise = new Promise(function (resolve, reject) { | ||
self._resolve = resolve; | ||
self._reject = reject; | ||
}).timeout(timeout); | ||
this.ready = defer(); | ||
this.promise = this.ready.promise.timeout(timeout); | ||
} | ||
PendingOperation.prototype.abort = function () { | ||
this._reject(new Error('aborted')); | ||
this.ready.reject(new Error('aborted')); | ||
}; | ||
PendingOperation.prototype.isRejected = function () { | ||
return this.promise.isRejected(); | ||
return this.ready.promise.isRejected(); | ||
}; | ||
@@ -314,2 +340,18 @@ | ||
function defer() { | ||
var resolve = null; | ||
var reject = null; | ||
var promise = new Promise(function (resolver, rejecter) { | ||
resolve = resolver; | ||
reject = rejecter; | ||
}); | ||
return { | ||
promise: promise, | ||
resolve: resolve, | ||
reject: reject | ||
}; | ||
} | ||
module.exports = { | ||
@@ -316,0 +358,0 @@ Tarn: Tarn, |
{ | ||
"name": "tarn", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Simple and robust resource pool for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -82,2 +82,8 @@ [![Build Status](https://travis-ci.org/Vincit/tarn.js.svg?branch=master)](https://travis-ci.org/Vincit/tarn.js) | ||
pool.numPendingCreates() | ||
// waits for all resources to be returned to the pool and destroys them. | ||
// pool cannot be used after this. | ||
poo.destroy().then(() => { | ||
console.log('pool destroyed'); | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12912
275
89