FEventEmitter.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "FirebaseDatabase/Sources/Utilities/FEventEmitter.h"
  17. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  18. #import "FirebaseDatabase/Sources/Core/FRepoManager.h"
  19. #import "FirebaseDatabase/Sources/Utilities/FUtilities.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
  37. queue:(dispatch_queue_t)queue {
  38. if (theAllowedEvents == nil || [theAllowedEvents count] == 0) {
  39. @throw [NSException
  40. exceptionWithName:@"AllowedEventsValidation"
  41. reason:@"FEventEmitters must be initialized with at "
  42. @"least one valid event."
  43. userInfo:nil];
  44. }
  45. self = [super init];
  46. if (self) {
  47. self.allowedEvents = [theAllowedEvents copy];
  48. self.listeners = [[NSMutableDictionary alloc] init];
  49. self.queue = queue;
  50. }
  51. return self;
  52. }
  53. - (id)getInitialEventForType:(NSString *)eventType {
  54. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  55. reason:@"You must override getInitialEvent: "
  56. @"when subclassing FEventEmitter"
  57. userInfo:nil];
  58. }
  59. - (void)triggerEventType:(NSString *)eventType data:(id)data {
  60. [self validateEventType:eventType];
  61. NSMutableDictionary *eventTypeListeners =
  62. [self.listeners objectForKey:eventType];
  63. for (FEventListener *listener in eventTypeListeners) {
  64. [self triggerListener:listener withData:data];
  65. }
  66. }
  67. - (void)triggerListener:(FEventListener *)listener withData:(id)data {
  68. // TODO, should probably get this from FRepo or something although it ends
  69. // up being the same. (Except maybe for testing)
  70. if (listener.userCallback) {
  71. dispatch_async(self.queue, ^{
  72. listener.userCallback(data);
  73. });
  74. }
  75. }
  76. - (FIRDatabaseHandle)observeEventType:(NSString *)eventType
  77. withBlock:(fbt_void_id)block {
  78. [self validateEventType:eventType];
  79. // Create listener
  80. FEventListener *listener = [[FEventListener alloc] init];
  81. listener.handle = [[FUtilities LUIDGenerator] integerValue];
  82. listener.userCallback = block; // copies block automatically
  83. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  84. [self addEventListener:listener forEventType:eventType];
  85. });
  86. return listener.handle;
  87. }
  88. - (void)addEventListener:(FEventListener *)listener
  89. forEventType:(NSString *)eventType {
  90. // Get or initializer listeners map [FIRDatabaseHandle -> callback block]
  91. // for eventType
  92. NSMutableArray *eventTypeListeners =
  93. [self.listeners objectForKey:eventType];
  94. if (eventTypeListeners == nil) {
  95. eventTypeListeners = [[NSMutableArray alloc] init];
  96. [self.listeners setObject:eventTypeListeners forKey:eventType];
  97. }
  98. // Add listener and fire the current event for this listener
  99. [eventTypeListeners addObject:listener];
  100. id initialData = [self getInitialEventForType:eventType];
  101. [self triggerListener:listener withData:initialData];
  102. }
  103. - (void)removeObserverForEventType:(NSString *)eventType
  104. withHandle:(FIRDatabaseHandle)handle {
  105. [self validateEventType:eventType];
  106. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  107. [self removeEventListenerWithHandle:handle forEventType:eventType];
  108. });
  109. }
  110. - (void)removeEventListenerWithHandle:(FIRDatabaseHandle)handle
  111. forEventType:(NSString *)eventType {
  112. NSMutableArray *eventTypeListeners =
  113. [self.listeners objectForKey:eventType];
  114. for (FEventListener *listener in [eventTypeListeners copy]) {
  115. if (handle == NSNotFound || handle == listener.handle) {
  116. [eventTypeListeners removeObject:listener];
  117. }
  118. }
  119. }
  120. - (void)validateEventType:(NSString *)eventType {
  121. if ([self.allowedEvents indexOfObject:eventType] == NSNotFound) {
  122. @throw [NSException
  123. exceptionWithName:@"InvalidEventType"
  124. reason:[NSString stringWithFormat:
  125. @"%@ is not a valid event type. %@ "
  126. @"is the list of valid events.",
  127. eventType, self.allowedEvents]
  128. userInfo:nil];
  129. }
  130. }
  131. @end