tiny-essentials 1.9.0 → 1.9.1

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.
@@ -1,4 +1,29 @@
1
1
  export default TinyLevelUp;
2
+ /**
3
+ * Represents a user object used.
4
+ */
5
+ export type UserEditor = {
6
+ /**
7
+ * - Current experience points of the user.
8
+ */
9
+ exp: number;
10
+ /**
11
+ * - Current level of the user.
12
+ */
13
+ level: number;
14
+ /**
15
+ * - Total accumulated experience.
16
+ */
17
+ totalExp: number;
18
+ };
19
+ /**
20
+ * Represents a user object used.
21
+ *
22
+ * @typedef {Object} UserEditor
23
+ * @property {number} exp - Current experience points of the user.
24
+ * @property {number} level - Current level of the user.
25
+ * @property {number} totalExp - Total accumulated experience.
26
+ */
2
27
  /**
3
28
  * Class to manage user level-up logic based on experience points.
4
29
  */
@@ -12,104 +37,106 @@ declare class TinyLevelUp {
12
37
  giveExp: number;
13
38
  expLevel: number;
14
39
  /**
15
- * @typedef {{ exp: number, level: number, totalExp: number }} UserResult
40
+ * Creates a new user object starting at level 0 with 0 experience.
41
+ * @returns {UserEditor} A fresh user object.
42
+ */
43
+ createUser(): UserEditor;
44
+ /**
45
+ * Validates if the given user object has valid numeric properties.
46
+ * Throws an error if any property is invalid.
47
+ *
48
+ * @param {UserEditor} user - The user object to validate.
49
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
16
50
  */
51
+ validateUser(user: UserEditor): void;
17
52
  /**
18
- * @typedef {{ exp: number, level: number, totalExp: any }} UserEditor
53
+ * Checks if the given user object is valid by verifying its numeric properties.
54
+ *
55
+ * @param {UserEditor} user - The user object to check.
56
+ * @returns {boolean} `true` if all properties (exp, level, totalExp) are valid numbers; otherwise `false`.
19
57
  */
58
+ isValidUser(user: UserEditor): boolean;
59
+ /**
60
+ * Returns the base experience value used for random experience generation.
61
+ * Throws an error if the internal giveExp value is not a valid number.
62
+ *
63
+ * @returns {number} The base experience value.
64
+ * @throws {Error} If giveExp is not a valid number.
65
+ */
66
+ getGiveExpBase(): number;
67
+ /**
68
+ * Returns the base experience required to level up.
69
+ * Throws an error if the internal expLevel value is not a valid number.
70
+ *
71
+ * @returns {number} The base experience needed per level.
72
+ * @throws {Error} If expLevel is not a valid number.
73
+ */
74
+ getExpLevelBase(): number;
20
75
  /**
21
76
  * Validates and adjusts the user's level based on their current experience.
22
77
  * @param {UserEditor} user - The user object containing experience and level properties.
23
- * @returns {UserResult} The updated user object.
24
- */
25
- expValidator(user: {
26
- exp: number;
27
- level: number;
28
- totalExp: any;
29
- }): {
30
- exp: number;
31
- level: number;
32
- totalExp: number;
33
- };
78
+ * @returns {UserEditor} The updated user object.
79
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
80
+ */
81
+ expValidator(user: UserEditor): UserEditor;
34
82
  /**
35
83
  * Calculates the total experience based on the user's level.
36
- * @param {{ exp: number, level: number }} user - The user object containing experience and level properties.
84
+ * @param {UserEditor} user - The user object containing experience and level properties.
37
85
  * @returns {number} The total experience of the user.
86
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
38
87
  */
39
- getTotalExp(user: {
40
- exp: number;
41
- level: number;
42
- }): number;
88
+ getTotalExp(user: UserEditor): number;
43
89
  /**
44
90
  * Generates random experience points based on the configured multiplier.
45
91
  * @param {number} [multi] - A multiplier for the generated experience.
46
92
  * @returns {number} The generated experience points.
47
93
  */
48
94
  expGenerator(multi?: number): number;
95
+ /**
96
+ * Calculates how much experience is missing to next level.
97
+ * @param {UserEditor} user
98
+ * @returns {number}
99
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
100
+ */
101
+ getMissingExp(user: UserEditor): number;
49
102
  /**
50
103
  * Gets the experience points required to reach the next level.
51
- * @param {{ level: number }} user - The user object containing the level.
104
+ * @param {UserEditor} user - The user object containing the level.
52
105
  * @returns {number} The experience required for the next level.
106
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
53
107
  */
54
- progress(user: {
55
- level: number;
56
- }): number;
108
+ progress(user: UserEditor): number;
57
109
  /**
58
110
  * Gets the experience points required to reach the next level.
59
- * @param {{ level: number }} user - The user object containing the level.
111
+ * @param {UserEditor} user - The user object containing the level.
60
112
  * @returns {number} The experience required for the next level.
113
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
61
114
  */
62
- getProgress(user: {
63
- level: number;
64
- }): number;
115
+ getProgress(user: UserEditor): number;
65
116
  /**
66
117
  * Sets the experience value for the user, adjusting their level if necessary.
67
118
  * @param {UserEditor} user - The user object.
68
119
  * @param {number} value - The new experience value to set.
69
- * @returns {UserResult} The updated user object.
70
- */
71
- set(user: {
72
- exp: number;
73
- level: number;
74
- totalExp: any;
75
- }, value: number): {
76
- exp: number;
77
- level: number;
78
- totalExp: number;
79
- };
120
+ * @returns {UserEditor} The updated user object.
121
+ */
122
+ set(user: UserEditor, value: number): UserEditor;
80
123
  /**
81
124
  * Adds experience to the user, adjusting their level if necessary.
82
125
  * @param {UserEditor} user - The user object.
83
126
  * @param {number} [extraExp] - Additional experience to be added.
84
127
  * @param {'add' | 'extra'} [type] - Type of addition ('add' or 'extra').
85
128
  * @param {number} [multi] - Multiplier for experience generation.
86
- * @returns {UserResult} The updated user object.
87
- */
88
- give(user: {
89
- exp: number;
90
- level: number;
91
- totalExp: any;
92
- }, extraExp?: number, type?: "add" | "extra", multi?: number): {
93
- exp: number;
94
- level: number;
95
- totalExp: number;
96
- };
129
+ * @returns {UserEditor} The updated user object.
130
+ */
131
+ give(user: UserEditor, extraExp?: number, type?: "add" | "extra", multi?: number): UserEditor;
97
132
  /**
98
133
  * Removes experience from the user, adjusting their level if necessary.
99
134
  * @param {UserEditor} user - The user object.
100
135
  * @param {number} [extraExp] - Additional experience to remove.
101
136
  * @param {'add' | 'extra'} [type] - Type of removal ('add' or 'extra').
102
137
  * @param {number} [multi] - Multiplier for experience generation.
103
- * @returns {UserResult} The updated user object.
104
- */
105
- remove(user: {
106
- exp: number;
107
- level: number;
108
- totalExp: any;
109
- }, extraExp?: number, type?: "add" | "extra", multi?: number): {
110
- exp: number;
111
- level: number;
112
- totalExp: number;
113
- };
138
+ * @returns {UserEditor} The updated user object.
139
+ */
140
+ remove(user: UserEditor, extraExp?: number, type?: "add" | "extra", multi?: number): UserEditor;
114
141
  }
