Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a99767986d | |||
| 647a9ab4cb | |||
| ffb4d1c29e | |||
| f4480aa289 | |||
| f952c71e2d | |||
| 4edbd04c77 | |||
| 9acff87870 | |||
| 767b3d6e42 | |||
| deb8f2004d | |||
| 62a5975fb3 | |||
| 645a1ca111 | |||
| e67b148c41 | |||
| f462bb1641 | |||
| 553bfa2735 | |||
| 75358d0b66 |
104
.gitlab-ci.yml
104
.gitlab-ci.yml
@@ -1,59 +1,141 @@
|
||||
image: hosttoday/ht-docker-node:npmts
|
||||
# gitzone standard
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- trigger
|
||||
- pages
|
||||
- metadata
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
stage: security
|
||||
script:
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
testLEGACY:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test legacy
|
||||
- npmci node install legacy
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test lts
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test stable
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci publish
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
image: docker:stable
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
script:
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SOURCE_CODE="$PWD"
|
||||
--volume "$PWD":/code
|
||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
||||
artifacts:
|
||||
paths: [codeclimate.json]
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: trigger
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmpage
|
||||
stage: pages
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npmpage --publish gitlab
|
||||
- npmci command npm install -g npmpage
|
||||
- npmci command npmpage
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
||||
windowsCompatibility:
|
||||
image: stefanscherer/node-windows:10-build-tools
|
||||
stage: metadata
|
||||
script:
|
||||
- npm install & npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- windows
|
||||
allow_failure: true
|
||||
|
||||
14
README.md
14
README.md
@@ -1,13 +1,16 @@
|
||||
# smartdelay
|
||||
# @pushrocks/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)
|
||||
@@ -19,15 +22,16 @@ timeouts for the async/await era, written in TypeScript
|
||||
[](http://standardjs.com/)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
```javascript
|
||||
import * as smartdelay from 'smartdelay'
|
||||
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()
|
||||
})()
|
||||
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=
|
||||
6
npmextra.json
Normal file
6
npmextra.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
}
|
||||
}
|
||||
1663
package-lock.json
generated
Normal file
1663
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@@ -1,11 +1,13 @@
|
||||
{
|
||||
"name": "smartdelay",
|
||||
"version": "1.0.1",
|
||||
"name": "@pushrocks/smartdelay",
|
||||
"private": false,
|
||||
"version": "2.0.3",
|
||||
"description": "timeouts for the async/await era, written in TypeScript",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(npmts)"
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -18,11 +20,13 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartdelay#README",
|
||||
"dependencies": {
|
||||
"typings-global": "^1.0.14"
|
||||
"@pushrocks/smartpromise": "^3.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/should": "^8.1.30",
|
||||
"should": "^11.1.2",
|
||||
"typings-test": "^1.0.3"
|
||||
"@gitzone/tsbuild": "^2.1.8",
|
||||
"@gitzone/tsrun": "^1.2.1",
|
||||
"@gitzone/tstest": "^1.0.18",
|
||||
"@types/node": "^11.11.7",
|
||||
"tapbundle": "^2.0.2"
|
||||
}
|
||||
}
|
||||
|
||||
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==
|
||||
74
test/test.ts
74
test/test.ts
@@ -1,31 +1,51 @@
|
||||
import 'typings-test'
|
||||
import { expect, tap } from 'tapbundle';
|
||||
|
||||
import * as should from 'should'
|
||||
import * as smartdelay from '../ts/index';
|
||||
|
||||
import * as smartdelay from '../dist/index'
|
||||
|
||||
describe('smartdelay', function () {
|
||||
it('.delayFor should delay async', function(done) {
|
||||
this.timeout(5000)
|
||||
let timePassed = false
|
||||
tap.test('.delayFor should delay async', async tools => {
|
||||
tools.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
|
||||
timePassed = true;
|
||||
}, 2000);
|
||||
await smartdelay.delayFor(3000).then(async () => {
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(timePassed).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('.delayForRandpm should delay async for a random time period', async tools => {
|
||||
tools.timeout(5000);
|
||||
let timePassed = false;
|
||||
setTimeout(() => {
|
||||
timePassed = true
|
||||
},2000)
|
||||
let hey = 'heyThere'
|
||||
smartdelay.delayFor<string>(3000, hey).then((stringArg) => {
|
||||
should(stringArg).equal('heyThere')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
timePassed = true;
|
||||
}, 3000);
|
||||
await smartdelay.delayForRandom(3000, 4900).then(async () => {
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(timePassed).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
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();
|
||||
|
||||
61
ts/index.ts
61
ts/index.ts
@@ -1,13 +1,56 @@
|
||||
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) => {
|
||||
await new Promise((resolve, reject) => {
|
||||
setTimeout(
|
||||
() => {
|
||||
resolve()
|
||||
},
|
||||
timeInMillisecond
|
||||
)
|
||||
})
|
||||
return passOn
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, timeInMillisecond);
|
||||
});
|
||||
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);
|
||||
}
|
||||
|
||||
makeUnrefed() {
|
||||
this._timeout.unref();
|
||||
}
|
||||
|
||||
cancel() {
|
||||
this._cancelled = true;
|
||||
this.makeUnrefed();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user