Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9f305070eb | |||
| 062f99990b | |||
| d3201b0d35 | |||
| 5df94778dd | |||
| 2dc91dc2f0 | |||
| 3784de03ce | |||
| e49be654eb | |||
| 9f599e79a1 | |||
| 3821799070 | |||
| 80de670cad |
@@ -19,23 +19,36 @@ mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
audit:
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --production --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
@@ -50,9 +63,7 @@ testStable:
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
@@ -63,9 +74,7 @@ testBuild:
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
@@ -85,6 +94,8 @@ release:
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
|
||||
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@@ -15,7 +15,7 @@
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm"]
|
||||
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24575
package-lock.json
generated
24575
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdelay",
|
||||
"private": false,
|
||||
"version": "2.0.8",
|
||||
"version": "2.0.13",
|
||||
"description": "timeouts for the async/await era, written in TypeScript",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild && tsbundle npm)"
|
||||
"build": "(tsbuild --web && tsbundle npm)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -28,7 +28,7 @@
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.28",
|
||||
"@pushrocks/tapbundle": "^3.2.1",
|
||||
"@types/node": "^14.0.5",
|
||||
"@types/node": "^15.12.0",
|
||||
"tslint": "^6.1.2",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
@@ -43,5 +43,8 @@
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
}
|
||||
|
||||
21
readme.md
21
readme.md
@@ -8,13 +8,20 @@ timeouts for the async/await era, written in TypeScript
|
||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartdelay/)
|
||||
|
||||
## Status for master
|
||||
[](https://gitlab.com/pushrocks/smartdelay/commits/master)
|
||||
[](https://gitlab.com/pushrocks/smartdelay/commits/master)
|
||||
[](https://www.npmjs.com/package/@pushrocks/smartdelay)
|
||||
[](https://snyk.io/test/npm/@pushrocks/smartdelay)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://prettier.io/)
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
10
test/test.ts
10
test/test.ts
@@ -2,7 +2,7 @@ import { expect, tap } from '@pushrocks/tapbundle';
|
||||
|
||||
import * as smartdelay from '../ts/index';
|
||||
|
||||
tap.test('.delayFor should delay async', async tools => {
|
||||
tap.test('.delayFor should delay async', async (tools) => {
|
||||
tools.timeout(5000);
|
||||
let timePassed = false;
|
||||
setTimeout(() => {
|
||||
@@ -14,7 +14,7 @@ tap.test('.delayFor should delay async', async tools => {
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('.delayForRandom should delay async for a random time period', async tools => {
|
||||
tap.test('.delayForRandom should delay async for a random time period', async (tools) => {
|
||||
let timePassedBefore = false;
|
||||
let timePassedAfter = false;
|
||||
setTimeout(() => {
|
||||
@@ -28,14 +28,14 @@ tap.test('.delayForRandom should delay async for a random time period', async to
|
||||
expect(timePassedAfter).to.be.false;
|
||||
});
|
||||
|
||||
tap.test('.delayFor should pass on a type', async tools => {
|
||||
tap.test('.delayFor should pass on a type', async (tools) => {
|
||||
tools.timeout(5000);
|
||||
let timePassed = false;
|
||||
setTimeout(() => {
|
||||
timePassed = true;
|
||||
}, 2000);
|
||||
let hey = 'heyThere';
|
||||
await smartdelay.delayFor<string>(3000, hey).then(async stringArg => {
|
||||
await smartdelay.delayFor<string>(3000, hey).then(async (stringArg) => {
|
||||
expect(stringArg).equal('heyThere');
|
||||
});
|
||||
});
|
||||
@@ -45,7 +45,7 @@ tap.test('smartdelay.Timeout', async () => {
|
||||
await timeout.promise;
|
||||
});
|
||||
|
||||
tap.test('smartdelay.Timeout should cancel', async tools => {
|
||||
tap.test('smartdelay.Timeout should cancel', async (tools) => {
|
||||
let timeout = new smartdelay.Timeout(60000);
|
||||
timeout.cancel();
|
||||
});
|
||||
|
||||
57
ts/index.ts
57
ts/index.ts
@@ -2,41 +2,47 @@ import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
/**
|
||||
* delay something, works like setTimeout
|
||||
* @param timeInMillisecond
|
||||
* @param passOn
|
||||
* @param timeInMillisecondArg
|
||||
* @param passOnArg
|
||||
*/
|
||||
export let delayFor = async <T>(timeInMillisecond: number, passOn?: T) => {
|
||||
await new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, timeInMillisecond);
|
||||
});
|
||||
return passOn;
|
||||
export let delayFor = async <T>(
|
||||
timeInMillisecondArg: number,
|
||||
passOnArg?: T,
|
||||
unrefedArg = false
|
||||
) => {
|
||||
const timeout = new Timeout(timeInMillisecondArg, null, unrefedArg);
|
||||
await timeout.promise;
|
||||
return passOnArg;
|
||||
};
|
||||
|
||||
/**
|
||||
* delay for a random time
|
||||
*/
|
||||
export let delayForRandom = async <T>(
|
||||
timeMinInMillisecond: number,
|
||||
timeMaxInMillisecond: number,
|
||||
passOn?: T
|
||||
timeMinInMillisecondArg: number,
|
||||
timeMaxInMillisecondArg: number,
|
||||
passOnArg?: T,
|
||||
unrefedArg = false
|
||||
) => {
|
||||
await new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, Math.random() * (timeMaxInMillisecond - timeMinInMillisecond) + timeMinInMillisecond);
|
||||
});
|
||||
return passOn;
|
||||
await delayFor(
|
||||
Math.random() * (timeMaxInMillisecondArg - timeMinInMillisecondArg) + timeMinInMillisecondArg,
|
||||
null,
|
||||
unrefedArg
|
||||
);
|
||||
return passOnArg;
|
||||
};
|
||||
|
||||
export class Timeout<T> {
|
||||
promise: Promise<T>;
|
||||
private _deferred: smartpromise.Deferred<T>;
|
||||
private _timeout: any;
|
||||
private _timeout;
|
||||
private _cancelled: boolean = false;
|
||||
|
||||
constructor(timeInMillisecondArg, passOn?: T) {
|
||||
private timeoutInMillis: number;
|
||||
private started: number;
|
||||
|
||||
constructor(timeInMillisecondArg, passOn?: T, unrefedArg = false) {
|
||||
this.timeoutInMillis = timeInMillisecondArg;
|
||||
this._deferred = smartpromise.defer<T>();
|
||||
this.promise = this._deferred.promise;
|
||||
this._timeout = setTimeout(() => {
|
||||
@@ -44,6 +50,10 @@ export class Timeout<T> {
|
||||
this._deferred.resolve(passOn);
|
||||
}
|
||||
}, timeInMillisecondArg);
|
||||
this.started = Date.now();
|
||||
if (unrefedArg) {
|
||||
this.makeUnrefed();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,6 +68,11 @@ export class Timeout<T> {
|
||||
*/
|
||||
public cancel() {
|
||||
this._cancelled = true;
|
||||
this.makeUnrefed();
|
||||
clearTimeout(this._timeout);
|
||||
}
|
||||
|
||||
public getTimeLeft() {
|
||||
const result = this.started + this.timeoutInMillis - Date.now();
|
||||
return result > 0 ? result : 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user