@nejs/basic-extensions 1.7.0 → 1.9.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.
Files changed (37) hide show
  1. package/README.md +518 -103
  2. package/dist/@nejs/basic-extensions.bundle.1.8.0.js +4 -0
  3. package/dist/@nejs/basic-extensions.bundle.1.8.0.js.map +7 -0
  4. package/dist/cjs/functionextensions.d.ts +1 -0
  5. package/dist/cjs/functionextensions.js +128 -18
  6. package/dist/cjs/functionextensions.js.map +1 -1
  7. package/dist/cjs/index.d.ts +5 -2
  8. package/dist/cjs/index.js +28 -5
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/newClasses/deferred.d.ts +126 -0
  11. package/dist/cjs/newClasses/deferred.js +306 -0
  12. package/dist/cjs/newClasses/deferred.js.map +1 -0
  13. package/dist/cjs/setextensions.d.ts +2 -0
  14. package/dist/cjs/setextensions.js +204 -0
  15. package/dist/cjs/setextensions.js.map +1 -0
  16. package/dist/mjs/functionextensions.d.ts +1 -0
  17. package/dist/mjs/functionextensions.js +127 -17
  18. package/dist/mjs/functionextensions.js.map +1 -1
  19. package/dist/mjs/index.d.ts +5 -2
  20. package/dist/mjs/index.js +29 -6
  21. package/dist/mjs/index.js.map +1 -1
  22. package/dist/mjs/newClasses/deferred.d.ts +126 -0
  23. package/dist/mjs/newClasses/deferred.js +230 -0
  24. package/dist/mjs/newClasses/deferred.js.map +1 -0
  25. package/dist/mjs/setextensions.d.ts +2 -0
  26. package/dist/mjs/setextensions.js +201 -0
  27. package/dist/mjs/setextensions.js.map +1 -0
  28. package/docs/index.html +6119 -3461
  29. package/package.json +7 -4
  30. package/src/functionextensions.js +150 -24
  31. package/src/index.js +35 -6
  32. package/src/newClasses/deferred.js +253 -0
  33. package/src/setextensions.js +242 -0
  34. package/tests/newClasses/deferred.test.js +86 -0
  35. package/tests/setextensions.test.js +89 -0
  36. package/dist/@nejs/basic-extensions.bundle.1.6.1.js +0 -4
  37. package/dist/@nejs/basic-extensions.bundle.1.6.1.js.map +0 -7
@@ -1,4 +1,6 @@
1
1
  import { Patch } from '@nejs/extension';
2
+ import { ObjectExtensions } from './objectextensions.js';
3
+ const { getStringTag } = ObjectExtensions.patches;
2
4
  /**
3
5
  * The `FunctionExtensions` class is a patch applied to the built-in JavaScript
4
6
  * `Function` constructor. It extends `Function` with additional utility methods
@@ -9,6 +11,68 @@ import { Patch } from '@nejs/extension';
9
11
  * capabilities of function handling and introspection in JavaScript.
10
12
  */
