Compare commits

...

24 Commits

Author SHA1 Message Date
97724b3f54 update description 2024-05-29 14:12:32 +02:00
df02fb8b08 update tsconfig 2024-04-14 17:28:31 +02:00
f6531310b2 update npmextra.json: githost 2024-04-01 21:34:30 +02:00
427450f52c update npmextra.json: githost 2024-04-01 19:57:59 +02:00
8bb4e9399f update npmextra.json: githost 2024-03-30 21:46:57 +01:00
7c561b2a25 3.0.5 2023-07-13 09:49:50 +02:00
de4342aa18 fix(core): update 2023-07-13 09:49:50 +02:00
eeb05aa987 3.0.4 2023-07-13 09:49:38 +02:00
1cc575d2c8 fix(core): update 2023-07-13 09:49:37 +02:00
3bcddff4ab 3.0.3 2023-07-13 09:44:41 +02:00
b7da538595 fix(core): update 2023-07-13 09:44:40 +02:00
aee7e17035 3.0.2 2023-07-10 23:13:12 +02:00
ddd6e45662 fix(core): update 2023-07-10 23:13:11 +02:00
3a9a8686a1 switch to new org scheme 2023-07-10 02:48:34 +02:00
499ae24297 3.0.1 2023-04-05 16:31:35 +02:00
820c6ccb74 fix(core): update 2023-04-05 16:31:34 +02:00
9b8198d592 3.0.0 2023-04-05 16:29:29 +02:00
1f0e574854 BREAKING CHANGE(core): update 2023-04-05 16:29:29 +02:00
9f305070eb 2.0.13 2021-06-04 15:35:42 +02:00
062f99990b fix(core): update 2021-06-04 15:35:41 +02:00
d3201b0d35 2.0.12 2021-06-04 14:11:27 +02:00
5df94778dd fix(core): update 2021-06-04 14:11:27 +02:00
2dc91dc2f0 2.0.11 2021-06-04 13:54:21 +02:00
3784de03ce fix(core): update 2021-06-04 13:54:21 +02:00
14 changed files with 4958 additions and 3516 deletions

View File

@ -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
View File

@ -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"
}
]
}

View File

@ -15,7 +15,7 @@
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm"]
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}

View File

@ -6,12 +6,27 @@
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartdelay",
"shortDescription": "timeouts for the async/await era, written in TypeScript",
"npmPackagename": "@pushrocks/smartdelay",
"license": "MIT"
"description": "A TypeScript library providing enhanced timeout functions compatible with async/await patterns.",
"npmPackagename": "@push.rocks/smartdelay",
"license": "MIT",
"keywords": [
"TypeScript",
"async/await",
"timeouts",
"delay",
"scheduling",
"task delay",
"asynchronous",
"programming utility",
"promise",
"timeout management"
]
}
},
"tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
}
}

3365
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,36 +1,35 @@
{
"name": "@pushrocks/smartdelay",
"name": "@push.rocks/smartdelay",
"private": false,
"version": "2.0.10",
"description": "timeouts for the async/await era, written in TypeScript",
"version": "3.0.5",
"description": "A TypeScript library providing enhanced timeout functions compatible with async/await patterns.",
"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",
"url": "git+ssh://git@gitlab.com/pushrocks/smartdelay.git"
"url": "https://code.foss.global/push.rocks/smartdelay.git"
},
"author": "Lossless GmbH",
"license": "MIT",
"bugs": {
"url": "https://gitlab.com/pushrocks/smartdelay/issues"
},
"homepage": "https://gitlab.com/pushrocks/smartdelay#README",
"homepage": "https://code.foss.global/push.rocks/smartdelay",
"dependencies": {
"@pushrocks/smartpromise": "^3.0.6"
"@push.rocks/smartpromise": "^4.0.2"
},
"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.66",
"@gitzone/tsbundle": "^2.0.8",
"@gitzone/tsrun": "^1.2.42",
"@gitzone/tstest": "^1.0.74",
"@push.rocks/tapbundle": "^5.0.8",
"@types/node": "^20.4.1"
},
"files": [
"ts/**/*",
@ -43,5 +42,21 @@
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
],
"type": "module",
"keywords": [
"TypeScript",
"async/await",
"timeouts",
"delay",
"scheduling",
"task delay",
"asynchronous",
"programming utility",
"promise",
"timeout management"
]
}
}

4711
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

1
readme.hints.md Normal file
View File

@ -0,0 +1 @@

134
readme.md
View File

