Compare commits
71 Commits
Author | SHA1 | Date | |
---|---|---|---|
9f6d88e162 | |||
255d6415a5 | |||
84a8d8431c | |||
95d27d52a7 | |||
1363aa7957 | |||
095ea36e5d | |||
b6ab079de0 | |||
734d5aca35 | |||
3b0052602f | |||
9693ec04d6 | |||
4ee932873c | |||
3bedd3581e | |||
ee3c1c6e79 | |||
8c6ac181b0 | |||
51bce0b317 | |||
374d73f87e | |||
d922c691ed | |||
5aecc44ad7 | |||
f7492c4656 | |||
63dd46ed49 | |||
984c2bc9d8 | |||
16030d5ee8 | |||
ddce50ac0f | |||
baf956d0ed | |||
3340f1a895 | |||
ee336af699 | |||
0526c45ea1 | |||
9933d72784 | |||
d1bc689b35 | |||
25c0deb3f3 | |||
5138219563 | |||
93f426cce0 | |||
e42061047a | |||
3b07f99488 | |||
3d252a5e3e | |||
d82722296b | |||
22317ef83d | |||
054ec0afa4 | |||
19d434533f | |||
9b8440f743 | |||
564953e25f | |||
98f190ea21 | |||
2b77c8e019 | |||
d8e5fbf3bc | |||
1069d5a38e | |||
eb82d16859 | |||
082f533975 | |||
54b2fb0ae5 | |||
31c7e607cc | |||
b15cdb48a3 | |||
7c1605eccf | |||
cea619e964 | |||
fb866b36af | |||
a8934950ef | |||
590dc27d20 | |||
3bcff82d31 | |||
bfbea0acde | |||
ba18b31f2f | |||
4fc619970f | |||
e0cfa6ca29 | |||
843d217698 | |||
dbd2fe73b8 | |||
1eea124cf4 | |||
6a4ee22f36 | |||
e23f946e50 | |||
8d62fc6ef1 | |||
e39ad3b19b | |||
7220959662 | |||
8ece5891f7 | |||
d7b482184b | |||
de3f582226 |
23
.gitignore
vendored
23
.gitignore
vendored
@ -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
|
114
.gitlab-ci.yml
114
.gitlab-ci.yml
@ -1,59 +1,137 @@
|
||||
image: hosttoday/ht-docker-node:npmts
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- test
|
||||
- release
|
||||
- trigger
|
||||
- pages
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
testLEGACY:
|
||||
stage: test
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci test legacy
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- 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
|
||||
|
||||
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
|
||||
|
||||
testLTS:
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test lts
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
testSTABLE:
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test stable
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci publish
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: trigger
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmpage
|
||||
stage: pages
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npmpage --host gitlab
|
||||
- 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:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
- public
|
||||
allow_failure: true
|
||||
|
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal 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
26
.vscode/settings.json
vendored
Normal 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"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
24
README.md
24
README.md
@ -1,24 +0,0 @@
|
||||
# smartq
|
||||
dropin replacement for q
|
||||
|
||||
## Availabililty
|
||||
[](https://www.npmjs.com/package/smartq)
|
||||
[](https://GitLab.com/pushrocks/smartq)
|
||||
[](https://github.com/pushrocks/smartq)
|
||||
[](https://pushrocks.gitlab.io/smartq/)
|
||||
|
||||
## Status for master
|
||||
[](https://GitLab.com/pushrocks/smartq/commits/master)
|
||||
[](https://GitLab.com/pushrocks/smartq/commits/master)
|
||||
[](https://www.npmjs.com/package/smartq)
|
||||
[](https://david-dm.org/pushrocks/smartq)
|
||||
[](https://www.bithound.io/github/pushrocks/smartq/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartq)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
## Usage
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
[](https://push.rocks)
|
17
npmextra.json
Normal file
17
npmextra.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"npmci": {
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
11064
package-lock.json
generated
Normal file
11064
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
40
package.json
40
package.json
@ -1,11 +1,13 @@
|
||||
{
|
||||
"name": "smartq",
|
||||
"version": "1.0.1",
|
||||
"description": "dropin replacement for q",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"name": "@pushrocks/smartpromise",
|
||||
"private": false,
|
||||
"version": "3.1.5",
|
||||
"description": "simple promises and Deferred constructs",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(npmts)"
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -16,5 +18,29 @@
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartq#README"
|
||||
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@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"
|
||||
]
|
||||
}
|
||||
|
99
readme.md
Normal file
99
readme.md
Normal file
@ -0,0 +1,99 @@
|
||||
# @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
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
|
||||
## 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
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
## Contribution
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
30
test/test.both.ts
Normal file
30
test/test.both.ts
Normal 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();
|
109
ts/index.ts
Normal file
109
ts/index.ts
Normal file
@ -0,0 +1,109 @@
|
||||
export interface IResolve<T> {
|
||||
(value?: T | PromiseLike<T>): void;
|
||||
}
|
||||
|
||||
export interface IReject {
|
||||
(reason?: any): void;
|
||||
}
|
||||
|
||||
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
|
||||
|
||||
export class Deferred<T> {
|
||||
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 = (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 const defer = <T>() => {
|
||||
return new Deferred<T>();
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new resolved promise for the provided value.
|
||||
*/
|
||||
export const resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||
return Promise.resolve(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new rejected promise for the provided reason.
|
||||
*/
|
||||
export const rejectedPromise = (err) => {
|
||||
return Promise.reject(err);
|
||||
};
|
||||
|
||||
interface IAsyncFunction<T> {
|
||||
(someArg: T): Promise<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) => {
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
||||
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
|
||||
const done = defer<boolean>();
|
||||
for (const promiseArg of promisesArg) {
|
||||
promiseArg.then((resultArg) => {
|
||||
if (resultArg === true) {
|
||||
done.resolve(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
Promise.all(promisesArg).then(() => {
|
||||
done.resolve(false);
|
||||
});
|
||||
return done.promise;
|
||||
};
|
16
tslint.json
16
tslint.json
@ -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"
|
||||
}
|
||||
|
Reference in New Issue
Block a user