From df2fd32f9f5875734c4c3204194363dd73287504 Mon Sep 17 00:00:00 2001 From: Phil Date: Mon, 16 Jan 2017 14:54:08 +0100 Subject: [PATCH] add implementation --- .gitignore | 4 ++++ README.md | 9 +++++++++ dist/index.d.ts | 2 ++ dist/index.js | 19 +++++++++++++++++++ package.json | 11 ++++++++++- test/test.d.ts | 1 + test/test.js | 30 ++++++++++++++++++++++++++++++ test/test.ts | 31 +++++++++++++++++++++++++++++++ ts/index.ts | 13 +++++++++++++ 9 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 dist/index.d.ts create mode 100644 dist/index.js create mode 100644 test/test.d.ts create mode 100644 test/test.js create mode 100644 test/test.ts create mode 100644 ts/index.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6554d78 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +docs/ +coverage/ +public/ \ No newline at end of file diff --git a/README.md b/README.md index 28358e9..477a948 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,13 @@ timeouts for the async/await era, written in TypeScript ## 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() +})() +``` + [![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks) diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..9bb461f --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,2 @@ +import 'typings-global'; +export declare let delayFor: (timeInMillisecond: number, passOn?: T) => Promise; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..7ff57c3 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,19 @@ +"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= \ No newline at end of file diff --git a/package.json b/package.json index f20660a..8db5a06 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "timeouts for the async/await era, written in TypeScript", "main": "dist/index.js", + "typings": "dist/index.d.ts", "scripts": { "test": "(npmts)" }, @@ -15,5 +16,13 @@ "bugs": { "url": "https://gitlab.com/pushrocks/smartdelay/issues" }, - "homepage": "https://gitlab.com/pushrocks/smartdelay#README" + "homepage": "https://gitlab.com/pushrocks/smartdelay#README", + "dependencies": { + "typings-global": "^1.0.14" + }, + "devDependencies": { + "@types/should": "^8.1.30", + "should": "^11.1.2", + "typings-test": "^1.0.3" + } } diff --git a/test/test.d.ts b/test/test.d.ts new file mode 100644 index 0000000..2fd432a --- /dev/null +++ b/test/test.d.ts @@ -0,0 +1 @@ +import 'typings-test'; diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..1217757 --- /dev/null +++ b/test/test.js @@ -0,0 +1,30 @@ +"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== \ No newline at end of file diff --git a/test/test.ts b/test/test.ts new file mode 100644 index 0000000..58c0962 --- /dev/null +++ b/test/test.ts @@ -0,0 +1,31 @@ +import 'typings-test' + +import * as should from 'should' + +import * as smartdelay from '../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() + }) + }) +}) diff --git a/ts/index.ts b/ts/index.ts new file mode 100644 index 0000000..00fb994 --- /dev/null +++ b/ts/index.ts @@ -0,0 +1,13 @@ +import 'typings-global' + +export let delayFor = async (timeInMillisecond: number, passOn?: T) => { + await new Promise((resolve, reject) => { + setTimeout( + () => { + resolve() + }, + timeInMillisecond + ) + }) + return passOn +}