@awesomeeng/awesome-utils
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -6,3 +6,3 @@ "use strict"; | ||
parserOptions: { | ||
ecmaVersion: 8 | ||
ecmaVersion: 2018 | ||
}, | ||
@@ -14,2 +14,6 @@ env: { | ||
}, | ||
globals: { | ||
Atomics: true, | ||
SharedArrayBuffer: true | ||
}, | ||
rules: { | ||
@@ -16,0 +20,0 @@ "no-self-assign": [ |
@@ -555,4 +555,5 @@ ## Classes | ||
* [PromiseUtils](#PromiseUtils) | ||
* [.sleep(ms)](#PromiseUtils+sleep) ⇒ <code>Promise</code> | ||
* [.series(array)](#PromiseUtils+series) ⇒ <code>Promise</code> | ||
* [.sleep(duration)](#PromiseUtils+sleep) ⇒ <code>Promise</code> | ||
* [.series(array, f)](#PromiseUtils+series) ⇒ <code>Promise</code> | ||
* [.timeout(promise, [ttl], [timeoutException])](#PromiseUtils+timeout) ⇒ <code>Promise</code> | ||
@@ -564,3 +565,3 @@ | ||
### promiseUtils.sleep(ms) ⇒ <code>Promise</code> | ||
### promiseUtils.sleep(duration) ⇒ <code>Promise</code> | ||
Creates a promise that resolves after n milliseconds. Great for usage | ||
@@ -573,3 +574,3 @@ with await for delaying some period of time. | ||
| --- | --- | | ||
| ms | <code>number</code> | | ||
| duration | <code>number</code> | | ||
@@ -581,8 +582,12 @@ | ||
### promiseUtils.series(array) ⇒ <code>Promise</code> | ||
Execute an array of Promises in serial order. Resolve when | ||
all have resolve. Reject when any reject. Reject will short | ||
circuit and any remaining promises after one rejects will | ||
not be executed. | ||
### promiseUtils.series(array, f) ⇒ <code>Promise</code> | ||
Execute the given function for each cell of the array, in series order. | ||
If the given function returns a Promise, the promise will await resolution | ||
before the function is called next. This creates a series execution of | ||
an array of Promise executions. On a reject, all remaining executions | ||
are skipped. | ||
The given function is called with the signature | ||
f(item,index,originalArray,resultsArray). | ||
**Kind**: instance method of [<code>PromiseUtils</code>](#PromiseUtils) | ||
@@ -593,2 +598,3 @@ | ||
| array | <code>Array.<Promise></code> | | ||
| f | <code>function</code> | | ||
@@ -598,2 +604,24 @@ | ||
<a name="PromiseUtils+timeout"></a> | ||
### promiseUtils.timeout(promise, [ttl], [timeoutException]) ⇒ <code>Promise</code> | ||
In essence this lets you wrap a promise in a timeout such that if the | ||
timeout occurs before the promise resolves or reject, this rejects. | ||
Returns a promise that will resolve/reject if the passed in promise resolves | ||
or rejects before the passed in ttl time has elapsed. If the ttl time does | ||
elsapse, the returned promise will reject (with the optional exception) and | ||
the passed in promise resolve or reject will be swallowed. | ||
**Kind**: instance method of [<code>PromiseUtils</code>](#PromiseUtils) | ||
| Param | Type | Default | | ||
| --- | --- | --- | | ||
| promise | <code>Promise</code> | | | ||
| [ttl] | <code>number</code> | <code>30000</code> | | ||
| [timeoutException] | <code>Error</code> | <code>new Error("Timed out.")</code> | | ||
* * * | ||
<a name="VMUtils"></a> | ||
@@ -600,0 +628,0 @@ |
{ | ||
"name": "@awesomeeng/awesome-utils", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"author": "the awesome engineering company", | ||
@@ -20,5 +20,7 @@ "license": "MIT", | ||
"scripts": { | ||
"docs": "jsdoc2md src/** --separators > docs/API.md" | ||
"docs": "jsdoc2md src/** --separators > docs/API.md", | ||
"test": "mocha test", | ||
"tests": "mocha test" | ||
}, | ||
"description": "Javascript Utility functions for Enterprise Ready nodejs applications", | ||
"description": "Javascript Utility functions for enterprise ready nodejs applications.", | ||
"directories": { | ||
@@ -25,0 +27,0 @@ "doc": "docs", |
# AwesomeUtils Release Notes | ||
#### **Version 1.4.0** | ||
- AwesomeUtils.Workers: Switch worker locks to use threadId instead of processId since workers use the same processId as their parents. | ||
- AwesomeUtils.Workers: Renamed lock methods. no longer require SAB for lock objects. | ||
- AwesomeUtils.Workers: Adds lock/unlock for workers and SharedArrayBuffers. | ||
- AwesomeUtils.Workers: Added. Utilities for working with worker_threads. | ||
- AwesomeUtils.Request: Adds better parsing of contentType and contentEncoding. | ||
- AwesomeUtils.Request: Use lowercase header names, more in line with how node does it. | ||
- AwesomeUtils.Sequence: Added. For generating sequence number. | ||
- AwesomeUtils.Request: Added. HTTP Request utilities. | ||
- AwesomeUtils.Random: Fixed typo in names(). | ||
- AwesomeUtils.Compartor: Added. Utilities for sorting. | ||
- AwesomeUtils.Promise: Adds timeout(). | ||
#### **Version 1.3.0** | ||
- Adds Object.deepStrictEqual(). | ||
- Fixes Promise.series() to work as expected. Prior to this the promises would each start when created, and then series() was just waiting on them resolving. Now you pass series an array and a function to execute for each cell in the array. If the function returns a promise, series() waits for that promise to resolve before moving to the next item in the series. This is a change to how series() is structured. | ||
- Adds Random.uuid(). | ||
- Adds tests for Promise.sleep() and Promise.series(). | ||
- AwesomeUtils.Object: Adds Object.deepStrictEqual(). | ||
- AwesomeUtils.Promise: Fixes Promise.series() to work as expected. Prior to this the promises would each start when created, and then series() was just waiting on them resolving. Now you pass series an array and a function to execute for each cell in the array. If the function returns a promise, series() waits for that promise to resolve before moving to the next item in the series. This is a change to how series() is structured. | ||
- AwesomeUtils.Random: Adds Random.uuid(). | ||
- AwesomeUtils.Promise: Adds tests for Promise.sleep() and Promise.series(). | ||
#### **Version 1.2.0** | ||
- Splits the Module.stack() call up into module.moduleStack() and VM.executionStack() as these are different things. | ||
- AwesomeUtils.Module/AwesomeUtils.VM: Splits the Module.stack() call up into module.moduleStack() and VM.executionStack() as these are different things. | ||
#### **Version 1.1.2** | ||
- Make Object.deepFreeze() try not to freeze primatives. | ||
- AwesomeUtils.Object: Make Object.deepFreeze() try not to freeze primatives. | ||
#### **Version 1.1.1** | ||
- Adds Object.deepFreeze(). | ||
- AwesomeUtils.Object: Adds Object.deepFreeze(). | ||
@@ -22,0 +36,0 @@ #### **Version 1.1.0** |
@@ -5,2 +5,3 @@ module.exports = { | ||
Class: require("./Class.js"), | ||
Comparator: require("./Comparator.js"), | ||
Date: require("./Date.js"), | ||
@@ -14,3 +15,6 @@ FS: require("./FS.js"), | ||
Random: require("./Random.js"), | ||
VM: require("./VM.js") | ||
Request: require("./Request.js"), | ||
Sequence: require("./Sequence.js"), | ||
VM: require("./VM.js"), | ||
Workers: require("./Workers.js") | ||
}; |
// (c) 2018, The Awesome Engineering Company, https://awesomeneg.com | ||
"use strict"; | ||
const $TIMEOUTS = Symbol("timeouts"); | ||
const $TIMEOUTTIMER = Symbol("timeoutTimer"); | ||
/** | ||
@@ -8,2 +12,7 @@ * Utilities for dealing with Promises. | ||
class PromiseUtils { | ||
constructor() { | ||
this[$TIMEOUTS] = new Set(); | ||
this[$TIMEOUTTIMER] = null; | ||
} | ||
/** | ||
@@ -79,4 +88,43 @@ * Creates a promise that resolves after n milliseconds. Great for usage | ||
} | ||
/** | ||
* In essence this lets you wrap a promise in a timeout such that if the | ||
* timeout occurs before the promise resolves or reject, this rejects. | ||
* | ||
* Returns a promise that will resolve/reject if the passed in promise resolves | ||
* or rejects before the passed in ttl time has elapsed. If the ttl time does | ||
* elsapse, the returned promise will reject (with the optional exception) and | ||
* the passed in promise resolve or reject will be swallowed. | ||
* | ||
* @param {Promise} promise | ||
* @param {number} [ttl=30000] | ||
* @param {Error} [timeoutException=new Error("Timed out.")] | ||
* @return {Promise} | ||
*/ | ||
timeout(promise,ttl=30000,timeoutException=new Error("Timed out.")) { | ||
if (!promise) throw new Error("Missing promise."); | ||
if (!(promise instanceof Promise)) throw new Error("Invalid promise."); | ||
if (!ttl) throw new Error("Missing ttl."); | ||
if (typeof ttl!=="number") throw new Error("Invalid ttl."); | ||
return new Promise((resolve,reject)=>{ | ||
let timedOut = false; | ||
let timer = setTimeout(()=>{ | ||
timedOut = true; | ||
reject(timeoutException); | ||
},ttl); | ||
promise.then((result)=>{ | ||
if (timedOut) return; | ||
clearTimeout(timer); | ||
resolve(result); | ||
}); | ||
promise.catch((err)=>{ | ||
if (timedOut) return; | ||
clearTimeout(timer); | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
} | ||
module.exports = new PromiseUtils(); |
@@ -54,3 +54,3 @@ // (c) 2018, The Awesome Engineering Company, https://awesomeneg.com | ||
"AspenMountain", | ||
"AtlanticPea", | ||
"AtlanticPeak", | ||
"BadgerMountain", | ||
@@ -64,3 +64,3 @@ "BaldMountain", | ||
"BisonMountain", | ||
"BlancaPea", | ||
"BlancaPeak", | ||
"BlairMountain", | ||
@@ -75,3 +75,3 @@ "BlancaPeak", | ||
"CapitolPeak", | ||
"CastlePea", | ||
"CastlePeak", | ||
"CarbonPeak", | ||
@@ -93,3 +93,3 @@ "CarbonateMountain", | ||
"ConundrumPeak", | ||
"CrestonePea", | ||
"CrestonePeak", | ||
"CraterPeak", | ||
@@ -119,3 +119,3 @@ "CrestedButte", | ||
"ElliottMountain", | ||
"FishersPea", | ||
"FishersPeak", | ||
"EmeraldPeak", | ||
@@ -130,6 +130,6 @@ "EngelmannPeak", | ||
"GarfieldPeak", | ||
"GeminiPea", | ||
"GeminiPeak", | ||
"GilpinPeak", | ||
"GladstonePeak", | ||
"GraysPea", | ||
"GraysPeak", | ||
"GoatHill", | ||
@@ -162,3 +162,3 @@ "GothicMountain", | ||
"IceMountain", | ||
"IowaPea", | ||
"IowaPeak", | ||
"IronMountain", | ||
@@ -170,3 +170,3 @@ "ItalianMountain", | ||
"JonesMountain", | ||
"LaPlataPea", | ||
"LaPlataPeak", | ||
"JupiterMountain", | ||
@@ -182,3 +182,3 @@ "KitCarsonMountain", | ||
"LizardHead", | ||
"LongsPea", | ||
"LongsPeak", | ||
"LoneCone", | ||
@@ -283,3 +283,3 @@ "LoneEaglePeak", | ||
"PetitGrepon", | ||
"PikesPea", | ||
"PikesPeak", | ||
"PettingellPeak", | ||
@@ -340,3 +340,3 @@ "PhoenixPeak", | ||
"TowerMountain", | ||
"TraverPea", | ||
"TraverPeak", | ||
"TreasureMountain", | ||
@@ -343,0 +343,0 @@ "TrinityPeak", |
@@ -15,13 +15,25 @@ // (c) 2018, The Awesome Engineering Company, https://awesomeneg.com | ||
it("sleep",async function(){ | ||
this.slow(250); | ||
let start,spent; | ||
assert.throws(()=>{ | ||
PromiseUtils.sleep(); | ||
}); | ||
assert.throws(()=>{ | ||
PromiseUtils.sleep(null); | ||
}); | ||
assert.throws(()=>{ | ||
PromiseUtils.sleep("asdf"); | ||
}); | ||
start = Date.now(); | ||
await PromiseUtils.sleep(25); | ||
await PromiseUtils.sleep(10); | ||
spent = Date.now()-start; | ||
assert(spent>=25); | ||
assert(spent>=10); | ||
start = Date.now(); | ||
await PromiseUtils.sleep(0); | ||
await PromiseUtils.sleep(1); | ||
spent = Date.now()-start; | ||
assert(spent<5); | ||
assert(spent<25); | ||
@@ -31,16 +43,9 @@ start = Date.now(); | ||
spent = Date.now()-start; | ||
assert(spent<5); | ||
assert(spent<25); | ||
assert.throws(()=>{ | ||
PromiseUtils.sleep(); | ||
}); | ||
assert.throws(()=>{ | ||
PromiseUtils.sleep(null); | ||
}); | ||
assert.throws(()=>{ | ||
PromiseUtils.sleep("asdf"); | ||
}); | ||
}); | ||
it("series",async function(){ | ||
this.slow(250); | ||
let x = 2; | ||
@@ -75,4 +80,26 @@ let answer = await PromiseUtils.series([2,3,4,5],(y)=>{ | ||
it("timeout",async function(){ | ||
this.slow(250); | ||
try { | ||
await PromiseUtils.timeout(PromiseUtils.sleep(50),5); | ||
assert.fail("Promise should not resolve, timeout should have occured."); | ||
} | ||
catch (ex) { | ||
assert(true); | ||
} | ||
try { | ||
await PromiseUtils.timeout(PromiseUtils.sleep(5),50); | ||
assert(true); | ||
} | ||
catch (ex) { | ||
console.log(ex); | ||
assert.fail("Promise should resolve before timeout."); | ||
} | ||
}); | ||
}); |
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
Network access
Supply chain riskThis module accesses the network.
Found 2 instances 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
110010
28
2766
3