root/as3/Commands/src/commands/CommandContainer.as

リビジョン 609, 2.5 kB (コミッタ: fladdict, コミット時期: 4 年 前)

--

Line 
1 package commands
2 {
3         import flash.utils.Dictionary;
4         import flash.events.Event;
5        
6         /**
7          * 実行中の非同期コマンドがガベージコレクトされないよう、参照を保持するクラス.
8          *
9          * SerialCommand, ParallelCommand, AsyncCommandなど非同期のコマンドが、実行中にガベージコレクションされるのを防ぐのに用いられます。
10          * 実行終了後、CommandContainerに格納されたICommandインスタンスへの参照は開放されます。
11          *
12          * @example 以下の例ではSerialCommandを実行し、その終了までSerialCommandの参照を保持します。
13          * <listing version="3.0">
14          * var serialCommand = new SerialCommand([
15          *   new Command(null, trace, ["test"]);
16          *   new WaitCommand(10000);
17          *   new Command(null, trace, ["test"]);
18          * ])
19          *
20          * CommandContainer.execute( serialCommand );
21          * </listing>
22          */
23         public class CommandContainer
24         {
25                 protected static var _commandDict:Dictionary
26                 protected static var _numCommands:int = 0;
27                
28                 /**
29                  * 引数として渡したICommandを実行し、処理が終了するまで参照を保持します。
30                  *
31                  * コマンドの終了時、CommandContainer内に保持された参照は破棄されます。
32                  */
33                 public static function execute(command:ICommand):void
34                 {
35                         if(_commandDict==null)
36                                 _commandDict = new Dictionary();
37                                
38                         if(_commandDict[command]){
39                                 throw new Error("CommandContainer.execute() this command is alrealdy registerd");
40                         }
41                        
42                         _commandDict[command] = command;
43                         _numCommands++;
44                        
45                         command.addEventListener(Event.COMPLETE, executeHandler);
46                         command.execute();
47                 }
48                
49                
50                 //デバッグ用、現在実行中のコマンドの数を返す。
51                 public static function get numCommands():int
52                 {
53                         return _numCommands;
54                 }
55                
56                
57                 //デバッグ用、現在実行中のコマンドをダンプする。
58                 public static function dump():void
59                 {
60                         for (var prop:* in _commandDict){
61                                 trace(_commandDict[prop]);
62                         }
63                 }
64                
65                
66                 /*
67                         コマンドの終了のハンドリング。
68                         終了したコマンドのリスナ解除と、参照の廃棄を行う。
69                 */
70                 protected static function executeHandler(e:Event):void
71                 {
72                         var command:ICommand = ICommand(e.target);
73                         command.removeEventListener(Event.COMPLETE, executeHandler);
74                         _numCommands--;
75                        
76                         //すぐ消さないずに1フレームぐらい待ったほうがいいのか??
77                         _commandDict[command] = null;
78                         delete _commandDict[command];
79                 }
80         }
81 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。