How to perform repetitive actions in PureMVC flex application?

342 views Asked by At

Currently working on a flex AIR project based on PureMVC framework. There was a new requirement to the project, an operation is to be performed repetitively at regular interval which has some business logic. As per PureMVC this should be placed in the command, but then command would get garbage collected the moment its executed.

Given the scenario there are few doubts/confusions/clarifications...

  1. Creating a command with business logic would be one solution ... but then who would trigger it at specific interval? (shouldn't this be done by a command as per PureMVC)
  2. Placing the command trigger in a mediator or placing the logic in a mediator (this would make pureMVC a bit impure :) )

How do I find a solution for this kind of scenario?

4

There are 4 answers

0
Creynders On BEST ANSWER

You need to pull apart the async process you want to run and the repetitive triggering of said process. My advice is to create a Proxy for the async process and a separate Proxy that's a wrapper for a Timer which simply sends a notification upon timeout. The notification is coupled to a command, which in turn calls the async proxy's methods. That way you can add logic to the command for instance what to do if the process is still busy.

The benefits of creating two proxies: you adhere to SRP. You can easily swap/modify/remove the timing proxy w/o touching the async proxy. Everything is nicely separated.

0
Ctrlspc On

Try this:

  • Create a Singleton class - singleton class in Flex
  • Have a function in this class (eg. called start) that when called starts a timer, the callback function of which sends a notification, that triggers a command that does your business logic.
  • When you are ready to start your regular actions simply call the get instance method on your singleton, to create the singleton and then call it's start() function. *Optionally have a stop function that cancels the timer and stops the regular notifications being sent.

    package
    {
     import flash.events.TimerEvent;
     import flash.utils.Timer;
    
    
     public final class RepititiveSingleton
     {
        private var timer:Timer;
    
        private static var instance:RepititiveSingleton= new RepititiveSingleton();
    
        public function RepititiveSingleton() {
            if( RepititiveSingleton.instance ) {
                throw new Error( 
                    "ReptitiveSingleton can only be accessed through Singleton.getInstance()" ); 
            }
        }
    
        public static function getInstance():RepititiveSingleton{                        
            return RepititiveSingleton.instance;
        }
    
        public function start(interval:Number = 1000):void {
            timer = new Timer(interval);
            timer.addEventListener(TimerEvent.TIMER, onTimer);
        }
    
        private function onTimer(e:TimerEvent):void {
                     ApplicationFacade.getInstance().sendNotification(Constants.REPTITIVE_ACTION_NOTIFICATION));
        }
    
     }
    }
    

This code assumes that you have your Concrete facade named ApplicationFacade, and have registered a notification using a String constant that is referenced from a class called constants.

Then in an appropriate place (maybe in your startup command) you can add:

RepetitiveSingleton.getInstance().start();

Hope this helps you.

1
Philipp Kyeck On

depends on what the Command should do - if it updates the Model put a Timer in one of your Proxy class and send a Notification every xx seconds which is mapped to a Command that does whatever it is you want it to do.

If it should just update the View you could add the Timer to the corresponding Mediator but then you wouldn't need a Command at all.

**

Don't create more Singletons than you need. ApplicationFacade is already one - try and keep it that way.

**

If you have to do any async calls to the backend, just make sure to add the EventListener without weakReference set to true - then everything should be ok...

0
Mark Robbins On

IMO, the timer belongs in a mediator. Let it- well, mediate the asynch process messaging. It will be a little state machine to make sure everything is running smoothly. Commands still do the heavy lifting, it just sits around like a 911 operator.