Comparing version 0.2.5 to 0.2.6
@@ -14,5 +14,5 @@ /** | ||
var DEFAULT_OPTIONS = { | ||
idPrefix: '', | ||
persistent: false, | ||
timeout: 0 | ||
autoRemove: false, | ||
timeout: 0, | ||
idPrefix: '' | ||
}; | ||
@@ -25,5 +25,5 @@ | ||
* @param {Object} [options] | ||
* @param {Number} [options.autoRemove=false] automatically remove fulfilled promises | ||
* @param {Number} [options.timeout=0] default timeout for all promises | ||
* @param {String} [options.idPrefix=''] prefix for generated IDs | ||
* @param {Number} [options.timeout=0] default timeout for all promises | ||
* @param {Number} [options.persistent=false] should list keep promises after fulfillment | ||
*/ | ||
@@ -93,3 +93,3 @@ function Pendings(options) { | ||
/** | ||
* Checks if promise with specified `id` is pending. | ||
* Checks if promise with specified `id` is exists in the list. | ||
* | ||
@@ -125,7 +125,7 @@ * @param {String} id | ||
/** | ||
* Rejects pending promise by `id` with specified `reason`. | ||
* Rejects pending promise by `id` with specified `value`. | ||
* Throws if promise does not exist or is already fulfilled. | ||
* | ||
* @param {String} id | ||
* @param {*} [reason] | ||
* @param {*} [value] | ||
*/ | ||
@@ -135,5 +135,5 @@ | ||
key: 'reject', | ||
value: function reject(id, reason) { | ||
value: function reject(id, value) { | ||
if (this.has(id)) { | ||
this._map[id].reject(reason); | ||
this._map[id].reject(value); | ||
} else { | ||
@@ -149,4 +149,4 @@ throw createNoPendingError(id); | ||
* @param {String} id | ||
* @param {*} [value] | ||
* @param {*} [reason] | ||
* @param {*} [resolveValue] | ||
* @param {*} [rejectValue] | ||
*/ | ||
@@ -156,5 +156,5 @@ | ||
key: 'fulfill', | ||
value: function fulfill(id, value, reason) { | ||
value: function fulfill(id, resolveValue, rejectValue) { | ||
if (this.has(id)) { | ||
this._map[id].fulfill(value, reason); | ||
this._map[id].fulfill(resolveValue, rejectValue); | ||
} else { | ||
@@ -181,6 +181,6 @@ throw createNoPendingError(id); | ||
/** | ||
* Rejects pending promise by `id` with specified `reason` if it exists. | ||
* Rejects pending promise by `id` with specified `value` if it exists. | ||
* | ||
* @param {String} id | ||
* @param {*} [reason] | ||
* @param {*} [value] | ||
*/ | ||
@@ -190,5 +190,5 @@ | ||
key: 'tryReject', | ||
value: function tryReject(id, reason) { | ||
value: function tryReject(id, value) { | ||
if (this.has(id)) { | ||
this._map[id].reject(reason); | ||
this._map[id].reject(value); | ||
} | ||
@@ -201,4 +201,4 @@ } | ||
* @param {String} id | ||
* @param {*} [value] | ||
* @param {*} [reason] | ||
* @param {*} [resolveValue] | ||
* @param {*} [rejectValue] | ||
*/ | ||
@@ -208,5 +208,5 @@ | ||
key: 'tryFulfill', | ||
value: function tryFulfill(id, value, reason) { | ||
value: function tryFulfill(id, resolveValue, rejectValue) { | ||
if (this.has(id)) { | ||
this._map[id].fulfill(value, reason); | ||
this._map[id].fulfill(resolveValue, rejectValue); | ||
} | ||
@@ -216,5 +216,5 @@ } | ||
/** | ||
* Rejects all pending promises with specified `reason`. Useful for cleanup. | ||
* Rejects all pending promises with specified `value`. Useful for cleanup. | ||
* | ||
* @param {*} [reason] | ||
* @param {*} [value] | ||
*/ | ||
@@ -224,7 +224,7 @@ | ||
key: 'rejectAll', | ||
value: function rejectAll(reason) { | ||
value: function rejectAll(value) { | ||
var _this2 = this; | ||
Object.keys(this._map).forEach(function (id) { | ||
return _this2.tryReject(id, reason); | ||
return _this2.tryReject(id, value); | ||
}); | ||
@@ -277,3 +277,3 @@ } | ||
value: function _onFulfilled(id) { | ||
if (!this._options.persistent) { | ||
if (this._options.autoRemove) { | ||
delete this._map[id]; | ||
@@ -309,13 +309,8 @@ } | ||
var resolved = {}; | ||
var rejected = {}; | ||
var result = { resolved: {}, rejected: {} }; | ||
Object.keys(this._map).forEach(function (id) { | ||
var pending = _this5._map[id]; | ||
if (pending.isResolved) { | ||
resolved[id] = pending.value; | ||
} else { | ||
rejected[id] = pending.value; | ||
} | ||
result[pending.isResolved ? 'resolved' : 'rejected'][id] = pending.value; | ||
}); | ||
return { resolved: resolved, rejected: rejected }; | ||
return result; | ||
} | ||
@@ -322,0 +317,0 @@ }, { |
{ | ||
"name": "pendings", | ||
"version": "0.2.5", | ||
"description": "Better control of pending promises", | ||
"version": "0.2.6", | ||
"description": "Better control of promises", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Vitaliy Potapov", |
@@ -7,6 +7,7 @@ # Pendings | ||
> Better control of pending [Promises](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) | ||
> Better control of [Promises](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) | ||
*Pendings* is a library for control of pending promises. | ||
It manages `resolve` / `reject` callbacks and provides convenient access to them for promise fulfillment. | ||
*Pendings* is a library for more flexible control over promises. It is useful in event-based code | ||
where you need to manually store `resolve` / `reject` callbacks for later fulfillment. | ||
It reduces boilerplate code and allows to split business logic from promise manipulation. | ||
@@ -61,4 +62,3 @@ ## Installation | ||
[Pendings](#Pendings) class is useful for dynamic list of promises. | ||
Each promise can automatically get unique `id` and can be fulfilled later by that id. | ||
After fulfillment promise is removed from list. | ||
Each promise gets unique `id` (manually or auto-generated) and can be resolved later by that id. | ||
```js | ||
@@ -74,3 +74,3 @@ const Pendings = require('pendings'); | ||
return this.pendings.add(id => { | ||
this.send({id, foo: 'bar'}); // mark request with unique `id` generated by Pendings | ||
this.send({id, foo: 'bar'}); // mark request with unique generated `id` | ||
}); | ||
@@ -77,0 +77,0 @@ } |
@@ -7,6 +7,7 @@ # Pendings | ||
> Better control of pending [Promises](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) | ||
> Better control of [Promises](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) | ||
*Pendings* is a library for control of pending promises. | ||
It manages `resolve` / `reject` callbacks and provides convenient access to them for promise fulfillment. | ||
*Pendings* is a library for more flexible control over promises. It is useful in event-based code | ||
where you need to manually store `resolve` / `reject` callbacks for later fulfillment. | ||
It reduces boilerplate code and allows to split business logic from promise manipulation. | ||
@@ -61,4 +62,3 @@ ## Installation | ||
[Pendings](#Pendings) class is useful for dynamic list of promises. | ||
Each promise can automatically get unique `id` and can be fulfilled later by that id. | ||
After fulfillment promise is removed from list. | ||
Each promise gets unique `id` (manually or auto-generated) and can be resolved later by that id. | ||
```js | ||
@@ -74,3 +74,3 @@ const Pendings = require('pendings'); | ||
return this.pendings.add(id => { | ||
this.send({id, foo: 'bar'}); // mark request with unique `id` generated by Pendings | ||
this.send({id, foo: 'bar'}); // mark request with unique generated `id` | ||
}); | ||
@@ -242,2 +242,3 @@ } | ||
* [new Pendings([options])](#new_Pendings_new) | ||
* [.count](#Pendings+count) ⇒ <code>Number</code> | ||
* [.add(fn, [options])](#Pendings+add) ⇒ <code>Promise</code> | ||
@@ -247,9 +248,10 @@ * [.set(id, fn, [options])](#Pendings+set) ⇒ <code>Promise</code> | ||
* [.resolve(id, [value])](#Pendings+resolve) | ||
* [.reject(id, [reason])](#Pendings+reject) | ||
* [.fulfill(id, [value], [reason])](#Pendings+fulfill) | ||
* [.reject(id, [value])](#Pendings+reject) | ||
* [.fulfill(id, [resolveValue], [rejectValue])](#Pendings+fulfill) | ||
* [.tryResolve(id, [value])](#Pendings+tryResolve) | ||
* [.tryReject(id, [reason])](#Pendings+tryReject) | ||
* [.tryFulfill(id, [value], [reason])](#Pendings+tryFulfill) | ||
* [.rejectAll([reason])](#Pendings+rejectAll) | ||
* [.tryReject(id, [value])](#Pendings+tryReject) | ||
* [.tryFulfill(id, [resolveValue], [rejectValue])](#Pendings+tryFulfill) | ||
* [.rejectAll([value])](#Pendings+rejectAll) | ||
* [.waitAll()](#Pendings+waitAll) ⇒ <code>Promise</code> | ||
* [.clear()](#Pendings+clear) | ||
* [.generateId()](#Pendings+generateId) ⇒ <code>String</code> | ||
@@ -266,6 +268,12 @@ | ||
| [options] | <code>Object</code> | | | | ||
| [options.autoRemove] | <code>Number</code> | <code>false</code> | automatically remove fulfilled promises | | ||
| [options.timeout] | <code>Number</code> | <code>0</code> | default timeout for all promises | | ||
| [options.idPrefix] | <code>String</code> | <code>''</code> | prefix for generated IDs | | ||
| [options.timeout] | <code>Number</code> | <code>0</code> | default timeout for all promises | | ||
| [options.persistent] | <code>Number</code> | <code>false</code> | should list keep promises after fulfillment | | ||
<a name="Pendings+count"></a> | ||
### pendings.count ⇒ <code>Number</code> | ||
Returns count of pending / fulfilled promises in the list. | ||
**Kind**: instance property of [<code>Pendings</code>](#Pendings) | ||
<a name="Pendings+add"></a> | ||
@@ -302,3 +310,3 @@ | ||
### pendings.has(id) ⇒ <code>Boolean</code> | ||
Checks if promise with specified `id` is pending. | ||
Checks if promise with specified `id` is exists in the list. | ||
@@ -326,4 +334,4 @@ **Kind**: instance method of [<code>Pendings</code>](#Pendings) | ||
### pendings.reject(id, [reason]) | ||
Rejects pending promise by `id` with specified `reason`. | ||
### pendings.reject(id, [value]) | ||
Rejects pending promise by `id` with specified `value`. | ||
Throws if promise does not exist or is already fulfilled. | ||
@@ -336,7 +344,7 @@ | ||
| id | <code>String</code> | | ||
| [reason] | <code>\*</code> | | ||
| [value] | <code>\*</code> | | ||
<a name="Pendings+fulfill"></a> | ||
### pendings.fulfill(id, [value], [reason]) | ||
### pendings.fulfill(id, [resolveValue], [rejectValue]) | ||
Rejects pending promise by `id` if `reason` is truthy, otherwise resolves with `value`. | ||
@@ -350,4 +358,4 @@ Throws if promise does not exist or is already fulfilled. | ||
| id | <code>String</code> | | ||
| [value] | <code>\*</code> | | ||
| [reason] | <code>\*</code> | | ||
| [resolveValue] | <code>\*</code> | | ||
| [rejectValue] | <code>\*</code> | | ||
@@ -368,4 +376,4 @@ <a name="Pendings+tryResolve"></a> | ||
### pendings.tryReject(id, [reason]) | ||
Rejects pending promise by `id` with specified `reason` if it exists. | ||
### pendings.tryReject(id, [value]) | ||
Rejects pending promise by `id` with specified `value` if it exists. | ||
@@ -377,7 +385,7 @@ **Kind**: instance method of [<code>Pendings</code>](#Pendings) | ||
| id | <code>String</code> | | ||
| [reason] | <code>\*</code> | | ||
| [value] | <code>\*</code> | | ||
<a name="Pendings+tryFulfill"></a> | ||
### pendings.tryFulfill(id, [value], [reason]) | ||
### pendings.tryFulfill(id, [resolveValue], [rejectValue]) | ||
Rejects pending promise by `id` if `reason` is truthy, otherwise resolves with `value`. | ||
@@ -390,9 +398,9 @@ | ||
| id | <code>String</code> | | ||
| [value] | <code>\*</code> | | ||
| [reason] | <code>\*</code> | | ||
| [resolveValue] | <code>\*</code> | | ||
| [rejectValue] | <code>\*</code> | | ||
<a name="Pendings+rejectAll"></a> | ||
### pendings.rejectAll([reason]) | ||
Rejects all pending promises with specified `reason`. Useful for cleanup. | ||
### pendings.rejectAll([value]) | ||
Rejects all pending promises with specified `value`. Useful for cleanup. | ||
@@ -403,3 +411,3 @@ **Kind**: instance method of [<code>Pendings</code>](#Pendings) | ||
| --- | --- | | ||
| [reason] | <code>\*</code> | | ||
| [value] | <code>\*</code> | | ||
@@ -413,2 +421,9 @@ <a name="Pendings+waitAll"></a> | ||
**Returns**: <code>Promise</code> - promise resolved with `{resolved: <Array>, rejected: <Array>}` | ||
<a name="Pendings+clear"></a> | ||
### pendings.clear() | ||
Removes all items from list. | ||
If there is waitAll promise - it will be resolved with empty results. | ||
**Kind**: instance method of [<code>Pendings</code>](#Pendings) | ||
<a name="Pendings+generateId"></a> | ||
@@ -415,0 +430,0 @@ |
@@ -10,5 +10,5 @@ /** | ||
const DEFAULT_OPTIONS = { | ||
autoRemove: false, | ||
timeout: 0, | ||
idPrefix: '', | ||
persistent: false, | ||
timeout: 0, | ||
}; | ||
@@ -21,5 +21,5 @@ | ||
* @param {Object} [options] | ||
* @param {Number} [options.autoRemove=false] automatically remove fulfilled promises | ||
* @param {Number} [options.timeout=0] default timeout for all promises | ||
* @param {String} [options.idPrefix=''] prefix for generated IDs | ||
* @param {Number} [options.timeout=0] default timeout for all promises | ||
* @param {Number} [options.persistent=false] should list keep promises after fulfillment | ||
*/ | ||
@@ -76,3 +76,3 @@ constructor(options) { | ||
/** | ||
* Checks if promise with specified `id` is pending. | ||
* Checks if promise with specified `id` is exists in the list. | ||
* | ||
@@ -102,11 +102,11 @@ * @param {String} id | ||
/** | ||
* Rejects pending promise by `id` with specified `reason`. | ||
* Rejects pending promise by `id` with specified `value`. | ||
* Throws if promise does not exist or is already fulfilled. | ||
* | ||
* @param {String} id | ||
* @param {*} [reason] | ||
* @param {*} [value] | ||
*/ | ||
reject(id, reason) { | ||
reject(id, value) { | ||
if (this.has(id)) { | ||
this._map[id].reject(reason); | ||
this._map[id].reject(value); | ||
} else { | ||
@@ -122,8 +122,8 @@ throw createNoPendingError(id); | ||
* @param {String} id | ||
* @param {*} [value] | ||
* @param {*} [reason] | ||
* @param {*} [resolveValue] | ||
* @param {*} [rejectValue] | ||
*/ | ||
fulfill(id, value, reason) { | ||
fulfill(id, resolveValue, rejectValue) { | ||
if (this.has(id)) { | ||
this._map[id].fulfill(value, reason); | ||
this._map[id].fulfill(resolveValue, rejectValue); | ||
} else { | ||
@@ -147,10 +147,10 @@ throw createNoPendingError(id); | ||
/** | ||
* Rejects pending promise by `id` with specified `reason` if it exists. | ||
* Rejects pending promise by `id` with specified `value` if it exists. | ||
* | ||
* @param {String} id | ||
* @param {*} [reason] | ||
* @param {*} [value] | ||
*/ | ||
tryReject(id, reason) { | ||
tryReject(id, value) { | ||
if (this.has(id)) { | ||
this._map[id].reject(reason); | ||
this._map[id].reject(value); | ||
} | ||
@@ -163,8 +163,8 @@ } | ||
* @param {String} id | ||
* @param {*} [value] | ||
* @param {*} [reason] | ||
* @param {*} [resolveValue] | ||
* @param {*} [rejectValue] | ||
*/ | ||
tryFulfill(id, value, reason) { | ||
tryFulfill(id, resolveValue, rejectValue) { | ||
if (this.has(id)) { | ||
this._map[id].fulfill(value, reason); | ||
this._map[id].fulfill(resolveValue, rejectValue); | ||
} | ||
@@ -174,8 +174,8 @@ } | ||
/** | ||
* Rejects all pending promises with specified `reason`. Useful for cleanup. | ||
* Rejects all pending promises with specified `value`. Useful for cleanup. | ||
* | ||
* @param {*} [reason] | ||
* @param {*} [value] | ||
*/ | ||
rejectAll(reason) { | ||
Object.keys(this._map).forEach(id => this.tryReject(id, reason)); | ||
rejectAll(value) { | ||
Object.keys(this._map).forEach(id => this.tryReject(id, value)); | ||
} | ||
@@ -213,3 +213,3 @@ | ||
_onFulfilled(id) { | ||
if (!this._options.persistent) { | ||
if (this._options.autoRemove) { | ||
delete this._map[id]; | ||
@@ -236,13 +236,8 @@ } | ||
_getAllValues() { | ||
const resolved = {}; | ||
const rejected = {}; | ||
const result = {resolved: {}, rejected: {}}; | ||
Object.keys(this._map).forEach(id => { | ||
const pending = this._map[id]; | ||
if (pending.isResolved) { | ||
resolved[id] = pending.value; | ||
} else { | ||
rejected[id] = pending.value; | ||
} | ||
result[pending.isResolved ? 'resolved' : 'rejected'][id] = pending.value; | ||
}); | ||
return {resolved, rejected}; | ||
return result; | ||
} | ||
@@ -249,0 +244,0 @@ } |
@@ -6,6 +6,10 @@ 'use strict'; | ||
const noop = () => {}; | ||
const wait = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
describe('pendings', function () { | ||
let pendings; | ||
beforeEach(function () { | ||
this.pendings = new Pendings(); | ||
pendings = new Pendings(); | ||
}); | ||
@@ -29,3 +33,3 @@ | ||
it('should return Promise', function () { | ||
const res = this.pendings.add(noop); | ||
const res = pendings.add(noop); | ||
assert(res instanceof Promise); | ||
@@ -36,3 +40,3 @@ }); | ||
let a = 0; | ||
this.pendings.add(() => a++); | ||
pendings.add(() => a++); | ||
assert.equal(a, 1); | ||
@@ -42,3 +46,3 @@ }); | ||
it('should reject in case of error in fn', function () { | ||
const res = this.pendings.add(() => { | ||
const res = pendings.add(() => { | ||
throw new Error('err'); | ||
@@ -52,3 +56,3 @@ }); | ||
it('should return Promise', function () { | ||
const res = this.pendings.set(1, noop); | ||
const res = pendings.set(1, noop); | ||
assert.instanceOf(res, Promise); | ||
@@ -59,3 +63,3 @@ }); | ||
let a = 0; | ||
this.pendings.set(1, () => a++); | ||
pendings.set(1, () => a++); | ||
assert.equal(a, 1); | ||
@@ -65,3 +69,3 @@ }); | ||
it('should reject in case of error in fn', function () { | ||
const res = this.pendings.set(1, () => { | ||
const res = pendings.set(1, () => { | ||
throw new Error('err'); | ||
@@ -73,4 +77,4 @@ }); | ||
it('should return the same promise for second call with the same id', function () { | ||
const p1 = this.pendings.set(1, noop); | ||
const p2 = this.pendings.set(1, noop); | ||
const p1 = pendings.set(1, noop); | ||
const p2 = pendings.set(1, noop); | ||
assert.equal(p1, p2); | ||
@@ -82,34 +86,40 @@ }); | ||
it('should return false for non-existing promise', function () { | ||
assert.notOk(this.pendings.has(1)); | ||
assert.notOk(pendings.has(1)); | ||
}); | ||
it('should return true for pending promise', function () { | ||
this.pendings.set(1, noop); | ||
assert.ok(this.pendings.has(1)); | ||
pendings.set(1, noop); | ||
assert.ok(pendings.has(1)); | ||
}); | ||
it('should return false for resolve', function () { | ||
this.pendings.set(1, noop); | ||
this.pendings.resolve(1); | ||
assert.notOk(this.pendings.has(1)); | ||
}); | ||
describe('(autoRemove = false)', function () { | ||
it('should return true after resolve', function () { | ||
pendings.set(1, noop); | ||
pendings.resolve(1); | ||
assert.ok(pendings.has(1)); | ||
}); | ||
it('should return false for manual reject', function () { | ||
this.pendings.set(1, noop); | ||
this.pendings.reject(1); | ||
assert.notOk(this.pendings.has(1)); | ||
it('should return true after reject', function () { | ||
pendings.set(1, noop); | ||
pendings.reject(1); | ||
assert.ok(pendings.has(1)); | ||
}); | ||
}); | ||
it('should return false for reject by fn', function () { | ||
this.pendings.set(1, () => { throw new Error('foo'); }); | ||
assert.notOk(this.pendings.has(1)); | ||
}); | ||
describe('(autoRemove = true)', function () { | ||
beforeEach(function () { | ||
pendings = new Pendings({autoRemove: true}); | ||
}); | ||
it('should return false for reject by timeout', function (done) { | ||
this.pendings.set(1, noop, {timeout: 5}); | ||
assert.ok(this.pendings.has(1)); | ||
setTimeout(() => { | ||
assert.notOk(this.pendings.has(1)); | ||
done(); | ||
}, 10); | ||
it('should return false after resolve', function () { | ||
pendings.set(1, noop); | ||
pendings.resolve(1); | ||
assert.notOk(pendings.has(1)); | ||
}); | ||
it('should return false after reject', function () { | ||
pendings.set(1, noop); | ||
pendings.reject(1); | ||
assert.notOk(pendings.has(1)); | ||
}); | ||
}); | ||
@@ -120,4 +130,4 @@ }); | ||
it('should resolve', function () { | ||
const res = this.pendings.set(1, noop); | ||
this.pendings.resolve(1, 'foo'); | ||
const res = pendings.set(1, noop); | ||
pendings.resolve(1, 'foo'); | ||
return assert.eventually.equal(res, 'foo'); | ||
@@ -127,4 +137,4 @@ }); | ||
it('should throw for incorrect id', function () { | ||
this.pendings.set(1, noop); | ||
assert.throws(() => this.pendings.resolve(2, 'foo'), 'Pending promise not found with id: 2'); | ||
pendings.set(1, noop); | ||
assert.throws(() => pendings.resolve(2, 'foo'), 'Pending promise not found with id: 2'); | ||
}); | ||
@@ -135,5 +145,5 @@ }); | ||
it('should resolve', function () { | ||
const res = this.pendings.set(1, noop); | ||
this.pendings.tryResolve(1, 'foo'); | ||
this.pendings.tryResolve(1, 'bar'); | ||
const res = pendings.set(1, noop); | ||
pendings.tryResolve(1, 'foo'); | ||
pendings.tryResolve(1, 'bar'); | ||
return assert.eventually.equal(res, 'foo'); | ||
@@ -143,4 +153,4 @@ }); | ||
it('should not throw for incorrect id', function () { | ||
this.pendings.set(1, noop); | ||
assert.doesNotThrow(() => this.pendings.tryResolve(2, 'foo')); | ||
pendings.set(1, noop); | ||
assert.doesNotThrow(() => pendings.tryResolve(2, 'foo')); | ||
}); | ||
@@ -150,11 +160,21 @@ }); | ||
describe('reject', function () { | ||
it('should reject', function () { | ||
const res = this.pendings.set(1, noop); | ||
this.pendings.reject(1, new Error('err')); | ||
it('should reject manually', function () { | ||
const res = pendings.set(1, noop); | ||
pendings.reject(1, new Error('err')); | ||
return assert.isRejected(res, 'err'); | ||
}); | ||
it('should reject by fn', function () { | ||
const res = pendings.set(1, () => { throw new Error('err'); }); | ||
return assert.isRejected(res, 'err'); | ||
}); | ||
it('should reject by timeout', function () { | ||
const res = pendings.set(1, noop, {timeout: 5}); | ||
return wait(10).then(() => assert.isRejected(res, 'Promise rejected by timeout (5 ms)')); | ||
}); | ||
it('should throw for incorrect id', function () { | ||
this.pendings.set(1, noop); | ||
assert.throws(() => this.pendings.reject(2, 'foo'), 'Pending promise not found with id: 2'); | ||
pendings.set(1, noop); | ||
assert.throws(() => pendings.reject(2, 'foo'), 'Pending promise not found with id: 2'); | ||
}); | ||
@@ -165,10 +185,10 @@ }); | ||
it('should not throw for incorrect id', function () { | ||
this.pendings.set(1, noop); | ||
assert.doesNotThrow(() => this.pendings.tryReject(2, 'foo')); | ||
pendings.set(1, noop); | ||
assert.doesNotThrow(() => pendings.tryReject(2, 'foo')); | ||
}); | ||
it('should reject', function () { | ||
const res = this.pendings.set(1, noop); | ||
this.pendings.tryReject(1, new Error('err')); | ||
this.pendings.tryReject(1, new Error('err2')); | ||
const res = pendings.set(1, noop); | ||
pendings.tryReject(1, new Error('err')); | ||
pendings.tryReject(1, new Error('err2')); | ||
return assert.isRejected(res, 'err'); | ||
@@ -180,4 +200,4 @@ }); | ||
it('should resolve', function () { | ||
const res = this.pendings.set(1, noop); | ||
this.pendings.fulfill(1, 'foo'); | ||
const res = pendings.set(1, noop); | ||
pendings.fulfill(1, 'foo'); | ||
return assert.eventually.equal(res, 'foo'); | ||
@@ -187,4 +207,4 @@ }); | ||
it('should reject', function () { | ||
const res = this.pendings.set(1, noop); | ||
this.pendings.fulfill(1, 'foo', new Error('err')); | ||
const res = pendings.set(1, noop); | ||
pendings.fulfill(1, 'foo', new Error('err')); | ||
return assert.isRejected(res, 'err'); | ||
@@ -194,4 +214,4 @@ }); | ||
it('should throw for incorrect id', function () { | ||
this.pendings.set(1, noop); | ||
assert.throws(() => this.pendings.fulfill(2, 'foo'), 'Pending promise not found with id: 2'); | ||
pendings.set(1, noop); | ||
assert.throws(() => pendings.fulfill(2, 'foo'), 'Pending promise not found with id: 2'); | ||
}); | ||
@@ -202,10 +222,10 @@ }); | ||
it('should not throw for incorrect id', function () { | ||
this.pendings.set(1, noop); | ||
assert.doesNotThrow(() => this.pendings.tryFulfill(2, 'foo')); | ||
pendings.set(1, noop); | ||
assert.doesNotThrow(() => pendings.tryFulfill(2, 'foo')); | ||
}); | ||
it('should resolve', function () { | ||
const res = this.pendings.set(1, noop); | ||
this.pendings.tryFulfill(1, 'foo'); | ||
this.pendings.tryFulfill(1, 'bar'); | ||
const res = pendings.set(1, noop); | ||
pendings.tryFulfill(1, 'foo'); | ||
pendings.tryFulfill(1, 'bar'); | ||
return assert.eventually.equal(res, 'foo'); | ||
@@ -215,5 +235,5 @@ }); | ||
it('should reject', function () { | ||
const res = this.pendings.set(1, noop); | ||
this.pendings.tryFulfill(1, 'foo', new Error('err')); | ||
this.pendings.tryFulfill(1, 'foo', new Error('err2')); | ||
const res = pendings.set(1, noop); | ||
pendings.tryFulfill(1, 'foo', new Error('err')); | ||
pendings.tryFulfill(1, 'foo', new Error('err2')); | ||
return assert.isRejected(res, 'err'); | ||
@@ -225,5 +245,5 @@ }); | ||
it('should reject all promises', function () { | ||
const p1 = this.pendings.add(noop); | ||
const p2 = this.pendings.set(1, noop); | ||
this.pendings.rejectAll('err'); | ||
const p1 = pendings.add(noop); | ||
const p2 = pendings.set(1, noop); | ||
pendings.rejectAll('err'); | ||
return Promise.all([ | ||
@@ -238,3 +258,2 @@ assert.isRejected(p1, 'err'), | ||
it('should remove all promises from list', function () { | ||
const pendings = new Pendings({persistent: true}); | ||
pendings.add(noop); | ||
@@ -251,3 +270,2 @@ pendings.set(1, noop); | ||
it('should resolve waitAll with empty result', function () { | ||
const pendings = new Pendings({persistent: true}); | ||
pendings.add(noop); | ||
@@ -264,3 +282,2 @@ pendings.set(1, noop); | ||
it('should resolve with empty for empty list', function () { | ||
const pendings = new Pendings(); | ||
const res = pendings.waitAll(); | ||
@@ -270,18 +287,5 @@ return assert.eventually.deepEqual(res, {resolved: {}, rejected: {}}); | ||
it('should resolve with empty result for persistent: false', function () { | ||
const pendings = new Pendings({persistent: false}); | ||
it('should resolve with resolved/rejected values', function () { | ||
pendings.set(1, noop); | ||
pendings.set(2, noop); | ||
pendings.set(3, noop).catch(() => {}); | ||
const res = pendings.waitAll(); | ||
pendings.resolve(1, 'foo'); | ||
setTimeout(() => pendings.resolve(2, 'foo2'), 10); | ||
pendings.reject(3); | ||
return assert.eventually.deepEqual(res, {resolved: {}, rejected: {}}); | ||
}); | ||
it('should resolve with resolved/rejected values for persistent: true', function () { | ||
const pendings = new Pendings({persistent: true}); | ||
pendings.set(1, noop); | ||
pendings.set(2, noop); | ||
pendings.set(3, () => Promise.resolve('bar')); | ||
@@ -306,3 +310,2 @@ pendings.set(4, noop).catch(() => {}); | ||
it('should accumulate resolved values for several calls', function () { | ||
const pendings = new Pendings({persistent: true}); | ||
pendings.set(1, noop); | ||
@@ -325,16 +328,42 @@ pendings.resolve(1, 'foo'); | ||
}); | ||
it('should resolve with empty result for autoRemove: true', function () { | ||
const pendings = new Pendings({autoRemove: true}); | ||
pendings.set(1, noop); | ||
pendings.set(2, noop); | ||
pendings.set(3, noop).catch(() => {}); | ||
const res = pendings.waitAll(); | ||
pendings.resolve(1, 'foo'); | ||
setTimeout(() => pendings.resolve(2, 'foo2'), 10); | ||
pendings.reject(3); | ||
return assert.eventually.deepEqual(res, {resolved: {}, rejected: {}}); | ||
}); | ||
}); | ||
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); | ||
it('should increase and decrease count of promises (autoRemove: true)', function () { | ||
pendings = new Pendings({autoRemove: true}); | ||
assert.equal(pendings.count, 0); | ||
pendings.set(1, noop); | ||
assert.equal(pendings.count, 1); | ||
pendings.set(2, noop).catch(() => {}); | ||
assert.equal(pendings.count, 2); | ||
pendings.resolve(1); | ||
assert.equal(pendings.count, 1); | ||
pendings.reject(2); | ||
assert.equal(pendings.count, 0); | ||
}); | ||
it('should increase count of promises (autoRemove: false)', function () { | ||
pendings = new Pendings({autoRemove: false}); | ||
assert.equal(pendings.count, 0); | ||
pendings.set(1, noop); | ||
assert.equal(pendings.count, 1); | ||
pendings.set(2, noop).catch(() => {}); | ||
assert.equal(pendings.count, 2); | ||
pendings.resolve(1); | ||
assert.equal(pendings.count, 2); | ||
pendings.reject(2); | ||
assert.equal(pendings.count, 2); | ||
}); | ||
}); | ||
@@ -344,4 +373,4 @@ | ||
it('should change generated ids', function () { | ||
this.pendings.generateId = () => 1; | ||
this.pendings.add(id => assert.equal(id, 1)); | ||
pendings.generateId = () => 1; | ||
pendings.add(id => assert.equal(id, 1)); | ||
}); | ||
@@ -352,4 +381,4 @@ }); | ||
it('should resolve before timeout', function () { | ||
const res = this.pendings.set(1, noop, {timeout: 10}); | ||
setTimeout(() => this.pendings.resolve(1, 'foo'), 5); | ||
const res = pendings.set(1, noop, {timeout: 10}); | ||
setTimeout(() => pendings.resolve(1, 'foo'), 5); | ||
return assert.eventually.equal(res, 'foo'); | ||
@@ -359,3 +388,3 @@ }); | ||
it('should reject after timeout', function () { | ||
const res = this.pendings.set(1, noop, {timeout: 5}); | ||
const res = pendings.set(1, noop, {timeout: 5}); | ||
return assert.isRejected(res, 'Promise rejected by timeout (5 ms)'); | ||
@@ -365,3 +394,3 @@ }); | ||
it('should reject after default timeout', function () { | ||
const pendings = new Pendings({timeout: 5}); | ||
pendings = new Pendings({timeout: 5}); | ||
const res = pendings.set(1, noop); | ||
@@ -372,3 +401,3 @@ return assert.isRejected(res, 'Promise rejected by timeout (5 ms)'); | ||
it('should overwrite default timeout', function () { | ||
const pendings = new Pendings({timeout: 10}); | ||
pendings = new Pendings({timeout: 10}); | ||
const res = pendings.set(1, noop, {timeout: 5}); | ||
@@ -382,3 +411,3 @@ return assert.isRejected(res, 'Promise rejected by timeout (5 ms)'); | ||
let id; | ||
const pendings = new Pendings({idPrefix: 'client1'}); | ||
pendings = new Pendings({idPrefix: 'client1'}); | ||
pendings.add(_id => id = _id); | ||
@@ -389,5 +418,5 @@ assert.equal(id.indexOf('client1'), 0); | ||
describe('options: persistent', function () { | ||
it('should not store fulfilled pendings for persistent = false', function () { | ||
const pendings = new Pendings({persistent: false}); | ||
describe('options: autoRemove', function () { | ||
it('should store fulfilled pendings for autoRemove = false (default)', function () { | ||
pendings = new Pendings({autoRemove: false}); | ||
pendings.set(1, noop); | ||
@@ -398,7 +427,7 @@ pendings.set(2, noop).catch(() => {}); | ||
pendings.reject(2); | ||
assert.equal(pendings.count, 1); | ||
assert.equal(pendings.count, 3); | ||
}); | ||
it('should store fulfilled pendings for persistent = true', function () { | ||
const pendings = new Pendings({persistent: true}); | ||
it('should not store fulfilled pendings for autoRemove = true', function () { | ||
pendings = new Pendings({autoRemove: true}); | ||
pendings.set(1, noop); | ||
@@ -409,5 +438,5 @@ pendings.set(2, noop).catch(() => {}); | ||
pendings.reject(2); | ||
assert.equal(pendings.count, 3); | ||
assert.equal(pendings.count, 1); | ||
}); | ||
}); | ||
}); |
67009
1588
437