Browse Source

fix: lower stacktracelimit

Sv443 1 year ago
parent
commit
0c716a667b
3 changed files with 12 additions and 8 deletions
  1. 5 0
      .changeset/quiet-cobras-float.md
  2. 1 1
      README.md
  3. 6 7
      lib/dom.ts

+ 5 - 0
.changeset/quiet-cobras-float.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": patch
+---
+
+Lowered the `Error.stackTraceLimit` by a multiple of 10 to preserve memory

+ 1 - 1
README.md

@@ -604,7 +604,7 @@ interceptEvent(
   
   
 Intercepts all events dispatched on the `eventObject` and prevents the listeners from being called as long as the predicate function returns a truthy value.  
 Intercepts all events dispatched on the `eventObject` and prevents the listeners from being called as long as the predicate function returns a truthy value.  
 If no predicate is specified, all events will be discarded.  
 If no predicate is specified, all events will be discarded.  
-Calling this function will set the `Error.stackTraceLimit` to 1000 (if it's not already higher) to ensure the stack trace is preserved.  
+Calling this function will set the `Error.stackTraceLimit` to 100 (if it's not already higher) to ensure the stack trace is preserved.  
   
   
 ⚠️ This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are *attached* after this function is called.  
 ⚠️ This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are *attached* after this function is called.  
   
   

+ 6 - 7
lib/dom.ts

@@ -91,19 +91,18 @@ export function openInNewTab(href: string) {
  * Intercepts the specified event on the passed object and prevents it from being called if the called {@linkcode predicate} function returns a truthy value.  
  * Intercepts the specified event on the passed object and prevents it from being called if the called {@linkcode predicate} function returns a truthy value.  
  * If no predicate is specified, all events will be discarded.  
  * If no predicate is specified, all events will be discarded.  
  * This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are added after this function is called.  
  * This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are added after this function is called.  
- * Calling this function will set the `Error.stackTraceLimit` to 1000 to ensure the stack trace is preserved.
+ * Calling this function will set `Error.stackTraceLimit = 100` (if not already higher) to ensure the stack trace is preserved.
  */
  */
 export function interceptEvent<TEvtObj extends EventTarget, TPredicateEvt extends Event>(
 export function interceptEvent<TEvtObj extends EventTarget, TPredicateEvt extends Event>(
   eventObject: TEvtObj,
   eventObject: TEvtObj,
   eventName: Parameters<TEvtObj["addEventListener"]>[0],
   eventName: Parameters<TEvtObj["addEventListener"]>[0],
   predicate: (event: TPredicateEvt) => boolean = () => true,
   predicate: (event: TPredicateEvt) => boolean = () => true,
 ) {
 ) {
-  // default is between 10 and 100 on conventional browsers so this should hopefully be more than enough
+  // default is 25 on FF so this should hopefully be more than enough
   // @ts-ignore
   // @ts-ignore
-  if(typeof Error.stackTraceLimit === "number" && Error.stackTraceLimit < 1000) {
-    // @ts-ignore
-    Error.stackTraceLimit = 1000;
-  }
+  Error.stackTraceLimit = Math.max(Error.stackTraceLimit, 100);
+  if(isNaN(Error.stackTraceLimit))
+    Error.stackTraceLimit = 100;
 
 
   (function(original: typeof eventObject.addEventListener) {
   (function(original: typeof eventObject.addEventListener) {
     // @ts-ignore
     // @ts-ignore
@@ -125,7 +124,7 @@ export function interceptEvent<TEvtObj extends EventTarget, TPredicateEvt extend
  * Intercepts the specified event on the window object and prevents it from being called if the called {@linkcode predicate} function returns a truthy value.  
  * Intercepts the specified event on the window object and prevents it from being called if the called {@linkcode predicate} function returns a truthy value.  
  * If no predicate is specified, all events will be discarded.  
  * If no predicate is specified, all events will be discarded.  
  * This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are added after this function is called.  
  * This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are added after this function is called.  
- * Calling this function will set the `Error.stackTraceLimit` to 1000 to ensure the stack trace is preserved.
+ * Calling this function will set `Error.stackTraceLimit = 100` (if not already higher) to ensure the stack trace is preserved.
  */
  */
 export function interceptWindowEvent<TEvtKey extends keyof WindowEventMap>(
 export function interceptWindowEvent<TEvtKey extends keyof WindowEventMap>(
   eventName: TEvtKey,
   eventName: TEvtKey,