Subscribe

  • Subscribe  

Time-delaying Cairngorm Commands Execution

Posted by Harry Vikatos | July 8, 2008 .

In cases where data are edited in a Flex client and subsequently have to be saved on a server, one has to manage the frequency of those server calls.

One way of doing this, is adding an extra step, for the user to save the changes made. So image you have an editable data grid, presenting some objects you want to persist on your server. User edits data but then has to press the ‘Save’ button for the changes to be saved. This way you can aggregate all your changes into one server call, instead of making a call for each cell edit on the grid.

However that a far from ideal approach for modern user friendly applications. We shall present below a mechanism for aggregating all data changes concerning an object within a time period, into one server call, using the Cairngorm framework.

Here’s the idea. When the Command for persisting an object on the server is executed, it register itself within a singleton object. The singleton takes care of calling back the Command for executing the server call, after a set time delay. If before that happens, another Command concerning persisting the same object, registers itself, the timer is reset. When the delay elapses only one command is executed, resulting in saving the object carrying both changes.

The code below, shows a way for implementing this mechanism. In this case we chose to put the singleton control within the FrontController of our application, which I think is an appropriate place to put it.

FrontController with time delay mechanism:

public class TimeDelayController extends FrontController
    {
        private static var instance: TimeDelayController ;
        private var updateQueue:ArrayCollection = new ArrayCollection;

        public static function getInstance() : TimeDelayController {
             if ( instance == null ) instance = new TimeDelayController();
             return instance;
        }

        public function TimeDelayController () {
             if (instance != null)    {
                throw new Error("Only one TimeDelayController
			instance should be instantiated!");
             }
             TimeDelayController.instance = this;
             addCommand("persistObject", PersistObject);
        }

        public function registerUpdateQueue(c:PersistObject):void {
            for each(var r:Object in updateQueue) {
                if(r.object== c.object) {
                    clearTimeout(r.timer);
                    r.timer = setTimeout(triggerUpdate, 5000, r);
                    return;
                }
            }
            r = new Object;
            r.object= c.object;
            r.command = c;
            r.timer = setTimeout(triggerUpdate, 5000, r);
            updateQueue.addItem(r);
        }

        private function triggerUpdate(reg:Object):void {
            (reg.command as PersistObject).triggerUpdate();
            updateQueue.removeItemAt(updateQueue.getItemIndex(reg));
        }

        public function triggerAllUpdates():void {
            for each(var reg:Object in updateQueue) {
                clearTimeout(reg.timer);
                (reg.command as PersistObject).triggerUpdate();
            }
            updateQueue.removeAll();
        }

Command using the time delay mechanism:

public class PersistObject implements ICommand
    {
        public var object:MyObject;

        public function execute(event:CairngormEvent):void
        {
            object= (event as MyEvent).object;
            TimeDelayController.getInstance().registerUpdateQueue(this);
        }

        public function triggerUpdate():void {
           new ServiceDelegate().updateObject(object);
        }
}

Notice the function triggerAllUpdates in the controller, which may be used for flashing the queue and forcing all pending updates. Necessary for example in case the user wants to exit the application.

Leave a Comment

If you would like to make a comment, please fill out the form below.


Name

Email

Website

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Comments

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Related Posts

<

Direct TV Offers - usdirect has the best directtv deals