FEventEmitter.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FEventEmitter.h"
  17. #import "FUtilities.h"
  18. #import "FRepoManager.h"
  19. #import "FIRDatabaseQuery_Private.h"
  20. @interface FEventListener : NSObject
  21. @property (nonatomic, copy) fbt_void_id userCallback;
  22. @property (nonatomic) FIRDatabaseHandle handle;
  23. @end
  24. @implementation FEventListener
  25. @synthesize userCallback;
  26. @synthesize handle;
  27. @end
  28. @interface FEventEmitter ()
  29. @property (nonatomic, strong) NSArray *allowedEvents;
  30. @property (nonatomic, strong) NSMutableDictionary *listeners;
  31. @property (nonatomic, strong) dispatch_queue_t queue;
  32. @end
  33. @implementation FEventEmitter
  34. @synthesize allowedEvents;
  35. @synthesize listeners;
  36. - (id) initWithAllowedEvents:(NSArray *)theAllowedEvents queue:(dispatch_queue_t)queue {
  37. if (theAllowedEvents == nil || [theAllowedEvents count] == 0) {
  38. @throw [NSException exceptionWithName:@"AllowedEventsValidation" reason:@"FEventEmitters must be initialized with at least one valid event." userInfo:nil];
  39. }
  40. self = [super init];
  41. if (self) {
  42. self.allowedEvents = [theAllowedEvents copy];
  43. self.listeners = [[NSMutableDictionary alloc] init];
  44. self.queue = queue;
  45. }
  46. return self;
  47. }
  48. - (id) getInitialEventForType:(NSString *)eventType {
  49. @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"You must override getInitialEvent: when subclassing FEventEmitter" userInfo:nil];
  50. }
  51. - (void) triggerEventType:(NSString *)eventType data:(id)data {
  52. [self validateEventType:eventType];
  53. NSMutableDictionary *eventTypeListeners = [self.listeners objectForKey:eventType];
  54. for (FEventListener *listener in eventTypeListeners) {
  55. [self triggerListener:listener withData:data];
  56. }
  57. }
  58. - (void) triggerListener:(FEventListener *)listener withData:(id)data {
  59. // TODO, should probably get this from FRepo or something although it ends up being the same. (Except maybe for testing)
  60. if (listener.userCallback) {
  61. dispatch_async(self.queue, ^{
  62. listener.userCallback(data);
  63. });
  64. }
  65. }
  66. - (FIRDatabaseHandle)observeEventType:(NSString *)eventType withBlock:(fbt_void_id)block {
  67. [self validateEventType:eventType];
  68. // Create listener
  69. FEventListener *listener = [[FEventListener alloc] init];
  70. listener.handle = [[FUtilities LUIDGenerator] integerValue];
  71. listener.userCallback = block; // copies block automatically
  72. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  73. [self addEventListener:listener forEventType:eventType];
  74. });
  75. return listener.handle;
  76. }
  77. - (void) addEventListener:(FEventListener *)listener forEventType:(NSString *)eventType {
  78. // Get or initializer listeners map [FIRDatabaseHandle -> callback block] for eventType
  79. NSMutableArray *eventTypeListeners = [self.listeners objectForKey:eventType];
  80. if (eventTypeListeners == nil) {
  81. eventTypeListeners = [[NSMutableArray alloc] init];
  82. [self.listeners setObject:eventTypeListeners forKey:eventType];
  83. }
  84. // Add listener and fire the current event for this listener
  85. [eventTypeListeners addObject:listener];
  86. id initialData = [self getInitialEventForType:eventType];
  87. [self triggerListener:listener withData:initialData];
  88. }
  89. - (void) removeObserverForEventType:(NSString *)eventType withHandle:(FIRDatabaseHandle)handle {
  90. [self validateEventType:eventType];
  91. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  92. [self removeEventListenerWithHandle:handle forEventType:eventType];
  93. });
  94. }
  95. - (void)removeEventListenerWithHandle:(FIRDatabaseHandle)handle forEventType:(NSString *)eventType {
  96. NSMutableArray *eventTypeListeners = [self.listeners objectForKey:eventType];
  97. for (FEventListener *listener in [eventTypeListeners copy]) {
  98. if (handle == NSNotFound || handle == listener.handle) {
  99. [eventTypeListeners removeObject:listener];
  100. }
  101. }
  102. }
  103. - (void) validateEventType:(NSString *)eventType {
  104. if ([self.allowedEvents indexOfObject:eventType] == NSNotFound) {
  105. @throw [NSException exceptionWithName:@"InvalidEventType"
  106. reason:[NSString stringWithFormat:@"%@ is not a valid event type. %@ is the list of valid events.",
  107. eventType, self.allowedEvents]
  108. userInfo:nil];
  109. }
  110. }
  111. @end