service-pool
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -8,5 +8,2 @@ 'use strict' | ||
class ServicePool { | ||
/** | ||
* @param {Object} config | ||
*/ | ||
constructor (config) { | ||
@@ -39,3 +36,6 @@ config = config || {} | ||
for (let x in this.pool) { | ||
this.pool[x].destroy(this.pool[x].service) | ||
if(this.pool[x].destroy) { | ||
this.pool[x].destroy(this.pool[x].service) | ||
} | ||
delete this.pool[x] | ||
@@ -47,47 +47,41 @@ } | ||
add (entry, callback) { | ||
async add (entry) { | ||
let length = 0 | ||
let msg = null; | ||
// Validate entry object returned | ||
if (typeDetect(entry) !== EnumsTypeDetect.OBJECT) { | ||
msg = 'Invalid JS Object passed as parameter' | ||
} else if (!entry.onAdd) { | ||
msg = 'JS Object missing onAdd function call' | ||
} | ||
return new Promise((resolve, reject) => { | ||
// Validate entry object returned | ||
if (typeDetect(entry) !== EnumsTypeDetect.OBJECT) { | ||
msg = 'Invalid JS Object passed as parameter' | ||
} else if (!entry.onAdd) { | ||
msg = 'JS Object missing onAdd function call' | ||
} | ||
if(msg) { | ||
if(callback) { | ||
return callback(msg) | ||
} else { | ||
return msg | ||
if(msg) { | ||
reject(msg) | ||
} | ||
} | ||
for (let x in this.pool) { | ||
if (x === entry.id) { | ||
// Destroy current service | ||
this.destroy(x) | ||
for (let x in this.pool) { | ||
if (x === entry.id) { | ||
// Destroy current service | ||
this.destroy(x) | ||
} | ||
} | ||
} | ||
// If max is reached remove last service | ||
length = Object.keys(this.pool).length | ||
// If max is reached remove last service | ||
length = Object.keys(this.pool).length | ||
if ((this.max > 0) && (length >= this.max)) { | ||
this.destroy(Object.keys(this.pool)[0]) | ||
} | ||
if ((this.max > 0) && (length >= this.max)) { | ||
this.destroy(Object.keys(this.pool)[0]) | ||
} | ||
// Add new Service | ||
this.pool[entry.id] = { | ||
service: entry.onAdd, | ||
test: entry.onTest, | ||
destroy: entry.onDestroy | ||
} | ||
// Add new Service | ||
this.pool[entry.id] = { | ||
service: entry.onAdd, | ||
test: entry.onTest, | ||
destroy: entry.onDestroy | ||
} | ||
if(callback) { | ||
return callback(null, this.pool[entry.id].service) | ||
} else { | ||
return this.pool[entry.id].service | ||
} | ||
resolve(this.pool[entry.id].service) | ||
}) | ||
} | ||
@@ -94,0 +88,0 @@ |
{ | ||
"name": "service-pool", | ||
"license": "MIT", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A simple, lightweight, Resource Pool Manager for NodeJS", | ||
@@ -6,0 +6,0 @@ "homepage": "https://agilite.io", |
@@ -44,8 +44,6 @@ # service-pool | ||
// Step 4: Add service entry to pool | ||
pool.add(serviceEntry, (err, service) => { | ||
// service - the returned connection to MongoDb in this case | ||
}) | ||
let service = pool.add(serviceEntry) // service[Promise] = the returned connection to MongoDb in this case | ||
// Step 5: Get a service entry by id | ||
let service = pool.get(id) // service - the returned connection to MongoDb in this case | ||
let service = pool.get(id) // service[Object] - the returned connection to MongoDb in this case | ||
@@ -52,0 +50,0 @@ // Step 6: Test a service entry by id |
@@ -18,3 +18,3 @@ 'use strict' | ||
const service1 = { | ||
const serviceEntry1 = { | ||
id: id1, | ||
@@ -30,3 +30,3 @@ onAdd: mongoose.createConnection('mongodb://127.0.0.1/dbname'), | ||
const service2 = { | ||
const serviceEntry2 = { | ||
id: id2, | ||
@@ -42,3 +42,3 @@ onAdd: mongoose.createConnection('mongodb://127.0.0.1/dbname'), | ||
const service22 = { | ||
const serviceEntry22 = { | ||
id: id2, | ||
@@ -54,3 +54,3 @@ onAdd: mongoose.createConnection('mongodb://127.0.0.1/dbname'), | ||
const service3 = { | ||
const serviceEntry3 = { | ||
id: id3, | ||
@@ -67,2 +67,6 @@ onAdd: mongoose.createConnection('mongodb://127.0.0.1/dbname'), | ||
describe ('Mongoose', () => { | ||
let service1 = null | ||
let service2 = null | ||
let service3 = null | ||
it ('Check Max Prop is valid', (done) => { | ||
@@ -75,6 +79,5 @@ expect(pool.getConfig()).to.haveOwnProperty('max') | ||
it ('Create First Pool Entry', (done) => { | ||
pool.add(service1, (err, result) => { | ||
expect(err).to.equal(null) | ||
service1 = pool.add(serviceEntry1) | ||
expect(typeDetect(service1)).to.equal(enumsTypeDetect.PROMISE) | ||
done() | ||
}) | ||
}) | ||
@@ -103,14 +106,10 @@ | ||
it ('Create 3 Pool Entries', (done) => { | ||
pool.add(service1, (err) => { | ||
expect(err).to.equal(null) | ||
pool.add(service2, (err2) => { | ||
expect(err2).to.equal(null) | ||
pool.add(service3, (err) => { | ||
expect(err).to.equal(null) | ||
done() | ||
}) | ||
}) | ||
}) | ||
service1 = pool.add(serviceEntry1) | ||
service2 = pool.add(serviceEntry2) | ||
service3 = pool.add(serviceEntry3) | ||
expect(typeDetect(service1)).to.equal(enumsTypeDetect.PROMISE) | ||
expect(typeDetect(service2)).to.equal(enumsTypeDetect.PROMISE) | ||
expect(typeDetect(service3)).to.equal(enumsTypeDetect.PROMISE) | ||
done() | ||
}) | ||
@@ -124,6 +123,5 @@ | ||
it ('Add Duplicate Entry', (done) => { | ||
pool.add(service22, (err, result) => { | ||
expect(err).to.equal(null) | ||
done() | ||
}) | ||
service2 = pool.add(serviceEntry22) | ||
expect(typeDetect(service2)).to.equal(enumsTypeDetect.PROMISE) | ||
done() | ||
}) | ||
@@ -130,0 +128,0 @@ |
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
17154
253
68