Comparing version 0.2.4 to 0.2.5
@@ -37,8 +37,5 @@ /** | ||
/** | ||
* Calls `fn` and returns new promise. `fn` gets generated unique `id` as parameter. | ||
* Returns count of pending / fulfilled promises in the list. | ||
* | ||
* @param {Function} fn | ||
* @param {Object} [options] | ||
* @param {Number} [options.timeout] custom timeout for particular promise | ||
* @returns {Promise} | ||
* @returns {Number} | ||
*/ | ||
@@ -49,2 +46,12 @@ | ||
key: 'add', | ||
/** | ||
* Calls `fn` and returns new promise. `fn` gets generated unique `id` as parameter. | ||
* | ||
* @param {Function} fn | ||
* @param {Object} [options] | ||
* @param {Number} [options.timeout] custom timeout for particular promise | ||
* @returns {Promise} | ||
*/ | ||
value: function add(fn, options) { | ||
@@ -232,2 +239,16 @@ var id = this.generateId(); | ||
/** | ||
* Removes all items from list. | ||
* If there is waitAll promise - it will be resolved with empty results. | ||
*/ | ||
}, { | ||
key: 'clear', | ||
value: function clear() { | ||
this._map = Object.create(null); | ||
if (this._waitingAll.isPending) { | ||
this._checkAllFulfilled(); | ||
} | ||
} | ||
/** | ||
* Generates unique ID. Can be overwritten. | ||
@@ -289,2 +310,7 @@ * | ||
} | ||
}, { | ||
key: 'count', | ||
get: function get() { | ||
return Object.keys(this._map).length; | ||
} | ||
}]); | ||
@@ -291,0 +317,0 @@ |
{ | ||
"name": "pendings", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Better control of pending promises", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -31,2 +31,11 @@ /** | ||
/** | ||
* Returns count of pending / fulfilled promises in the list. | ||
* | ||
* @returns {Number} | ||
*/ | ||
get count() { | ||
return Object.keys(this._map).length; | ||
} | ||
/** | ||
* Calls `fn` and returns new promise. `fn` gets generated unique `id` as parameter. | ||
@@ -177,2 +186,13 @@ * | ||
/** | ||
* Removes all items from list. | ||
* If there is waitAll promise - it will be resolved with empty results. | ||
*/ | ||
clear() { | ||
this._map = Object.create(null); | ||
if (this._waitingAll.isPending) { | ||
this._checkAllFulfilled(); | ||
} | ||
} | ||
/** | ||
* Generates unique ID. Can be overwritten. | ||
@@ -179,0 +199,0 @@ * |
@@ -12,2 +12,16 @@ 'use strict'; | ||
describe('exports', function () { | ||
it('should export Pendings', function () { | ||
assert.isFunction(Pendings); | ||
}); | ||
it('should export Pending', function () { | ||
assert.isFunction(Pendings.Pending); | ||
}); | ||
it('should export TimeoutError', function () { | ||
assert.isFunction(Pendings.TimeoutError); | ||
}); | ||
}); | ||
describe('add', function () { | ||
@@ -202,2 +216,26 @@ it('should return Promise', function () { | ||
describe('clear', function () { | ||
it('should remove all promises from list', function () { | ||
const pendings = new Pendings({persistent: true}); | ||
pendings.add(noop); | ||
pendings.set(1, noop); | ||
pendings.set(2, noop).catch(() => {}); | ||
pendings.resolve(1, 'foo'); | ||
pendings.reject(2, 'err'); | ||
assert.equal(pendings.count, 3); | ||
pendings.clear(); | ||
assert.equal(pendings.count, 0); | ||
}); | ||
it('should resolve waitAll with empty result', function () { | ||
const pendings = new Pendings({persistent: true}); | ||
pendings.add(noop); | ||
pendings.set(1, noop); | ||
pendings.set(2, noop).catch(() => {}); | ||
const res = pendings.waitAll(); | ||
pendings.clear(); | ||
return assert.eventually.deepEqual(res, {resolved: {}, rejected: {}}); | ||
}); | ||
}); | ||
describe('waitAll', function () { | ||
@@ -265,2 +303,16 @@ it('should resolve with empty for empty list', function () { | ||
describe('count', function () { | ||
it('should return count of promises', function () { | ||
assert.equal(this.pendings.count, 0); | ||
this.pendings.set(1, noop); | ||
assert.equal(this.pendings.count, 1); | ||
this.pendings.set(2, noop).catch(() => {}); | ||
assert.equal(this.pendings.count, 2); | ||
this.pendings.resolve(1); | ||
assert.equal(this.pendings.count, 1); | ||
this.pendings.reject(2); | ||
assert.equal(this.pendings.count, 0); | ||
}); | ||
}); | ||
describe('generateId', function () { | ||
@@ -315,3 +367,3 @@ it('should change generated ids', function () { | ||
pendings.reject(2); | ||
assert.equal(Object.keys(pendings._map).length, 1); | ||
assert.equal(pendings.count, 1); | ||
}); | ||
@@ -326,16 +378,5 @@ | ||
pendings.reject(2); | ||
assert.equal(Object.keys(pendings._map).length, 3); | ||
assert.equal(pendings.count, 3); | ||
}); | ||
}); | ||
describe('exports', function () { | ||
it('should export Pending', function () { | ||
assert.ok(Pendings.Pending); | ||
}); | ||
it('should export TimeoutError', function () { | ||
assert.ok(Pendings.TimeoutError); | ||
}); | ||
}); | ||
}); |
65871
1575