The callback paradigm could still be a convenient option. There definitely
are times when a single listener is all you need.

- Josh

On Tue, Jul 5, 2016 at 7:08 AM, Harbs <harbs.li...@gmail.com> wrote:

> OK. I’ll leave it an EventDispatcher. But, should we try to emulate the
> callback paradigm in addition?
>
> On Jul 5, 2016, at 3:07 PM, Josh Tynjala <joshtynj...@gmail.com> wrote:
>
> > There have been multiple cases where a developer decided to use callbacks
> > instead of events in their API because they felt that no one would ever
> > need multiple listeners, and I ended up actually needing them. The
> > workarounds required can be ugly. Having been there, I try to avoid
> making
> > assumptions like that in my own code.
> >
> > Just my personal experience.
> >
> > - Josh
> > On Jul 5, 2016 3:25 AM, "Harbs" <harbs.li...@gmail.com> wrote:
> >
> >> I’d like to start a discussion on what I did here.
> >>
> >> There is a pretty popular pattern in Javascript which allows for
> chaining
> >> of callbacks. Instead of lots of addEventListeners and such, you would
> do
> >> myClass.doSomething().success(handleSuccess).error(handleError). It’s
> >> currently giving the class object to the handler, but it could take an
> >> event object instead. I’m not sure what makes more sense.
> >>
> >> You can also specify things like myClass.onError = handleError.
> >>
> >> This approach works very well when there’s a single object which needs
> the
> >> callbacks. For those types of situations, EventDispatcher is not really
> >> necessary at all. I’m thinking of removing the dependency on
> >> EventDispatcher completely for URLLoader and related classes. Requiring
> >> clients to call add and removeEventListeners is cumbersome and
> error-prone.
> >>
> >> Thoughts?
> >>
> >> Harbs
> >>
> >> On Jul 5, 2016, at 1:09 PM, ha...@apache.org wrote:
> >>
> >>> Repository: flex-asjs
> >>> Updated Branches:
> >>> refs/heads/develop cc22300be -> 298d2041f
> >>>
> >>>
> >>> Added callbacks
> >>>
> >>>
> >>> Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
> >>> Commit:
> http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/298d2041
> >>> Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/298d2041
> >>> Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/298d2041
> >>>
> >>> Branch: refs/heads/develop
> >>> Commit: 298d2041ff2c02c203764ed22fb11e131e4b092e
> >>> Parents: cc22300
> >>> Author: Harbs <ha...@in-tools.com>
> >>> Authored: Tue Jul 5 13:09:05 2016 +0300
> >>> Committer: Harbs <ha...@in-tools.com>
> >>> Committed: Tue Jul 5 13:09:05 2016 +0300
> >>>
> >>> ----------------------------------------------------------------------
> >>> .../flex/org/apache/flex/net/URLBinaryLoader.as | 37 +++++++--
> >>> .../main/flex/org/apache/flex/net/URLLoader.as  | 78 ++++++++++++++++++
> >>> .../main/flex/org/apache/flex/net/URLStream.as  | 86
> +++++++++++++++++++-
> >>> 3 files changed, 191 insertions(+), 10 deletions(-)
> >>> ----------------------------------------------------------------------
> >>>
> >>>
> >>>
> >>
> http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/298d2041/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
> >>> ----------------------------------------------------------------------
> >>> diff --git
> >>
> a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
> >>
> b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
> >>> index 2dfc490..ff9121b 100644
> >>> ---
> >>
> a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
> >>> +++
> >>
> b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
> >>> @@ -20,7 +20,6 @@ package org.apache.flex.net
> >>> {
> >>>
> >>>    import org.apache.flex.events.Event;
> >>> -    import org.apache.flex.events.EventDispatcher;
> >>>    import org.apache.flex.events.ProgressEvent;
> >>>    import org.apache.flex.utils.BinaryData;
> >>>
> >>> @@ -73,43 +72,63 @@ package org.apache.flex.net
> >>>        {
> >>>            super();
> >>>            stream = new URLStream();
> >>> -            stream.addEventListener(HTTPConstants.COMPLETE,
> onComplete);
> >>> +            stream.addEventListener(HTTPConstants.COMPLETE,
> >> completeHandler);
> >>>        }
> >>>
> >>> +             /**
> >>> +              *  Makes the URL request.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>>        public function load(request:URLRequest):void
> >>>        {
> >>>            stream.load(request);
> >>>        }
> >>>
> >>> +             /**
> >>> +              *  Cancels the URL request
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>>        public function close():void
> >>>        {
> >>>            stream.close();
> >>> +                     //TODO do we need a callback for camceling?
> >>>        }
> >>>
> >>> -        private function redirectEvent(event:Event):void
> >>> -        {
> >>> -            dispatchEvent(event);
> >>> -        }
> >>> -
> >>> -        private function onComplete(event:Event):void
> >>> +        private function completeHandler(event:Event):void
> >>>        {
> >>>            data = stream.response;
> >>>            if (data)
> >>>            {
> >>>                dispatchEvent(event);
> >>> +                             if(onComplete)
> >>> +                                     onComplete(this);
> >>> +
> >>>            }
> >>>            else
> >>>            {
> >>>                // TODO dipatch error event?
> >>>                dispatchEvent(new Event(HTTPConstants.IO_ERROR));
> >>> +                             if(onError)
> >>> +                                     onError(this);
> >>>            }
> >>> +                     cleanupCallbacks();
> >>>        }
> >>>
> >>> -        private function onProgress(event:ProgressEvent):void
> >>> +        private function progressHandler(event:ProgressEvent):void
> >>>        {
> >>>            this.bytesLoaded = event.current
> >>>            this.bytesTotal = event.total;
> >>>            dispatchEvent(event);
> >>> +                     if(onProgress)
> >>> +                             onProgress(this);
> >>>        }
> >>>    }
> >>> }
> >>>
> >>>
> >>
> http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/298d2041/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLLoader.as
> >>> ----------------------------------------------------------------------
> >>> diff --git
> >>
> a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLLoader.as
> >>
> b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLLoader.as
> >>> index 809e120..102f525 100644
> >>> ---
> >>
> a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLLoader.as
> >>> +++
> >>
> b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLLoader.as
> >>> @@ -86,5 +86,83 @@ package org.apache.flex.net
> >>>              {
> >>>                      throw new Error("URLLoader should not be
> >> instantiated. Use a derived class instead.")
> >>>              }
> >>> +
> >>> +             protected function cleanupCallbacks():void
> >>> +             {
> >>> +                     onComplete = null;
> >>> +                     onError = null;
> >>> +                     onProgress = null;
> >>> +             }
> >>> +             /**
> >>> +              *  Callback for complete event.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public var onComplete:Function;
> >>> +
> >>> +             /**
> >>> +              *  Callback for error event.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public var onError:Function;
> >>> +
> >>> +             /**
> >>> +              *  Callback for progress event.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public var onProgress:Function;
> >>> +
> >>> +             /**
> >>> +              *  Convenience function for complete event to allow
> >> chaining.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public function complete(callback:Function):URLLoader
> >>> +             {
> >>> +                     onComplete = callback;
> >>> +                     return this;
> >>> +             }
> >>> +
> >>> +             /**
> >>> +              *  Convenience function for error event to allow
> chaining.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public function error(callback:Function):URLLoader
> >>> +             {
> >>> +                     onError = callback;
> >>> +                     return this;
> >>> +             }
> >>> +
> >>> +             /**
> >>> +              *  Convenience function for progress event to allow
> >> chaining.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public function progress(callback:Function):URLLoader
> >>> +             {
> >>> +                     onProgress = callback;
> >>> +                     return this;
> >>> +             }
> >>>      }
> >>> }
> >>> \ No newline at end of file
> >>>
> >>>
> >>
> http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/298d2041/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as
> >>> ----------------------------------------------------------------------
> >>> diff --git
> >>
> a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as
> >>
> b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as
> >>> index 7b5ff89..e2b1654 100644
> >>> ---
> >>
> a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as
> >>> +++
> >>
> b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as
> >>> @@ -107,6 +107,9 @@ package org.apache.flex.net
> >>>        protected function flash_complete(event:flash.events.Event):void
> >>>        {
> >>>            dispatchEvent(new
> >> org.apache.flex.events.Event(HTTPConstants.COMPLETE));
> >>> +                     if(onComplete)
> >>> +                             onComplete();
> >>> +                     cleanupCallbacks();
> >>>        }
> >>>        COMPILE::SWF
> >>>        protected function
> >> flash_progress(event:flash.events.ProgressEvent):void
> >>> @@ -129,6 +132,9 @@ package org.apache.flex.net
> >>>            if (xhr.readyState == 4 && xhr.status == 200)
> >>>            {
> >>>                dispatchEvent(new
> >> org.apache.flex.events.Event(HTTPConstants.COMPLETE));
> >>> +                             if(onComplete)
> >>> +                                     onComplete();
> >>> +                             cleanupHandlers();
> >>>            }else if (xhr.readyState==4&&xhr.status==404){
> >>>                //                    dispatchEvent(new
> >> IOErrorEvent(IOErrorEvent.IO_ERROR));
> >>>            }
> >>> @@ -146,9 +152,87 @@ package org.apache.flex.net
> >>>            }
> >>>
> >>>            //TODO send an event that it's been aborted
> >>> +
> >>> +                     cleanupCallbacks();
> >>> +
> >>>        }
> >>> +             private function cleanupCallbacks():void
> >>> +             {
> >>> +                     onComplete = null;
> >>> +                     onError = null;
> >>> +                     onProgress = null;
> >>> +             }
> >>> +             /**
> >>> +              *  Callback for complete event.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>>        public var onComplete:Function;
> >>> +
> >>> +             /**
> >>> +              *  Callback for error event.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>>        public var onError:Function;
> >>> -    }
> >>> +
> >>> +             /**
> >>> +              *  Callback for progress event.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public var onProgress:Function;
> >>> +
> >>> +             /**
> >>> +              *  Convenience function for complete event to allow
> >> chaining.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public function
> >> complete(callback:Function):org.apache.flex.net.URLStream
> >>> +             {
> >>> +                     onComplete = callback;
> >>> +                     return this;
> >>> +             }
> >>> +
> >>> +             /**
> >>> +              *  Convenience function for error event to allow
> chaining.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public function
> >> error(callback:Function):org.apache.flex.net.URLStream
> >>> +             {
> >>> +                     onError = callback;
> >>> +                     return this;
> >>> +             }
> >>> +
> >>> +             /**
> >>> +              *  Convenience function for progress event to allow
> >> chaining.
> >>> +              *
> >>> +              *  @langversion 3.0
> >>> +              *  @playerversion Flash 10.2
> >>> +              *  @playerversion AIR 2.6
> >>> +              *  @productversion FlexJS 0.7.0
> >>> +              */
> >>> +             public function
> >> progress(callback:Function):org.apache.flex.net.URLStream
> >>> +             {
> >>> +                     onProgress = callback;
> >>> +                     return this;
> >>> +             }
> >>> +}
> >>> }
> >>>
> >>>
> >>
> >>
>
>

Reply via email to