Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be Able to Avoid Endless Loop When Listening for and Firing Same Event #525

Closed
philippfromme opened this issue Jan 21, 2021 · 5 comments
Closed
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@philippfromme
Copy link
Contributor

philippfromme commented Jan 21, 2021

Listening for an event and firing that same event in the callback will result in an endless loop:

const listener = () => {
  eventBus.fire('foo');
};

eventBus.on('foo', listener);

eventBus.fire('foo');

Currently, the only way to prevent this is to stop and start listening again in the callback:

const listener = () => {
  eventBus.off('foo', listener);

  eventBus.fire('foo');

  eventBus.on('foo', listener);
};

eventBus.on('foo', listener);

eventBus.fire('foo');

There should be a more elegant way of avoiding this kind of situation. Sometimes the user might not be aware that the event he is listening for is fired through the callback.

Related #524

@nikku
Copy link
Member

nikku commented Jan 21, 2021

I'm not sure if this is something we should solve / support in an elegant way on the EventBus layer.

Solely from the API perspective, this looks like expected behavior to me: If I fire foo inside a listener, listening for foo, surely that is going to result in an endless loop? What else should I expect?

@nikku nikku added the help wanted Extra attention is needed label Jan 21, 2021
@philippfromme
Copy link
Contributor Author

I'm not saying we should solve this in the event bus. This issue is simply meant to start the discussion. 😉

@nikku
Copy link
Member

nikku commented Jan 21, 2021

What is the concrete instance of that problem? I guess, looking into bpmn-io/align-to-origin#2 it is the fact that you cannot safe the diagram on commandStack.changed when some component changes the command stack on diagram save?

@philippfromme
Copy link
Contributor Author

Yes, this is the original issue. There is no way of listening to the diagram has changed to export it when using the align-to-origin module.

@nikku
Copy link
Member

nikku commented Jan 27, 2021

Let's close this one. Now that #524 is closed a simple way to achieve this is:

function listenAndRepeat() {
  
  eventBus.fire('listenOnlyOnce', ...);

  eventBus.once('listenOnlyOnce', listenAndRepeat);
}

eventBus.once('listenOnlyOnce', listenAndRepeat);

In the above case listenAndRepeat can safely fire listenAndRepeat without looping.

@nikku nikku closed this as completed Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants