fix(core): update
This commit is contained in:
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: '@pushrocks/smartpromise',
|
||||
version: '3.1.8',
|
||||
description: 'simple promises and Deferred constructs'
|
||||
}
|
61
ts/index.ts
61
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.commulativepromise.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,7 +42,11 @@ export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
||||
return resultArray;
|
||||
};
|
||||
|
||||
export const timeoutWrap = async <T = any>(promiseArg: Promise<T>, timeoutInMsArg: number, rejectArg = true) => {
|
||||
export const timeoutWrap = async <T = any>(
|
||||
promiseArg: Promise<T>,
|
||||
timeoutInMsArg: number,
|
||||
rejectArg = true
|
||||
) => {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
if (rejectArg) {
|
||||
@ -97,9 +59,12 @@ export const timeoutWrap = async <T = any>(promiseArg: Promise<T>, timeoutInMsAr
|
||||
});
|
||||
};
|
||||
|
||||
export const timeoutAndContinue = async <T = any>(promiseArg: Promise<T>, timeoutInMsArg = 60000) => {
|
||||
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>();
|
||||
|
26
ts/smartpromise.classes.commulativepromise.ts
Normal file
26
ts/smartpromise.classes.commulativepromise.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { defer } from "./smartpromise.classes.deferred.js";
|
||||
|
||||
export class ComulativeDeferred {
|
||||
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 commulativeDefer = () => {
|
||||
return new ComulativeDeferred();
|
||||
}
|
47
ts/smartpromise.classes.deferred.ts
Normal file
47
ts/smartpromise.classes.deferred.ts
Normal file
@ -0,0 +1,47 @@
|
||||
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>();
|
||||
};
|
Reference in New Issue
Block a user