more-proms 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ export declare class SettledPromise<T = unknown> extends Promise<T> {
2
+ settled: boolean;
3
+ onSettled: Promise<void>;
4
+ constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void);
5
+ }
6
+ export declare class ResablePromise<T = unknown> extends SettledPromise<T> {
7
+ readonly res: (t: T) => void;
8
+ readonly rej: (err: any) => void;
9
+ constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void);
10
+ }
11
+ export declare class CancelAblePromise<T = unknown, C = unknown> extends SettledPromise<T> {
12
+ cancelled: boolean;
13
+ cancel: () => void;
14
+ constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, cancel: () => C);
15
+ then<TResult1 = T, TResult2 = never>(onfulfilled: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): CancelAblePromise<TResult1 | TResult2>;
16
+ catch<TResult = never>(onrejected: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): CancelAblePromise<T | TResult>;
17
+ }
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ var keyIndex = require('key-index');
4
+
5
+ // export function latestLatentRequest<Args extends unknown[], Ret>(cb: (...args: Args) => Promise<Ret>, ...thens: ((ret: Ret, ...args: Args) => void)[]) {
6
+ // let globalRecent = Symbol()
7
+ // async function request(...args: Args) {
8
+ // const recent = globalRecent = Symbol()
9
+ // let ret = await cb(...args)
10
+ // for (const then of thens) {
11
+ // if (globalRecent === recent) {
12
+ // then(ret, ...args)
13
+ // }
14
+ // else break
15
+ // }
16
+ // return ret
17
+ // }
18
+ // return request
19
+ // }
20
+ class SettledPromise extends Promise {
21
+ constructor(executor) {
22
+ super((res, rej) => {
23
+ executor((a) => {
24
+ this.settled = true;
25
+ r();
26
+ res(a);
27
+ }, (a) => {
28
+ this.settled = true;
29
+ r();
30
+ rej(a);
31
+ });
32
+ });
33
+ this.settled = false;
34
+ let r;
35
+ this.onSettled = new Promise((res) => {
36
+ r = res;
37
+ });
38
+ }
39
+ }
40
+ class ResablePromise extends SettledPromise {
41
+ constructor(executor) {
42
+ let res;
43
+ let rej;
44
+ super((r, rj) => {
45
+ res = r;
46
+ rej = rj;
47
+ executor(r, rj);
48
+ });
49
+ this.res = res;
50
+ this.rej = rej;
51
+ }
52
+ }
53
+ class CancelAblePromise extends SettledPromise {
54
+ constructor(executor, cancel) {
55
+ super((res, rej) => {
56
+ const r = (a) => {
57
+ if (this.cancelled)
58
+ return;
59
+ res(a);
60
+ };
61
+ const rj = (a) => {
62
+ if (this.cancelled)
63
+ return;
64
+ rej(a);
65
+ };
66
+ executor(r, rj);
67
+ });
68
+ this.cancelled = false;
69
+ this.cancel = keyIndex.memoize(() => {
70
+ if (this.settled)
71
+ return;
72
+ this.cancelled = true;
73
+ return cancel();
74
+ });
75
+ }
76
+ then(onfulfilled, onrejected) {
77
+ const r = super.then(onfulfilled, onrejected);
78
+ r.cancel = this.cancel;
79
+ return r;
80
+ }
81
+ catch(onrejected) {
82
+ const r = super.catch(onrejected);
83
+ r.cancel = this.cancel;
84
+ return r;
85
+ }
86
+ }
87
+
88
+ exports.CancelAblePromise = CancelAblePromise;
89
+ exports.ResablePromise = ResablePromise;
90
+ exports.SettledPromise = SettledPromise;
@@ -0,0 +1,17 @@
1
+ export declare class SettledPromise<T = unknown> extends Promise<T> {
2
+ settled: boolean;
3
+ onSettled: Promise<void>;
4
+ constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void);
5
+ }
6
+ export declare class ResablePromise<T = unknown> extends SettledPromise<T> {
7
+ readonly res: (t: T) => void;
8
+ readonly rej: (err: any) => void;
9
+ constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void);
10
+ }
11
+ export declare class CancelAblePromise<T = unknown, C = unknown> extends SettledPromise<T> {
12
+ cancelled: boolean;
13
+ cancel: () => void;
14
+ constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, cancel: () => C);
15
+ then<TResult1 = T, TResult2 = never>(onfulfilled: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): CancelAblePromise<TResult1 | TResult2>;
16
+ catch<TResult = never>(onrejected: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): CancelAblePromise<T | TResult>;
17
+ }
@@ -0,0 +1,83 @@
1
+ import { memoize } from "key-index";
2
+ // export function latestLatentRequest<Args extends unknown[], Ret>(cb: (...args: Args) => Promise<Ret>, ...thens: ((ret: Ret, ...args: Args) => void)[]) {
3
+ // let globalRecent = Symbol()
4
+ // async function request(...args: Args) {
5
+ // const recent = globalRecent = Symbol()
6
+ // let ret = await cb(...args)
7
+ // for (const then of thens) {
8
+ // if (globalRecent === recent) {
9
+ // then(ret, ...args)
10
+ // }
11
+ // else break
12
+ // }
13
+ // return ret
14
+ // }
15
+ // return request
16
+ // }
17
+ export class SettledPromise extends Promise {
18
+ constructor(executor) {
19
+ super((res, rej) => {
20
+ executor((a) => {
21
+ this.settled = true;
22
+ r();
23
+ res(a);
24
+ }, (a) => {
25
+ this.settled = true;
26
+ r();
27
+ rej(a);
28
+ });
29
+ });
30
+ this.settled = false;
31
+ let r;
32
+ this.onSettled = new Promise((res) => {
33
+ r = res;
34
+ });
35
+ }
36
+ }
37
+ export class ResablePromise extends SettledPromise {
38
+ constructor(executor) {
39
+ let res;
40
+ let rej;
41
+ super((r, rj) => {
42
+ res = r;
43
+ rej = rj;
44
+ executor(r, rj);
45
+ });
46
+ this.res = res;
47
+ this.rej = rej;
48
+ }
49
+ }
50
+ export class CancelAblePromise extends SettledPromise {
51
+ constructor(executor, cancel) {
52
+ super((res, rej) => {
53
+ const r = (a) => {
54
+ if (this.cancelled)
55
+ return;
56
+ res(a);
57
+ };
58
+ const rj = (a) => {
59
+ if (this.cancelled)
60
+ return;
61
+ rej(a);
62
+ };
63
+ executor(r, rj);
64
+ });
65
+ this.cancelled = false;
66
+ this.cancel = memoize(() => {
67
+ if (this.settled)
68
+ return;
69
+ this.cancelled = true;
70
+ return cancel();
71
+ });
72
+ }
73
+ then(onfulfilled, onrejected) {
74
+ const r = super.then(onfulfilled, onrejected);
75
+ r.cancel = this.cancel;
76
+ return r;
77
+ }
78
+ catch(onrejected) {
79
+ const r = super.catch(onrejected);
80
+ r.cancel = this.cancel;
81
+ return r;
82
+ }
83
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "more-proms",
3
- "version": "0.0.1",
3
+ "version": "1.0.0",
4
4
  "description": "A collection of additional promise extending classes. Including a (from the outside) ResablePromise, CancelAblePromise and a latestLatent utility function.",
5
5
  "main": "./app/dist/esm/moreProms.mjs",
6
6
  "types": "./app/dist/esm/moreProms.d.ts",
@@ -15,10 +15,10 @@
15
15
  "build": "del-cli app/dist && concurrently \"npm run buildESM\" \"npm run buildCJS\" --raw",
16
16
  "buildESM": "tsc -p ./tsconfig.prod.esm.json && mjsify app/dist esm cjs",
17
17
  "buildCJS": "tsc -p ./tsconfig.prod.cjs.json && npm run buildCJS2",
18
- "buildCJS2": "concurrently \"rollup --config rollup.node.prod.config.mjs\" \"rollup --config rollup.node.prod.cli.config.mjs\" --hide 0",
18
+ "buildCJS2": "rollup --config rollup.node.prod.config.mjs",
19
19
  "dev": "npm run devWeb",
20
20
  "devWeb": "concurrently \"rollup --config rollup.web.dev.config.mjs -w\" \"node devServer.mjs\" --raw ",
21
- "devNode": "concurrently \"rollup --config rollup.node.dev.config.mjs -w\" \"rollup --config rollup.node.prod.cli.config.mjs\" \"wait-on repl/dist/crossPlatformSpecs-repl.js && echo && echo Run \\'npm run repl\\' to run repl.\" --raw",
21
+ "devNode": "concurrently \"rollup --config rollup.node.dev.config.mjs -w\" \"wait-on repl/dist/crossPlatformSpecs-repl.js && echo && echo Run \\'npm run repl\\' to run repl.\" --raw",
22
22
  "deploy": "npm run build && npm publish",
23
23
  "repl": "node ./repl/dist/moreProms-repl.js",
24
24
  "start": "npm run repl",
@@ -30,20 +30,20 @@
30
30
  "url": "git+https://github.com/maximilianMairinger/moreProms.git"
31
31
  },
