Event: A once listener is only removed after it returns
Event.add(listener, true) is documented as receiving "an event dispatch only once". The dispatch loop calls the listener first and only removes it once the call has returned (EventMacro.hx#L101-L111), so while it runs it is still a listener. That breaks it in two ways.
A once listener that dispatches the same event gets the nested dispatch too:
var event = new Event<Int->Void>();
event.add(function(value) {
trace(value);
if (value == 1) event.dispatch(2);
}, true);
event.dispatch(1); // traces 1, then 2
If it dispatched unconditionally, it would recurse until the stack ran out.
A once listener that throws is never removed, since the removal is skipped along with the rest of the loop. It stays registered and is called again on the next dispatch, and on every dispatch after that until one returns normally.
Expected
A once listener is called at most once: it stops being a listener before it is called, so neither a nested dispatch nor an exception can reach it a second time.
Notes:
- Removing it before the call, rather than after, fixes both cases without wrapping the listener call, so the exception call sites kept by 11afac03 stay as they are.
- 8.3.2 removes once listeners after the call too, so this isn't new in 8.4.0. The only visible change is that
event.has(listener)inside a once listener becomes false, so it can land in a patch release. - Covered by
app.EventTest.testOnceListenerIsNotCalledAgainByANestedDispatchandapp.EventTest.testOnceListenerThatThrowsIsStillRemovedin tests/unit.
For #2145, I can help make two explicit traces: a once listener dispatches recursively, and a once listener throws before a second dispatch. A useful extension would run the same traces on the Haxe targets you support and compare callback counts/order. I maintain interaction-check, but a native Haxe test is the first step; the external runner only earns its place if target comparison is useful.