4 #import "keychain/ckks/CKKSListenerCollection.h"
5 #import "keychain/ot/ObjCImprovements.h"
7 @interface CKKSListenerCollection ()
8 @property NSString* name;
9 @property NSMapTable<dispatch_queue_t, id>* listeners;
12 @implementation CKKSListenerCollection
14 - (instancetype)initWithName:(NSString*)name
16 if((self = [super init])) {
18 // Backwards from how we'd like, but it's the best way to have weak pointers to ListenerTypes.
19 _listeners = [NSMapTable strongToWeakObjectsMapTable];
24 - (NSString*)description
26 @synchronized(self.listeners) {
27 return [NSString stringWithFormat:@"<CKKSListenerCollection(%@): %@>", self.name, [[self.listeners objectEnumerator] allObjects]];
31 - (void)registerListener:(id)listener
33 @synchronized(self.listeners) {
34 bool alreadyRegisteredListener = false;
35 NSEnumerator *enumerator = [self.listeners objectEnumerator];
38 while ((value = [enumerator nextObject])) {
39 // actually use pointer comparison
40 alreadyRegisteredListener |= (value == listener);
43 if(listener && !alreadyRegisteredListener) {
44 NSString* queueName = [NSString stringWithFormat: @"%@-%@", self.name, listener];
46 dispatch_queue_t objQueue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
47 [self.listeners setObject: listener forKey: objQueue];
52 - (void)iterateListeners:(void (^)(id))block
54 @synchronized(self.listeners) {
55 NSEnumerator *enumerator = [self.listeners keyEnumerator];
58 // Queue up the changes for each listener.
59 while ((dq = [enumerator nextObject])) {
60 id listener = [self.listeners objectForKey: dq];