Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
1f1e0e4ee1 | |||
139d4cb56d | |||
8038e54f53 | |||
37284da2cc | |||
b0772e2372 | |||
f3eac4a5f7 | |||
fde31ec25d | |||
810b299caa | |||
c71ac16090 | |||
91b52982c0 | |||
97f8327fe9 | |||
42f40449ad | |||
960c23fecd | |||
40642fd6f6 | |||
4c3565c618 | |||
1848006601 | |||
c761cc4460 |
@ -12,40 +12,36 @@ stages:
|
||||
- release
|
||||
- metadata
|
||||
|
||||
before_script:
|
||||
- pnpm install -g pnpm
|
||||
- pnpm install -g @shipzone/npmci
|
||||
- npmci npm prepare
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- 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
|
||||
- npmci command pnpm audit --audit-level=high --prod
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
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
|
||||
- npmci command pnpm audit --audit-level=high --dev
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
@ -56,7 +52,6 @@ auditDevDependencies:
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
@ -67,10 +62,9 @@ testStable:
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
- npmci npm build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
@ -96,10 +90,9 @@ codequality:
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci command npm install -g typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
@ -119,11 +112,9 @@ trigger:
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
- npmci command npm run buildDocs
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -2,28 +2,10 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "current file",
|
||||
"type": "node",
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"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"
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -7,10 +7,10 @@
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "smartpromise",
|
||||
"shortDescription": "simple promises and Deferred constructs",
|
||||
"npmPackagename": "@pushrocks/smartpromise",
|
||||
"description": "simple promises and Deferred constructs",
|
||||
"npmPackagename": "@push.rocks/smartpromise",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
|
11064
package-lock.json
generated
11064
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@ -1,13 +1,14 @@
|
||||
{
|
||||
"name": "@pushrocks/smartpromise",
|
||||
"name": "@push.rocks/smartpromise",
|
||||
"private": false,
|
||||
"version": "3.1.5",
|
||||
"version": "4.0.3",
|
||||
"description": "simple promises and Deferred constructs",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)"
|
||||
"build": "(tsbuild --web --allowimplicitany)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -19,14 +20,12 @@
|
||||
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
||||
},
|
||||
"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"
|
||||
"@gitzone/tsbuild": "^2.1.66",
|
||||
"@gitzone/tsrun": "^1.2.42",
|
||||
"@gitzone/tstest": "^1.0.74",
|
||||
"@pushrocks/tapbundle": "^5.0.8",
|
||||
"@types/node": "^20.4.1"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
@ -42,5 +41,6 @@
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
],
|
||||
"type": "module"
|
||||
}
|
||||
|
4691
pnpm-lock.yaml
generated
Normal file
4691
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -21,7 +21,6 @@ Code Style | [](htt
|
||||
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
|
||||
|
||||
@ -86,14 +85,12 @@ myPromisedFunction('helloThere', 2).then((x) => {
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
## 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)
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
||||
|
@ -1,19 +1,18 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartpromise from '../ts/index';
|
||||
import * as smartpromise from '../ts/index.js';
|
||||
|
||||
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;
|
||||
await myDeferred.promise;
|
||||
});
|
||||
|
||||
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;
|
||||
const result = await myDeferred.promise;
|
||||
expect(result).toEqual('someString');
|
||||
});
|
||||
|
||||
tap.test('should map callbacks', async () => {
|
||||
@ -21,10 +20,8 @@ tap.test('should map callbacks', async () => {
|
||||
const myPromisified = async (myInput) => {
|
||||
return myInput;
|
||||
};
|
||||
const expectPromise = expect(
|
||||
smartpromise.map(inputArray, myPromisified)
|
||||
).to.eventually.deep.equal(inputArray);
|
||||
return expectPromise;
|
||||
const result = await smartpromise.map(inputArray, myPromisified);
|
||||
expect(result).toEqual(inputArray);
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
53
test/test.readme.node.ts
Normal file
53
test/test.readme.node.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import * as smartpromise from '../ts/index.js';
|
||||
|
||||
// using Deferreds
|
||||
// simple deferred;
|
||||
const done = smartpromise.defer();
|
||||
done.promise.then((stringArg) => {
|
||||
console.log(stringArg);
|
||||
});
|
||||
done.resolve('hello'); // whenever you are ready
|
||||
|
||||
// using deferreds in async functions to cater callback style apis
|
||||
const myAsyncFunction = async (): Promise<string> => {
|
||||
const done = smartpromise.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";
|
||||
await done.promise;
|
||||
console.log(done.status); // logs "fullfilled"
|
||||
console.log(done.duration); // outputs the duration in millisenconds
|
||||
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
let myAsyncFunction2 = async () => {
|
||||
let aString = await myAsyncFunction();
|
||||
console.log(aString); // will log 'hi' to console
|
||||
};
|
||||
|
||||
myAsyncFunction2();
|
||||
|
||||
// Resolved and Rejected promises
|
||||
// ------------------------------------------------
|
||||
smartpromise.resolvedPromise(`I'll get logged to console soon`).then((x) => {
|
||||
console.log(x);
|
||||
});
|
||||
|
||||
smartpromise
|
||||
.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);
|
||||
});
|
||||
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
|
||||
tap.test('runs through', async () => {});
|
||||
tap.start();
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartpromise',
|
||||
version: '4.0.3',
|
||||
description: 'simple promises and Deferred constructs'
|
||||
}
|
67
ts/index.ts
67
ts/index.ts
@ -1,50 +1,8 @@
|
||||
export interface IResolve<T> {
|
||||
(value?: T | PromiseLike<T>): void;
|
||||
}
|
||||
import { defer } from './smartpromise.classes.deferred.js';
|
||||
|
||||
export interface IReject {
|
||||
(reason?: any): void;
|
||||
}
|
||||
export * from './smartpromise.classes.cumulativedeferred.js';
|
||||
export * from './smartpromise.classes.deferred.js';
|
||||
|
||||
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.
|
||||
@ -84,15 +42,30 @@ export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
||||
return resultArray;
|
||||
};
|
||||
|
||||
export const timeoutWrap = <T = any>(promiseArg: Promise<T>, timeoutInMs: number) => {
|
||||
export const timeoutWrap = async <T = any>(
|
||||
promiseArg: Promise<T>,
|
||||
timeoutInMsArg: number,
|
||||
rejectArg = true
|
||||
) => {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
if (rejectArg) {
|
||||
reject(new Error('timeout'));
|
||||
}, timeoutInMs);
|
||||
} else {
|
||||
resolve(null);
|
||||
}
|
||||
}, timeoutInMsArg);
|
||||
promiseArg.then(resolve, reject);
|
||||
});
|
||||
};
|
||||
|
||||
export const timeoutAndContinue = async <T = any>(
|
||||
promiseArg: Promise<T>,
|
||||
timeoutInMsArg = 60000
|
||||
) => {
|
||||
return timeoutWrap(promiseArg, timeoutInMsArg, false);
|
||||
};
|
||||
|
||||
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
|
||||
const done = defer<boolean>();
|
||||
for (const promiseArg of promisesArg) {
|
||||
|
26
ts/smartpromise.classes.cumulativedeferred.ts
Normal file
26
ts/smartpromise.classes.cumulativedeferred.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { defer } from "./smartpromise.classes.deferred.js";
|
||||
|
||||
export class CumulativeDeferred {
|
||||
private accumulatedPromises: Promise<any>[] = [];
|
||||
private deferred = defer();
|
||||
public promise = this.deferred.promise;
|
||||
|
||||
constructor() {
|
||||
setTimeout(async () => {
|
||||
while (this.accumulatedPromises.length > 0) {
|
||||
const poppedPromise = this.accumulatedPromises.shift();
|
||||
await poppedPromise;
|
||||
}
|
||||
this.deferred.resolve();
|
||||
}, 0);
|
||||
}
|
||||
|
||||
public addPromise(promiseArg: Promise<any>) {
|
||||
this.accumulatedPromises.push(promiseArg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const cumulativeDefer = () => {
|
||||
return new CumulativeDeferred();
|
||||
}
|
54
ts/smartpromise.classes.deferred.ts
Normal file
54
ts/smartpromise.classes.deferred.ts
Normal file
@ -0,0 +1,54 @@
|
||||
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 claimed = false;
|
||||
public claim() {
|
||||
if (this.claimed) {
|
||||
throw new Error('Deferred already claimed');
|
||||
}
|
||||
this.claimed = true;
|
||||
}
|
||||
|
||||
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>();
|
||||
};
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
||||
{
|
||||
"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