@gumbee/utils 1.0.0 → 1.1.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.
package/dist/index.cjs CHANGED
@@ -30,9 +30,11 @@ var __export = (target, all) => {
30
30
  // src/index.ts
31
31
  var exports_src = {};
32
32
  __export(exports_src, {
33
+ throttle: () => throttle,
33
34
  logWarn: () => logWarn,
34
35
  logError: () => logError,
35
36
  log: () => log,
37
+ debounce: () => debounce,
36
38
  clamp: () => clamp,
37
39
  Emitter: () => Emitter
38
40
  });
@@ -171,3 +173,85 @@ class Emitter {
171
173
  this.#events = {};
172
174
  }
173
175
  }
176
+ // src/timing.ts
177
+ function debounce(fn, delay) {
178
+ let timeoutId = null;
179
+ let latestArgs = null;
180
+ const debounced = (...args) => {
181
+ latestArgs = args;
182
+ if (timeoutId !== null) {
183
+ clearTimeout(timeoutId);
184
+ }
185
+ timeoutId = setTimeout(() => {
186
+ if (latestArgs) {
187
+ fn(...latestArgs);
188
+ latestArgs = null;
189
+ }
190
+ timeoutId = null;
191
+ }, delay);
192
+ };
193
+ debounced.cancel = () => {
194
+ if (timeoutId !== null) {
195
+ clearTimeout(timeoutId);
196
+ timeoutId = null;
197
+ }
198
+ latestArgs = null;
199
+ };
200
+ debounced.flush = () => {
201
+ if (timeoutId !== null) {
202
+ clearTimeout(timeoutId);
203
+ timeoutId = null;
204
+ if (latestArgs) {
205
+ fn(...latestArgs);
206
+ latestArgs = null;
207
+ }
208
+ }
209
+ };
210
+ return debounced;
211
+ }
212
+ function throttle(fn, delay) {
213
+ let timeoutId = null;
214
+ let lastExecTime = 0;
215
+ let latestArgs = null;
216
+ const throttled = (...args) => {
217
+ const now = Date.now();
218
+ latestArgs = args;
219
+ if (!lastExecTime && timeoutId === null) {
220
+ fn(...args);
221
+ lastExecTime = now;
222
+ latestArgs = null;
223
+ } else {
224
+ if (timeoutId === null) {
225
+ const remaining = delay - (now - lastExecTime);
226
+ timeoutId = setTimeout(() => {
227
+ if (latestArgs) {
228
+ fn(...latestArgs);
229
+ lastExecTime = Date.now();
230
+ latestArgs = null;
231
+ }
232
+ timeoutId = null;
233
+ }, Math.max(0, remaining));
234
+ }
235
+ }
236
+ };
237
+ throttled.cancel = () => {
238
+ if (timeoutId !== null) {
239
+ clearTimeout(timeoutId);
240
+ timeoutId = null;
241
+ }
242
+ lastExecTime = 0;
243
+ latestArgs = null;
244
+ };
245
+ throttled.flush = () => {
246
+ if (timeoutId !== null) {
247
+ clearTimeout(timeoutId);
248
+ timeoutId = null;
249
+ }
250
+ if (latestArgs) {
251
+ fn(...latestArgs);
252
+ lastExecTime = Date.now();
253
+ latestArgs = null;
254
+ }
255
+ };
256
+ return throttled;
257
+ }
package/dist/index.d.cts CHANGED
@@ -52,4 +52,41 @@ declare class Emitter<Events extends Record<string, any>> {
52
52
  emit<K extends Exclude<keyof Events, KeysWithVoidPayload<Events>>>(event: K, args: Events[K]): void;
53
53
  destroy(): void;
54
54
  }
55
- export { logWarn, logError, log, clamp, Emitter };
55
+ /**
56
+ * A function that can be cancelled or flushed.
57
+ */
58
+ interface DebouncedFunction<T extends (...args: any[]) => void> {
59
+ (...args: Parameters<T>): void;
60
+ cancel(): void;
61
+ flush(): void;
62
+ }
63
+ /**
64
+ * A function that can be cancelled or flushed.
65
+ */
66
+ interface ThrottledFunction<T extends (...args: any[]) => void> {
67
+ (...args: Parameters<T>): void;
68
+ cancel(): void;
69
+ flush(): void;
70
+ }
71
+ /**
72
+ * Creates a debounced function that delays invoking `fn` until after `delay` milliseconds
73
+ * have elapsed since the last time the debounced function was invoked.
74
+ *
75
+ * The latest arguments provided to the debounced function are passed to `fn`.
76
+ *
77
+ * @param fn - The function to debounce
78
+ * @param delay - The number of milliseconds to delay
79
+ * @returns A debounced function with .cancel() and .flush() methods
80
+ */
81
+ declare function debounce<T extends (...args: any[]) => void>(fn: T, delay: number): DebouncedFunction<T>;
82
+ /**
83
+ * Creates a throttled function that only invokes `fn` at most once per every `delay` milliseconds.
84
+ * The first call executes immediately. Subsequent calls are ignored until the delay passes,
85
+ * but if calls were made during the delay, the latest one will execute at the end of the delay.
86
+ *
87
+ * @param fn - The function to throttle
88
+ * @param delay - The number of milliseconds to throttle
89
+ * @returns A throttled function with .cancel() and .flush() methods
90
+ */
91
+ declare function throttle<T extends (...args: any[]) => void>(fn: T, delay: number): ThrottledFunction<T>;
92
+ export { throttle, logWarn, logError, log, debounce, clamp, ThrottledFunction, Emitter, DebouncedFunction };
package/dist/index.d.ts CHANGED
@@ -52,4 +52,41 @@ declare class Emitter<Events extends Record<string, any>> {
52
52
  emit<K extends Exclude<keyof Events, KeysWithVoidPayload<Events>>>(event: K, args: Events[K]): void;
53
53
  destroy(): void;
54
54
  }