@ -1,45 +1,117 @@
# @pushrocks/smartdelay
timeouts for the async/await era, written in TypeScript
# @push.rocks/smartdelay
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartdelay)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartdelay)
* [github.com (source mirror)](https://github.com/pushrocks/smartdelay)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartdelay/)
@push.rocks/smartdelay is a modern library designed to simplify working with timeouts in the async/await era, all while being fully written in TypeScript. This tool offers a range of functionalities that streamline the process of implementing delays and timeouts in your asynchronous JavaScript code, making it more readable and maintainable.
## Status for master
[![pipeline status](https://gitlab.com/pushrocks/smartdelay/badges/master/pipeline.svg)](https://gitlab.com/pushrocks/smartdelay/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartdelay/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartdelay/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartdelay.svg)](https://www.npmjs.com/package/@pushrocks/smartdelay)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartdelay/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartdelay)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
## Install
To integrate @push.rocks/smartdelay into your project, you can install it via npm. Run the following command in your project directory:
```bash
npm install @push.rocks/smartdelay --save
```
This command adds the package to your project's dependencies, ensuring that you can easily import and utilize smartdelay's functions in your TypeScript files.
## Usage
Use TypeScript for best in class instellisense.
@push.rocks/smartdelay simplifies the handling of timeouts within async functions, offering methods to introduce specified delays or randomized time intervals. Below are detailed examples to demonstrate its usage. These examples are crafted using ECMAScript Modules (ESM) syntax and TypeScript.
```javascript
import * as smartdelay from 'smartdelay';
### Basic Delay
(async () => {
await smartdelay.delayFor(3000); // excution will halt here 3 seconds for this function scope BUT NOT BLOCK anything else
console.log('hi there');
To introduce a basic delay in your asynchronous function, use the `delayFor` function. This method halts the execution for a specified number of milliseconds.
// You can also go random
await smartdelay.delayForRandom(2000, 6000); // this will delay exection somewhere between 2 and 6 seconds.
console.log('Yay. You did not see me coming at this exact time');
})();
```typescript
import { delayFor } from '@push.rocks/smartdelay';
async function basicDelayExample() {
console.log('Delay start');
await delayFor(3000); // Execution will pause here for 3 seconds
console.log('Delay ended');
}
basicDelayExample();
```
## Contribution
In the above example, the program prints "Delay start", waits for 3 seconds due to `delayFor`, and then prints "Delay ended".
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
### Delay with Randomization
For further information read the linked docs at the top of this readme.
For scenarios where you need a delay within a random time range, `delayForRandom` can be utilized. This introduces a non-deterministic delay duration, making it ideal for simulating real-world scenarios or for testing purposes.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
```typescript
import { delayForRandom } from '@push.rocks/smartdelay';
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)
async function randomDelayExample() {
console.log('Random delay start');
await delayForRandom(2000, 5000); // Delay execution for a random duration between 2 and 5 seconds
console.log('Random delay ended');
}
randomDelayExample();
```
This function takes two parameters: the minimum and maximum bounds (in milliseconds) for the random delay.
### Passing Through Values
Both `delayFor` and `delayForRandom` can be used to pass through values after the delay. This feature can be particularly useful when chaining asynchronous operations.
```typescript
import { delayFor } from '@push.rocks/smartdelay';
async function passThroughExample() {
const result = await delayFor(3000, 'Hello after delay');
console.log(result); // Outputs: Hello after delay
}
passThroughExample();
```
### Advanced Usage: Timeout Class
@push.rocks/smartdelay provides a `Timeout` class for more granular control over timeouts, including support for cancellation.
```typescript
import { Timeout } from '@push.rocks/smartdelay';
async function timeoutExample() {
const timeout = new Timeout<string>(5000, 'Result after 5 seconds');
// Cancel the timeout if needed
// timeout.cancel();
try {
const result = await timeout.promise;
console.log(result); // Result after 5 seconds (if not cancelled)
} catch (error) {
console.error('Timeout was cancelled', error);
}
}
timeoutExample();
```
This class allows you to programmatically cancel the timeout before it completes, providing flexibility for dynamic timeout management situations.
## Conclusion
@push.rocks/smartdelay offers a TypeScript-friendly, easy-to-use solution for managing timeouts and delays in asynchronous JavaScript. By leveraging this module, developers can write cleaner, more readable async code with minimal boilerplate. Whether you're implementing a simple delay, a random delay, or need finer control over your timeout logic, smartdelay provides the tools you need to get the job done effectively.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

View File

@ -1,8 +1,8 @@
import { expect, tap } from '@pushrocks/tapbundle';
import { expect, tap } from '@push.rocks/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
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartdelay',
version: '3.0.5',
description: 'timeouts for the async/await era, written in TypeScript'
}

View File

@ -1,33 +1,35 @@
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartpromise from '@push.rocks/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> {
@ -39,7 +41,7 @@ export class Timeout<T> {
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();
}
}
/**
@ -65,7 +70,7 @@ export class Timeout<T> {
this._cancelled = true;
clearTimeout(this._timeout);
}
public getTimeLeft() {
const result = this.started + this.timeoutInMillis - Date.now();
return result > 0 ? result : 0;

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
},
"exclude": [
"dist_*/**/*.d.ts"
]
}

View File

@ -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"
}