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

Is there a way to wait until all listeners have completed (async) tasks? #42

Open
julkue opened this issue Dec 17, 2014 · 2 comments
Open

Comments

@julkue
Copy link

julkue commented Dec 17, 2014

Hi,

I am using MediatorJS with requireJS modules. After a page change all my listeners will be informed and they will execute some tasks. Some tasks are async. This has the effect that this will not work constantly:

                    showLoader();

                    // Publish page change
                    mediator_.publish(
                        config_.mediator.channel.afterContentNavigation,
                        {
                            "content": $frameDoc,
                            "page": page
                        }
                    );

                    hideLoader();

As you can imagine if a components listens to the afterContentNavigation it will be triggered sure, but the hideLoader() function will may be called before the async task is completed.

Is there a way to handle such things with Mediator-JS?

@ajacksified
Copy link
Owner

Just to clarify, this is my understanding of your situation:

  • Call showLoader
  • Publish an event that fires 1-n async events
  • After those 1-n async events are finished, you want to call hideLoader

Is that correct?

One way you might accomplish that is to have one of the async handlers fire another event that says "hey, I'm ready to hide the loader now, I have content"- maybe one subscribing function calls out to an AJAX event to load some data, for example. If that's the case, you could do something like:

function myAsyncMethod(properties) {
  ajax.getData(function(data){ 
    renderTemplate();
    props.mediator.publish('page:loaded'); // <- add this publish
  });
} 

showLoader();
mediator.publish('...');

// `once` subscribes to the event, but immediately unsubscribes once fired
mediator.once('page:loaded', hideLoader);

Would that work for your code?

@julkue
Copy link
Author

julkue commented Dec 17, 2014

@ajacksified Thank you for the fast response. Yes, you have correct understand me. I have multiple requireJS-modules. After a page load the app will publish page:afterContentNavigation (as used in your example) and therefore all listeners will be called.
If I had only one module that is async it could fire that named event "hey, I'm ready to hide the loader". But what if I have multiple events? If I would hide the loader if the event gets published it only would wait for the first publish.

So imagin I have three modules:

  1. ABC
  2. DEF
  3. GHI

All this three modules are listening to the event page:afterContentNavigation. In the app it will be published. So I call mediator.publish('page:afterContentNavigation');. Now, how could I wait until all async methods inside those plugins are completed? I would have to make a event for each module and build a counter which checks if all those events are fired. If so, it would call the hideLoader().

I'm not happy with that solution and I thought, maybe mediator-js has a solution for this frequently occurring problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants