robcalcroft / react-native-in-app-notification

:bell: Customisable in-app notification component for React Native
MIT License
270 stars 78 forks source link

consecutive calls shows just first one #40

Open dengue8830 opened 5 years ago

dengue8830 commented 5 years ago

Problem

if i call the notification two or more times consecutively it renders just the first one. If i use a timeout for the second one i get a good behaviour.

Proposal

I'm thinking implement a delayed queue in order to show consecutive calls in a queued way and show them one by one with a timeout. Do you accept a pull request for this?

robcalcroft commented 5 years ago

Sure please propose a PR I’ll be happy to take a look :)

On 23 Oct 2019, at 16:36, David Rearte notifications@github.com wrote:

 Problem

if i call the notification two or more times consecutively it renders just the first one. If i use a timeout for the second one i get a good behaviour.

Proposal

I'm thinking implement a delayed queue in order to show consecutive calls in a queued way and show them one by one with a timeout. Do you accept a pull request for this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

dengue8830 commented 5 years ago

I will try to add this feature next week, by the moment i used this code (if someone find it useful) in my project

This is a resume of the real code

function timeout(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

class NotificationService {
    // you can call this one several times
    show(notif) {
          this.queue.push(notif);
          this.evaluateQueue();
    }

    async evaluateQueue() {
      if (!this.queue.length || this.isEvaluationProcessLocked) {
        return;
      }
      this.isEvaluationProcessLocked = true;
      // We can use a recursive approach too
      while (this.queue.length) {
        this._show(this.queue.splice(0, 1)[0]); // the real showNotification api
        await timeout(4000); // closeInterval, check docs of this lib
      }
      this.isEvaluationProcessLocked = false;
  }
}