git-watchtower 2.0.1 → 2.0.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/utils/async.js +20 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "git-watchtower",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Terminal-based Git branch monitor with activity sparklines and optional dev server with live reload",
5
5
  "main": "bin/git-watchtower.js",
6
6
  "bin": {
@@ -206,17 +206,24 @@ function debounce(fn, delay) {
206
206
 
207
207
  /**
208
208
  * Throttle a function - execute at most once per interval
209
+ *
210
+ * Mirrors debounce() in returning a wrapped function with a `.cancel()`
211
+ * method. If a trailing call has been scheduled (throttle fires on the
212
+ * leading edge and coalesces further calls during the interval into a
213
+ * single trailing call), cancel() drops it. Wire cancel() into shutdown
214
+ * paths so a UI-event-driven throttle can't fire a deferred call after
215
+ * the target module has torn down and leave state partially written.
216
+ *
209
217
  * @template {(...args: any[]) => void} T
210
218
  * @param {T} fn - Function to throttle
211
219
  * @param {number} interval - Minimum interval between calls in milliseconds
212
- * @returns {T}
220
+ * @returns {T & { cancel: () => void }}
213
221
  */
214
222
  function throttle(fn, interval) {
215
223
  let lastCall = 0;
216
224
  let timeoutId = null;
217
225
 
218
- // @ts-ignore - TypeScript can't verify generic function return type
219
- return (...args) => {
226
+ const throttled = (...args) => {
220
227
  const now = Date.now();
221
228
  const timeSinceLastCall = now - lastCall;
222
229
 
@@ -232,6 +239,16 @@ function throttle(fn, interval) {
232
239
  }, interval - timeSinceLastCall);
233
240
  }
234
241
  };
242
+
243
+ throttled.cancel = () => {
244
+ if (timeoutId) {
245
+ clearTimeout(timeoutId);
246
+ timeoutId = null;
247
+ }
248
+ };
249
+
250
+ // @ts-ignore - TypeScript can't verify generic function augmentation
251
+ return throttled;
235
252
  }
236
253
 
237
254
  module.exports = {