55
- export { logWarn, logError, log, clamp, Emitter };
55
+ /**
56
+ * A function that can be cancelled or flushed.
57
+ */
58
+ interface DebouncedFunction<T extends (...args: any[]) => void> {
59
+ (...args: Parameters<T>): void;
60
+ cancel(): void;
61
+ flush(): void;
62
+ }
63
+ /**
64
+ * A function that can be cancelled or flushed.
65
+ */
66
+ interface ThrottledFunction<T extends (...args: any[]) => void> {
67
+ (...args: Parameters<T>): void;
68
+ cancel(): void;
69
+ flush(): void;
70
+ }
71
+ /**
72
+ * Creates a debounced function that delays invoking `fn` until after `delay` milliseconds
73
+ * have elapsed since the last time the debounced function was invoked.
74
+ *
75
+ * The latest arguments provided to the debounced function are passed to `fn`.
76
+ *
77
+ * @param fn - The function to debounce
78
+ * @param delay - The number of milliseconds to delay
79
+ * @returns A debounced function with .cancel() and .flush() methods
80
+ */
81
+ declare function debounce<T extends (...args: any[]) => void>(fn: T, delay: number): DebouncedFunction<T>;
82
+ /**
83
+ * Creates a throttled function that only invokes `fn` at most once per every `delay` milliseconds.
84
+ * The first call executes immediately. Subsequent calls are ignored until the delay passes,
85
+ * but if calls were made during the delay, the latest one will execute at the end of the delay.
86
+ *
87
+ * @param fn - The function to throttle
88
+ * @param delay - The number of milliseconds to throttle
89
+ * @returns A throttled function with .cancel() and .flush() methods
90
+ */
91
+ declare function throttle<T extends (...args: any[]) => void>(fn: T, delay: number): ThrottledFunction<T>;
92
+ export { throttle, logWarn, logError, log, debounce, clamp, ThrottledFunction, Emitter, DebouncedFunction };
package/dist/index.js CHANGED
@@ -131,10 +131,94 @@ class Emitter {
131
131
  this.#events = {};
132
132
  }
133
133
  }
134
+ // src/timing.ts
135
+ function debounce(fn, delay) {
136
+ let timeoutId = null;
137
+ let latestArgs = null;
138
+ const debounced = (...args) => {
139
+ latestArgs = args;
140
+ if (timeoutId !== null) {
141
+ clearTimeout(timeoutId);
142
+ }
143
+ timeoutId = setTimeout(() => {
144
+ if (latestArgs) {
145
+ fn(...latestArgs);
146
+ latestArgs = null;
147
+ }
148
+ timeoutId = null;
149
+ }, delay);
150
+ };
151
+ debounced.cancel = () => {
152
+ if (timeoutId !== null) {
153
+ clearTimeout(timeoutId);
154
+ timeoutId = null;
155
+ }
156
+ latestArgs = null;
157
+ };
158
+ debounced.flush = () => {
159
+ if (timeoutId !== null) {
160
+ clearTimeout(timeoutId);
161
+ timeoutId = null;
162
+ if (latestArgs) {
163
+ fn(...latestArgs);
164
+ latestArgs = null;
165
+ }
166
+ }
167
+ };
168
+ return debounced;
169
+ }
170
+ function throttle(fn, delay) {
171
+ let timeoutId = null;
172
+ let lastExecTime = 0;
173
+ let latestArgs = null;
174
+ const throttled = (...args) => {
175
+ const now = Date.now();
176
+ latestArgs = args;
177
+ if (!lastExecTime && timeoutId === null) {
178
+ fn(...args);
179
+ lastExecTime = now;
180
+ latestArgs = null;
181
+ } else {
182
+ if (timeoutId === null) {
183
+ const remaining = delay - (now - lastExecTime);
184
+ timeoutId = setTimeout(() => {
185
+ if (latestArgs) {
186
+ fn(...latestArgs);
187
+ lastExecTime = Date.now();
188
+ latestArgs = null;
189
+ }
190
+ timeoutId = null;
191
+ }, Math.max(0, remaining));
192
+ }
193
+ }
194
+ };
195
+ throttled.cancel = () => {
196
+ if (timeoutId !== null) {
197
+ clearTimeout(timeoutId);
198
+ timeoutId = null;
199
+ }
200
+ lastExecTime = 0;
201
+ latestArgs = null;
202
+ };
203
+ throttled.flush = () => {
204
+ if (timeoutId !== null) {
205
+ clearTimeout(timeoutId);
206
+ timeoutId = null;
207
+ }
208
+ if (latestArgs) {
209
+ fn(...latestArgs);
210
+ lastExecTime = Date.now();
211
+ latestArgs = null;
212
+ }
213
+ };
214
+ return throttled;
215
+ }
134
216
  export {
217
+ throttle,
135
218
  logWarn,
136
219
  logError,
137
220
  log,
221
+ debounce,
138
222
  clamp,
139
223
  Emitter
140
224
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gumbee/utils",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "author": "Gumbee <mugeeb.hassan@gmail.com>",
5
5
  "repository": {
6
6
  "type": "git",