Compare commits
42 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| aee7e17035 | |||
| ddd6e45662 | |||
| 3a9a8686a1 | |||
| 499ae24297 | |||
| 820c6ccb74 | |||
| 9b8198d592 | |||
| 1f0e574854 | |||
| 9f305070eb | |||
| 062f99990b | |||
| d3201b0d35 | |||
| 5df94778dd | |||
| 2dc91dc2f0 | |||
| 3784de03ce | |||
| e49be654eb | |||
| 9f599e79a1 | |||
| 3821799070 | |||
| 80de670cad | |||
| 2d64c43b55 | |||
| 0da614f1c0 | |||
| 7daefcac11 | |||
| 912bdc61fb | |||
| 4b5581a6f1 | |||
| 4aa3d18219 | |||
| 21f3a1dc59 | |||
| 69d9881215 | |||
| f218d24c97 | |||
| 29983e44fa | |||
| a99767986d | |||
| 647a9ab4cb | |||
| ffb4d1c29e | |||
| f4480aa289 | |||
| f952c71e2d | |||
| 4edbd04c77 | |||
| 9acff87870 | |||
| 767b3d6e42 | |||
| deb8f2004d | |||
| 62a5975fb3 | |||
| 645a1ca111 | |||
| e67b148c41 | |||
| f462bb1641 | |||
| 553bfa2735 | |||
| 75358d0b66 |
20
.gitignore
vendored
20
.gitignore
vendored
@@ -1,4 +1,20 @@
|
||||
node_modules/
|
||||
docs/
|
||||
.nogit/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_*/
|
||||
|
||||
# custom
|
||||
105
.gitlab-ci.yml
105
.gitlab-ci.yml
@@ -1,59 +1,128 @@
|
||||
image: hosttoday/ht-docker-node:npmts
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- test
|
||||
- release
|
||||
- trigger
|
||||
- pages
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
testLEGACY:
|
||||
stage: test
|
||||
before_script:
|
||||
- pnpm install -g pnpm
|
||||
- pnpm install -g @shipzone/npmci
|
||||
- npmci npm prepare
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci test legacy
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command pnpm audit --audit-level=high --prod
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
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
|
||||
# ====================
|
||||
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test lts
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
testSTABLE:
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test stable
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci publish
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: trigger
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmpage
|
||||
stage: pages
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npmpage --publish gitlab
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run buildDocs
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
- public
|
||||
allow_failure: true
|
||||
|
||||
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"request": "launch",
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
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", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
33
README.md
33
README.md
@@ -1,33 +0,0 @@
|
||||
# smartdelay
|
||||
timeouts for the async/await era, written in TypeScript
|
||||
|
||||
## Availabililty
|
||||
[](https://www.npmjs.com/package/smartdelay)
|
||||
[](https://GitLab.com/pushrocks/smartdelay)
|
||||
[](https://github.com/pushrocks/smartdelay)
|
||||
[](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/smartdelay)
|
||||
[](https://david-dm.org/pushrocks/smartdelay)
|
||||
[](https://www.bithound.io/github/pushrocks/smartdelay/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartdelay)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
## Usage
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
```javascript
|
||||
import * as smartdelay from 'smartdelay'
|
||||
|
||||
(async () => {
|
||||
await smartdelay.delayFor('3000') // excution will halt here 3 seconds for this function scope BUT NOT BLOCK anything else
|
||||
console.log()
|
||||
})()
|
||||
```
|
||||
|
||||
[](https://push.rocks)
|
||||
2
dist/index.d.ts
vendored
2
dist/index.d.ts
vendored
@@ -1,2 +0,0 @@
|
||||
import 'typings-global';
|
||||
export declare let delayFor: <T>(timeInMillisecond: number, passOn?: T) => Promise<T>;
|
||||
19
dist/index.js
vendored
19
dist/index.js
vendored
@@ -1,19 +0,0 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
require("typings-global");
|
||||
exports.delayFor = (timeInMillisecond, passOn) => __awaiter(this, void 0, void 0, function* () {
|
||||
yield new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, timeInMillisecond);
|
||||
});
|
||||
return passOn;
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFBQSwwQkFBdUI7QUFFWixRQUFBLFFBQVEsR0FBRyxDQUFVLGlCQUF5QixFQUFFLE1BQVU7SUFDakUsTUFBTSxJQUFJLE9BQU8sQ0FBQyxDQUFDLE9BQU8sRUFBRSxNQUFNO1FBQzlCLFVBQVUsQ0FDTjtZQUNJLE9BQU8sRUFBRSxDQUFBO1FBQ2IsQ0FBQyxFQUNELGlCQUFpQixDQUNwQixDQUFBO0lBQ0wsQ0FBQyxDQUFDLENBQUE7SUFDRixNQUFNLENBQUMsTUFBTSxDQUFBO0FBQ2pCLENBQUMsQ0FBQSxDQUFBIn0=
|
||||
17
npmextra.json
Normal file
17
npmextra.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "smartdelay",
|
||||
"description": "timeouts for the async/await era, written in TypeScript",
|
||||
"npmPackagename": "@push.rocks/smartdelay",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
||||
42
package.json
42
package.json
@@ -1,11 +1,14 @@
|
||||
{
|
||||
"name": "smartdelay",
|
||||
"version": "1.0.1",
|
||||
"name": "@push.rocks/smartdelay",
|
||||
"private": false,
|
||||
"version": "3.0.2",
|
||||
"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": "(npmts)"
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -18,11 +21,30 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartdelay#README",
|
||||
"dependencies": {
|
||||
"typings-global": "^1.0.14"
|
||||
"@pushrocks/smartpromise": "^4.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/should": "^8.1.30",
|
||||
"should": "^11.1.2",
|
||||
"typings-test": "^1.0.3"
|
||||
}
|
||||
"@gitzone/tsbuild": "^2.1.66",
|
||||
"@gitzone/tsbundle": "^2.0.8",
|
||||
"@gitzone/tsrun": "^1.2.42",
|
||||
"@gitzone/tstest": "^1.0.74",
|
||||
"@pushrocks/tapbundle": "^5.0.8",
|
||||
"@types/node": "^20.4.1"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
],
|
||||
"type": "module"
|
||||
}
|
||||
|
||||
4690
pnpm-lock.yaml
generated
Normal file
4690
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
50
readme.md
Normal file
50
readme.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# @pushrocks/smartdelay
|
||||
timeouts for the async/await era, written in TypeScript
|
||||
|
||||
## 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/)
|
||||
|
||||
## Status for master
|
||||
|
||||
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
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
```javascript
|
||||
import * as smartdelay from 'smartdelay';
|
||||
|
||||
(async () => {
|
||||
await smartdelay.delayFor(3000); // excution will halt here 3 seconds for this function scope BUT NOT BLOCK anything else
|
||||
console.log('hi there');
|
||||
|
||||
// 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');
|
||||
})();
|
||||
```
|
||||
|
||||
## Contribution
|
||||
|
||||
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). :)
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
## 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)
|
||||
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@@ -1 +0,0 @@
|
||||
import 'typings-test';
|
||||
30
test/test.js
30
test/test.js
@@ -1,30 +0,0 @@
|
||||
"use strict";
|
||||
require("typings-test");
|
||||
const should = require("should");
|
||||
const smartdelay = require("../dist/index");
|
||||
describe('smartdelay', function () {
|
||||
it('.delayFor should delay async', function (done) {
|
||||
this.timeout(5000);
|
||||
let timePassed = false;
|
||||
setTimeout(() => {
|
||||
timePassed = true;
|
||||
}, 2000);
|
||||
smartdelay.delayFor(3000).then(() => {
|
||||
should(timePassed).be.true();
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('.delayFor should pass on a type', function (done) {
|
||||
this.timeout(5000);
|
||||
let timePassed = false;
|
||||
setTimeout(() => {
|
||||
timePassed = true;
|
||||
}, 2000);
|
||||
let hey = 'heyThere';
|
||||
smartdelay.delayFor(3000, hey).then((stringArg) => {
|
||||
should(stringArg).equal('heyThere');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQixpQ0FBZ0M7QUFFaEMsNENBQTJDO0FBRTNDLFFBQVEsQ0FBQyxZQUFZLEVBQUU7SUFDbkIsRUFBRSxDQUFDLDhCQUE4QixFQUFFLFVBQVMsSUFBSTtRQUM1QyxJQUFJLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxDQUFBO1FBQ2xCLElBQUksVUFBVSxHQUFHLEtBQUssQ0FBQTtRQUN0QixVQUFVLENBQUM7WUFDUCxVQUFVLEdBQUcsSUFBSSxDQUFBO1FBQ3JCLENBQUMsRUFBQyxJQUFJLENBQUMsQ0FBQTtRQUNQLFVBQVUsQ0FBQyxRQUFRLENBQUMsSUFBSSxDQUFDLENBQUMsSUFBSSxDQUFDO1lBQzNCLE1BQU0sQ0FBQyxVQUFVLENBQUMsQ0FBQyxFQUFFLENBQUMsSUFBSSxFQUFFLENBQUE7WUFDNUIsSUFBSSxFQUFFLENBQUE7UUFDVixDQUFDLENBQUMsQ0FBQTtJQUNOLENBQUMsQ0FBQyxDQUFBO0lBQ0YsRUFBRSxDQUFDLGlDQUFpQyxFQUFFLFVBQVMsSUFBSTtRQUMvQyxJQUFJLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxDQUFBO1FBQ2xCLElBQUksVUFBVSxHQUFHLEtBQUssQ0FBQTtRQUN0QixVQUFVLENBQUM7WUFDUCxVQUFVLEdBQUcsSUFBSSxDQUFBO1FBQ3JCLENBQUMsRUFBQyxJQUFJLENBQUMsQ0FBQTtRQUNQLElBQUksR0FBRyxHQUFHLFVBQVUsQ0FBQTtRQUNwQixVQUFVLENBQUMsUUFBUSxDQUFTLElBQUksRUFBRSxHQUFHLENBQUMsQ0FBQyxJQUFJLENBQUMsQ0FBQyxTQUFTO1lBQ2xELE1BQU0sQ0FBQyxTQUFTLENBQUMsQ0FBQyxLQUFLLENBQUMsVUFBVSxDQUFDLENBQUE7WUFDbkMsSUFBSSxFQUFFLENBQUE7UUFDVixDQUFDLENBQUMsQ0FBQTtJQUNOLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==
|
||||
78
test/test.ts
78
test/test.ts
@@ -1,31 +1,53 @@
|
||||
import 'typings-test'
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
|
||||
import * as should from 'should'
|
||||
import * as smartdelay from '../ts/index.js';
|
||||
|
||||
import * as smartdelay from '../dist/index'
|
||||
tap.test('.delayFor should delay async', async (tools) => {
|
||||
tools.timeout(5000);
|
||||
let timePassed = false;
|
||||
setTimeout(() => {
|
||||
timePassed = true;
|
||||
}, 2000);
|
||||
await smartdelay.delayFor(3000).then(async () => {
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(timePassed).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('smartdelay', function () {
|
||||
it('.delayFor should delay async', function(done) {
|
||||
this.timeout(5000)
|
||||
let timePassed = false
|
||||
setTimeout(() => {
|
||||
timePassed = true
|
||||
},2000)
|
||||
smartdelay.delayFor(3000).then(() => {
|
||||
should(timePassed).be.true()
|
||||
done()
|
||||
})
|
||||
})
|
||||
it('.delayFor should pass on a type', function(done) {
|
||||
this.timeout(5000)
|
||||
let timePassed = false
|
||||
setTimeout(() => {
|
||||
timePassed = true
|
||||
},2000)
|
||||
let hey = 'heyThere'
|
||||
smartdelay.delayFor<string>(3000, hey).then((stringArg) => {
|
||||
should(stringArg).equal('heyThere')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
tap.test('.delayForRandom should delay async for a random time period', async (tools) => {
|
||||
let timePassedBefore = false;
|
||||
let timePassedAfter = false;
|
||||
setTimeout(() => {
|
||||
timePassedBefore = true;
|
||||
}, 3000);
|
||||
setTimeout(() => {
|
||||
timePassedAfter = true;
|
||||
}, 5000);
|
||||
await smartdelay.delayForRandom(3000, 4900);
|
||||
expect(timePassedBefore).toBeTrue();
|
||||
expect(timePassedAfter).toBeFalse();
|
||||
});
|
||||
|
||||
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).toEqual('heyThere');
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('smartdelay.Timeout', async () => {
|
||||
let timeout = new smartdelay.Timeout(2000);
|
||||
await timeout.promise;
|
||||
});
|
||||
|
||||
tap.test('smartdelay.Timeout should cancel', async (tools) => {
|
||||
let timeout = new smartdelay.Timeout(60000);
|
||||
timeout.cancel();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
||||
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: '@push.rocks/smartdelay',
|
||||
version: '3.0.2',
|
||||
description: 'timeouts for the async/await era, written in TypeScript'
|
||||
}
|
||||
87
ts/index.ts
87
ts/index.ts
@@ -1,13 +1,78 @@
|
||||
import 'typings-global'
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
export let delayFor = async <T>(timeInMillisecond: number, passOn?: T) => {
|
||||
await new Promise((resolve, reject) => {
|
||||
setTimeout(
|
||||
() => {
|
||||
resolve()
|
||||
},
|
||||
timeInMillisecond
|
||||
)
|
||||
})
|
||||
return passOn
|
||||
/**
|
||||
* delay something, works like setTimeout
|
||||
* @param timeInMillisecondArg
|
||||
* @param passOnArg
|
||||
*/
|
||||
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>(
|
||||
timeMinInMillisecondArg: number,
|
||||
timeMaxInMillisecondArg: number,
|
||||
passOnArg?: T,
|
||||
unrefedArg = false
|
||||
) => {
|
||||
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;
|
||||
private _cancelled: boolean = false;
|
||||
|
||||
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(() => {
|
||||
if (!this._cancelled) {
|
||||
this._deferred.resolve(passOn);
|
||||
}
|
||||
}, timeInMillisecondArg);
|
||||
this.started = Date.now();
|
||||
if (unrefedArg) {
|
||||
this.makeUnrefed();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* unreffing a timeout causes the node process to not wait for completion before exit
|
||||
*/
|
||||
public makeUnrefed() {
|
||||
this._timeout.unref();
|
||||
}
|
||||
|
||||
/**
|
||||
* cancels the timer
|
||||
*/
|
||||
public cancel() {
|
||||
this._cancelled = true;
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
}
|
||||
Reference in New Issue
Block a user