Index: src/XMPPMulticastDelegate.h ================================================================== --- src/XMPPMulticastDelegate.h +++ src/XMPPMulticastDelegate.h @@ -28,10 +28,11 @@ * \brief A class to provide multiple delegates in a single class */ @interface XMPPMulticastDelegate: OFObject { OFDataArray *_delegates; + size_t handlerIndex; } /** * \brief Adds a delegate which should receive the broadcasts. * Index: src/XMPPMulticastDelegate.m ================================================================== --- src/XMPPMulticastDelegate.m +++ src/XMPPMulticastDelegate.m @@ -59,53 +59,66 @@ { id *items = [_delegates items]; size_t i, count = [_delegates count]; for (i = 0; i < count; i++) { - if (items[i] == delegate) { - [_delegates removeItemAtIndex: i]; - return; - } + if (items[i] != delegate) + continue; + + [_delegates removeItemAtIndex: i]; + if (i <= handlerIndex) + handlerIndex--; + return; } } - (BOOL)broadcastSelector: (SEL)selector withObject: (id)object { + size_t count = [_delegates count]; id *items = [_delegates items]; - size_t i, count = [_delegates count]; BOOL handled = NO; - for (i = 0; i < count; i++) { - if (![items[i] respondsToSelector: selector]) + for (handlerIndex = 0; handlerIndex < count; handlerIndex++) { + id responder = items[handlerIndex]; + if (![responder respondsToSelector: selector]) continue; BOOL (*imp)(id, SEL, id) = (BOOL(*)(id, SEL, id)) - [items[i] methodForSelector: selector]; + [responder methodForSelector: selector]; - handled |= imp(items[i], selector, object); + handled |= imp(responder, selector, object); + // Update count and items, since the handler might have + // changed them. + count = [_delegates count]; + items = [_delegates items]; } return handled; } - (BOOL)broadcastSelector: (SEL)selector withObject: (id)object1 withObject: (id)object2 { + size_t count = [_delegates count]; id *items = [_delegates items]; - size_t i, count = [_delegates count]; BOOL handled = NO; - for (i = 0; i < count; i++) { - if (![items[i] respondsToSelector: selector]) + for (handlerIndex = 0; handlerIndex < count; handlerIndex++) { + id responder = items[handlerIndex]; + if (![responder respondsToSelector: selector]) continue; BOOL (*imp)(id, SEL, id, id) = (BOOL(*)(id, SEL, id, id)) - [items[i] methodForSelector: selector]; + [responder methodForSelector: selector]; - handled |= imp(items[i], selector, object1, object2); + handled |= imp(responder, selector, object1, object2); + // Update count and items, since the handler might have + // changed them. + count = [_delegates count]; + items = [_delegates items]; } return handled; } @end