goaded.utils 1.2.0 → 1.2.2
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/async.js +7 -7
- package/package.json +1 -1
package/async.js
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
class AsyncHandler {
|
|
20
20
|
constructor() {
|
|
21
21
|
/** @type {Object.<string, Function>} */
|
|
22
|
-
|
|
22
|
+
this.tasks = {};
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
@@ -40,7 +40,7 @@ class AsyncHandler {
|
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
42
|
* Executes a registered task and handles its lifecycle using the provided handlers.
|
|
43
|
-
* @param {
|
|
43
|
+
* @param {Object} fn - the object to store key and args.
|
|
44
44
|
* @param {TaskHandlers} [handlers={}] - An object containing success and error callbacks.
|
|
45
45
|
* @returns {Promise<void>}
|
|
46
46
|
*
|
|
@@ -51,12 +51,12 @@ class AsyncHandler {
|
|
|
51
51
|
* onError: (err) => console.error('Caught some other error:', err)
|
|
52
52
|
* });
|
|
53
53
|
*/
|
|
54
|
-
async runTask(
|
|
55
|
-
const task = this.tasks[key];
|
|
54
|
+
async runTask(fn, handlers = {}) {
|
|
55
|
+
const task = this.tasks[fn.key];
|
|
56
56
|
if (!task) return;
|
|
57
57
|
|
|
58
58
|
try {
|
|
59
|
-
let res = task();
|
|
59
|
+
let res = task(...fn.args);
|
|
60
60
|
if (res instanceof Promise) {
|
|
61
61
|
res = await res;
|
|
62
62
|
}
|
|
@@ -71,7 +71,7 @@ class AsyncHandler {
|
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
73
|
* Executes a function once with the provided handlers without registering it.
|
|
74
|
-
* @param {
|
|
74
|
+
* @param {Object} fn - The function to execute, with a `fn` and `args` properties.
|
|
75
75
|
* @param {TaskHandlers} [handlers={}] - An object containing success and error callbacks.
|
|
76
76
|
* @returns {Promise<void>}
|
|
77
77
|
*
|
|
@@ -85,7 +85,7 @@ class AsyncHandler {
|
|
|
85
85
|
*/
|
|
86
86
|
async once(fn, handlers = {}) {
|
|
87
87
|
try {
|
|
88
|
-
let res = fn();
|
|
88
|
+
let res = fn.fn(...fn.args);
|
|
89
89
|
if (res instanceof Promise) {
|
|
90
90
|
res = await res;
|
|
91
91
|
}
|