scottcorgan / tiny-emitter

A tiny (less than 1k) event emitter library
MIT License
935 stars 67 forks source link

Feature: Add Persistent to Event #46

Open acarlstein opened 1 year ago

acarlstein commented 1 year ago

First, thank you for creating this Event Bus. It was easy to setup. Much appreciated.

Problem

If I send an event prior the on handler is setup, that event is lost by the time the on handle is setup; therefore, the event is not persistent.

It would be nice to have a way to indicate that the event being emitted is persistent. In this way, if the on handler is set later, it will still receive the event.

Replicate

Create a Tabs project using Ionic + Vue3 with tiny-emitter (Ionic have an example of this)

Create two tabs (or whatever number you want).

On tab 1, I select an image and send an event with the data of the image.

EventBus().emitter.emit("selectedImage", image); 

On tab 2, I have the event handler (consumer) on

const instance = getCurrentInstance();    
    EventBus().emitter.on("selectedImage", (image: Image) => {     
      if (instance){
        instance.data.image = image.webviewPath
      }
 })

I have tried in many places in the code such as beforeCreated, Created, beforeMount, mounted, setup, onMounted (inside setup), etc.

Nice to have

It would be nice to indicate that the event emitted should be persistent. This would allow the consumption of the event the moment a on handler is set.