kheina-com / Blue-Blocker

Blocks all Twitter Blue verified users on twitter.com
Mozilla Public License 2.0
321 stars 28 forks source link

use exponential backoff when messaging background script #285

Open kheina opened 1 month ago

kheina commented 1 month ago

fixes https://github.com/kheina-com/Blue-Blocker/issues/232

kheina commented 1 month ago

I like the exponential back off loop a lot, I think we could make it its own function though. Something like

function attemptWithBackoff(action: string, data: any, errorMsg: string = undefined, maxAttempts: number = 5) {
  const message = errorMsg || `unable to execute ${action} action`
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
          response = await api.runtime.sendMessage<RuntimeMessage, MessageResponse>({
          action: action,
          data: data,
      });
      if (response.status === SuccessStatus) {
          // I know typescript is probably going to hate this, but idk what types to use here
          return response?.result;
      }
              else {
          console.error(logstr, message, response)
          await new Promise(r => setTimeout(r, attempt ** 2 * 1000));
      }
  }
  throw new Error(`${message}, after ${maxAttempts} retries`);
}

yeah, I thought about that too, making a sendMessage or sendInternalMessage util func, but I'd want to do it with templating and I decided to do it the easy way first idk.

also a heads up, these comments should be made in the code itself so that the discussion can happen in a thread rather than through these quote replys~ if there's multiple places where it applies you can usually just pick whichever and then reference the original