diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..936e5c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +/package-lock.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1267b3d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Damien Duboeuf + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b5e7380 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# GollumTS-Time + +Add setTimeout, async conditionnal testing, next image frame... with standard Promise + +## Install: + +``` +npm install --save gollumts-time +``` + +## Exemple with async + +### child component + +```typescript + + import Time from 'gollumts-time'; + + (async () => { + + console.log('wait 5 secondes...'); + await Time.timeout(5000); + console.log('OK timeout'); + + console.log('wait next animation frame...'); + await Time.nextAnimationFrame(); + console.log('OK Animation frame'); + + isOk = false; + Time.timeout(5000).then(() => isOk = true); + + console.log('wait consitional callback'); + await Time.wait(() => isOk, 300); // 300 is conditionnal tick for check callback (default = 200) + console.log('OK Conditionnal'); + + })(); + +``` \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..588e19a --- /dev/null +++ b/index.js @@ -0,0 +1,89 @@ +"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()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var Time = (function () { + function Time() { + } + Time.nextAnimationFrame = function () { + return new Promise(function (resolve) { + var frame = window.requestAnimationFrame(function () { + resolve(frame); + }); + }); + }; + Time.timeout = function (time) { + return new Promise(function (resolve) { + setTimeout(resolve, time); + }); + }; + Time.wait = function (conditionCallback, time) { + if (time === void 0) { time = 200; } + return __awaiter(this, void 0, void 0, function () { + var test; + var _this = this; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + test = function () { return __awaiter(_this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!conditionCallback()) return [3, 1]; + return [2]; + case 1: return [4, this.timeout(time)]; + case 2: + _a.sent(); + return [4, test()]; + case 3: + _a.sent(); + return [2]; + } + }); + }); }; + return [4, test()]; + case 1: + _a.sent(); + return [2]; + } + }); + }); + }; + return Time; +}()); +exports.default = Time; +; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/index.js.map b/index.js.map new file mode 100644 index 0000000..1a8eede --- /dev/null +++ b/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;IAAA;IA8BA,CAAC;IA5Bc,uBAAkB,GAAhC;QACC,OAAO,IAAI,OAAO,CAAS,UAAC,OAAO;YAClC,IAAM,KAAK,GAAG,MAAM,CAAC,qBAAqB,CAAC;gBAC1C,OAAO,CAAC,KAAK,CAAC,CAAC;YAChB,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACJ,CAAC;IAEa,YAAO,GAArB,UAAsB,IAAY;QACjC,OAAO,IAAI,OAAO,CAAO,UAAC,OAAO;YAChC,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAC3B,CAAC,CAAC,CAAC;IACJ,CAAC;IAEmB,SAAI,GAAxB,UAAyB,iBAAgC,EAAE,IAAkB;QAAlB,qBAAA,EAAA,UAAkB;;;;;;;wBACtE,IAAI,GAAG;;;;6CACR,iBAAiB,EAAE,EAAnB,cAAmB;wCACtB,WAAO;4CAEP,WAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAA;;wCAAxB,SAAwB,CAAC;wCACzB,WAAM,IAAI,EAAE,EAAA;;wCAAZ,SAAY,CAAC;wCACb,WAAO;;;6BAER,CAAC;wBACF,WAAM,IAAI,EAAE,EAAA;;wBAAZ,SAAY,CAAC;wBACb,WAAO;;;;KACP;IAEF,WAAC;AAAD,CAAC,AA9BD,IA8BC;;AAAA,CAAC"} \ No newline at end of file diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..ddd07c5 --- /dev/null +++ b/index.ts @@ -0,0 +1,31 @@ +export default class Time { + + public static nextAnimationFrame() : Promise { + return new Promise((resolve) => { + const frame = window.requestAnimationFrame(() => { + resolve(frame); + }); + }); + } + + public static timeout(time: number): Promise { + return new Promise((resolve) => { + setTimeout(resolve, time); + }); + } + + public static async wait(conditionCallback: () => boolean, time: number = 200): Promise { + const test = async (): Promise => { + if (conditionCallback()) { + return; + } else { + await this.timeout(time); + await test(); + return; + } + }; + await test(); + return; + } + +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..570c30c --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "gollumts-time", + "version": "1.0.0", + "description": "Add setTimeout, async conditionnal testing, next image frame... with standard Promise", + "main": "index.js", + "scripts": { + "build": "tsc" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/GollumJS/GollumTS-Time.git" + }, + "keywords": [ + "promise", + "setTimeout", + "async", + "requestAnimationFrame", + "typescript" + ], + "author": "Damien Duboeuf ", + "license": "MIT", + "bugs": { + "url": "https://github.com/GollumJS/GollumTS-Time/issues" + }, + "homepage": "https://github.com/GollumJS/GollumTS-Time#readme", + "dependencies": {}, + "devDependencies": { + "typescript": "^2.9.2" + }, + "files": [ + "index.ts", + "index.js", + "index.js.map", + "LICENSE", + "README.md", + "package.json", + "package-lock.json", + "README.md", + "tsconfig.json", + ".gitignore" + ] +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f2bc3b5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,17 @@ + +{ + "compilerOptions": { + "target": "ES5", + "module": "commonjs", + "removeComments": true, + "sourceMap": true, + "lib": [ + "es5", + "es2015.Promise", + "DOM" + ] + }, + "files": [ + "index.ts" + ] +}