Skip to content

Instantly share code, notes, and snippets.

@creynders
Created November 21, 2012 12:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creynders/4124722 to your computer and use it in GitHub Desktop.
Save creynders/4124722 to your computer and use it in GitHub Desktop.
Maintains parameterized event handlers
/*
Copyright (c) 2012 Camille Reynders, http://www.creynders.be
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package be.creynders.lib.events
{
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.utils.Dictionary;
/**
* @author creynder
*/
public class ParameterizedHandlerContainer{
public function ParameterizedHandlerContainer()
{
_handlers = new Dictionary( true );
}
private var _handlers : Dictionary;
public function registerHandler( dispatcher : IEventDispatcher, eventType : String, callback : Function, ...args ) : Function{
var f : Function = function( event : Event ) : void{
args.unshift( event );
callback.apply( null, args );
}
_handlers[ callback ] = new HandlerConfig( f, dispatcher );
dispatcher.addEventListener( eventType, f );
return f;
}
public function destroyHandler( callback : Function, event : Event ) : void{
var config : HandlerConfig = _handlers[ callback ];
if( config ){
config.dispatcher.removeEventListener( event.type, config.handler );
delete _handlers[ callback ];
}
}
}
}
import flash.events.IEventDispatcher;
internal class HandlerConfig{
public var handler : Function;
public var dispatcher : IEventDispatcher;
public function HandlerConfig( handler : Function, dispatcher : IEventDispatcher ) : void{
this.handler = handler;
this.dispatcher = dispatcher;
}
}
public class Usage extends Sprite{
private var _handlers : ParameterizedHandlerContainer;
public function ParameterizedEvents()
{
_handlers = new ParameterizedHandlerContainer();
_handlers.registerHandler( this, Event.ADDED_TO_STAGE, someMethod, 3000 );
}
private function someMethod( event : Event, amount : uint ):void
{
_handlers.destroyHandler( someMethod, event );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment