You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@flex.apache.org by Gary Yang <fl...@gmail.com> on 2014/05/23 20:16:08 UTC

Confusing code in org.apache.flex.collections.ArrayList

Hi,

I am reading

org.apache.flex.collections.ArrayList.as

I am confused about this private property:



    /**
     *  @private
     *  Indicates if events should be dispatched.
     *  calls to enableEvents() and disableEvents() effect the value when
== 0
     *  events should be dispatched.
     */
    private var _dispatchEvents:int = 0;

It is changed in following 2 private methods only:


    /**
     *  Disables event dispatch for this list.
     *  To re-enable events call enableEvents(), enableEvents() must be
called
     *  a matching number of times as disableEvents().
     *
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    private function disableEvents():void
    {
        _dispatchEvents--;
    }



    /**
     *  Enables event dispatch for this list.
     *
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    private function enableEvents():void
    {
        _dispatchEvents++;
        if (_dispatchEvents > 0)
            _dispatchEvents = 0;
    }



And these 2 methods only get called in constructor:


    public function ArrayList(source:Array = null)
    {
        super();

        disableEvents();
        this.source = source;
        enableEvents();
    }



I think this private property can be simplified as:

private var _constructed:Boolean = false;


and:
    public function ArrayList(source:Array = null)
    {
        super();

        this.source = source;
        _constructed = true;
    }

Could anyone help me see the difference plz??

Best,

-Gary

RE: Confusing code in org.apache.flex.collections.ArrayList

Posted by "Michael A. Labriola" <la...@digitalprimates.net>.
>Its intended so that if multiple people call disable() or enable() that it will only dispatch events if the net result gets back to 0.
>So, if two listeners disable() and one says enable() it shouldn't dispatch again until the last one says enable()

On a side note, I also see these methods called in addAllAt() but I do agree that the implementation is kind of silly.

Mike


RE: Confusing code in org.apache.flex.collections.ArrayList

Posted by "Michael A. Labriola" <la...@digitalprimates.net>.
Gary,

Its intended so that if multiple people call disable() or enable() that it will only dispatch events if the net result gets back to 0.

So, if two listeners disable() and one says enable() it shouldn't dispatch again until the last one says enable()

Mike


-----Original Message-----
From: Gary Yang [mailto:flashflexpro@gmail.com] 
Sent: Friday, May 23, 2014 1:16 PM
To: dev@flex.apache.org
Subject: Confusing code in org.apache.flex.collections.ArrayList

Hi,

I am reading

org.apache.flex.collections.ArrayList.as

I am confused about this private property:



    /**
     *  @private
     *  Indicates if events should be dispatched.
     *  calls to enableEvents() and disableEvents() effect the value when == 0
     *  events should be dispatched.
     */
    private var _dispatchEvents:int = 0;

It is changed in following 2 private methods only:


    /**
     *  Disables event dispatch for this list.
     *  To re-enable events call enableEvents(), enableEvents() must be called
     *  a matching number of times as disableEvents().
     *
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    private function disableEvents():void
    {
        _dispatchEvents--;
    }



    /**
     *  Enables event dispatch for this list.
     *
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    private function enableEvents():void
    {
        _dispatchEvents++;
        if (_dispatchEvents > 0)
            _dispatchEvents = 0;
    }



And these 2 methods only get called in constructor:


    public function ArrayList(source:Array = null)
    {
        super();

        disableEvents();
        this.source = source;
        enableEvents();
    }



I think this private property can be simplified as:

private var _constructed:Boolean = false;


and:
    public function ArrayList(source:Array = null)
    {
        super();

        this.source = source;
        _constructed = true;
    }

Could anyone help me see the difference plz??

Best,

-Gary