32
32
  "keywords": [
33
- "more",
34
- "promises",
35
- "collection",
36
- "cancelable",
37
- "promise",
38
- "cancel",
39
- "able",
40
- "cancelablepromise",
41
- "resolve",
42
- "res",
43
- "resable",
44
- "resolveable",
45
- "latent"
46
- ],
33
+ "more",
34
+ "promises",
35
+ "collection",
36
+ "cancelable",
37
+ "promise",
38
+ "cancel",
39
+ "able",
40
+ "cancelablepromise",
41
+ "resolve",
42
+ "res",
43
+ "resable",
44
+ "resolveable",
45
+ "latent"
46
+ ],
47
47
  "author": "maximilianMairinger",
48
48
  "license": "ISC",
49
49
  "bugs": {
@@ -66,14 +66,16 @@
66
66
  "mjsify": "^2.0.6",
67
67
  "open": "^9.1.0",
68
68
  "rollup": "^3.21.7",
69
+ "tiny-delay": "^1.0.6",
69
70
  "tslib": "2.0.0",
70
71
  "typescript": "^5.0.4",
71
72
  "wait-on": "^7.0.1",
72
73
  "webpack-merge": "^5.0.9"
73
74
  },
74
75
  "dependencies": {
75
- "commander": "^10.0.1",
76
76
  "colorful-cli-logger": "^1.0.0",
77
+ "commander": "^10.0.1",
78
+ "key-index": "^1.4.18",
77
79
  "req-package-json": "^2.1.2"
78
80
  }
79
81
  }