115
142
  //# sourceMappingURL=userLevel.d.mts.map
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Represents a user object used.
3
+ *
4
+ * @typedef {Object} UserEditor
5
+ * @property {number} exp - Current experience points of the user.
6
+ * @property {number} level - Current level of the user.
7
+ * @property {number} totalExp - Total accumulated experience.
8
+ */
1
9
  /**
2
10
  * Class to manage user level-up logic based on experience points.
3
11
  */
@@ -8,23 +16,93 @@ class TinyLevelUp {
8
16
  * @param {number} expLevel - Base experience needed to level up (per level).
9
17
  */
10
18
  constructor(giveExp, expLevel) {
19
+ if (typeof giveExp !== 'number' || Number.isNaN(giveExp))
20
+ throw new Error('giveExp must be a valid number');
21
+ if (typeof expLevel !== 'number' || Number.isNaN(expLevel))
22
+ throw new Error('expLevel must be a valid number');
11
23
  this.giveExp = giveExp;
12
24
  this.expLevel = expLevel;
13
25
  }
14
26
  /**
15
- * @typedef {{ exp: number, level: number, totalExp: number }} UserResult
27
+ * Creates a new user object starting at level 0 with 0 experience.
28
+ * @returns {UserEditor} A fresh user object.
16
29
  */
30
+ createUser() {
31
+ return {
32
+ exp: 0,
33
+ level: 1,
34
+ totalExp: 0,
35
+ };
36
+ }
17
37
  /**
18
- * @typedef {{ exp: number, level: number, totalExp: any }} UserEditor
38
+ * Validates if the given user object has valid numeric properties.
39
+ * Throws an error if any property is invalid.
40
+ *
41
+ * @param {UserEditor} user - The user object to validate.
42
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
19
43
  */
44
+ validateUser(user) {
45
+ if (typeof user.exp !== 'number' || Number.isNaN(user.exp))
46
+ throw new Error('exp must be a valid number');
47
+ if (typeof user.level !== 'number' || Number.isNaN(user.level))
48
+ throw new Error('level must be a valid number');
49
+ if (user.level < 1)
50
+ throw new Error('level must be at least 1');
51
+ if (typeof user.totalExp !== 'number' || Number.isNaN(user.totalExp))
52
+ throw new Error('totalExp must be a valid number');
53
+ }
54
+ /**
55
+ * Checks if the given user object is valid by verifying its numeric properties.
56
+ *
57
+ * @param {UserEditor} user - The user object to check.
58
+ * @returns {boolean} `true` if all properties (exp, level, totalExp) are valid numbers; otherwise `false`.
59
+ */
60
+ isValidUser(user) {
61
+ if (typeof user.exp !== 'number' || Number.isNaN(user.exp))
62
+ return false;
63
+ if (typeof user.level !== 'number' || Number.isNaN(user.level))
64
+ return false;
65
+ if (user.level < 1)
66
+ return false;
67
+ if (typeof user.totalExp !== 'number' || Number.isNaN(user.totalExp))
68
+ return false;
69
+ return true;
70
+ }
71
+ /**
72
+ * Returns the base experience value used for random experience generation.
73
+ * Throws an error if the internal giveExp value is not a valid number.
74
+ *
75
+ * @returns {number} The base experience value.
76
+ * @throws {Error} If giveExp is not a valid number.
77
+ */
78
+ getGiveExpBase() {
79
+ if (typeof this.giveExp !== 'number' || Number.isNaN(this.giveExp))
80
+ throw new Error('giveExp must be a valid number');
81
+ return this.giveExp;
82
+ }
83
+ /**
84
+ * Returns the base experience required to level up.
85
+ * Throws an error if the internal expLevel value is not a valid number.
86
+ *
87
+ * @returns {number} The base experience needed per level.
88
+ * @throws {Error} If expLevel is not a valid number.
89
+ */
90
+ getExpLevelBase() {
91
+ if (typeof this.expLevel !== 'number' || Number.isNaN(this.expLevel))
92
+ throw new Error('expLevel must be a valid number');
93
+ return this.expLevel;
94
+ }
20
95
  /**
21
96
  * Validates and adjusts the user's level based on their current experience.
22
97
  * @param {UserEditor} user - The user object containing experience and level properties.
23
- * @returns {UserResult} The updated user object.
98
+ * @returns {UserEditor} The updated user object.
99
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
24
100
  */
25
101
  expValidator(user) {
102
+ const expLevel = this.getExpLevelBase();
103
+ this.validateUser(user);
26
104
  let extraValue = 0;
27
- const nextLevelExp = this.expLevel * user.level;
105
+ const nextLevelExp = expLevel * user.level;
28
106
  // Level Up
29
107
  if (user.exp >= nextLevelExp) {
30
108
  user.level++;
@@ -37,7 +115,7 @@ class TinyLevelUp {
37
115
  if (user.exp < 1 && user.level > 1) {
38
116
  user.level--;
39
117
  extraValue = Math.abs(user.exp);
40
- user.exp = this.expLevel * user.level;
118
+ user.exp = expLevel * user.level;
41
119
  if (extraValue > 0)
42
120
  return this.remove(user, extraValue, 'extra');
43
121
  }
@@ -45,13 +123,15 @@ class TinyLevelUp {
45
123
  }
46
124
  /**
47
125
  * Calculates the total experience based on the user's level.
48
- * @param {{ exp: number, level: number }} user - The user object containing experience and level properties.
126
+ * @param {UserEditor} user - The user object containing experience and level properties.
49
127
  * @returns {number} The total experience of the user.
128
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
50
129
  */
51
130
  getTotalExp(user) {
131
+ this.validateUser(user);
52
132
  let totalExp = 0;
53
133
  for (let p = 1; p <= user.level; p++)
54
- totalExp += this.expLevel * p;
134
+ totalExp += this.getExpLevelBase() * p;
55
135
  totalExp += user.exp;
56
136
  return totalExp;
57
137
  }
@@ -61,31 +141,47 @@ class TinyLevelUp {
61
141
  * @returns {number} The generated experience points.
62
142
  */
63
143
  expGenerator(multi = 1) {
64
- return Math.floor(Math.random() * this.giveExp) + 1 * multi;
144
+ if (typeof multi !== 'number' || Number.isNaN(multi))
145
+ throw new Error('multi must be a valid number');
146
+ return Math.floor(Math.random() * this.getGiveExpBase()) * multi;
147
+ }
148
+ /**
149
+ * Calculates how much experience is missing to next level.
150
+ * @param {UserEditor} user
151
+ * @returns {number}
152
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
153
+ */
154
+ getMissingExp(user) {
155
+ return this.getProgress(user) - user.exp;
65
156
  }
66
157
  /**
67
158
  * Gets the experience points required to reach the next level.
68
- * @param {{ level: number }} user - The user object containing the level.
159
+ * @param {UserEditor} user - The user object containing the level.
69
160
  * @returns {number} The experience required for the next level.
161
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
70
162
  */
71
163
  progress(user) {
72
- return this.expLevel * user.level;
164
+ return this.getProgress(user);
73
165
  }
74
166
  /**
75
167
  * Gets the experience points required to reach the next level.
76
- * @param {{ level: number }} user - The user object containing the level.
168
+ * @param {UserEditor} user - The user object containing the level.
77
169
  * @returns {number} The experience required for the next level.
170
+ * @throws {Error} If any property (exp, level, totalExp) is not a valid number.
78
171
  */
79
172
  getProgress(user) {
80
- return this.expLevel * user.level;
173
+ this.validateUser(user);
174
+ return this.getExpLevelBase() * user.level;
81
175
  }
82
176
  /**
83
177
  * Sets the experience value for the user, adjusting their level if necessary.
84
178
  * @param {UserEditor} user - The user object.
85
179
  * @param {number} value - The new experience value to set.
86
- * @returns {UserResult} The updated user object.
180
+ * @returns {UserEditor} The updated user object.
87
181
  */
88
182
  set(user, value) {
183
+ if (typeof value !== 'number' || Number.isNaN(value))
184
+ throw new Error('value must be a valid number');
89
185
  user.exp = value;
90
186
  this.expValidator(user);
91
187
  user.totalExp = this.getTotalExp(user);
@@ -97,9 +193,15 @@ class TinyLevelUp {
97
193
  * @param {number} [extraExp] - Additional experience to be added.
98
194
  * @param {'add' | 'extra'} [type] - Type of addition ('add' or 'extra').
99
195
  * @param {number} [multi] - Multiplier for experience generation.
100
- * @returns {UserResult} The updated user object.
196
+ * @returns {UserEditor} The updated user object.
101
197
  */
102
198
  give(user, extraExp = 0, type = 'add', multi = 1) {
199
+ if (typeof multi !== 'number' || Number.isNaN(multi))
200
+ throw new Error('multi must be a valid number');
201
+ if (typeof extraExp !== 'number' || Number.isNaN(extraExp))
202
+ throw new Error('extraExp must be a valid number');
203
+ if (typeof type !== 'string')
204
+ throw new Error('type must be a valid string');
103
205
  if (type === 'add')
104
206
  user.exp += this.expGenerator(multi) + extraExp;
105
207
  else if (type === 'extra')
@@ -114,9 +216,15 @@ class TinyLevelUp {
114
216
  * @param {number} [extraExp] - Additional experience to remove.
115
217
  * @param {'add' | 'extra'} [type] - Type of removal ('add' or 'extra').
116
218
  * @param {number} [multi] - Multiplier for experience generation.
117
- * @returns {UserResult} The updated user object.
219
+ * @returns {UserEditor} The updated user object.
118
220
  */
119
221
  remove(user, extraExp = 0, type = 'add', multi = 1) {
222
+ if (typeof multi !== 'number' || Number.isNaN(multi))
223
+ throw new Error('multi must be a valid number');
224
+ if (typeof extraExp !== 'number' || Number.isNaN(extraExp))
225
+ throw new Error('extraExp must be a valid number');
226
+ if (typeof type !== 'string')
227
+ throw new Error('type must be a valid string');
120
228
  if (type === 'add')
121
229
  user.exp -= this.expGenerator(multi) + extraExp;
122
230
  else if (type === 'extra')
@@ -1,5 +1,15 @@
1
1
  'use strict';
2
2
 
3
+ /**
4
+ * @typedef {Object} QueuedTask
5
+ * @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
6
+ * @property {(value: any) => any} resolve - The resolve function from the Promise.
7
+ * @property {(reason?: any) => any} reject - The reject function from the Promise.
8
+ * @property {string|undefined} [id] - Optional identifier for the task.
9
+ * @property {string|null|undefined} [marker] - Optional marker for the task.
10
+ * @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
11
+ */
12
+
3
13
  /**
4
14
  * A queue system for managing and executing asynchronous tasks sequentially, one at a time.
5
15
  *
@@ -9,20 +19,10 @@
9
19
  * @class
10
20
  */
11
21
  class TinyPromiseQueue {
12
- /**
13
- * @typedef {Object} QueuedTask
14
- * @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
15
- * @property {(value: any) => any} resolve - The resolve function from the Promise.
16
- * @property {(reason?: any) => any} reject - The reject function from the Promise.
17
- * @property {string|undefined} [id] - Optional identifier for the task.
18
- * @property {string|null|undefined} [marker] - Optional marker for the task.
19
- * @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
20
- */
21
-
22
- /** @type {Array<QueuedTask>} */
22
+ /** @type {QueuedTask[]} */
23
23
  #queue = [];
24
24
  #running = false;
25
- /** @type {Record<string, *>} */
25
+ /** @type {Record<string, ReturnType<typeof setTimeout>>} */
26
26
  #timeouts = {};
27
27
  /** @type {Set<string>} */
28
28
  #blacklist = new Set();
@@ -185,8 +185,13 @@ class TinyPromiseQueue {
185
185
  * @param {(...args: any[]) => Promise<any>|Promise<any>} task A function that returns a Promise.
186
186
  * @param {string} [id] Optional ID to identify the task in the queue.
187
187
  * @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
188
+ * @throws {Error} Throws if param is invalid.
188
189
  */
189
190
  async enqueuePoint(task, id) {
191
+ if (typeof task !== 'function')
192
+ return Promise.reject(new Error('Task must be a function returning a Promise.'));
193
+ if (typeof id !== 'undefined' && typeof id !== 'string')
194
+ throw new Error('The "id" parameter must be a string.');
190
195
  if (!this.#running) return task();
191
196
  return new Promise((resolve, reject) => {
192
197
  this.#queue.push({ marker: 'POINT_MARKER', task, resolve, reject, id });
@@ -205,8 +210,16 @@ class TinyPromiseQueue {
205
210
  * @param {number|null} [delay] Optional delay (in ms) before the task is executed.
206
211
  * @param {string} [id] Optional ID to identify the task in the queue.
207
212
  * @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
213
+ * @throws {Error} Throws if param is invalid.
208
214
  */
209
215
  enqueue(task, delay, id) {
216
+ if (typeof task !== 'function')
217
+ return Promise.reject(new Error('Task must be a function returning a Promise.'));
218
+ if (typeof delay !== 'undefined' && (typeof delay !== 'number' || delay < 0))
219
+ return Promise.reject(new Error('Delay must be a positive number or undefined.'));
220
+ if (typeof id !== 'undefined' && typeof id !== 'string')
221
+ throw new Error('The "id" parameter must be a string.');
222
+
210
223
  return new Promise((resolve, reject) => {
211
224
  this.#queue.push({ task, resolve, reject, id, delay });
212
225
  this.#processQueue();
@@ -219,9 +232,10 @@ class TinyPromiseQueue {
219
232
  *
220
233
  * @param {string} id The ID of the task to cancel.
221
234
  * @returns {boolean} True if a delay was cancelled and the task was removed.
235
+ * @throws {Error} Throws if `id` is not a string.
222
236
  */
223
237
  cancelTask(id) {
224
- if (!id) return false;
238
+ if (typeof id !== 'string') throw new Error('The "id" parameter must be a string.');
225
239
  let cancelled = false;
226
240
 
227
241
  if (id in this.#timeouts) {
@@ -1,4 +1,39 @@
1
1
  export default TinyPromiseQueue;
2
+ export type QueuedTask = {
3
+ /**
4
+ * - The async task to execute.
5
+ */
6
+ task: (...args: any[]) => Promise<any> | Promise<any>;
7
+ /**
8
+ * - The resolve function from the Promise.
9
+ */
10
+ resolve: (value: any) => any;
11
+ /**
12
+ * - The reject function from the Promise.
13
+ */
14
+ reject: (reason?: any) => any;
15
+ /**
16
+ * - Optional identifier for the task.
17
+ */
18
+ id?: string | undefined;
19
+ /**
20
+ * - Optional marker for the task.
21
+ */
22
+ marker?: string | null | undefined;
23
+ /**
24
+ * - Optional delay (in ms) before the task is executed.
25
+ */
26
+ delay?: number | null | undefined;
27
+ };
28
+ /**
29
+ * @typedef {Object} QueuedTask
30
+ * @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
31
+ * @property {(value: any) => any} resolve - The resolve function from the Promise.
32
+ * @property {(reason?: any) => any} reject - The reject function from the Promise.
33
+ * @property {string|undefined} [id] - Optional identifier for the task.
34
+ * @property {string|null|undefined} [marker] - Optional marker for the task.
35
+ * @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
36
+ */
2
37
  /**
3
38
  * A queue system for managing and executing asynchronous tasks sequentially, one at a time.
4
39
  *
@@ -44,6 +79,7 @@ declare class TinyPromiseQueue {
44
79
  * @param {(...args: any[]) => Promise<any>|Promise<any>} task A function that returns a Promise.
45
80
  * @param {string} [id] Optional ID to identify the task in the queue.
46
81
  * @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
82
+ * @throws {Error} Throws if param is invalid.
47
83
  */
48
84
  enqueuePoint(task: (...args: any[]) => Promise<any> | Promise<any>, id?: string): Promise<any>;
49
85
  /**
@@ -57,6 +93,7 @@ declare class TinyPromiseQueue {
57
93
  * @param {number|null} [delay] Optional delay (in ms) before the task is executed.
58
94
  * @param {string} [id] Optional ID to identify the task in the queue.
59
95
  * @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
96
+ * @throws {Error} Throws if param is invalid.
60
97
  */
61
98
  enqueue(task: (...args: any[]) => Promise<any> | Promise<any>, delay?: number | null, id?: string): Promise<any>;
62
99
  /**
@@ -65,6 +102,7 @@ declare class TinyPromiseQueue {
65
102
  *
66
103
  * @param {string} id The ID of the task to cancel.
67
104
  * @returns {boolean} True if a delay was cancelled and the task was removed.
105
+ * @throws {Error} Throws if `id` is not a string.
68
106
  */
69
107
  cancelTask(id: string): boolean;
70
108
  #private;
@@ -1,3 +1,12 @@
1
+ /**
2
+ * @typedef {Object} QueuedTask
3
+ * @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
4
+ * @property {(value: any) => any} resolve - The resolve function from the Promise.
5
+ * @property {(reason?: any) => any} reject - The reject function from the Promise.
6
+ * @property {string|undefined} [id] - Optional identifier for the task.
7
+ * @property {string|null|undefined} [marker] - Optional marker for the task.
8
+ * @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
9
+ */
1
10
  /**
2
11
  * A queue system for managing and executing asynchronous tasks sequentially, one at a time.
3
12
  *
@@ -7,19 +16,10 @@
7
16
  * @class
8
17
  */
9
18
  class TinyPromiseQueue {
10
- /**
11
- * @typedef {Object} QueuedTask
12
- * @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
13
- * @property {(value: any) => any} resolve - The resolve function from the Promise.
14
- * @property {(reason?: any) => any} reject - The reject function from the Promise.
15
- * @property {string|undefined} [id] - Optional identifier for the task.
16
- * @property {string|null|undefined} [marker] - Optional marker for the task.
17
- * @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
18
- */
19
- /** @type {Array<QueuedTask>} */
19
+ /** @type {QueuedTask[]} */
20
20
  #queue = [];
21
21
  #running = false;
22
- /** @type {Record<string, *>} */
22
+ /** @type {Record<string, ReturnType<typeof setTimeout>>} */
23
23
  #timeouts = {};
24
24
  /** @type {Set<string>} */
25
25
  #blacklist = new Set();
@@ -165,8 +165,13 @@ class TinyPromiseQueue {
165
165
  * @param {(...args: any[]) => Promise<any>|Promise<any>} task A function that returns a Promise.
166
166
  * @param {string} [id] Optional ID to identify the task in the queue.
167
167
  * @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
168
+ * @throws {Error} Throws if param is invalid.
168
169
  */
169
170
  async enqueuePoint(task, id) {
171
+ if (typeof task !== 'function')
172
+ return Promise.reject(new Error('Task must be a function returning a Promise.'));
173
+ if (typeof id !== 'undefined' && typeof id !== 'string')
174
+ throw new Error('The "id" parameter must be a string.');
170
175
  if (!this.#running)
171
176
  return task();
172
177
  return new Promise((resolve, reject) => {
@@ -185,8 +190,15 @@ class TinyPromiseQueue {
185
190
  * @param {number|null} [delay] Optional delay (in ms) before the task is executed.
186
191
  * @param {string} [id] Optional ID to identify the task in the queue.
187
192
  * @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
193
+ * @throws {Error} Throws if param is invalid.
188
194
  */
189
195
  enqueue(task, delay, id) {
196
+ if (typeof task !== 'function')
197
+ return Promise.reject(new Error('Task must be a function returning a Promise.'));
198
+ if (typeof delay !== 'undefined' && (typeof delay !== 'number' || delay < 0))
199
+ return Promise.reject(new Error('Delay must be a positive number or undefined.'));
200
+ if (typeof id !== 'undefined' && typeof id !== 'string')
201
+ throw new Error('The "id" parameter must be a string.');
190
202
  return new Promise((resolve, reject) => {
191
203
  this.#queue.push({ task, resolve, reject, id, delay });
192
204
  this.#processQueue();
@@ -198,10 +210,11 @@ class TinyPromiseQueue {
198
210
  *
199
211
  * @param {string} id The ID of the task to cancel.
200
212
  * @returns {boolean} True if a delay was cancelled and the task was removed.
213
+ * @throws {Error} Throws if `id` is not a string.
201
214
  */
202
215
  cancelTask(id) {
203
- if (!id)
204
- return false;
216
+ if (typeof id !== 'string')
217
+ throw new Error('The "id" parameter must be a string.');
205
218
  let cancelled = false;
206
219
  if (id in this.#timeouts) {
207
220
  clearTimeout(this.#timeouts[id]);