Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 499ae24297 | |||
| 820c6ccb74 | |||
| 9b8198d592 | |||
| 1f0e574854 | |||
| 9f305070eb | |||
| 062f99990b | |||
| d3201b0d35 | |||
| 5df94778dd | |||
| 2dc91dc2f0 | |||
| 3784de03ce | |||
| e49be654eb | |||
| 9f599e79a1 |
@@ -12,30 +12,38 @@ stages:
|
||||
- release
|
||||
- metadata
|
||||
|
||||
before_script:
|
||||
- pnpm install -g pnpm
|
||||
- pnpm install -g @shipzone/npmci
|
||||
- npmci npm prepare
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
audit:
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
auditProductionDependencies:
|
||||
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 config set registry https://registry.npmjs.org
|
||||
- npmci command pnpm audit --audit-level=high --prod
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command pnpm audit --audit-level=high --dev
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
@@ -44,28 +52,22 @@ audit:
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
- npmci npm build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
@@ -85,11 +87,12 @@ release:
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci command npm install -g typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
@@ -109,11 +112,9 @@ trigger:
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
- npmci command npm run buildDocs
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
|
||||
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@@ -2,28 +2,10 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "current file",
|
||||
"type": "node",
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"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"
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
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"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"gitrepo": "smartdelay",
|
||||
"shortDescription": "timeouts for the async/await era, written in TypeScript",
|
||||
"description": "timeouts for the async/await era, written in TypeScript",
|
||||
"npmPackagename": "@pushrocks/smartdelay",
|
||||
"license": "MIT"
|
||||
}
|
||||
|
||||
3365
package-lock.json
generated
3365
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
27
package.json
27
package.json
@@ -1,13 +1,14 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdelay",
|
||||
"private": false,
|
||||
"version": "2.0.9",
|
||||
"version": "3.0.1",
|
||||
"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 --allowimplicitany && tsbundle npm)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -20,17 +21,15 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartdelay#README",
|
||||
"dependencies": {
|
||||
"@pushrocks/smartpromise": "^3.0.6"
|
||||
"@pushrocks/smartpromise": "^4.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.24",
|
||||
"@gitzone/tsbundle": "^1.0.69",
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.28",
|
||||
"@pushrocks/tapbundle": "^3.2.1",
|
||||
"@types/node": "^14.0.5",
|
||||
"tslint": "^6.1.2",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
"@gitzone/tsbuild": "^2.1.65",
|
||||
"@gitzone/tsbundle": "^2.0.7",
|
||||
"@gitzone/tsrun": "^1.2.39",
|
||||
"@gitzone/tstest": "^1.0.74",
|
||||
"@pushrocks/tapbundle": "^5.0.4",
|
||||
"@types/node": "^18.15.11"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
@@ -43,5 +42,9 @@
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
]
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
],
|
||||
"type": "module"
|
||||
}
|
||||
|
||||
4471
pnpm-lock.yaml
generated
Normal file
4471
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
25
readme.md
25
readme.md
@@ -8,13 +8,19 @@ 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)
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -39,7 +45,6 @@ We are always happy for code contributions. If you are not the code contributing
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
||||
|
||||
20
test/test.ts
20
test/test.ts
@@ -1,8 +1,8 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
|
||||
import * as smartdelay from '../ts/index';
|
||||
import * as smartdelay from '../ts/index.js';
|
||||
|
||||
tap.test('.delayFor should delay async', async tools => {
|
||||
tap.test('.delayFor should delay async', async (tools) => {
|
||||
tools.timeout(5000);
|
||||
let timePassed = false;
|
||||
setTimeout(() => {
|
||||
@@ -10,11 +10,11 @@ tap.test('.delayFor should delay async', async tools => {
|
||||
}, 2000);
|
||||
await smartdelay.delayFor(3000).then(async () => {
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(timePassed).to.be.true;
|
||||
expect(timePassed).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
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(() => {
|
||||
@@ -24,19 +24,19 @@ tap.test('.delayForRandom should delay async for a random time period', async to
|
||||
timePassedAfter = true;
|
||||
}, 5000);
|
||||
await smartdelay.delayForRandom(3000, 4900);
|
||||
expect(timePassedBefore).to.be.true;
|
||||
expect(timePassedAfter).to.be.false;
|
||||
expect(timePassedBefore).toBeTrue();
|
||||
expect(timePassedAfter).toBeFalse();
|
||||
});
|
||||
|
||||
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 => {
|
||||
expect(stringArg).equal('heyThere');
|
||||
await smartdelay.delayFor<string>(3000, hey).then(async (stringArg) => {
|
||||
expect(stringArg).toEqual('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();
|
||||
});
|
||||
|
||||
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/smartdelay',
|
||||
version: '3.0.1',
|
||||
description: 'timeouts for the async/await era, written in TypeScript'
|
||||
}
|
||||
49
ts/index.ts
49
ts/index.ts
@@ -2,44 +2,46 @@ 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;
|
||||
|
||||
private timeoutInMillis: number;
|
||||
private started: number;
|
||||
|
||||
constructor(timeInMillisecondArg, passOn?: T) {
|
||||
constructor(timeInMillisecondArg, passOn?: T, unrefedArg = false) {
|
||||
this.timeoutInMillis = timeInMillisecondArg;
|
||||
this._deferred = smartpromise.defer<T>();
|
||||
this.promise = this._deferred.promise;
|
||||
@@ -49,6 +51,9 @@ export class Timeout<T> {
|
||||
}
|
||||
}, timeInMillisecondArg);
|
||||
this.started = Date.now();
|
||||
if (unrefedArg) {
|
||||
this.makeUnrefed();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,9 +68,9 @@ 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;
|
||||
|
||||
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
||||
17
tslint.json
17
tslint.json
@@ -1,17 +0,0 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
||||
Reference in New Issue
Block a user