goaded.utils 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/events.js +95 -12
  2. package/package.json +1 -1
package/events.js CHANGED
@@ -1,20 +1,52 @@
1
+ /**
2
+ * A simple event emitter class that manages event subscriptions and emissions.
3
+ * Supports synchronous and asynchronous execution, one-time events, and repeated emissions.
4
+ */
1
5
  class emitter {
6
+ /**
7
+ * Internal storage for event listeners.
8
+ * @type {Array<{event: string, callback: Function}>}
9
+ */
2
10
  listeners = [];
3
-
11
+
12
+ /**
13
+ * Creates a new instance of the emitter.
14
+ */
4
15
  constructor() {}
5
-
16
+
17
+ /**
18
+ * Subscribes a callback function to a specific event.
19
+ *
20
+ * @param {string} event - The name of the event to subscribe to.
21
+ * @param {Function} callback - The function to execute when the event is emitted.
22
+ * @returns {emitter} The current instance for chaining.
23
+ */
6
24
  on(event, callback) {
7
25
  this.listeners.push({event, callback});
8
26
  return this;
9
27
  }
10
-
28
+
29
+ /**
30
+ * Unsubscribes a specific callback from an event.
31
+ *
32
+ * @param {string} event - The name of the event.
33
+ * @param {Function} callback - The specific callback function to remove.
34
+ * @returns {emitter} The current instance for chaining.
35
+ */
11
36
  off(event, callback) {
12
37
  this.listeners = this.listeners.filter(
13
38
  listener => listener.event !== event || listener.callback !== callback
14
39
  );
15
40
  return this;
16
41
  }
17
-
42
+
43
+ /**
44
+ * Subscribes to an event only once. The callback is removed after the first execution.
45
+ *
46
+ * @param {string} event - The name of the event.
47
+ * @param {Function} callback - The function to execute once.
48
+ * @returns {emitter} The current instance for chaining.
49
+ */
18
50
  once(event, callback) {
19
51
  const wrappedCallback = (...args) => {
20
52
  callback(...args);
@@ -23,7 +55,15 @@ class emitter {
23
55
  this.on(event, wrappedCallback);
24
56
  return this;
25
57
  }
26
-
58
+
59
+ /**
60
+ * Synchronously calls all listeners registered for the specified event.
61
+ * Errors in listeners are caught and logged, preventing the loop from breaking.
62
+ *
63
+ * @param {string} event - The name of the event to emit.
64
+ * @param {...*} args - Arguments to pass to the callback functions.
65
+ * @returns {void}
66
+ */
27
67
  emit(event, ...args) {
28
68
  this.listeners.forEach(listener => {
29
69
  if(listener.event === event) {
@@ -35,7 +75,16 @@ class emitter {
35
75
  }
36
76
  });
37
77
  }
38
-
78
+
79
+ /**
80
+ * Asynchronously calls all listeners registered for the specified event.
81
+ * Waits for all promises returned by listeners to resolve.
82
+ *
83
+ * @async
84
+ * @param {string} event - The name of the event to emit.
85
+ * @param {...*} args - Arguments to pass to the callback functions.
86
+ * @returns {Promise<void>} A promise that resolves when all listeners have completed.
87
+ */
39
88
  async emitAsync(event, ...args) {
40
89
  const promises = this.listeners
41
90
  .filter(listener => listener.event === event)
@@ -43,7 +92,15 @@ class emitter {
43
92
 
44
93
  await Promise.all(promises);
45
94
  }
46
-
95
+
96
+ /**
97
+ * Synchronously calls all listeners for an event and then immediately removes them.
98
+ * This effectively "flushes" the listeners for that specific event.
99
+ *
100
+ * @param {string} event - The name of the event.
101
+ * @param {...*} args - Arguments to pass to the callback functions.
102
+ * @returns {void}
103
+ */
47
104
  emitOnce(event, ...args) {
48
105
  this.listeners = this.listeners.filter(listener => {
49
106
  if(listener.event === event) {
@@ -52,12 +109,20 @@ class emitter {
52
109
  } catch(error) {
53
110
  console.error(`Error in event listener for "${event}":`, error);
54
111
  }
55
- return false;
112
+ return false; // Remove listener
56
113
  }
57
- return true;
114
+ return true; // Keep listener
58
115
  });
59
116
  }
60
-
117
+
118
+ /**
119
+ * Asynchronously calls all listeners for an event and then removes them.
120
+ *
121
+ * @async
122
+ * @param {string} event - The name of the event.
123
+ * @param {...*} args - Arguments to pass to the callback functions.
124
+ * @returns {Promise<void>} A promise that resolves when all listeners have completed.
125
+ */
61
126
  async emitOnceAsync(event, ...args) {
62
127
  const matchingListeners = this.listeners.filter(
63
128
  listener => listener.event === event
@@ -73,7 +138,15 @@ class emitter {
73
138
  )
74
139
  );
75
140
  }
76
-
141
+
142
+ /**
143
+ * Synchronously triggers listeners for an event a specific number of times.
144
+ *
145
+ * @param {string} event - The name of the event.
146
+ * @param {number} times - The number of times to trigger the listeners.
147
+ * @param {...*} args - Arguments to pass to the callback functions.
148
+ * @returns {void}
149
+ */
77
150
  emitRepeat(event, times, ...args) {
78
151
  this.listeners.forEach(listener => {
79
152
  if(listener.event === event) {
@@ -87,7 +160,17 @@ class emitter {
87
160
  }
88
161
  });
89
162
  }
90
-
163
+
164
+ /**
165
+ * Asynchronously triggers listeners for an event a specific number of times.
166
+ * It waits for all listeners to complete one round before starting the next iteration.
167
+ *
168
+ * @async
169
+ * @param {string} event - The name of the event.
170
+ * @param {number} times - The number of times to trigger the listeners.
171
+ * @param {...*} args - Arguments to pass to the callback functions.
172
+ * @returns {Promise<void>} A promise that resolves when all repetitions are complete.
173
+ */
91
174
  async emitRepeatAsync(event, times, ...args) {
92
175
  const matchingListeners = this.listeners.filter(
93
176
  listener => listener.event === event
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "sqlite3": "^5.1.7"
4
4
  },
5
5
  "name": "goaded.utils",
6
- "version": "1.0.0",
6
+ "version": "1.0.1",
7
7
  "description": "",
8
8
  "main": "services.js",
9
9
  "devDependencies": {},