///////////////////////////////////////////////////////////////////////////////// // Copyright © 2008 W. Kowarschick // // Permission is granted to copy, distribute and/or modify this document // under the terms of the WK License, Version 1.0 (WKL 1.0) or any later version // published by Wolfgang Kowarschick. ///////////////////////////////////////////////////////////////////////////////// package service { import flash.events.Event; import mx.collections.ArrayCollection; public class ServiceQueue { ///////////////////////////////////////////////////////////////////////////// // Private Members ///////////////////////////////////////////////////////////////////////////// private var v_waiting_operations: ArrayCollection = new ArrayCollection(); private var v_current_operation: IService; private var v_finished: Boolean = true; ///////////////////////////////////////////////////////////////////////////// // Public Methods ///////////////////////////////////////////////////////////////////////////// public function execute(p_service: IService): void { v_waiting_operations.addItem(p_service); if (v_finished) perform_next(); } ///////////////////////////////////////////////////////////////////////////// // Event Handlers ///////////////////////////////////////////////////////////////////////////// private function perform_next(): void { if (v_current_operation != null) { v_current_operation.removeEventListener(Event.COMPLETE, eh_completed); v_current_operation = null; }; if (v_waiting_operations.length > 0) { v_current_operation = v_waiting_operations.removeItemAt(0) as IService; trace("perform: " + v_current_operation); v_current_operation.addEventListener(Event.COMPLETE, eh_completed); v_current_operation.execute(); v_finished = false; }; } private function eh_completed(p_event: Event): void { trace("completed: " + v_current_operation); v_finished = true; perform_next(); } ///////////////////////////////////////////////////////////////////////////// // End of Class ///////////////////////////////////////////////////////////////////////////// } }