Hello PureMVC users!
Im a new user to puremvc and have been playing with PureMVC the last week to find out how its working. I ported the puremvc4gwt loginsample to this puremvc multicore version and saw a small difference in how the code notifies observers.
From the loginsample from puremvc4gwt I found out that the StartupCommand adds two subcommands (PrepView and PrepModel) which when executed with the Startup event registers the proxy and mediators.
In the official multicore version in method notifyObservers in the View class I found that (marked in bold in the code below) the code creates a private copy of the observer list and uses this to iterate.
The issue I saw when running the loginsample with the official puremvc multicore version was that any new or changes to observersMap registered during the notification of the STARTUP was ignored.
As you see in the code below, the private copy doesnt see any new observers, but actually there has been one registered by the StartupCommand that also listens to the same Startup notification (SplashScreenMediator also listens to STARTUP command). It is present in the observermap and observers_ref but not in the array copy observers.
I did a quick change to the code, to work on the "live" list of observers_ref instead of a private copy, and this worked. Any changes to the list of observers are added to the end of the list so if any new observers are registered they are executed in the end of the iteration. This is also how puremvc4gwt does it in its code.
What is the reason behind isolation the observerlist in a private copy? Is it the loginsample that is not following best practice?
public void notifyObservers( INotification note )
{
List<IObserver> observers_ref = (List<IObserver>) this.observerMap.get(note.getName());
if (observers_ref != null) {
// Copy observers from reference array to working array,
// since the reference array may change during the
// notification loop
Object[] observers = (Object[])observers_ref.toArray();
// Notify Observers from the working array
for (int i = 0; i < observers.length; i++) {
IObserver observer = (IObserver)observers;
observer.notifyObserver(note);
}
}
}