Compare commits

...

26 Commits

Author SHA1 Message Date
1363aa7957 3.1.3 2020-10-16 02:06:50 +00:00
095ea36e5d fix(core): update 2020-10-16 02:06:50 +00:00
b6ab079de0 3.1.2 2020-10-16 01:39:19 +00:00
734d5aca35 fix(core): update 2020-10-16 01:39:19 +00:00
3b0052602f 3.1.1 2020-10-15 18:19:14 +00:00
9693ec04d6 fix(core): update 2020-10-15 18:19:12 +00:00
4ee932873c 3.1.0 2020-10-15 18:16:17 +00:00
3bedd3581e feat(timeouts): allows the wrapping of promises in timeouts 2020-10-15 18:16:17 +00:00
ee3c1c6e79 3.0.9 2020-10-15 18:14:53 +00:00
8c6ac181b0 fix(core): update 2020-10-15 18:14:53 +00:00
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
d1bc689b35 3.0.1 2019-03-26 12:07:13 +01:00
25c0deb3f3 fix(core): update 2019-03-26 12:07:12 +01:00
13 changed files with 11023 additions and 653 deletions

23
.gitignore vendored
View File

@ -1,5 +1,20 @@
coverage/
pages/
node_modules/
public/
.nogit/
# artifacts
coverage/
public/
pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_*/
# custom

View File

@ -1,16 +1,16 @@
# gitzone standard
image: hosttoday/ht-docker-node:npmci
# gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
- .npmci_cache/
key: '$CI_BUILD_STAGE'
stages:
- security
- test
- release
- metadata
- security
- test
- release
- metadata
# ====================
# security stage
@ -18,108 +18,114 @@ stages:
mirror:
stage: security
script:
- npmci git mirror
- npmci git mirror
only:
- tags
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
snyk:
auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
- npmci npm prepare
- npmci command npm install --production --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
- notpriv
- docker
auditDevDependencies:
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 --only=dev
tags:
- docker
allow_failure: true
# ====================
# test stage
# ====================
testLEGACY:
stage: test
script:
- npmci node install legacy
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
allow_failure: true
testLTS:
testStable:
stage: test
script:
- npmci node install lts
- npmci npm install
- npmci npm test
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testSTABLE:
- docker
testBuild:
stage: test
script:
- npmci node install stable
- npmci npm install
- npmci npm test
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
- docker
release:
stage: release
script:
- npmci node install stable
- npmci npm publish
- npmci node install stable
- npmci npm publish
only:
- tags
- tags
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
image: docker:stable
allow_failure: true
services:
- docker:stable-dind
only:
- tags
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]
- npmci command npm install -g tslint typescript
- npmci npm prepare
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- docker
- priv
- lossless
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
- npmci trigger
only:
- tags
- tags
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata
script:
- npmci command npm install -g npmpage
- npmci command npmpage
- 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:
@ -127,15 +133,5 @@ pages:
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
- public
allow_failure: true

29
.vscode/launch.json vendored Normal file
View 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
View 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", "wcc"]
}
}
}
}
}
}
]
}

View File

@ -1,84 +0,0 @@
# @pushrocks/smartpromise
smart helpers that work with promises
## Availabililty
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartq)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartq)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartq)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartq/)
## Status for master
[![build status](https://GitLab.com/pushrocks/smartq/badges/master/build.svg)](https://GitLab.com/pushrocks/smartq/commits/master)
[![coverage report](https://GitLab.com/pushrocks/smartq/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smartq/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/smartq.svg)](https://www.npmjs.com/package/smartq)
[![Dependency Status](https://david-dm.org/pushrocks/smartq.svg)](https://david-dm.org/pushrocks/smartq)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartq/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartq/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartq/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartq)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%208.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage
Use TypeScript for best in class instellisense.
> Note: smartq uses native ES6 promises
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
```javascript
import * as q from '@pushrocks/smartpromise'
// Deferred
// -----------------------------------------------
let myAsyncFunction = (): Promise<string> => {
let done = q.defer<string>() // returns your typical Deferred object
setTimeout(() => {
done.resolve('hi') // will throw type error for other types than string as argument ;)
},6000)
return done.promise
}
let myAsyncFunction2 = async () => {
let aString = await myAsyncFunction()
console.log(aString) // will log 'hi' to console
}
myAsyncFunction2();
// Resolved and Rejected promises
// ------------------------------------------------
q.resolvedPromise(`I'll get logged to console soon`)
.then(x => {
console.log(x)
})
q.rejectedPromise(`what a lovely error message`)
.then(() => {
console.log('This never makes it to console')
}/*, alternatively put a reject function here */)
.catch(err => {
console.log(err)
})
// Promisify (typed)
// ------------------------------------------------
let myCallbackedFunction = (someString: string, someNumber: number, cb) => {
cb(null, someString)
}
let myPromisedFunction = q.promisify(myCallbackFunction)
myPromisedFunction('helloThere', 2).then(x => {
console.log(x) // will log 'helloThere' to console
})
```
For further information read the linked docs at the top of this README.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)

View File

@ -1,8 +1,17 @@
{
"npmci": {
"npmGlobalTools": [
"npmts"
],
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "smartpromise",
"shortDescription": "simple promises and Deferred constructs",
"npmPackagename": "@pushrocks/smartpromise",
"license": "MIT"
}
}
}

11080
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,13 @@
{
"name": "@pushrocks/smartpromise",
"private": false,
"version": "3.0.0",
"description": "dropin replacement for q",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"version": "3.1.3",
"description": "simple promises and Deferred constructs",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"scripts": {
"test": "(tsrun test/test.ts)",
"build": "(npmts)"
"test": "(tstest test/ --web)",
"build": "(tsbuild --web)"
},
"repository": {
"type": "git",
@ -21,8 +21,26 @@
"homepage": "https://gitlab.com/pushrocks/smartq#README",
"dependencies": {},
"devDependencies": {
"@gitzone/tsrun": "^1.0.18",
"@types/node": "^10.5.1",
"tapbundle": "^2.0.0"
}
"@gitzone/tsbuild": "^2.1.25",
"@gitzone/tstest": "^1.0.52",
"@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_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
]
}

90
readme.md Normal file
View File

@ -0,0 +1,90 @@
# @pushrocks/smartpromise
simple promises and Deferred constructs
## Availabililty and Links
- [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpromise)
- [gitlab.com (source)](https://gitlab.com/pushrocks/smartpromise)
- [github.com (source mirror)](https://github.com/pushrocks/smartpromise)
- [docs (typedoc)](https://pushrocks.gitlab.io/smartpromise/)
## Status for 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)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartpromise.svg)](https://www.npmjs.com/package/@pushrocks/smartpromise)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartpromise/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartpromise)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
## Usage
Use TypeScript for best in class instellisense.
> Note: smartq uses native ES6 promises
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
```typescript
import * as q from '@pushrocks/smartpromise'
// Deferred
// -----------------------------------------------
let myAsyncFunction = (): Promise<string> => {
let done = q.defer<string>() // returns your typical Deferred object
setTimeout(() => {
done.resolve('hi') // will throw type error for other types than string as argument ;)
},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
}
let myAsyncFunction2 = async () => {
let aString = await myAsyncFunction()
console.log(aString) // will log 'hi' to console
}
myAsyncFunction2();
// Resolved and Rejected promises
// ------------------------------------------------
q.resolvedPromise(`I'll get logged to console soon`)
.then(x => {
console.log(x)
})
q.rejectedPromise(`what a lovely error message`)
.then(() => {
console.log('This never makes it to console')
}/*, alternatively put a reject function here */)
.catch(err => {
console.log(err)
})
// Promisify (typed)
// ------------------------------------------------
let myCallbackedFunction = (someString: string, someNumber: number, cb) => {
cb(null, someString)
}
let myPromisedFunction = q.promisify(myCallbackFunction)
myPromisedFunction('helloThere', 2).then(x => {
console.log(x) // will log 'helloThere' to console
})
```
For further information read the linked docs at the top of this readme.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

30
test/test.both.ts Normal file
View File

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

View File

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

View File

@ -1,5 +1,5 @@
export interface IResolve<T> {
(value?: T | Promise<T>): void;
(value?: T | PromiseLike<T>): void;
}
export interface IReject {
@ -9,51 +9,86 @@ export interface IReject {
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
export class Deferred<T> {
promise: Promise<T>;
resolve: IResolve<T>;
reject: IReject;
status: TDeferredStatus;
public promise: Promise<T>;
public resolve: IResolve<T>;
public reject: IReject;
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() {
this.promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.resolve = (valueArg: T | PromiseLike<T>) => {
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';
});
}
}
export let defer = <T>() => {
export const defer = <T>() => {
return new Deferred<T>();
};
/**
* 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);
};
/**
* Creates a new rejected promise for the provided reason.
*/
export let rejectedPromise = err => {
export const rejectedPromise = (err) => {
return Promise.reject(err);
};
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>[] = [];
let resultArray = [];
for (let item of inputArg) {
let promise: Promise<any> = functionArg(item);
/**
* accepts an array of inputs and a function that accepts the input.
* runs all items with the function and returns the result array when all items have run
* @param inputArg
* @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);
promise.then(x => {
promise.then((x) => {
resultArray.push(x);
});
}
await Promise.all(promiseArray);
return resultArray;
};
export const timeoutWrap = <T = any>(promiseArg: Promise<T>, timeoutInMs: number) => {
return new Promise<T>((resolve, reject) => {
setTimeout(() => {
reject(new Error('timeout'));
}, timeoutInMs);
promiseArg.then(resolve, reject);
});
};

View File

@ -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"
}