Compare commits

...

14 Commits

Author SHA1 Message Date
51bce0b317 3.0.8 2020-10-15 18:13:58 +00:00
374d73f87e fix(core): update 2020-10-15 18:13:58 +00:00
d922c691ed 3.0.7 2020-10-15 18:02:14 +00:00
5aecc44ad7 fix(core): update 2020-10-15 18:02:14 +00:00
f7492c4656 3.0.6 2019-10-01 18:06:29 +02:00
63dd46ed49 fix(core): update 2019-10-01 18:06:29 +02:00
984c2bc9d8 3.0.5 2019-09-11 16:44:21 +02:00
16030d5ee8 fix(core): update 2019-09-11 16:44:21 +02:00
ddce50ac0f 3.0.4 2019-09-11 15:38:37 +02:00
baf956d0ed fix(core): update 2019-09-11 15:38:37 +02:00
3340f1a895 3.0.3 2019-09-11 15:37:18 +02:00
ee336af699 fix(core): update 2019-09-11 15:37:17 +02:00
0526c45ea1 3.0.2 2019-03-26 12:11:25 +01:00
9933d72784 fix(core): update 2019-03-26 12:11:24 +01:00
8 changed files with 10801 additions and 514 deletions

25
.gitignore vendored
View File

@ -1,5 +1,22 @@
coverage/ .nogit/
pages/
node_modules/
public/
# artifacts
coverage/
public/
pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
# custom

View File

@ -1,5 +1,5 @@
# gitzone standard # gitzone ci_default
image: hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache: cache:
paths: paths:
@ -38,19 +38,7 @@ snyk:
# test stage # test stage
# ==================== # ====================
testLTS: testStable:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testSTABLE:
stage: test stage: test
script: script:
- npmci npm prepare - npmci npm prepare
@ -60,6 +48,18 @@ testSTABLE:
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- priv
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv - notpriv
release: release:
@ -78,19 +78,11 @@ release:
# ==================== # ====================
codequality: codequality:
stage: metadata stage: metadata
image: docker:stable
allow_failure: true allow_failure: true
services:
- docker:stable-dind
script: script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/') - npmci command npm install -g tslint typescript
- docker run - npmci npm install
--env SOURCE_CODE="$PWD" - npmci command "tslint -c tslint.json ./ts/**/*.ts"
--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: tags:
- docker - docker
- priv - priv
@ -106,13 +98,15 @@ trigger:
- notpriv - notpriv
pages: pages:
image: hosttoday/ht-docker-node:npmci image: hosttoday/ht-docker-dbase:npmci
services:
- docker:stable-dind
stage: metadata stage: metadata
script: script:
- npmci command npm install -g typedoc typescript - npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare - npmci npm prepare
- npmci npm install - npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/ - npmci command tsdoc
tags: tags:
- docker - docker
- notpriv - notpriv

View File

@ -1,13 +1,16 @@
# @pushrocks/smartpromise # @pushrocks/smartpromise
simple promises and Deferred constructs simple promises and Deferred constructs
## Availabililty and Links ## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpromise)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartpromise) - [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpromise)
* [github.com (source mirror)](https://github.com/pushrocks/smartpromise) - [gitlab.com (source)](https://gitlab.com/pushrocks/smartpromise)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartpromise/) - [github.com (source mirror)](https://github.com/pushrocks/smartpromise)
- [docs (typedoc)](https://pushrocks.gitlab.io/smartpromise/)
## Status for master ## Status for master
[![build status](https://gitlab.com/pushrocks/smartpromise/badges/master/build.svg)](https://gitlab.com/pushrocks/smartpromise/commits/master) [![build status](https://gitlab.com/pushrocks/smartpromise/badges/master/build.svg)](https://gitlab.com/pushrocks/smartpromise/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartpromise/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartpromise/commits/master) [![coverage report](https://gitlab.com/pushrocks/smartpromise/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartpromise/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartpromise.svg)](https://www.npmjs.com/package/@pushrocks/smartpromise) [![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartpromise.svg)](https://www.npmjs.com/package/@pushrocks/smartpromise)
@ -23,7 +26,7 @@ Use TypeScript for best in class instellisense.
> Note: smartq uses native ES6 promises > Note: smartq uses native ES6 promises
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all() > smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
```javascript ```typescript
import * as q from '@pushrocks/smartpromise' import * as q from '@pushrocks/smartpromise'
// Deferred // Deferred
@ -32,7 +35,14 @@ let myAsyncFunction = (): Promise<string> => {
let done = q.defer<string>() // returns your typical Deferred object let done = q.defer<string>() // returns your typical Deferred object
setTimeout(() => { setTimeout(() => {
done.resolve('hi') // will throw type error for other types than string as argument ;) done.resolve('hi') // will throw type error for other types than string as argument ;)
},6000) },6000);
console.log(done.status) // logs "pending";
done.promise.then(() => {
console.log(done.status) // logs "fullfilled"
console.log(done.duration) // logs the milliseconds between instantiation and fullfillment
})
return done.promise return done.promise
} }
@ -75,6 +85,6 @@ myPromisedFunction('helloThere', 2).then(x => {
For further information read the linked docs at the top of this readme. For further information read the linked docs at the top of this readme.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh) > MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html) > | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://maintainedby.lossless.com) [![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

View File

@ -1,11 +1,10 @@
{ {
"npmci": { "npmci": {
"npmGlobalTools": [ "npmGlobalTools": [],
"npmts"
],
"npmAccessLevel": "public" "npmAccessLevel": "public"
}, },
"gitzone": { "gitzone": {
"projectType": "npm",
"module": { "module": {
"githost": "gitlab.com", "githost": "gitlab.com",
"gitscope": "pushrocks", "gitscope": "pushrocks",

11450
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,13 @@
{ {
"name": "@pushrocks/smartpromise", "name": "@pushrocks/smartpromise",
"private": false, "private": false,
"version": "3.0.1", "version": "3.0.8",
"description": "dropin replacement for q", "description": "simple promises and Deferred constructs",
"main": "dist/index.js", "main": "dist_ts/index.js",
"typings": "dist/index.d.ts", "typings": "dist_ts/index.d.ts",
"scripts": { "scripts": {
"test": "(tsrun test/test.ts)", "test": "(tstest test/)",
"build": "(npmts)" "build": "(tsbuild)"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -21,8 +21,22 @@
"homepage": "https://gitlab.com/pushrocks/smartq#README", "homepage": "https://gitlab.com/pushrocks/smartq#README",
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"@gitzone/tsrun": "^1.0.18", "@gitzone/tsbuild": "^2.1.25",
"@types/node": "^10.5.1", "@gitzone/tstest": "^1.0.52",
"tapbundle": "^2.0.0" "@pushrocks/tapbundle": "^3.2.9",
} "@types/node": "^14.11.8",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
},
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"cli.js",
"npmextra.json",
"readme.md"
]
} }

View File

@ -1,27 +1,27 @@
import { expect, tap } from 'tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
import * as q from '../ts/index'; import * as smartpromise from '../ts/index';
tap.test('should return a Deferred for .defer()', async () => { tap.test('should return a Deferred for .defer()', async () => {
let myDeferred = q.defer(); const myDeferred = smartpromise.defer();
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled; const expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
myDeferred.resolve(); myDeferred.resolve();
return expectPromise; return expectPromise;
}); });
tap.test('should let types flow through the Promise', async () => { tap.test('should let types flow through the Promise', async () => {
let myString = 'someString'; const myString = 'someString';
let myDeferred = q.defer<string>(); const myDeferred = smartpromise.defer<string>();
let expectPromise = expect(myDeferred.promise).to.eventually.equal('someString'); const expectPromise = expect(myDeferred.promise).to.eventually.equal('someString');
myDeferred.resolve(myString); myDeferred.resolve(myString);
return expectPromise; return expectPromise;
}); });
tap.test('should map callbacks', async () => { tap.test('should map callbacks', async () => {
let inputArray = ['hi', 'awesome']; const inputArray = ['hi', 'awesome'];
const myPromisified = async myInput => { const myPromisified = async myInput => {
return myInput; return myInput;
}; };
const expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal( const expectPromise = expect(smartpromise.map(inputArray, myPromisified)).to.eventually.deep.equal(
inputArray inputArray
); );
return expectPromise; return expectPromise;

View File

@ -1,5 +1,5 @@
export interface IResolve<T> { export interface IResolve<T> {
(value?: T | Promise<T>): void; (value?: T | PromiseLike<T>): void;
} }
export interface IReject { export interface IReject {
@ -9,34 +9,54 @@ export interface IReject {
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected'; export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
export class Deferred<T> { export class Deferred<T> {
promise: Promise<T>; public promise: Promise<T>;
resolve: IResolve<T>; public resolve: IResolve<T>;
reject: IReject; public reject: IReject;
status: TDeferredStatus; public status: TDeferredStatus;
public startedAt: number;
public stoppedAt: number;
public get duration(): number {
if (this.stoppedAt) {
return this.stoppedAt - this.startedAt;
} else {
return Date.now() - this.startedAt;
}
}
constructor() { constructor() {
this.promise = new Promise<T>((resolve, reject) => { this.promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve; this.resolve = (valueArg: T | PromiseLike<T>) => {
this.reject = reject; this.status = 'fulfilled';
this.stoppedAt = Date.now();
resolve(valueArg);
};
this.reject = (reason: any) => {
this.status = 'rejected';
this.stoppedAt = Date.now();
reject(reason);
};
this.startedAt = Date.now();
this.status = 'pending'; this.status = 'pending';
}); });
} }
} }
export let defer = <T>() => { export const defer = <T>() => {
return new Deferred<T>(); return new Deferred<T>();
}; };
/** /**
* Creates a new resolved promise for the provided value. * Creates a new resolved promise for the provided value.
*/ */
export let resolvedPromise = <T>(value?: T): Promise<T> => { export const resolvedPromise = <T>(value?: T): Promise<T> => {
return Promise.resolve(value); return Promise.resolve(value);
}; };
/** /**
* Creates a new rejected promise for the provided reason. * Creates a new rejected promise for the provided reason.
*/ */
export let rejectedPromise = err => { export const rejectedPromise = (err) => {
return Promise.reject(err); return Promise.reject(err);
}; };
@ -44,16 +64,31 @@ interface IAsyncFunction<T> {
(someArg: T): Promise<T>; (someArg: T): Promise<T>;
} }
export let map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => { /**
let promiseArray: Promise<any>[] = []; * accepts an array of inputs and a function that accepts the input.
let resultArray = []; * runs all items with the function and returns the result array when all items have run
for (let item of inputArg) { * @param inputArg
let promise: Promise<any> = functionArg(item); * @param functionArg
*/
export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
const promiseArray: Promise<any>[] = [];
const resultArray = [];
for (const item of inputArg) {
const promise: Promise<any> = functionArg(item);
promiseArray.push(promise); promiseArray.push(promise);
promise.then(x => { promise.then((x) => {
resultArray.push(x); resultArray.push(x);
}); });
} }
await Promise.all(promiseArray); await Promise.all(promiseArray);
return resultArray; return resultArray;
}; };
export const timeoutWrap = (ms, promise) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('timeout'));
}, ms);
promise.then(resolve, reject);
});
};