Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iteration bug in FlxSignal #1420

Merged
merged 2 commits into from
Mar 27, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions flixel/util/FlxSignal.hx
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,15 @@ private class FlxBaseSignal<T> implements IFlxSignal<T>
*/
public var dispatch:T;

private var _handlers:Array<FlxSignalHandler<T>>;
private var handlers:Array<FlxSignalHandler<T>>;
private var pendingRemove:Array<FlxSignalHandler<T>>;
private var processingListeners:Bool = false;


public function new()
{
_handlers = [];
handlers = [];
pendingRemove = [];
}

public function add(listener:T)
Expand All @@ -123,13 +127,20 @@ private class FlxBaseSignal<T> implements IFlxSignal<T>
if (listener != null)
{
var handler = getHandler(listener);

if (handler != null)
{
_handlers.remove(handler);
handler.destroy();
handler = null;
if (processingListeners)
pendingRemove.push(handler);
else
{
handlers.remove(handler);
handler.destroy();
handler = null;
}
}
}

}

public function has(listener:T):Bool
Expand All @@ -141,13 +152,13 @@ private class FlxBaseSignal<T> implements IFlxSignal<T>

public inline function removeAll():Void
{
FlxDestroyUtil.destroyArray(_handlers);
FlxDestroyUtil.destroyArray(handlers);
}

public function destroy():Void
{
removeAll();
_handlers = null;
handlers = null;
}

private function registerListener(listener:T, dispatchOnce:Bool):FlxSignalHandler<T>
Expand All @@ -157,7 +168,7 @@ private class FlxBaseSignal<T> implements IFlxSignal<T>
if (handler == null)
{
handler = new FlxSignalHandler<T>(listener, dispatchOnce);
_handlers.push(handler);
handlers.push(handler);
return handler;
}
else
Expand All @@ -173,7 +184,7 @@ private class FlxBaseSignal<T> implements IFlxSignal<T>

private function getHandler(listener:T):FlxSignalHandler<T>
{
for (handler in _handlers)
for (handler in handlers)
{
if (
#if neko // simply comparing the functions doesn't do the trick on neko
Expand Down Expand Up @@ -277,13 +288,21 @@ private class Macro
{
return macro
{
for (handler in _handlers)
processingListeners = true;
for (handler in handlers)
{
handler.listener($a{exprs});

if (handler.dispatchOnce)
remove(handler.listener);
}

processingListeners = false;

for (handler in pendingRemove)
{
remove(handler.listener);
}
}
}
}