turtlebite
|
 |
« on: January 27, 2014, 12:32:55 » |
|
Hi all
I know there's Async Commands, but I use to do asynchronious loading in a really simple way, and it works perfectly (I'm using it in ActionScript and Java projects). Because it is so easy, I would like to know what you think of it. :-)
PrepareModelCommand is called when PREPARE_MODEL notification is sent. In it, a proxy is registered and the command dies. We have to wait for this proxy to finish it's setup (maybe it loads from a database or whatever). When it is ready, it sends a PROXY_INITIALIZED notification. This is again mapped to PrepareModelCommand, which checks who called it (I use "type" of the notification for this), and then the next proxy is registered and so on. If no async loading is needed, I may register some proxies at the same time, whatever the situation demands. In the end, the PREPARE_VIEW notification is sent, and in the PrepareViewCommand, the same procedure starts again.
In code it looks somehow like this (Java):
AppFacade:
... registerCommand(AppConstants.PREPARE_MODEL, new PrepareModelCommand()); registerCommand(AppConstants.PROXY_INITIALIZED, new PrepareModelCommand()); registerCommand(AppConstants.PREPARE_VIEW, new PrepareViewCommand()); registerCommand(AppConstants.MEDIATOR_INITIALIZED, new PrepareViewCommand()); ...
PrepareModelCommand:
@Override public void execute(final INotification note) throws IOException, ClassNotFoundException { if (note.getName() == AppConstants.PREPARE_MODEL) { getFacade().registerProxy(new AnswerProxy(new AnswerData())); getFacade().registerProxy(new AppProxy(new AppData((Stage) note.getBody()))); } else { if (note.getType() == AppProxy.NAME) { // AppProxy is ready. Do something... final AppData appData = (AppData) getFacade().retrieveProxy(AppProxy.NAME).getData(); final double w = (appData.getAppWidth() - 200) / 6; configData.setImageSize((int) w); // ... and register the next proxy. getFacade().registerProxy(new ConfigProxy(configData)); } else if (note.getType() == ConfigProxy.NAME) { // ConfigProxy is ready... load the next Proxy. getFacade().registerProxy(new HighScoreProxy(highScoreData)); } else if (note.getType() == HighScoreProxy.NAME) { // HighScoreProxy is ready... register UserProxy (we don't have to wait for this one) getFacade().registerProxy(new UserProxy(new UserData())); configData = (ConfigData) getFacade().retrieveProxy(ConfigProxy.NAME).getData(); // ...and register ImagesProxy getFacade().registerProxy(new ImagesProxy(configData, new ImagesData(AppConstants.IMAGE_BACKSIDE_PATH))); } else if (note.getType() == ImagesProxy.NAME) { // ImagesProxy is ready, load the images (get's them from a directory, so we need to wait for it) final ImagesProxy imagesProxy = (ImagesProxy) getFacade().retrieveProxy(ImagesProxy.NAME); imagesProxy.loadImages(); // ... but we can already register the StateProxy. getFacade().registerProxy(new StateProxy(new StateData())); } else if (note.getType() == StateProxy.NAME) { // StateProxy is ready, load the next one... getFacade().registerProxy(new SoundProxy(new SoundData(configData.getSoundEnabled()))); } else if (note.getType() == SoundProxy.NAME) { // SoundProxy is ready, load the final one, and because we don't have to wait for this, // the model is ready and the View can start it's initialisation final SoundProxy soundProxy = (SoundProxy) getFacade().retrieveProxy(SoundProxy.NAME); soundProxy.getData().playStart(); // PrepareModelCommand is not needed anymore, so let's get rid of it: getFacade().removeCommand(AppConstants.PREPARE_MODEL); // Model is ready, move on to the view! sendNotification(AppConstants.PREPARE_VIEW); } } }
ConfigProxy:
@Override public void onRegister() throws IOException, ClassNotFoundException { // Do something.... if (getData().getImagePath().isEmpty()) { getData().setImagePath(AppConstants.IMAGE_PATH); } // ...when you think this proxy is ready, send the notification so the next proxy is registered. sendNotification(AppConstants.PROXY_INITIALIZED, null, NAME); }
So, the simple idea is that the PrepareModelCommand (and PrepareViewCommad) is called more than once, each time another Proxy requested it. The command does it's job and dies. So we don't misuse a command and let it wait for a response. When a proxy is ready, it just calls the command again, it does the next piece of code and dies again.
What do you think about this procedure?
|