Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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/
|
.nogit/
|
||||||
docs/
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
public/
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_*/
|
||||||
|
|
||||||
|
# custom
|
||||||
112
.gitlab-ci.yml
112
.gitlab-ci.yml
@@ -1,59 +1,127 @@
|
|||||||
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:
|
stages:
|
||||||
- test
|
- security
|
||||||
- release
|
- test
|
||||||
- trigger
|
- release
|
||||||
- pages
|
- metadata
|
||||||
|
|
||||||
testLEGACY:
|
# ====================
|
||||||
|
# security stage
|
||||||
|
# ====================
|
||||||
|
mirror:
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci git mirror
|
||||||
|
tags:
|
||||||
|
- lossless
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
audit:
|
||||||
|
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
|
||||||
|
tags:
|
||||||
|
- lossless
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# test stage
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test legacy
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
allow_failure: true
|
- priv
|
||||||
|
|
||||||
testLTS:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test lts
|
- npmci npm prepare
|
||||||
tags:
|
- npmci node install stable
|
||||||
- docker
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
testSTABLE:
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci test stable
|
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci publish
|
- npmci node install stable
|
||||||
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# metadata stage
|
||||||
|
# ====================
|
||||||
|
codequality:
|
||||||
|
stage: metadata
|
||||||
|
allow_failure: true
|
||||||
|
script:
|
||||||
|
- npmci command npm install -g tslint typescript
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
|
tags:
|
||||||
|
- lossless
|
||||||
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
stage: trigger
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci trigger
|
- npmci trigger
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmpage
|
stage: metadata
|
||||||
stage: pages
|
|
||||||
script:
|
script:
|
||||||
- npmci command npmpage --publish gitlab
|
- npmci node install lts
|
||||||
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command tsdoc
|
||||||
|
tags:
|
||||||
|
- lossless
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
|||||||
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"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
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": "pushrocks",
|
||||||
|
"gitrepo": "smartdelay",
|
||||||
|
"shortDescription": "timeouts for the async/await era, written in TypeScript",
|
||||||
|
"npmPackagename": "@pushrocks/smartdelay",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3365
package-lock.json
generated
Normal file
3365
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
39
package.json
39
package.json
@@ -1,11 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "smartdelay",
|
"name": "@pushrocks/smartdelay",
|
||||||
"version": "1.0.1",
|
"private": false,
|
||||||
|
"version": "2.0.7",
|
||||||
"description": "timeouts for the async/await era, written in TypeScript",
|
"description": "timeouts for the async/await era, written in TypeScript",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(npmts)"
|
"test": "(tstest test/)",
|
||||||
|
"build": "(tsbuild && tsbundle npm)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -18,11 +20,28 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartdelay#README",
|
"homepage": "https://gitlab.com/pushrocks/smartdelay#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"typings-global": "^1.0.14"
|
"@pushrocks/smartpromise": "^3.0.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/should": "^8.1.30",
|
"@gitzone/tsbuild": "^2.1.24",
|
||||||
"should": "^11.1.2",
|
"@gitzone/tsbundle": "^1.0.69",
|
||||||
"typings-test": "^1.0.3"
|
"@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"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
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==
|
|
||||||
76
test/test.ts
76
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';
|
||||||
|
|
||||||
import * as smartdelay from '../dist/index'
|
tap.test('.delayFor should delay async', async tools => {
|
||||||
|
tools.timeout(5000);
|
||||||
describe('smartdelay', function () {
|
let timePassed = false;
|
||||||
it('.delayFor should delay async', function(done) {
|
|
||||||
this.timeout(5000)
|
|
||||||
let timePassed = false
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
timePassed = true
|
timePassed = true;
|
||||||
},2000)
|
}, 2000);
|
||||||
smartdelay.delayFor(3000).then(() => {
|
await smartdelay.delayFor(3000).then(async () => {
|
||||||
should(timePassed).be.true()
|
// tslint:disable-next-line:no-unused-expression
|
||||||
done()
|
expect(timePassed).to.be.true;
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
it('.delayFor should pass on a type', function(done) {
|
|
||||||
this.timeout(5000)
|
tap.test('.delayForRandom should delay async for a random time period', async tools => {
|
||||||
let timePassed = false
|
let timePassedBefore = false;
|
||||||
|
let timePassedAfter = false;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
timePassed = true
|
timePassedBefore = true;
|
||||||
},2000)
|
}, 3000);
|
||||||
let hey = 'heyThere'
|
setTimeout(() => {
|
||||||
smartdelay.delayFor<string>(3000, hey).then((stringArg) => {
|
timePassedAfter = true;
|
||||||
should(stringArg).equal('heyThere')
|
}, 5000);
|
||||||
done()
|
await smartdelay.delayForRandom(3000, 4900);
|
||||||
})
|
expect(timePassedBefore).to.be.true;
|
||||||
})
|
expect(timePassedAfter).to.be.false;
|
||||||
})
|
});
|
||||||
|
|
||||||
|
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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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();
|
||||||
|
|||||||
68
ts/index.ts
68
ts/index.ts
@@ -1,13 +1,63 @@
|
|||||||
import 'typings-global'
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* delay something, works like setTimeout
|
||||||
|
* @param timeInMillisecond
|
||||||
|
* @param passOn
|
||||||
|
*/
|
||||||
export let delayFor = async <T>(timeInMillisecond: number, passOn?: T) => {
|
export let delayFor = async <T>(timeInMillisecond: number, passOn?: T) => {
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
setTimeout(
|
setTimeout(() => {
|
||||||
() => {
|
resolve();
|
||||||
resolve()
|
}, timeInMillisecond);
|
||||||
},
|
});
|
||||||
timeInMillisecond
|
return passOn;
|
||||||
)
|
};
|
||||||
})
|
|
||||||
return passOn
|
/**
|
||||||
|
* delay for a random time
|
||||||
|
*/
|
||||||
|
export let delayForRandom = async <T>(
|
||||||
|
timeMinInMillisecond: number,
|
||||||
|
timeMaxInMillisecond: number,
|
||||||
|
passOn?: T
|
||||||
|
) => {
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
resolve();
|
||||||
|
}, Math.random() * (timeMaxInMillisecond - timeMinInMillisecond) + timeMinInMillisecond);
|
||||||
|
});
|
||||||
|
return passOn;
|
||||||
|
};
|
||||||
|
|
||||||
|
export class Timeout<T> {
|
||||||
|
promise: Promise<T>;
|
||||||
|
private _deferred: smartpromise.Deferred<T>;
|
||||||
|
private _timeout: any;
|
||||||
|
private _cancelled: boolean = false;
|
||||||
|
|
||||||
|
constructor(timeInMillisecondArg, passOn?: T) {
|
||||||
|
this._deferred = smartpromise.defer<T>();
|
||||||
|
this.promise = this._deferred.promise;
|
||||||
|
this._timeout = setTimeout(() => {
|
||||||
|
if (!this._cancelled) {
|
||||||
|
this._deferred.resolve(passOn);
|
||||||
|
}
|
||||||
|
}, timeInMillisecondArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
this.makeUnrefed();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
tslint.json
16
tslint.json
@@ -1,3 +1,17 @@
|
|||||||
{
|
{
|
||||||
"extends": "tslint-config-standard"
|
"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