ocetnik/react-native-background-timer

RangeError in long running timers

Opened this issue · 1 comments

I often have long-running timers. After a timer has been running for a long time I will start to get RangeError: Property storage exceeds 196607 properties errors on iOS.

This seems to be due to Hermes setting an arbitrary number of object keys. The recommendation in the RN issue tracker is to use a Map instead of Object.

I've patched this locally. For anyone else running into the same thing... here's a diff...

diff --git a/node_modules/react-native-background-timer/android/src/main/java/com/ocetnik/timer/BackgroundTimerModule.java b/node_modules/react-native-background-timer/android/src/main/java/com/ocetnik/timer/BackgroundTimerModule.java
index 1f87803..1ea2e08 100644
--- a/node_modules/react-native-background-timer/android/src/main/java/com/ocetnik/timer/BackgroundTimerModule.java
+++ b/node_modules/react-native-background-timer/android/src/main/java/com/ocetnik/timer/BackgroundTimerModule.java
@@ -89,6 +89,16 @@ public class BackgroundTimerModule extends ReactContextBaseJavaModule {
         }, (long) timeout);
     }
 

     /*@ReactMethod
     public void clearTimeout(final int id) {
         // todo one day..
diff --git a/node_modules/react-native-background-timer/index.js b/node_modules/react-native-background-timer/index.js
index f64e4b6..fe99508 100644
--- a/node_modules/react-native-background-timer/index.js
+++ b/node_modules/react-native-background-timer/index.js
@@ -12,14 +12,15 @@ const Emitter = new NativeEventEmitter(RNBackgroundTimer);
 class BackgroundTimer {
   constructor() {
     this.uniqueId = 0;
-    this.callbacks = {};
+    this.callbacks = new Map();
 
     Emitter.addListener('backgroundTimer.timeout', (id) => {
-      if (this.callbacks[id]) {
-        const callbackById = this.callbacks[id];
-        const { callback } = callbackById;
-        if (!this.callbacks[id].interval) {
-          delete this.callbacks[id];
+      if (this.callbacks.has(id)) {
+        const callbackById = this.callbacks.get(id);
+
+        const { callback, interval, timeout } = callbackById;
+        if (!interval) {
+          this.callbacks.delete(id);
         } else {
           RNBackgroundTimer.setTimeout(id, this.callbacks[id].timeout);
         }
@@ -68,18 +69,18 @@ class BackgroundTimer {
   setTimeout(callback, timeout) {
     this.uniqueId += 1;
     const timeoutId = this.uniqueId;
-    this.callbacks[timeoutId] = {
+    this.callbacks.set(timeoutId, {
       callback,
       interval: false,
       timeout,
-    };
+    });
     RNBackgroundTimer.setTimeout(timeoutId, timeout);
     return timeoutId;
   }
 
   clearTimeout(timeoutId) {
-    if (this.callbacks[timeoutId]) {
-      delete this.callbacks[timeoutId];
+    if (this.callbacks.has(timeoutId)) {
+      this.callbacks.delete(timeoutId);
       // RNBackgroundTimer.clearTimeout(timeoutId);
     }
   }
@@ -87,18 +88,18 @@ class BackgroundTimer {
   setInterval(callback, timeout) {
     this.uniqueId += 1;
     const intervalId = this.uniqueId;
-    this.callbacks[intervalId] = {
+    this.callbacks.set(intervalId, {
       callback,
       interval: true,
       timeout,
-    };
+    });
     RNBackgroundTimer.setTimeout(intervalId, timeout);
     return intervalId;
   }
 
   clearInterval(intervalId) {
-    if (this.callbacks[intervalId]) {
-      delete this.callbacks[intervalId];
+    if (this.callbacks.has(intervalId)) {
+      this.callbacks.delete(intervalId);
       // RNBackgroundTimer.clearTimeout(intervalId);
     }
   }

Thank you for this. Luckily I caught this before going to production.