feat(core): Add logging for when a manager ends processing early

This commit is contained in:
PapaTutuWawa 2023-09-27 18:57:13 +02:00
parent 6dbbf08be4
commit c7d58c3d3f

View File

@ -49,6 +49,19 @@ enum XmppConnectionState {
error error
} }
/// (The actual stanza handler, Name of the owning manager).
typedef _StanzaHandlerWrapper = (StanzaHandler, String);
/// Wrapper around [stanzaHandlerSortComparator] for [_StanzaHandlerWrapper].
int _stanzaHandlerWrapperSortComparator(
_StanzaHandlerWrapper a,
_StanzaHandlerWrapper b,
) {
final (ha, _) = a;
final (hb, _) = b;
return stanzaHandlerSortComparator(ha, hb);
}
/// This class is a connection to the server. /// This class is a connection to the server.
class XmppConnection { class XmppConnection {
XmppConnection( XmppConnection(
@ -112,13 +125,13 @@ class XmppConnection {
final StanzaAwaiter _stanzaAwaiter = StanzaAwaiter(); final StanzaAwaiter _stanzaAwaiter = StanzaAwaiter();
/// Sorted list of handlers that we call or incoming and outgoing stanzas /// Sorted list of handlers that we call or incoming and outgoing stanzas
final List<StanzaHandler> _incomingStanzaHandlers = final List<_StanzaHandlerWrapper> _incomingStanzaHandlers =
List.empty(growable: true); List.empty(growable: true);
final List<StanzaHandler> _incomingPreStanzaHandlers = final List<_StanzaHandlerWrapper> _incomingPreStanzaHandlers =
List.empty(growable: true); List.empty(growable: true);
final List<StanzaHandler> _outgoingPreStanzaHandlers = final List<_StanzaHandlerWrapper> _outgoingPreStanzaHandlers =
List.empty(growable: true); List.empty(growable: true);
final List<StanzaHandler> _outgoingPostStanzaHandlers = final List<_StanzaHandlerWrapper> _outgoingPostStanzaHandlers =
List.empty(growable: true); List.empty(growable: true);
final StreamController<XmppEvent> _eventStreamController = final StreamController<XmppEvent> _eventStreamController =
StreamController.broadcast(); StreamController.broadcast();
@ -198,18 +211,25 @@ class XmppConnection {
_xmppManagers[manager.id] = manager; _xmppManagers[manager.id] = manager;
_incomingStanzaHandlers.addAll(manager.getIncomingStanzaHandlers()); _incomingStanzaHandlers.addAll(
_incomingPreStanzaHandlers.addAll(manager.getIncomingPreStanzaHandlers()); manager.getIncomingStanzaHandlers().map((h) => (h, manager.name)),
_outgoingPreStanzaHandlers.addAll(manager.getOutgoingPreStanzaHandlers()); );
_outgoingPostStanzaHandlers _incomingPreStanzaHandlers.addAll(
.addAll(manager.getOutgoingPostStanzaHandlers()); manager.getIncomingPreStanzaHandlers().map((h) => (h, manager.name)),
);
_outgoingPreStanzaHandlers.addAll(
manager.getOutgoingPreStanzaHandlers().map((h) => (h, manager.name)),
);
_outgoingPostStanzaHandlers.addAll(
manager.getOutgoingPostStanzaHandlers().map((h) => (h, manager.name)),
);
} }
// Sort them // Sort them
_incomingStanzaHandlers.sort(stanzaHandlerSortComparator); _incomingStanzaHandlers.sort(_stanzaHandlerWrapperSortComparator);
_incomingPreStanzaHandlers.sort(stanzaHandlerSortComparator); _incomingPreStanzaHandlers.sort(_stanzaHandlerWrapperSortComparator);
_outgoingPreStanzaHandlers.sort(stanzaHandlerSortComparator); _outgoingPreStanzaHandlers.sort(_stanzaHandlerWrapperSortComparator);
_outgoingPostStanzaHandlers.sort(stanzaHandlerSortComparator); _outgoingPostStanzaHandlers.sort(_stanzaHandlerWrapperSortComparator);
// Run the post register callbacks // Run the post register callbacks
for (final manager in _xmppManagers.values) { for (final manager in _xmppManagers.values) {
@ -650,15 +670,21 @@ class XmppConnection {
/// call its callback and end the processing if the callback returned true; continue /// call its callback and end the processing if the callback returned true; continue
/// if it returned false. /// if it returned false.
Future<StanzaHandlerData> _runStanzaHandlers( Future<StanzaHandlerData> _runStanzaHandlers(
List<StanzaHandler> handlers, List<_StanzaHandlerWrapper> handlers,
Stanza stanza, { Stanza stanza, {
StanzaHandlerData? initial, StanzaHandlerData? initial,
}) async { }) async {
var state = initial ?? StanzaHandlerData(false, false, stanza, TypedMap()); var state = initial ?? StanzaHandlerData(false, false, stanza, TypedMap());
for (final handler in handlers) { for (final handlerRaw in handlers) {
final (handler, managerName) = handlerRaw;
if (handler.matches(state.stanza)) { if (handler.matches(state.stanza)) {
state = await handler.callback(state.stanza, state); state = await handler.callback(state.stanza, state);
if (state.done || state.cancel) return state; if (state.done || state.cancel) {
_log.finest(
'Processing ended early for ${stanza.tag} by $managerName',
);
return state;
}
} }
} }