11
13
  export const FunctionExtensions = new Patch(Function, {
14
+ /**
15
+ * Determines if a given value is an asynchronous function. It checks if the
16
+ * value is an instance of `Function` and if its string representation
17
+ * includes the keyword 'Async'. This method is particularly useful for
18
+ * identifying async functions.
19
+ *
20
+ * @param {*} value - The value to be checked.
21
+ * @returns {boolean} Returns `true` if the value is an async function,
22
+ * otherwise `false`.
23
+ */
24
+ isAsync(value) {
25
+ const stringTag = /(\w+)]/g.exec(Object.prototype.toString.call(value))[1];
26
+ return (value instanceof Function &&
27
+ stringTag.includes('Async'));
28
+ },
29
+ /**
30
+ * The function checks if a given value is an async generator function
31
+ *
32
+ * @param {any} value - The `value` parameter is the value that we want to
33
+ * check if it is a generator function.
34
+ * @returns {boolean} `true` if the value is an instance of a function and
35
+ * its string tag is 'AsyncGeneratorFunction', otherwise it returns `false`.
36
+ */
37
+ isAsyncGenerator(value) {
38
+ const stringTag = getStringTag(value);
39
+ return (value instanceof Function &&
40
+ stringTag == 'AsyncGeneratorFunction');
41
+ },
42
+ /**
43
+ * Checks if a given value is an arrow function. It verifies if the value is
44
+ * an instance of `Function`, if its string representation includes the '=>'
45
+ * symbol, and if it lacks a prototype, which is a characteristic of arrow
46
+ * functions in JavaScript.
47
+ *
48
+ * @param {*} value - The value to be checked.
49
+ * @returns {boolean} Returns `true` if the value is an arrow function,
50
+ * otherwise `false`.
51
+ */
52
+ isBigArrow(value) {
53
+ return (value instanceof Function &&
54
+ String(value).includes('=>') &&
55
+ !String(value).startsWith('bound') &&
56
+ !Reflect.has(value, 'prototype'));
57
+ },
58
+ /**
59
+ * Determines if a given value is a bound function. Bound functions are
60
+ * created using the `Function.prototype.bind` method, which allows setting
61
+ * the `this` value at the time of binding. This method checks if the value
62
+ * is an instance of `Function`, if its string representation starts with
63
+ * 'bound', and if it lacks a `prototype` property. These characteristics
64
+ * are indicative of bound functions in JavaScript.
65
+ *
66
+ * @param {*} value - The value to be checked, typically a function.
67
+ * @returns {boolean} Returns `true` if the value is a bound function,
68
+ * otherwise `false`. Bound functions have a specific format in their
69
+ * string representation and do not have their own `prototype` property.
70
+ */
71
+ isBound(value) {
72
+ return (value instanceof Function &&
73
+ String(value).startsWith('bound') &&
74
+ !Reflect.has(value, 'prototype'));
75
+ },
12
76
  /**
13
77
  * Determines if a given value is a class. It checks if the value is an
14
78
  * instance of `Function` and if its string representation includes the
@@ -32,22 +96,43 @@ export const FunctionExtensions = new Patch(Function, {
32
96
  * otherwise `false`.
33
97
  */
34
98
  isFunction(value) {
35
- return value instanceof Function;
99
+ return value instanceof Function && !Function.isClass(value);
36
100
  },
101
+ /**
102
+ * The function checks if a given value is a generator function
103
+ *
104
+ * @param {any} value - The `value` parameter is the value that we want to
105
+ * check if it is a generator function.
106
+ * @returns {boolean} `true` if the value is an instance of a function and
107
+ * its string tag is 'GeneratorFunction', otherwise it returns `false`.
108
+ */
109
+ isGenerator(value) {
110
+ const stringTag = getStringTag(value);
111
+ return (value instanceof Function &&
112
+ stringTag == 'GeneratorFunction');
113
+ },
114
+ });
115
+ export const FunctionPrototypeExtensions = new Patch(Function.prototype, {
37
116
  /**
38
117
  * Determines if a given value is an asynchronous function. It checks if the
39
118
  * value is an instance of `Function` and if its string representation
40
119
  * includes the keyword 'Async'. This method is particularly useful for
41
120
  * identifying async functions.
42
121
  *
43
- * @param {*} value - The value to be checked.
44
122
  * @returns {boolean} Returns `true` if the value is an async function,
45
123
  * otherwise `false`.
46
124
  */
47
- isAsync(value) {
48
- const stringTag = /(\w+)]/g.exec(Object.prototype.toString.call(value))[1];
49
- return (value instanceof Function &&
50
- stringTag.includes('Async'));
125
+ get isAsync() {
126
+ return Function.isAsync(this);
127
+ },
128
+ /**
129
+ * The function checks if a given value is an async generator function
130
+ *
131
+ * @returns {boolean} `true` if the value is an instance of a function and
132
+ * its string tag is 'AsyncGeneratorFunction', otherwise it returns `false`.
133
+ */
134
+ get isAsyncGenerator() {
135
+ return Function.isAsyncGenerator(this);
51
136
  },
52
137
  /**
53
138
  * Checks if a given value is an arrow function. It verifies if the value is
@@ -55,15 +140,11 @@ export const FunctionExtensions = new Patch(Function, {
55
140
  * symbol, and if it lacks a prototype, which is a characteristic of arrow
56
141
  * functions in JavaScript.
57
142
  *
58
- * @param {*} value - The value to be checked.
59
143
  * @returns {boolean} Returns `true` if the value is an arrow function,
60
144
  * otherwise `false`.
61
145
  */
62
- isBigArrow(value) {
63
- return (value instanceof Function &&
64
- String(value).includes('=>') &&
65
- !String(value).startsWith('bound') &&
66
- !Reflect.has(value, 'prototype'));
146
+ get isBigArrow() {
147
+ return Function.isBigArrow(this);
67
148
  },
68
149
  /**
69
150
  * Determines if a given value is a bound function. Bound functions are
@@ -73,15 +154,44 @@ export const FunctionExtensions = new Patch(Function, {
73
154
  * 'bound', and if it lacks a `prototype` property. These characteristics
74
155
  * are indicative of bound functions in JavaScript.
75
156
  *
76
- * @param {*} value - The value to be checked, typically a function.
77
157
  * @returns {boolean} Returns `true` if the value is a bound function,
78
158
  * otherwise `false`. Bound functions have a specific format in their
79
159
  * string representation and do not have their own `prototype` property.
80
160
  */
81
- isBound(value) {
82
- return (value instanceof Function &&
83
- String(value).startsWith('bound') &&
84
- !Reflect.has(value, 'prototype'));
161
+ get isBound() {
162
+ return Function.isBound(this);
163
+ },
164
+ /**
165
+ * Determines if a given value is a class. It checks if the value is an
166
+ * instance of `Function` and if its string representation includes the
167
+ * keyword 'class'. This method is useful for distinguishing classes from
168
+ * other function types in JavaScript.
169
+ *
170
+ * @returns {boolean} Returns `true` if the value is a class, otherwise
171
+ * `false`.
172
+ */
173
+ get isClass() {
174
+ return Function.isClass(this);
175
+ },
176
+ /**
177
+ * Checks if a given value is a regular function. This method verifies if
178
+ * the value is an instance of `Function`, which includes regular functions,
179
+ * classes, and async functions but excludes arrow functions.
180
+ *
181
+ * @returns {boolean} Returns `true` if the value is a regular function,
182
+ * otherwise `false`.
183
+ */
184
+ get isFunction() {
185
+ return Function.isFunction(this);
186
+ },
187
+ /**
188
+ * The function checks if a given value is a generator function
189
+ *
190
+ * @returns {boolean} `true` if the value is an instance of a function and
191
+ * its string tag is 'GeneratorFunction', otherwise it returns `false`.
192
+ */
193
+ get isGenerator() {
194
+ return Function.isGenerator(this);
85
195
  },
86
196
  });
87
197
  //# sourceMappingURL=functionextensions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"functionextensions.js","sourceRoot":"","sources":["../../src/functionextensions.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AAEvC;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,KAAK,CAAC,QAAQ,EAAE;IACpD;;;;;;;;;OASG;IACH,OAAO,CAAC,KAAK;QACX,OAAO,KAAK,YAAY,QAAQ,IAAI,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;IACtE,CAAC;IAED;;;;;;;;OAQG;IACH,UAAU,CAAC,KAAK;QACd,OAAO,KAAK,YAAY,QAAQ,CAAC;IACnC,CAAC;IAED;;;;;;;;;OASG;IACH,OAAO,CAAC,KAAK;QACX,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAC1E,OAAO,CACL,KAAK,YAAY,QAAQ;YACzB,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,CAC5B,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IACH,IAAI,OAAO;QACT,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC/B,CAAC;IAED;;;;;OAKG;IACH,IAAI,gBAAgB;QAClB,OAAO,QAAQ,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,IAAI,UAAU;QACZ,OAAO,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;IAClC,CAAC;IAED;;;;;;;;;;;OAWG;IACH,IAAI,OAAO;QACT,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC/B,CAAC;IAED;;;;;;;;OAQG;IACH,IAAI,OAAO;QACT,OAAO,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC/B,CAAC;IAED;;;;;;;OAOG;IACH,IAAI,UAAU;QACZ,OAAO,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;IAClC,CAAC;IAED;;;;;OAKG;IACH,IAAI,WAAW;QACb,OAAO,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;IACnC,CAAC;CACF,CAAC,CAAA"}
@@ -5,6 +5,9 @@ declare namespace results {
5
5
  export { Patches as patches };
6
6
  export { all };
7
7
  }
8
- export const Extensions: {};
9
- export const Patches: Map<Object, import("@nejs/extension").Patch>;
8
+ export namespace Extensions {
9
+ export { GlobalFunctionsAndProps as global };
10
+ }
11
+ export const Patches: Map<any, any>;
10
12
  export const Controls: {};
13
+ import { GlobalFunctionsAndProps } from './globals.js';
package/dist/mjs/index.js CHANGED
@@ -1,6 +1,7 @@
1
- import { FunctionExtensions } from './functionextensions.js';
1
+ import { FunctionExtensions, FunctionPrototypeExtensions } from './functionextensions.js';
2
2
  import { ObjectExtensions, ObjectPrototypeExtensions } from './objectextensions.js';
3
3
  import { MapPrototypeExtensions } from './mapextensions.js';
4
+ import { SetPrototypeExtensions } from './setextensions.js';
4
5
  import { ReflectExtensions } from './reflectextensions.js';
5
6
  import { StringExtensions } from './stringextensions.js';
6
7
  import { SymbolExtensions } from './symbolextensions.js';
@@ -9,27 +10,37 @@ import { DescriptorExtensions, Descriptor } from './newClasses/descriptor.js';
9
10
  import { GlobalFunctionsAndProps } from './globals.js';
10
11
  import { RefSetExtensions } from './newClasses/refset.js';
11
12
  import { RefMapExtensions } from './newClasses/refmap.js';
13
+ import { DeferredExtension } from './newClasses/deferred.js';
12
14
  import { AsyncIteratorExtensions, AsyncIterableExtensions } from './newClasses/asyncIterable.js';
13
15
  import { IteratorExtensions, IterableExtensions } from './newClasses/iterable.js';
14
- const Patches = new Map([
16
+ const StaticPatches = [
15
17
  [Object, ObjectExtensions],
16
18
  [Function, FunctionExtensions],
17
19
  [Reflect, ReflectExtensions],
18
20
  [String, StringExtensions],
19
21
  [Symbol, SymbolExtensions],
22
+ ];
23
+ const InstancePatches = [
20
24
  [Object.prototype, ObjectPrototypeExtensions],
25
+ [Function.prototype, FunctionPrototypeExtensions],
21
26
  [Array.prototype, ArrayPrototypeExtensions],
22
27
  [Map.prototype, MapPrototypeExtensions],
23
- [globalThis, GlobalFunctionsAndProps],
28
+ [Set.prototype, SetPrototypeExtensions],
29
+ ];
30
+ const Patches = new Map([
31
+ ...StaticPatches,
32
+ ...InstancePatches,
24
33
  ]);
25
34
  const Extensions = {
26
- [DescriptorExtensions.key]: DescriptorExtensions,
35
+ global: GlobalFunctionsAndProps,
27
36
  [AsyncIterableExtensions.key]: AsyncIterableExtensions,
28
37
  [AsyncIteratorExtensions.key]: AsyncIteratorExtensions,
38
+ [DeferredExtension.key]: DeferredExtension,
39
+ [DescriptorExtensions.key]: DescriptorExtensions,
29
40
  [IterableExtensions.key]: IterableExtensions,
30
41
  [IteratorExtensions.key]: IteratorExtensions,
31
- [RefSetExtensions.key]: RefSetExtensions,
32
42
  [RefMapExtensions.key]: RefMapExtensions,
43
+ [RefSetExtensions.key]: RefSetExtensions,
33
44
  };
34
45
  const Controls = {};
35
46
  Object.assign(Controls, {
@@ -40,16 +51,28 @@ Object.assign(Controls, {
40
51
  enablePatches() {
41
52
  Patches.forEach((extension) => { extension.apply(); });
42
53
  },
54
+ enableStaticPatches(filter = (extension) => true) {
55
+ StaticPatches.filter(filter).forEach(extension => extension.apply());
56
+ },
57
+ enableInstancePatches(filter = (extension) => true) {
58
+ InstancePatches.filter(filter).forEach(extension => extension.apply());
59
+ },
43
60
  enableExtensions() {
44
61
  Object.values(Extensions).forEach((extension) => { extension.apply(); });
45
62
  },
46
- disableAll(owners) {
63
+ disableAll() {
47
64
  Controls.disablePatches();
48
65
  Controls.disableExtensions();
49
66
  },
50
67
  disablePatches() {
51
68
  Patches.forEach((extension) => { extension.revert(); });
52
69
  },
70
+ disableStaticPatches(filter = (extension) => true) {
71
+ StaticPatches.filter(filter).forEach(extension => extension.revert());
72
+ },
73
+ disableInstancePatches(filter = (extension) => true) {
74
+ InstancePatches.filter(filter).forEach(extension => extension.revert());
75
+ },
53
76
  disableExtensions() {
54
77
  Object.values(Extensions).forEach((extension) => { extension.revert(); });
55
78
  },
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAA;AACnF,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAA;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAA;AAC/D,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AAC7E,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAA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AG,IAAI,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;;gBAEnD,WAAW,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAA;YAEnC,OAAO,WAAW,CAAA;QACpB,CAAC,EAAE,WAAW,CAAC,CAAA;QAEf,OAAO,WAAW,CAAA;IACpB,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN,OAAO,IAAI,CAAA;AACb,CAAC,CAAC,EAAE,CAAA;AAEJ,MAAM,OAAO,GAAG;IACd,GAAG,QAAQ;IACX,UAAU,EAAE,UAAU;IACtB,OAAO,EAAE,OAAO;IAChB,GAAG;CACJ,CAAA;AAED,eAAe,OAAO,CAAA;AAEtB,OAAO,EACL,UAAU,EACV,OAAO,EACP,QAAQ,GACT,CAAA"}
@@ -0,0 +1,126 @@
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
+ * Accessor for the promise managed by this Deferred instance.
95
+ *
96
+ * This getter provides access to the internal promise which is controlled
97
+ * by the Deferred's resolve and reject methods. It allows external code to
98
+ * attach callbacks for the resolution or rejection of the Deferred without
99
+ * the ability to directly resolve or reject it.
100
+ *
101
+ * @returns {Promise} The promise controlled by this Deferred instance.
102
+ */
103
+ get promise(): Promise<any>;
104
+ /**
105
+ * Resolves the Deferred with the given value. If the value is a thenable
106
+ * (i.e., has a "then" method), the Deferred will "follow" that thenable,
107
+ * adopting its eventual state; otherwise, the Deferred will be fulfilled
108
+ * with the value. This function behaves the same as Promise.resolve.
109
+ *
110
+ * @param {*} value - The value to resolve the Deferred with.
111
+ * @returns {Promise} A Promise that is resolved with the given value.
112
+ */
113
+ resolve(value: any): Promise<any>;
114
+ /**
115
+ * Rejects the Deferred with the given reason. This function behaves the
116
+ * same as Promise.reject. The Deferred will be rejected with the provided
117
+ * reason.
118
+ *
119
+ * @param {*} reason - The reason to reject the Deferred with.
120
+ * @returns {Promise} A Promise that is rejected with the given reason.
121
+ */
122
+ reject(reason: any): Promise<any>;
123
+ #private;
124
+ }
125
+ export const DeferredExtension: Extension;
126
+ import { Extension } from '@nejs/extension';
@@ -0,0 +1,230 @@
1
+ import { Extension } from '@nejs/extension';
2
+ /**
3
+ * Deferreds, which were first introduced by jQuery for browsers in the early
4
+ * 2000s, are a way to manage asynchronous operations. They have been widely
5
+ * used and replicated by engineers since then. Although the Promise class in
6
+ * modern JavaScript provides a static method called `withResolvers` that
7
+ * returns an object with similar properties to a Deferred, it is not directly
8
+ * supported by Node.js.
9
+ *
10
+ * ```
11
+ * const withResolvers = Promise.withResolvers()
12
+ * Reflect.has(withResolvers, 'promise') // true
13
+ * Reflect.has(withResolvers, 'resolve') // true
14
+ * Reflect.has(withResolvers, 'reject') // true
15
+ * ```
16
+ *
17
+ * This Deferred class extends the Promise class, allowing it to capture the
18
+ * value or reason for easy access after resolution, akin to
19
+ * {@link Promise.withResolvers}. As it extends {@link Promise}, it is
20
+ * 'thenable' and works with `await` as if it were a native Promise. This
21
+ * allows seamless integration with code expecting Promise-like objects.
22
+ */
23
+ export class Deferred extends Promise {
24
+ /**
25
+ * The promise backing this deferred object. Created when the constructor
26
+ * runs, this promise is what all `Promise.prototype` functions are routed
27
+ * to.
28
+ *
29
+ * @type {Promise}
30
+ */
31
+ #promise = null;
32
+ /**
33
+ * The reject() resolver that will be assigned when a new instance is
34
+ * created. Invoking this function with or without a `reason` will cause
35
+ * the deferred's promise to be settled.
36
+ *
37
+ * @type {function}
38
+ */
39
+ #reject = null;
40
+ /**
41
+ * The resolve() resolver that will be assigned when a new instance is
42
+ * created. Invoking this function with or without a `value` will cause
43
+ * the deferred's promise to be settled.
44
+ *
45
+ * @type {function}
46
+ */
47
+ #resolve = null;
48
+ /**
49
+ * When the Deferred is settled with {@link Deferred.resolve}, the `value`
50
+ * passed to that function will be set here as well.
51
+ *
52
+ * @type {*}
53
+ */
54
+ value = null;
55
+ /**
56
+ * When the Deferred is settled with {@link Deferred.reject}, the `reason`
57
+ * passed to that rejection will also be stored here.
58
+ *
59
+ * @type {*}
60
+ */
61
+ reason = null;
62
+ /**
63
+ * When either {@link Deferred.resolve} or {@link Deferred.reject} are called,
64
+ * this property is set to `true`. Its current status at any time can be
65
+ * queried using the {@link Deferred.settled} getter.
66
+ *
67
+ * @type {boolean}
68
+ */
69
+ #settled = false;
70
+ /**
71
+ * The constructor for Deferred instances. By default, a new Deferred will
72
+ * have three important properties: `promise`, `resolve`, and `reject`.
73
+ *
74
+ * The constructor takes an object called `options`. It can have the
75
+ * following properties:
76
+ *
77
+ * ```
78
+ * interface BaseDeferredOptions {
79
+ * // Deferreds store the value or reason. To turn this off, pass true
80
+ * // to this option.
81
+ * doNotTrackAnswers?: boolean;
82
+ * }
83
+ *
84
+ * interface ResolveDeferredOptions {
85
+ * // Passing in an option object with a resolve value will auto resolve
86
+ * // the Deferred with your value. An error will be raised if both
87
+ * // resolve and reject are supplied at the same time.
88
+ * resolve?: (value: any) => void;
89
+ * }
90
+ *
91
+ * interface RejectDeferredOptions {
92
+ * // Passing in an option object with a reject reason will auto reject
93
+ * // the Deferred with your reason. An error will be raised if both
94
+ * // resolve and reject are supplied at the same time.
95
+ * reject?: (reason: any) => void;
96
+ * }
97
+ *
98
+ * type DeferredOptions = BaseDeferredOptions &
99
+ * (ResolveDeferredOptions | RejectDeferredOptions)
100
+ * ```
101
+ *
102
+ * @param {object} options see above for examples on supported options, but
103
+ * when supplied, the constructor can take instructions on how to auto
104
+ * resolve or reject the deferred created here.
105
+ */
106
+ constructor(options) {
107
+ // Check if options is an object, if not, assign an empty object to config
108
+ const config = (options && typeof (options) === 'object'
109
+ ? options
110
+ : {});
111
+ // Throw an error if both resolve and reject options are provided
112
+ if (config?.resolve && config?.reject) {
113
+ throw new TypeError('resolve and reject options cannot be simultaneously provided');
114
+ }
115
+ // Create an empty object to store the resolve and reject functions
116
+ let _resolve, _reject;
117
+ // Create a new promise and assign its resolve and reject functions to resolvers
118
+ super((resolve, reject) => {
119
+ _resolve = resolve;
120
+ _reject = reject;
121
+ if (config?.executor && typeof (config?.executor) === 'function') {
122
+ config?.executor(resolve, reject);
123
+ }
124
+ });
125
+ // Define the resolve function for the Deferred instance
126
+ this.#resolve = (value) => {
127
+ // If doNotTrackAnswers is not set to true, store the value
128
+ if (config?.doNotTrackAnswers !== true) {
129
+ this.value = value;
130
+ }
131
+ // Mark the Deferred instance as settled
132
+ this.#settled = true;
133
+ // Resolve the promise with the provided value
134
+ return _resolve(value);
135
+ };
136
+ // Define the reject function for the Deferred instance
137
+ this.#reject = async (reason) => {
138
+ // If doNotTrackAnswers is not set to true, store the reason
139
+ if (config?.doNotTrackAnswers !== true) {
140
+ this.reason = reason;
141
+ }
142
+ // Mark the Deferred instance as settled
143
+ this.#settled = true;
144
+ // Reject the promise with the provided reason
145
+ return _reject(reason);
146
+ };
147
+ this.#promise = this;
148
+ // If a resolve option is provided, resolve the Deferred instance with it
149
+ if (config?.resolve) {
150
+ this.#resolve(config?.resolve);
151
+ }
152
+ // If a reject option is provided, reject the Deferred instance with it
153
+ else if (config?.reject) {
154
+ this.#reject(config?.reject);
155
+ }
156
+ }
157
+ /**
158
+ * Returns a boolean value that indicates whether or not this Deferred
159
+ * has been settled (either resolve or reject have been invoked).
160
+ *
161
+ * @returns {boolean} `true` if either {@link Deferred.resolve} or
162
+ * {@link Deferred.reject} have been invoked; `false` otherwise
163
+ */
164
+ get settled() {
165
+ return this.#settled;
166
+ }
167
+ /**
168
+ * Accessor for the promise managed by this Deferred instance.
169
+ *
170
+ * This getter provides access to the internal promise which is controlled
171
+ * by the Deferred's resolve and reject methods. It allows external code to
172
+ * attach callbacks for the resolution or rejection of the Deferred without
173
+ * the ability to directly resolve or reject it.
174
+ *
175
+ * @returns {Promise} The promise controlled by this Deferred instance.
176
+ */
177
+ get promise() {
178
+ return this.#promise;
179
+ }
180
+ /**
181
+ * Resolves the Deferred with the given value. If the value is a thenable
182
+ * (i.e., has a "then" method), the Deferred will "follow" that thenable,
183
+ * adopting its eventual state; otherwise, the Deferred will be fulfilled
184
+ * with the value. This function behaves the same as Promise.resolve.
185
+ *
186
+ * @param {*} value - The value to resolve the Deferred with.
187
+ * @returns {Promise} A Promise that is resolved with the given value.
188
+ */
189
+ resolve(value) {
190
+ return this.#resolve(value);
191
+ }
192
+ /**
193
+ * Rejects the Deferred with the given reason. This function behaves the
194
+ * same as Promise.reject. The Deferred will be rejected with the provided
195
+ * reason.
196
+ *
197
+ * @param {*} reason - The reason to reject the Deferred with.
198
+ * @returns {Promise} A Promise that is rejected with the given reason.
199
+ */
200
+ reject(reason) {
201
+ return this.#reject(reason);
202
+ }
203
+ /**
204
+ * A getter for the species symbol which returns a custom DeferredPromise
205
+ * class. This class extends from Deferred and is used to ensure that the
206
+ * constructor signature matches that of a Promise. The executor function
207
+ * passed to the constructor of this class is used to initialize the Deferred
208
+ * object with resolve and reject functions, similar to how a Promise would
209
+ * be initialized.
210
+ *
211
+ * @returns {DeferredPromise} A DeferredPromise class that extends Deferred.
212
+ */
213
+ static get [Symbol.species]() {
214
+ return class DeferredPromise extends Deferred {
215
+ /**
216
+ * The constructor for the DeferredPromise class.
217
+ * It takes an executor function which is used to initialize the Deferred.
218
+ *
219
+ * @param {Function} executor - A function that is passed with the resolve
220
+ * and reject functions. The executor is expected to initialize the
221
+ * Deferred by calling resolve or reject at some point.
222
+ */
223
+ constructor(executor) {
224
+ super({ executor });
225
+ }
226
+ };
227
+ }
228
+ }
229
+ export const DeferredExtension = new Extension(Deferred);
230
+ //# sourceMappingURL=deferred.js.map