root/as2/Seyself/src/com/seyself/utils/SyncMethod.as

リビジョン 106, 2.7 kB (コミッタ: michi, コミット時期: 1 年 前)

--

Line 
1 /*
2 * Copyright (c) 2007 michiyasu wada
3 * http://www.seyself.com/
4 *
5 * Distributed under The MIT License.
6 * [http://www.opensource.org/licenses/mit-license.php]
7 */
8
9 /**
10 * 既存のメソッドが実行されたときに、登録しておいた関数を実行させます。
11 * @author               Michiyasu Wada
12 */
13
14 class com.seyself.utils.SyncMethod
15 {
16        
17         /**
18         * @private
19         */
20         private function SyncMethod(){}
21        
22         /**
23         * 既存のメソッドが実行されたときに、その直後に実行させる関数を登録します。
24         * @param        target                  登録されるメソッドを持つオブジェクト
25         * @param        functionName    登録されるメソッドの名前
26         * @param        handler                 関連させる関数
27         */
28         public static function synchronize( target:Object, functionName:String, handler:Function ):Void
29         {
30                 var _execute, i:Number;
31                 if( typeof( target[functionName].execute ) == "function" ){
32                         _execute = target[functionName].execute;
33                 } else {
34                         _execute = target[functionName];
35                 }
36                 //if( !_execute.hasOwnProperty("syncHandler") ){
37                 if( _execute.syncHandler == undefined ){
38                         _execute.syncHandler = new Array();
39                 }
40                 _execute.syncHandler.push( handler );
41                 var hands:Array = _execute.syncHandler;
42                 var len:Number = hands.length;
43                 var sync:Object = {};
44                 sync.execute = function()
45                 {
46                         _execute.apply( target , arguments );
47                         for(i=0;i<len;i++){
48                                 hands[i].apply( target , arguments );
49                         }
50                 }
51                 target[functionName] = sync.execute;
52                 target[functionName].syncHandler = hands;
53                 target[functionName].execute = _execute;
54         }
55        
56         /**
57         * 関連して実行される関数を選択して解除します。
58         * @param        target                  登録されたメソッドを持つオブジェクト
59         * @param        functionName    登録されたメソッドの名前
60         * @param        handler                 関連を解除する関数
61         */
62         public static function remove( target:Object, functionName:String, handler:Function ):Void
63         {
64                 var _execute = target[functionName];
65                 //if( _execute.hasOwnProperty("syncHandler") ){
66                 if( _execute.syncHandler ){
67                         var hands:Array = _execute.syncHandler;
68                         var len:Number = hands.length;
69                         for(var i=0;i<len;i++){
70                                 if( hands[i] == handler ){
71                                         hands.splice( i , 1 );
72                                         break;
73                                 }
74                         }
75                 }
76         }
77        
78         /**
79         * 関連して実行される関数をすべて解除します。
80         * @param        target                  登録されたメソッドを持つオブジェクト
81         * @param        functionName    登録されたメソッドの名前
82         */
83         public static function removeAll( target:Object, functionName:String ):Void
84         {
85                 var _execute = target[functionName];
86                 //if( _execute.hasOwnProperty("execute") ){
87                 if( _execute.execute ){
88                         target[functionName] = _execute.execute;
89                 }
90         }
91        
92        
93        
94 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。