OSDN Git Service

Version 0.6.14, fix around callback.
authoritozyun <itozyun@user.sourceforge.jp>
Fri, 22 Nov 2013 20:34:04 +0000 (05:34 +0900)
committeritozyun <itozyun@user.sourceforge.jp>
Fri, 22 Nov 2013 20:34:04 +0000 (05:34 +0900)
0.6.x/js/core/03_XCallback.js
0.6.x/js/core/05_XTimer.js
0.6.x/js/core/06_XEventDispatcher.js
0.6.x/js/dom/12_XDomEvent.js

index b8ae2c7..72ee97e 100644 (file)
@@ -10,8 +10,9 @@ X.Callback = {
        MONOPOLY         : 16,  // move event を独占する\r
        SYS_CANCEL       : 32,\r
        \r
-       _LIVE_LIST : [],\r
-       _POOL_LIST : [],\r
+       _LIVE_LIST       : [],\r
+       _POOL_LIST       : [],\r
+       \r
        create : function( arg0, arg1, arg2 /* [ listener || ( context + function ) || function ][ args... ] */ ){\r
                var self  = X.Callback,\r
                        live  = self._LIVE_LIST,\r
@@ -23,7 +24,7 @@ X.Callback = {
                } else\r
                if( typeof arg0 === 'function' ){\r
                        callback = arg0;\r
-                       arg2     = arg1;\r
+                       if( !( arg2 = arg1 ) ) return callback; // function のみの場合 callback オブジェクトを作らない\r
                } else\r
                if( arg0 && typeof arg0.handleEvent === 'function' ){\r
                        listener = arg0;\r
@@ -63,36 +64,6 @@ X.Callback = {
                };\r
                return f;\r
        },\r
-       find : function( arg0, arg1 /* [ listener || ( context + function ) || function ] */ ){\r
-               var live     = X.Callback._LIVE_LIST,\r
-                       i        = live.length,\r
-                       listener, context, callback,\r
-                       f;\r
-               if( typeof arg1 === 'function' ){\r
-                       context  = arg0;\r
-                       callback = arg1;\r
-               } else\r
-               if( typeof arg0 === 'function' ){\r
-                       callback = arg0;\r
-               } else\r
-               if( arg0 && typeof arg0.handleEvent === 'function' ){\r
-                       listener = arg0;\r
-               };\r
-               \r
-               for( ; i; ){\r
-                       f = live[ --i ];\r
-                       if( listener && f.listener === listener ){\r
-                               return f;\r
-                       } else\r
-                       if( context && f.context === context && f.callback === callback ){\r
-                               return f;\r
-                       } else          \r
-                       if( f.callback === callback ){\r
-                               return f;\r
-                       };\r
-               };\r
-               return null;\r
-       },\r
        _contextCallback : function(){\r
                var f = this,\r
                        a = f.a,\r
@@ -104,8 +75,15 @@ X.Callback = {
                //s && s.push.apply( a, s );\r
                if( s && s.length ){\r
                        ary = [];\r
-                       a.length && ary.push.apply( ary, a );\r
-                       ary.push.apply( ary, s );\r
+                       a.length &&\r
+                               (\r
+                                       a.length === 1 ?\r
+                                               ( ary[ 0 ] = a[ 0 ] ) :\r
+                                               ary.push.apply( ary, a )\r
+                               );\r
+                       s.length === 1 ?\r
+                               ( ary[ ary.length ] = s[ 0 ] ) :\r
+                               ary.push.apply( ary, s );\r
                        a = ary;\r
                };\r
                r = a.length === 0 ? c.call( x ) : c.apply( x, a );\r
@@ -123,11 +101,22 @@ X.Callback = {
                //s && s.push.apply( a, s );\r
                if( s && s.length ){\r
                        ary = [];\r
-                       a.length && ary.push.apply( ary, a );\r
-                       ary.push.apply( ary, s );\r
+                       a.length &&\r
+                               (\r
+                                       a.length === 1 ?\r
+                                               ( ary[ 0 ] = a[ 0 ] ) :\r
+                                               ary.push.apply( ary, a )\r
+                               );\r
+                       s.length === 1 ?\r
+                               ( ary[ ary.length ] = s[ 0 ] ) :\r
+                               ary.push.apply( ary, s );\r
                        a = ary;\r
                };\r
-               r = a.length === 0 ? c() : c.apply( null, a );\r
+               r = a.length === 0 ?\r
+                               c() :\r
+                       a.length === 1 ?\r
+                               c( a[ 0 ] ) :\r
+                               c.apply( null, a );\r
                delete f.a;\r
                //delete f.t;\r
                return r;\r
index 85aa903..4ea26eb 100644 (file)
@@ -29,7 +29,7 @@ X.Timer = ( function(){
                        ret = f();\r
                        if( ret & X.Callback.UN_LISTEN || c === 1 ){\r
                                list.splice( i, 1 );\r
-                               f.kill();\r
+                               f.kill && f.kill();\r
                                queue.length = 0;\r
                                continue;\r
                        } else\r
@@ -85,7 +85,7 @@ X.Timer = ( function(){
            for( ; i; ){\r
                ( f = list[ --i ] )( time );\r
                delete f.uid;\r
-               f.kill();\r
+               f.kill && f.kill();\r
            };\r
            list.length = 0;\r
        };\r
@@ -112,11 +112,13 @@ X.Timer = ( function(){
                        var queue,\r
                                list = TICKET_LIST,\r
                                i    = list.length,\r
-                               l    = i;\r
+                               l    = i,\r
+                               f;\r
                        for( ; i; ){\r
                                if( ( queue = list[ --i ] )[ INDEX_UID ] === uid ){\r
                                        list.splice( i, 1 );\r
-                                       X.Callback._correct( queue[ INDEX_CALLBACK ] );\r
+                                       f = queue[ INDEX_CALLBACK ];\r
+                                       f.kill && f.kill();\r
                                        ( /* queue[ INDEX_COUNT ] <= next  || */ l === 1 ) && update();\r
                                        queue.length = 0;\r
                                        break;\r
@@ -148,7 +150,7 @@ X.Timer = ( function(){
                                if( f.uid === uid ){\r
                                        list.splice( i, 1 );\r
                                        delete f.uid;\r
-                                       X.Callback._correct( f );\r
+                                       f.kill && f.kill();\r
                                        l === 1 && cancelFrame ? cancelFrame( requestID ) : X.Timer.remove( requestID );\r
                                        break;                                  \r
                                };\r
index 1a9c78c..5755238 100644 (file)
@@ -5,14 +5,13 @@ X.EventDispatcher =
                        _listeners   : null,\r
                        _dispatching : 0, // dispatch 中の unlisten で使用\r
                        _unlistens   : null, // dispatch 中の unlisten で使用\r
+                       _needsIndex  : false,\r
                        listen : function( type, arg1, arg2, arg3 ){\r
                                var list = this._listeners,\r
-                                       i, f;\r
+                                       f;\r
+                               if( this.listening( type, arg1, arg2, arg3 ) ) return this;\r
                                if( !list ) list = this._listeners = {};\r
                                if( !( list = list[ type ] ) ) list = this._listeners[ type ] = [];\r
-                               for( i = list.length; i; ){\r
-                                       if( list[ --i ].same( arg1, arg2/* , arg3 */ ) ) return this;\r
-                               };\r
                                list[ list.length ] = f = X.Callback.create( arg1, arg2, arg3 );\r
                                f.once = X.EventDispatcher._once;\r
                                return this;\r
@@ -34,7 +33,7 @@ X.EventDispatcher =
                                        };\r
                                        return this;\r
                                };\r
-                               if( arg1 === undefined && arg2 === undefined ){\r
+                               if( arg1 === undefined ){\r
                                        // 同一タイプを全て削除\r
                                        if( !( list = list[ type ] ) ) return this;\r
                                        for( i = list.length; i; ){\r
@@ -42,31 +41,34 @@ X.EventDispatcher =
                                        };\r
                                        return this;\r
                                };\r
-                               if( !list || !( list = list[ type ] ) ) return this;\r
-                               for( i = list.length; i; ){\r
-                                       f = list[ --i ];\r
-                                       if( f.same( arg1, arg2, arg3 ) ){\r
-                                               if( this._dispatching ){\r
-                                                       this._unlistens[ this._unlistens.length ] = f;\r
-                                               } else {\r
-                                                       delete f.once;\r
-                                                       f.kill();\r
-                                                       list.splice( i, 1 );\r
-                                                       if( !list.length ){\r
-                                                               delete this._listeners[ type ];\r
-                                                               if( X.isEmptyObject( this._listeners ) ) delete this._listeners;\r
-                                                       };\r
-                                               };\r
-                                               return this;\r
+                               \r
+                               this._needsIndex = true;\r
+                               i = this.listening( type, arg1, arg2, arg3 );\r
+                               delete this._needsIndex;\r
+                               if( i === false ) return this;\r
+\r
+                               f = ( list = list[ type ] )[ i ];\r
+                               if( this._dispatching ){\r
+                                       this._unlistens[ this._unlistens.length ] = f;\r
+                               } else {\r
+                                       delete f.once;\r
+                                       f.kill && f.kill();\r
+                                       list.splice( i, 1 );\r
+                                       if( !list.length ){\r
+                                               delete this._listeners[ type ];\r
+                                               if( X.isEmptyObject( this._listeners ) ) delete this._listeners;\r
                                        };\r
                                };\r
                                return this;\r
                        },\r
                        listening : function( type, arg1, arg2, arg3 ){\r
-                               var list = this._listeners, i;\r
+                               var list = this._listeners, i, f;\r
+                               if( type === undefined ) return !!list;\r
                                if( !list || !( list = list[ type ] ) ) return false;\r
+                               if( arg1 === undefined ) return true;\r
                                for( i = list.length; i; ){\r
-                                       if( list[ --i ].same( arg1, arg2 /* , arg3 */ ) ) return true;\r
+                                       f = list[ --i ];\r
+                                       if( f === arg1 || ( f.same && f.same( arg1, arg2, arg3 ) ) ) return this._needsIndex ? i : true;\r
                                };\r
                                return false;\r
                        },\r
@@ -110,7 +112,7 @@ X.EventDispatcher =
                                        for( i = this._unlistens.length ; i; ){\r
                                                this.unlisten( e.type, this._unlistens[ --i ] );\r
                                        };\r
-                                       delete this._dispatching\r
+                                       delete this._dispatching;\r
                                        delete this._unlistens;                                 \r
                                };\r
                                \r
index b364cf2..89aa2cf 100644 (file)
@@ -143,16 +143,13 @@ X.Dom.Event._chashe2 = [];
 X.Dom.Node.prototype.listen = function( type, arg2, arg3, arg4 /* [ listener || ( context + function ) || function ][ arguments ] */ ){
        var elm;
        
-       if( this._xnodeType === 0 || this._xnodeType === 3 ) return this;
+       if( this._xnodeType === 0 || this._xnodeType === 3 || !arg2 ) return this;
        
        ( !this._listeners || !this._listeners[ type ] ) && this._addEvent( type );
        
-       if( typeof arg2 === 'function' ){
-               X.EventDispatcher.prototype.listen.call( this, type, this, arg2, arg3 );
-       } else {
+       return typeof arg2 === 'function' ?
+               X.EventDispatcher.prototype.listen.call( this, type, this, arg2, arg3 ) :
                X.EventDispatcher.prototype.listen.apply( this, arguments );
-       };
-       return this;
 };
 
 X.Dom.Node.prototype._addEvent =
@@ -173,14 +170,13 @@ X.Dom.Node.prototype._addEvent =
                });
 
 
-X.Dom.Node.prototype.unlisten = function( /* type, arg2, arg3, arg4 */ ){
-       var list    = this._listeners,
-               type    = arguments[ 0 ];
-               l       = list && type && list[ type ] && list[ type ].length;
+X.Dom.Node.prototype.unlisten = function( type /* , arg2, arg3, arg4 */ ){
+       var list = this._listeners,
+               l    = list && type && list[ type ] && list[ type ].length;
        
        X.EventDispatcher.prototype.unlisten.apply( this, arguments );
        
-       if( 1 < arguments.length && !this._dispatching && l && !list[ type ] ){
+       if( type !== undefined && !this._dispatching && l && !list[ type ] ){
                this._removeEvent( type );
        };
        
@@ -230,7 +226,7 @@ X.Dom.Node.prototype.handleEvent =
                        };
                }) :
                (function(){
-                       var ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( event, this.elm ) );
+                       var ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( event, this._rawNode ) );
 
                        if( ret & X.Callback.STOP_PROPAGATION ){
                                event.cancelBubble = true;
@@ -402,4 +398,6 @@ X.Dom.listenOnce( X.Dom.Event.DOM_INIT, function(){
        
        Node._systemNode = r.create( 'div' ).className( 'hidden-sysyem-node' );
        r._xnodes.splice( r._xnodes.indexOf( Node._systemNode ), 1 ); // hide from api user
-} );
\ No newline at end of file
+} );
+
+