@nejs/basic-extensions 2.21.5 → 2.22.6
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.
- package/README.md +78 -4
- package/dist/@nejs/basic-extensions.bundle.2.22.6.js +25 -0
- package/dist/@nejs/basic-extensions.bundle.2.22.6.js.map +7 -0
- package/dist/cjs/classes/index.cjs +11129 -0
- package/dist/cjs/classes/index.cjs.map +7 -0
- package/dist/cjs/index.cjs +15191 -0
- package/dist/cjs/index.cjs.map +7 -0
- package/dist/cjs/utils/index.cjs +3954 -0
- package/dist/cjs/utils/index.cjs.map +7 -0
- package/dist/esm/basic-extensions.mjs +25 -0
- package/dist/esm/basic-extensions.mjs.map +7 -0
- package/package.json +16 -22
- package/repl.bootstrap.js +4 -7
- package/repl.history +26 -26
- package/src/classes/enumeration.js +466 -0
- package/src/classes/index.js +5 -1
- package/src/index.js +3 -1
- package/src/regular.expression.extensions.js +0 -35
- package/src/utils/toolkit.js +699 -516
- package/tests/arrayextensions.test.js +3 -3
- package/tests/index.test.js +3 -1
- package/tests/newClasses/asyncIterable.test.js +3 -3
- package/tests/newClasses/deferred.test.js +3 -3
- package/tests/newClasses/descriptor.test.js +3 -3
- package/tests/newClasses/iterable.test.js +3 -3
- package/tests/newClasses/refmap.test.js +3 -3
- package/tests/newClasses/refset.test.js +3 -3
- package/tests/objectextensions.test.js +3 -3
- package/tests/setextensions.test.js +3 -3
- package/tests/stringextensions.test.js +3 -2
- package/tests/utils/descriptor.utils.test.js +1 -1
- package/tests/utils/toolkit.test.js +429 -163
- package/.esdoc.json +0 -9
- package/.vscode/settings.json +0 -5
- package/bin/build +0 -27
- package/bin/clean +0 -14
- package/bin/esbuild +0 -91
- package/bin/fixup +0 -13
- package/bin/repl.basics.js +0 -584
- package/bin/repl.signature.js +0 -63
- package/bin/version +0 -100
- package/dist/@nejs/basic-extensions.bundle.2.21.5.js +0 -25
- package/dist/@nejs/basic-extensions.bundle.2.21.5.js.map +0 -7
- package/dist/cjs/array.extensions.d.ts +0 -39
- package/dist/cjs/array.extensions.js +0 -477
- package/dist/cjs/array.extensions.js.map +0 -1
- package/dist/cjs/big.int.extension.d.ts +0 -31
- package/dist/cjs/big.int.extension.js +0 -273
- package/dist/cjs/big.int.extension.js.map +0 -1
- package/dist/cjs/classes/asyncIterable.d.ts +0 -126
- package/dist/cjs/classes/asyncIterable.js +0 -209
- package/dist/cjs/classes/asyncIterable.js.map +0 -1
- package/dist/cjs/classes/deferred.d.ts +0 -146
- package/dist/cjs/classes/deferred.js +0 -291
- package/dist/cjs/classes/deferred.js.map +0 -1
- package/dist/cjs/classes/descriptor.d.ts +0 -334
- package/dist/cjs/classes/descriptor.js +0 -537
- package/dist/cjs/classes/descriptor.js.map +0 -1
- package/dist/cjs/classes/enum.d.ts +0 -50
- package/dist/cjs/classes/enum.js +0 -405
- package/dist/cjs/classes/enum.js.map +0 -1
- package/dist/cjs/classes/index.d.ts +0 -15
- package/dist/cjs/classes/index.js +0 -63
- package/dist/cjs/classes/index.js.map +0 -1
- package/dist/cjs/classes/introspector.d.ts +0 -20
- package/dist/cjs/classes/introspector.js +0 -130
- package/dist/cjs/classes/introspector.js.map +0 -1
- package/dist/cjs/classes/iterable.d.ts +0 -169
- package/dist/cjs/classes/iterable.js +0 -268
- package/dist/cjs/classes/iterable.js.map +0 -1
- package/dist/cjs/classes/param.parser.d.ts +0 -221
- package/dist/cjs/classes/param.parser.js +0 -242
- package/dist/cjs/classes/param.parser.js.map +0 -1
- package/dist/cjs/classes/pluggable.proxy.d.ts +0 -153
- package/dist/cjs/classes/pluggable.proxy.js +0 -444
- package/dist/cjs/classes/pluggable.proxy.js.map +0 -1
- package/dist/cjs/classes/property.d.ts +0 -79
- package/dist/cjs/classes/property.js +0 -284
- package/dist/cjs/classes/property.js.map +0 -1
- package/dist/cjs/classes/refmap.d.ts +0 -238
- package/dist/cjs/classes/refmap.js +0 -421
- package/dist/cjs/classes/refmap.js.map +0 -1
- package/dist/cjs/classes/refset.d.ts +0 -186
- package/dist/cjs/classes/refset.js +0 -370
- package/dist/cjs/classes/refset.js.map +0 -1
- package/dist/cjs/classes/symkeys.d.ts +0 -349
- package/dist/cjs/classes/symkeys.js +0 -510
- package/dist/cjs/classes/symkeys.js.map +0 -1
- package/dist/cjs/classes/type.d.ts +0 -56
- package/dist/cjs/classes/type.js +0 -405
- package/dist/cjs/classes/type.js.map +0 -1
- package/dist/cjs/function.extensions.d.ts +0 -12
- package/dist/cjs/function.extensions.js +0 -758
- package/dist/cjs/function.extensions.js.map +0 -1
- package/dist/cjs/global.this.d.ts +0 -2
- package/dist/cjs/global.this.js +0 -300
- package/dist/cjs/global.this.js.map +0 -1
- package/dist/cjs/index.d.ts +0 -31
- package/dist/cjs/index.js +0 -228
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/json.extensions.d.ts +0 -2
- package/dist/cjs/json.extensions.js +0 -109
- package/dist/cjs/json.extensions.js.map +0 -1
- package/dist/cjs/map.extensions.d.ts +0 -3
- package/dist/cjs/map.extensions.js +0 -143
- package/dist/cjs/map.extensions.js.map +0 -1
- package/dist/cjs/math.extension.d.ts +0 -14
- package/dist/cjs/math.extension.js +0 -71
- package/dist/cjs/math.extension.js.map +0 -1
- package/dist/cjs/number.extension.d.ts +0 -44
- package/dist/cjs/number.extension.js +0 -278
- package/dist/cjs/number.extension.js.map +0 -1
- package/dist/cjs/object.extensions.d.ts +0 -33
- package/dist/cjs/object.extensions.js +0 -1091
- package/dist/cjs/object.extensions.js.map +0 -1
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/proxy.extensions.d.ts +0 -2
- package/dist/cjs/proxy.extensions.js +0 -207
- package/dist/cjs/proxy.extensions.js.map +0 -1
- package/dist/cjs/reflect.extensions.d.ts +0 -14
- package/dist/cjs/reflect.extensions.js +0 -316
- package/dist/cjs/reflect.extensions.js.map +0 -1
- package/dist/cjs/regular.expression.extensions.d.ts +0 -2
- package/dist/cjs/regular.expression.extensions.js +0 -423
- package/dist/cjs/regular.expression.extensions.js.map +0 -1
- package/dist/cjs/set.extensions.d.ts +0 -40
- package/dist/cjs/set.extensions.js +0 -355
- package/dist/cjs/set.extensions.js.map +0 -1
- package/dist/cjs/string.extensions.d.ts +0 -23
- package/dist/cjs/string.extensions.js +0 -704
- package/dist/cjs/string.extensions.js.map +0 -1
- package/dist/cjs/symbol.extensions.d.ts +0 -11
- package/dist/cjs/symbol.extensions.js +0 -735
- package/dist/cjs/symbol.extensions.js.map +0 -1
- package/dist/cjs/utils/copy.object.d.ts +0 -408
- package/dist/cjs/utils/copy.object.js +0 -720
- package/dist/cjs/utils/copy.object.js.map +0 -1
- package/dist/cjs/utils/descriptor.utils.d.ts +0 -298
- package/dist/cjs/utils/descriptor.utils.js +0 -889
- package/dist/cjs/utils/descriptor.utils.js.map +0 -1
- package/dist/cjs/utils/index.d.ts +0 -75
- package/dist/cjs/utils/index.js +0 -61
- package/dist/cjs/utils/index.js.map +0 -1
- package/dist/cjs/utils/stdout.d.ts +0 -742
- package/dist/cjs/utils/stdout.js +0 -1042
- package/dist/cjs/utils/stdout.js.map +0 -1
- package/dist/cjs/utils/toolkit.d.ts +0 -1898
- package/dist/cjs/utils/toolkit.js +0 -1378
- package/dist/cjs/utils/toolkit.js.map +0 -1
- package/dist/cjs/weakref.extensions.d.ts +0 -2
- package/dist/cjs/weakref.extensions.js +0 -19
- package/dist/cjs/weakref.extensions.js.map +0 -1
- package/dist/mjs/array.extensions.d.ts +0 -39
- package/dist/mjs/array.extensions.js +0 -474
- package/dist/mjs/array.extensions.js.map +0 -1
- package/dist/mjs/big.int.extension.d.ts +0 -31
- package/dist/mjs/big.int.extension.js +0 -270
- package/dist/mjs/big.int.extension.js.map +0 -1
- package/dist/mjs/classes/asyncIterable.d.ts +0 -126
- package/dist/mjs/classes/asyncIterable.js +0 -204
- package/dist/mjs/classes/asyncIterable.js.map +0 -1
- package/dist/mjs/classes/deferred.d.ts +0 -146
- package/dist/mjs/classes/deferred.js +0 -287
- package/dist/mjs/classes/deferred.js.map +0 -1
- package/dist/mjs/classes/descriptor.d.ts +0 -334
- package/dist/mjs/classes/descriptor.js +0 -533
- package/dist/mjs/classes/descriptor.js.map +0 -1
- package/dist/mjs/classes/enum.d.ts +0 -50
- package/dist/mjs/classes/enum.js +0 -400
- package/dist/mjs/classes/enum.js.map +0 -1
- package/dist/mjs/classes/index.d.ts +0 -15
- package/dist/mjs/classes/index.js +0 -46
- package/dist/mjs/classes/index.js.map +0 -1
- package/dist/mjs/classes/introspector.d.ts +0 -20
- package/dist/mjs/classes/introspector.js +0 -126
- package/dist/mjs/classes/introspector.js.map +0 -1
- package/dist/mjs/classes/iterable.d.ts +0 -169
- package/dist/mjs/classes/iterable.js +0 -263
- package/dist/mjs/classes/iterable.js.map +0 -1
- package/dist/mjs/classes/param.parser.d.ts +0 -221
- package/dist/mjs/classes/param.parser.js +0 -238
- package/dist/mjs/classes/param.parser.js.map +0 -1
- package/dist/mjs/classes/pluggable.proxy.d.ts +0 -153
- package/dist/mjs/classes/pluggable.proxy.js +0 -438
- package/dist/mjs/classes/pluggable.proxy.js.map +0 -1
- package/dist/mjs/classes/property.d.ts +0 -79
- package/dist/mjs/classes/property.js +0 -280
- package/dist/mjs/classes/property.js.map +0 -1
- package/dist/mjs/classes/refmap.d.ts +0 -238
- package/dist/mjs/classes/refmap.js +0 -417
- package/dist/mjs/classes/refmap.js.map +0 -1
- package/dist/mjs/classes/refset.d.ts +0 -186
- package/dist/mjs/classes/refset.js +0 -366
- package/dist/mjs/classes/refset.js.map +0 -1
- package/dist/mjs/classes/symkeys.d.ts +0 -349
- package/dist/mjs/classes/symkeys.js +0 -506
- package/dist/mjs/classes/symkeys.js.map +0 -1
- package/dist/mjs/classes/type.d.ts +0 -56
- package/dist/mjs/classes/type.js +0 -401
- package/dist/mjs/classes/type.js.map +0 -1
- package/dist/mjs/function.extensions.d.ts +0 -12
- package/dist/mjs/function.extensions.js +0 -755
- package/dist/mjs/function.extensions.js.map +0 -1
- package/dist/mjs/global.this.d.ts +0 -2
- package/dist/mjs/global.this.js +0 -264
- package/dist/mjs/global.this.js.map +0 -1
- package/dist/mjs/index.d.ts +0 -31
- package/dist/mjs/index.js +0 -206
- package/dist/mjs/index.js.map +0 -1
- package/dist/mjs/json.extensions.d.ts +0 -2
- package/dist/mjs/json.extensions.js +0 -106
- package/dist/mjs/json.extensions.js.map +0 -1
- package/dist/mjs/map.extensions.d.ts +0 -3
- package/dist/mjs/map.extensions.js +0 -140
- package/dist/mjs/map.extensions.js.map +0 -1
- package/dist/mjs/math.extension.d.ts +0 -14
- package/dist/mjs/math.extension.js +0 -68
- package/dist/mjs/math.extension.js.map +0 -1
- package/dist/mjs/number.extension.d.ts +0 -44
- package/dist/mjs/number.extension.js +0 -275
- package/dist/mjs/number.extension.js.map +0 -1
- package/dist/mjs/object.extensions.d.ts +0 -33
- package/dist/mjs/object.extensions.js +0 -1088
- package/dist/mjs/object.extensions.js.map +0 -1
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/proxy.extensions.d.ts +0 -2
- package/dist/mjs/proxy.extensions.js +0 -204
- package/dist/mjs/proxy.extensions.js.map +0 -1
- package/dist/mjs/reflect.extensions.d.ts +0 -14
- package/dist/mjs/reflect.extensions.js +0 -313
- package/dist/mjs/reflect.extensions.js.map +0 -1
- package/dist/mjs/regular.expression.extensions.d.ts +0 -2
- package/dist/mjs/regular.expression.extensions.js +0 -420
- package/dist/mjs/regular.expression.extensions.js.map +0 -1
- package/dist/mjs/set.extensions.d.ts +0 -40
- package/dist/mjs/set.extensions.js +0 -352
- package/dist/mjs/set.extensions.js.map +0 -1
- package/dist/mjs/string.extensions.d.ts +0 -23
- package/dist/mjs/string.extensions.js +0 -701
- package/dist/mjs/string.extensions.js.map +0 -1
- package/dist/mjs/symbol.extensions.d.ts +0 -11
- package/dist/mjs/symbol.extensions.js +0 -732
- package/dist/mjs/symbol.extensions.js.map +0 -1
- package/dist/mjs/utils/copy.object.d.ts +0 -408
- package/dist/mjs/utils/copy.object.js +0 -702
- package/dist/mjs/utils/copy.object.js.map +0 -1
- package/dist/mjs/utils/descriptor.utils.d.ts +0 -298
- package/dist/mjs/utils/descriptor.utils.js +0 -875
- package/dist/mjs/utils/descriptor.utils.js.map +0 -1
- package/dist/mjs/utils/index.d.ts +0 -75
- package/dist/mjs/utils/index.js +0 -45
- package/dist/mjs/utils/index.js.map +0 -1
- package/dist/mjs/utils/stdout.d.ts +0 -742
- package/dist/mjs/utils/stdout.js +0 -1037
- package/dist/mjs/utils/stdout.js.map +0 -1
- package/dist/mjs/utils/toolkit.d.ts +0 -1898
- package/dist/mjs/utils/toolkit.js +0 -1373
- package/dist/mjs/utils/toolkit.js.map +0 -1
- package/dist/mjs/weakref.extensions.d.ts +0 -2
- package/dist/mjs/weakref.extensions.js +0 -16
- package/dist/mjs/weakref.extensions.js.map +0 -1
- package/jsdoc-config.json +0 -31
- package/tsconfig.base.json +0 -28
- package/tsconfig.cjs.json +0 -8
- package/tsconfig.esm.json +0 -8
- package/vitest.config.js +0 -7
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Deferreds, which were first introduced by jQuery for browsers in the early
|
|
3
|
-
* 2000s, are a way to manage asynchronous operations. They have been widely
|
|
4
|
-
* used and replicated by engineers since then. Although the Promise class in
|
|
5
|
-
* modern JavaScript provides a static method called `withResolvers` that
|
|
6
|
-
* returns an object with similar properties to a Deferred, it is not directly
|
|
7
|
-
* supported by Node.js.
|
|
8
|
-
*
|
|
9
|
-
* ```
|
|
10
|
-
* const withResolvers = Promise.withResolvers()
|
|
11
|
-
* Reflect.has(withResolvers, 'promise') // true
|
|
12
|
-
* Reflect.has(withResolvers, 'resolve') // true
|
|
13
|
-
* Reflect.has(withResolvers, 'reject') // true
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* This Deferred class extends the Promise class, allowing it to capture the
|
|
17
|
-
* value or reason for easy access after resolution, akin to
|
|
18
|
-
* {@link Promise.withResolvers}. As it extends {@link Promise}, it is
|
|
19
|
-
* 'thenable' and works with `await` as if it were a native Promise. This
|
|
20
|
-
* allows seamless integration with code expecting Promise-like objects.
|
|
21
|
-
*/
|
|
22
|
-
export class Deferred extends Promise<any> {
|
|
23
|
-
/**
|
|
24
|
-
* A getter for the species symbol which returns a custom DeferredPromise
|
|
25
|
-
* class. This class extends from Deferred and is used to ensure that the
|
|
26
|
-
* constructor signature matches that of a Promise. The executor function
|
|
27
|
-
* passed to the constructor of this class is used to initialize the Deferred
|
|
28
|
-
* object with resolve and reject functions, similar to how a Promise would
|
|
29
|
-
* be initialized.
|
|
30
|
-
*
|
|
31
|
-
* @returns {DeferredPromise} A DeferredPromise class that extends Deferred.
|
|
32
|
-
*/
|
|
33
|
-
static get [Symbol.species](): DeferredPromise;
|
|
34
|
-
/**
|
|
35
|
-
* The constructor for Deferred instances. By default, a new Deferred will
|
|
36
|
-
* have three important properties: `promise`, `resolve`, and `reject`.
|
|
37
|
-
*
|
|
38
|
-
* The constructor takes an object called `options`. It can have the
|
|
39
|
-
* following properties:
|
|
40
|
-
*
|
|
41
|
-
* ```
|
|
42
|
-
* interface BaseDeferredOptions {
|
|
43
|
-
* // Deferreds store the value or reason. To turn this off, pass true
|
|
44
|
-
* // to this option.
|
|
45
|
-
* doNotTrackAnswers?: boolean;
|
|
46
|
-
* }
|
|
47
|
-
*
|
|
48
|
-
* interface ResolveDeferredOptions {
|
|
49
|
-
* // Passing in an option object with a resolve value will auto resolve
|
|
50
|
-
* // the Deferred with your value. An error will be raised if both
|
|
51
|
-
* // resolve and reject are supplied at the same time.
|
|
52
|
-
* resolve?: (value: any) => void;
|
|
53
|
-
* }
|
|
54
|
-
*
|
|
55
|
-
* interface RejectDeferredOptions {
|
|
56
|
-
* // Passing in an option object with a reject reason will auto reject
|
|
57
|
-
* // the Deferred with your reason. An error will be raised if both
|
|
58
|
-
* // resolve and reject are supplied at the same time.
|
|
59
|
-
* reject?: (reason: any) => void;
|
|
60
|
-
* }
|
|
61
|
-
*
|
|
62
|
-
* type DeferredOptions = BaseDeferredOptions &
|
|
63
|
-
* (ResolveDeferredOptions | RejectDeferredOptions)
|
|
64
|
-
* ```
|
|
65
|
-
*
|
|
66
|
-
* @param {object} options see above for examples on supported options, but
|
|
67
|
-
* when supplied, the constructor can take instructions on how to auto
|
|
68
|
-
* resolve or reject the deferred created here.
|
|
69
|
-
*/
|
|
70
|
-
constructor(options: object);
|
|
71
|
-
/**
|
|
72
|
-
* When the Deferred is settled with {@link Deferred.resolve}, the `value`
|
|
73
|
-
* passed to that function will be set here as well.
|
|
74
|
-
*
|
|
75
|
-
* @type {*}
|
|
76
|
-
*/
|
|
77
|
-
value: any;
|
|
78
|
-
/**
|
|
79
|
-
* When the Deferred is settled with {@link Deferred.reject}, the `reason`
|
|
80
|
-
* passed to that rejection will also be stored here.
|
|
81
|
-
*
|
|
82
|
-
* @type {*}
|
|
83
|
-
*/
|
|
84
|
-
reason: any;
|
|
85
|
-
/**
|
|
86
|
-
* Returns a boolean value that indicates whether or not this Deferred
|
|
87
|
-
* has been settled (either resolve or reject have been invoked).
|
|
88
|
-
*
|
|
89
|
-
* @returns {boolean} `true` if either {@link Deferred.resolve} or
|
|
90
|
-
* {@link Deferred.reject} have been invoked; `false` otherwise
|
|
91
|
-
*/
|
|
92
|
-
get settled(): boolean;
|
|
93
|
-
/**
|
|
94
|
-
* A getter that returns a boolean indicating whether the Deferred instance
|
|
95
|
-
* was rejected. This property can be used to check if the Deferred has been
|
|
96
|
-
* settled with a rejection. It is particularly useful in scenarios where
|
|
97
|
-
* the resolution status of the Deferred needs to be checked without
|
|
98
|
-
* accessing the rejection reason or invoking any additional logic.
|
|
99
|
-
*
|
|
100
|
-
* @returns {boolean} `true` if the Deferred was rejected, otherwise `false`.
|
|
101
|
-
*/
|
|
102
|
-
get wasRejected(): boolean;
|
|
103
|
-
/**
|
|
104
|
-
* A getter that returns a boolean indicating whether the Deferred instance
|
|
105
|
-
* was resolved. This property is useful for checking if the Deferred has been
|
|
106
|
-
* settled with a resolution, allowing for checks on the Deferred's status
|
|
107
|
-
* without needing to access the resolved value or trigger any additional
|
|
108
|
-
* logic.
|
|
109
|
-
*
|
|
110
|
-
* @returns {boolean} `true` if the Deferred was resolved, otherwise `false`.
|
|
111
|
-
*/
|
|
112
|
-
get wasResolved(): boolean;
|
|
113
|
-
/**
|
|
114
|
-
* Accessor for the promise managed by this Deferred instance.
|
|
115
|
-
*
|
|
116
|
-
* This getter provides access to the internal promise which is controlled
|
|
117
|
-
* by the Deferred's resolve and reject methods. It allows external code to
|
|
118
|
-
* attach callbacks for the resolution or rejection of the Deferred without
|
|
119
|
-
* the ability to directly resolve or reject it.
|
|
120
|
-
*
|
|
121
|
-
* @returns {Promise} The promise controlled by this Deferred instance.
|
|
122
|
-
*/
|
|
123
|
-
get promise(): Promise<any>;
|
|
124
|
-
/**
|
|
125
|
-
* Resolves the Deferred with the given value. If the value is a thenable
|
|
126
|
-
* (i.e., has a "then" method), the Deferred will "follow" that thenable,
|
|
127
|
-
* adopting its eventual state; otherwise, the Deferred will be fulfilled
|
|
128
|
-
* with the value. This function behaves the same as Promise.resolve.
|
|
129
|
-
*
|
|
130
|
-
* @param {*} value - The value to resolve the Deferred with.
|
|
131
|
-
* @returns {Promise} A Promise that is resolved with the given value.
|
|
132
|
-
*/
|
|
133
|
-
resolve(value: any): Promise<any>;
|
|
134
|
-
/**
|
|
135
|
-
* Rejects the Deferred with the given reason. This function behaves the
|
|
136
|
-
* same as Promise.reject. The Deferred will be rejected with the provided
|
|
137
|
-
* reason.
|
|
138
|
-
*
|
|
139
|
-
* @param {*} reason - The reason to reject the Deferred with.
|
|
140
|
-
* @returns {Promise} A Promise that is rejected with the given reason.
|
|
141
|
-
*/
|
|
142
|
-
reject(reason: any): Promise<any>;
|
|
143
|
-
#private;
|
|
144
|
-
}
|
|
145
|
-
export const DeferredExtension: Extension;
|
|
146
|
-
import { Extension } from '@nejs/extension';
|
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DeferredExtension = exports.Deferred = void 0;
|
|
4
|
-
const extension_1 = require("@nejs/extension");
|
|
5
|
-
/**
|
|
6
|
-
* Deferreds, which were first introduced by jQuery for browsers in the early
|
|
7
|
-
* 2000s, are a way to manage asynchronous operations. They have been widely
|
|
8
|
-
* used and replicated by engineers since then. Although the Promise class in
|
|
9
|
-
* modern JavaScript provides a static method called `withResolvers` that
|
|
10
|
-
* returns an object with similar properties to a Deferred, it is not directly
|
|
11
|
-
* supported by Node.js.
|
|
12
|
-
*
|
|
13
|
-
* ```
|
|
14
|
-
* const withResolvers = Promise.withResolvers()
|
|
15
|
-
* Reflect.has(withResolvers, 'promise') // true
|
|
16
|
-
* Reflect.has(withResolvers, 'resolve') // true
|
|
17
|
-
* Reflect.has(withResolvers, 'reject') // true
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* This Deferred class extends the Promise class, allowing it to capture the
|
|
21
|
-
* value or reason for easy access after resolution, akin to
|
|
22
|
-
* {@link Promise.withResolvers}. As it extends {@link Promise}, it is
|
|
23
|
-
* 'thenable' and works with `await` as if it were a native Promise. This
|
|
24
|
-
* allows seamless integration with code expecting Promise-like objects.
|
|
25
|
-
*/
|
|
26
|
-
class Deferred extends Promise {
|
|
27
|
-
/**
|
|
28
|
-
* The promise backing this deferred object. Created when the constructor
|
|
29
|
-
* runs, this promise is what all `Promise.prototype` functions are routed
|
|
30
|
-
* to.
|
|
31
|
-
*
|
|
32
|
-
* @type {Promise}
|
|
33
|
-
*/
|
|
34
|
-
#promise = null;
|
|
35
|
-
/**
|
|
36
|
-
* The reject() resolver that will be assigned when a new instance is
|
|
37
|
-
* created. Invoking this function with or without a `reason` will cause
|
|
38
|
-
* the deferred's promise to be settled.
|
|
39
|
-
*
|
|
40
|
-
* @type {function}
|
|
41
|
-
*/
|
|
42
|
-
#reject = null;
|
|
43
|
-
/**
|
|
44
|
-
* The resolve() resolver that will be assigned when a new instance is
|
|
45
|
-
* created. Invoking this function with or without a `value` will cause
|
|
46
|
-
* the deferred's promise to be settled.
|
|
47
|
-
*
|
|
48
|
-
* @type {function}
|
|
49
|
-
*/
|
|
50
|
-
#resolve = null;
|
|
51
|
-
#rejected = false;
|
|
52
|
-
#resolved = false;
|
|
53
|
-
/**
|
|
54
|
-
* When the Deferred is settled with {@link Deferred.resolve}, the `value`
|
|
55
|
-
* passed to that function will be set here as well.
|
|
56
|
-
*
|
|
57
|
-
* @type {*}
|
|
58
|
-
*/
|
|
59
|
-
value = null;
|
|
60
|
-
/**
|
|
61
|
-
* When the Deferred is settled with {@link Deferred.reject}, the `reason`
|
|
62
|
-
* passed to that rejection will also be stored here.
|
|
63
|
-
*
|
|
64
|
-
* @type {*}
|
|
65
|
-
*/
|
|
66
|
-
reason = null;
|
|
67
|
-
/**
|
|
68
|
-
* When either {@link Deferred.resolve} or {@link Deferred.reject} are called,
|
|
69
|
-
* this property is set to `true`. Its current status at any time can be
|
|
70
|
-
* queried using the {@link Deferred.settled} getter.
|
|
71
|
-
*
|
|
72
|
-
* @type {boolean}
|
|
73
|
-
*/
|
|
74
|
-
#settled = false;
|
|
75
|
-
/**
|
|
76
|
-
* The constructor for Deferred instances. By default, a new Deferred will
|
|
77
|
-
* have three important properties: `promise`, `resolve`, and `reject`.
|
|
78
|
-
*
|
|
79
|
-
* The constructor takes an object called `options`. It can have the
|
|
80
|
-
* following properties:
|
|
81
|
-
*
|
|
82
|
-
* ```
|
|
83
|
-
* interface BaseDeferredOptions {
|
|
84
|
-
* // Deferreds store the value or reason. To turn this off, pass true
|
|
85
|
-
* // to this option.
|
|
86
|
-
* doNotTrackAnswers?: boolean;
|
|
87
|
-
* }
|
|
88
|
-
*
|
|
89
|
-
* interface ResolveDeferredOptions {
|
|
90
|
-
* // Passing in an option object with a resolve value will auto resolve
|
|
91
|
-
* // the Deferred with your value. An error will be raised if both
|
|
92
|
-
* // resolve and reject are supplied at the same time.
|
|
93
|
-
* resolve?: (value: any) => void;
|
|
94
|
-
* }
|
|
95
|
-
*
|
|
96
|
-
* interface RejectDeferredOptions {
|
|
97
|
-
* // Passing in an option object with a reject reason will auto reject
|
|
98
|
-
* // the Deferred with your reason. An error will be raised if both
|
|
99
|
-
* // resolve and reject are supplied at the same time.
|
|
100
|
-
* reject?: (reason: any) => void;
|
|
101
|
-
* }
|
|
102
|
-
*
|
|
103
|
-
* type DeferredOptions = BaseDeferredOptions &
|
|
104
|
-
* (ResolveDeferredOptions | RejectDeferredOptions)
|
|
105
|
-
* ```
|
|
106
|
-
*
|
|
107
|
-
* @param {object} options see above for examples on supported options, but
|
|
108
|
-
* when supplied, the constructor can take instructions on how to auto
|
|
109
|
-
* resolve or reject the deferred created here.
|
|
110
|
-
*/
|
|
111
|
-
constructor(options) {
|
|
112
|
-
// Check if options is an object, if not, assign an empty object to config
|
|
113
|
-
const config = (options && typeof (options) === 'object'
|
|
114
|
-
? options
|
|
115
|
-
: {});
|
|
116
|
-
// Throw an error if both resolve and reject options are provided
|
|
117
|
-
if (config?.resolve && config?.reject) {
|
|
118
|
-
throw new TypeError('resolve and reject options cannot be simultaneously provided');
|
|
119
|
-
}
|
|
120
|
-
// Create an empty object to store the resolve and reject functions
|
|
121
|
-
let _resolve, _reject;
|
|
122
|
-
// Create a new promise and assign its resolve and reject functions to resolvers
|
|
123
|
-
super((resolve, reject) => {
|
|
124
|
-
_resolve = resolve;
|
|
125
|
-
_reject = reject;
|
|
126
|
-
if (config?.executor && typeof (config?.executor) === 'function') {
|
|
127
|
-
config?.executor(resolve, reject);
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
// Define the resolve function for the Deferred instance
|
|
131
|
-
this.#resolve = (value) => {
|
|
132
|
-
// If doNotTrackAnswers is not set to true, store the value
|
|
133
|
-
if (config?.doNotTrackAnswers !== true) {
|
|
134
|
-
this.value = value;
|
|
135
|
-
}
|
|
136
|
-
// Mark the Deferred instance as settled
|
|
137
|
-
this.#settled = true;
|
|
138
|
-
// Mark the Deferred instance as resolved
|
|
139
|
-
this.#resolved = true;
|
|
140
|
-
// Resolve the promise with the provided value
|
|
141
|
-
return _resolve(value);
|
|
142
|
-
};
|
|
143
|
-
// Define the reject function for the Deferred instance
|
|
144
|
-
this.#reject = async (reason) => {
|
|
145
|
-
// If doNotTrackAnswers is not set to true, store the reason
|
|
146
|
-
if (config?.doNotTrackAnswers !== true) {
|
|
147
|
-
this.reason = reason;
|
|
148
|
-
}
|
|
149
|
-
// Mark the Deferred instance as settled
|
|
150
|
-
this.#settled = true;
|
|
151
|
-
// Mark the Deferred as being rejected.
|
|
152
|
-
this.#rejected = true;
|
|
153
|
-
// Reject the promise with the provided reason
|
|
154
|
-
return _reject(reason);
|
|
155
|
-
};
|
|
156
|
-
this.#promise = this;
|
|
157
|
-
// If a resolve option is provided, resolve the Deferred instance with it
|
|
158
|
-
if (config?.resolve) {
|
|
159
|
-
this.#resolve(config?.resolve);
|
|
160
|
-
}
|
|
161
|
-
// If a reject option is provided, reject the Deferred instance with it
|
|
162
|
-
else if (config?.reject) {
|
|
163
|
-
this.#reject(config?.reject);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Returns a boolean value that indicates whether or not this Deferred
|
|
168
|
-
* has been settled (either resolve or reject have been invoked).
|
|
169
|
-
*
|
|
170
|
-
* @returns {boolean} `true` if either {@link Deferred.resolve} or
|
|
171
|
-
* {@link Deferred.reject} have been invoked; `false` otherwise
|
|
172
|
-
*/
|
|
173
|
-
get settled() {
|
|
174
|
-
return this.#settled;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* A getter that returns a boolean indicating whether the Deferred instance
|
|
178
|
-
* was rejected. This property can be used to check if the Deferred has been
|
|
179
|
-
* settled with a rejection. It is particularly useful in scenarios where
|
|
180
|
-
* the resolution status of the Deferred needs to be checked without
|
|
181
|
-
* accessing the rejection reason or invoking any additional logic.
|
|
182
|
-
*
|
|
183
|
-
* @returns {boolean} `true` if the Deferred was rejected, otherwise `false`.
|
|
184
|
-
*/
|
|
185
|
-
get wasRejected() {
|
|
186
|
-
return this.#rejected;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* A getter that returns a boolean indicating whether the Deferred instance
|
|
190
|
-
* was resolved. This property is useful for checking if the Deferred has been
|
|
191
|
-
* settled with a resolution, allowing for checks on the Deferred's status
|
|
192
|
-
* without needing to access the resolved value or trigger any additional
|
|
193
|
-
* logic.
|
|
194
|
-
*
|
|
195
|
-
* @returns {boolean} `true` if the Deferred was resolved, otherwise `false`.
|
|
196
|
-
*/
|
|
197
|
-
get wasResolved() {
|
|
198
|
-
return this.#resolved;
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Accessor for the promise managed by this Deferred instance.
|
|
202
|
-
*
|
|
203
|
-
* This getter provides access to the internal promise which is controlled
|
|
204
|
-
* by the Deferred's resolve and reject methods. It allows external code to
|
|
205
|
-
* attach callbacks for the resolution or rejection of the Deferred without
|
|
206
|
-
* the ability to directly resolve or reject it.
|
|
207
|
-
*
|
|
208
|
-
* @returns {Promise} The promise controlled by this Deferred instance.
|
|
209
|
-
*/
|
|
210
|
-
get promise() {
|
|
211
|
-
return this.#promise;
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Resolves the Deferred with the given value. If the value is a thenable
|
|
215
|
-
* (i.e., has a "then" method), the Deferred will "follow" that thenable,
|
|
216
|
-
* adopting its eventual state; otherwise, the Deferred will be fulfilled
|
|
217
|
-
* with the value. This function behaves the same as Promise.resolve.
|
|
218
|
-
*
|
|
219
|
-
* @param {*} value - The value to resolve the Deferred with.
|
|
220
|
-
* @returns {Promise} A Promise that is resolved with the given value.
|
|
221
|
-
*/
|
|
222
|
-
resolve(value) {
|
|
223
|
-
return this.#resolve(value);
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Rejects the Deferred with the given reason. This function behaves the
|
|
227
|
-
* same as Promise.reject. The Deferred will be rejected with the provided
|
|
228
|
-
* reason.
|
|
229
|
-
*
|
|
230
|
-
* @param {*} reason - The reason to reject the Deferred with.
|
|
231
|
-
* @returns {Promise} A Promise that is rejected with the given reason.
|
|
232
|
-
*/
|
|
233
|
-
reject(reason) {
|
|
234
|
-
return this.#reject(reason);
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* Customizes the output of `util.inspect` on instances of Deferred when
|
|
238
|
-
* used in Node.js. This method is invoked by Node.js's `util.inspect`
|
|
239
|
-
* utility to format the inspection output of a Deferred instance.
|
|
240
|
-
*
|
|
241
|
-
* The output includes the state of the Deferred (resolved, rejected, or
|
|
242
|
-
* unsettled) along with the resolved value or rejection reason, if
|
|
243
|
-
* applicable. This provides a quick, readable status of the Deferred
|
|
244
|
-
* instance directly in the console or debugging tools.
|
|
245
|
-
*
|
|
246
|
-
* @param {number} depth The depth to which `util.inspect` will recurse.
|
|
247
|
-
* @param {object} options Formatting options provided by `util.inspect`.
|
|
248
|
-
* @param {function} inspect Reference to the `util.inspect` function.
|
|
249
|
-
* @returns {string} A formatted string representing the Deferred instance.
|
|
250
|
-
*/
|
|
251
|
-
[Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
|
|
252
|
-
return [
|
|
253
|
-
'\x1b[1mDeferred [\x1b[22;3mPromise\x1b[23;1m]\x1b[22m ',
|
|
254
|
-
'{ ',
|
|
255
|
-
(this.settled
|
|
256
|
-
? (this.wasResolved
|
|
257
|
-
? `resolved with \x1b[32m${this.value}\x1b[39m`
|
|
258
|
-
: `rejected with \x1b[31m${this.reason?.message ?? this.reason}\x1b[39m`)
|
|
259
|
-
: '\x1b[33munsettled valued or reason\x1b[39m'),
|
|
260
|
-
' }'
|
|
261
|
-
].join('');
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* A getter for the species symbol which returns a custom DeferredPromise
|
|
265
|
-
* class. This class extends from Deferred and is used to ensure that the
|
|
266
|
-
* constructor signature matches that of a Promise. The executor function
|
|
267
|
-
* passed to the constructor of this class is used to initialize the Deferred
|
|
268
|
-
* object with resolve and reject functions, similar to how a Promise would
|
|
269
|
-
* be initialized.
|
|
270
|
-
*
|
|
271
|
-
* @returns {DeferredPromise} A DeferredPromise class that extends Deferred.
|
|
272
|
-
*/
|
|
273
|
-
static get [Symbol.species]() {
|
|
274
|
-
return class DeferredPromise extends Deferred {
|
|
275
|
-
/**
|
|
276
|
-
* The constructor for the DeferredPromise class.
|
|
277
|
-
* It takes an executor function which is used to initialize the Deferred.
|
|
278
|
-
*
|
|
279
|
-
* @param {Function} executor - A function that is passed with the resolve
|
|
280
|
-
* and reject functions. The executor is expected to initialize the
|
|
281
|
-
* Deferred by calling resolve or reject at some point.
|
|
282
|
-
*/
|
|
283
|
-
constructor(executor) {
|
|
284
|
-
super({ executor });
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
exports.Deferred = Deferred;
|
|
290
|
-
exports.DeferredExtension = new extension_1.Extension(Deferred);
|
|
291
|
-
//# sourceMappingURL=deferred.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"deferred.js","sourceRoot":"","sources":["../../../src/classes/deferred.js"],"names":[],"mappings":";;;AAAA,+CAA2C;AAE3C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,QAAS,SAAQ,OAAO;IACnC;;;;;;OAMG;IACH,QAAQ,GAAG,IAAI,CAAA;IAEf;;;;;;OAMG;IACH,OAAO,GAAG,IAAI,CAAA;IAEd;;;;;;OAMG;IACH,QAAQ,GAAG,IAAI,CAAA;IAEf,SAAS,GAAG,KAAK,CAAA;IAEjB,SAAS,GAAG,KAAK,CAAA;IAEjB;;;;;OAKG;IACH,KAAK,GAAG,IAAI,CAAA;IAEZ;;;;;OAKG;IACH,MAAM,GAAG,IAAI,CAAA;IAEb;;;;;;OAMG;IACH,QAAQ,GAAG,KAAK,CAAA;IAEhB;;;;;;;;;;;;;;;;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