Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2dc91dc2f0 | |||
| 3784de03ce | |||
| e49be654eb | |||
| 9f599e79a1 | |||
| 3821799070 | |||
| 80de670cad | |||
| 2d64c43b55 | |||
| 0da614f1c0 | |||
| 7daefcac11 | |||
| 912bdc61fb |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -15,8 +15,6 @@ node_modules/
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
dist_*/
|
||||
|
||||
# custom
|
||||
@@ -24,13 +24,14 @@ mirror:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
audit:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
|
||||
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "current file",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"${relativeFile}"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
},
|
||||
{
|
||||
"name": "test.ts",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"test/test.ts"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
}
|
||||
]
|
||||
}
|
||||
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/npmextra.json"],
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"npmci": {
|
||||
"type": "object",
|
||||
"description": "settings for npmci"
|
||||
},
|
||||
"gitzone": {
|
||||
"type": "object",
|
||||
"description": "settings for gitzone",
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
|
||||
25202
package-lock.json
generated
25202
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
20
package.json
20
package.json
@@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdelay",
|
||||
"private": false,
|
||||
"version": "2.0.6",
|
||||
"version": "2.0.11",
|
||||
"description": "timeouts for the async/await era, written in TypeScript",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)"
|
||||
"build": "(tsbuild && tsbundle npm)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -23,19 +23,21 @@
|
||||
"@pushrocks/smartpromise": "^3.0.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tsbuild": "^2.1.24",
|
||||
"@gitzone/tsbundle": "^1.0.69",
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.28",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^12.12.5",
|
||||
"tslint": "^5.20.0",
|
||||
"@pushrocks/tapbundle": "^3.2.1",
|
||||
"@types/node": "^15.12.0",
|
||||
"tslint": "^6.1.2",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_web/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
|
||||
@@ -8,7 +8,7 @@ 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://gitlab.com/pushrocks/smartdelay/commits/master)
|
||||
[](https://www.npmjs.com/package/@pushrocks/smartdelay)
|
||||
[](https://snyk.io/test/npm/@pushrocks/smartdelay)
|
||||
60
ts/index.ts
60
ts/index.ts
@@ -2,40 +2,39 @@ 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(() => {
|
||||
@@ -43,14 +42,29 @@ export class Timeout<T> {
|
||||
this._deferred.resolve(passOn);
|
||||
}
|
||||
}, timeInMillisecondArg);
|
||||
this.started = Date.now();
|
||||
if (unrefedArg) {
|
||||
this.makeUnrefed();
|
||||
}
|
||||
}
|
||||
|
||||
makeUnrefed() {
|
||||
/**
|
||||
* unreffing a timeout causes the node process to not wait for completion before exit
|
||||
*/
|
||||
public makeUnrefed() {
|
||||
this._timeout.unref();
|
||||
}
|
||||
|
||||
cancel() {
|
||||
/**
|
||||
* cancels the timer
|
||||
*/
|
||||
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