FChildEventRegistration.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/Core/View/FChildEventRegistration.h"
  17. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  18. #import "FirebaseDatabase/Sources/Api/Private/FIRDataSnapshot_Private.h"
  19. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  20. #import "FirebaseDatabase/Sources/Core/FQueryParams.h"
  21. #import "FirebaseDatabase/Sources/Core/FQuerySpec.h"
  22. #import "FirebaseDatabase/Sources/Core/View/FCancelEvent.h"
  23. #import "FirebaseDatabase/Sources/Core/View/FDataEvent.h"
  24. @interface FChildEventRegistration ()
  25. @property(nonatomic, strong) FRepo *repo;
  26. @property(nonatomic, copy, readwrite) NSDictionary *callbacks;
  27. @property(nonatomic, copy, readwrite) fbt_void_nserror cancelCallback;
  28. @property(nonatomic, readwrite) FIRDatabaseHandle handle;
  29. @end
  30. @implementation FChildEventRegistration
  31. - (id)initWithRepo:(id)repo
  32. handle:(FIRDatabaseHandle)fHandle
  33. callbacks:(NSDictionary *)callbackBlocks
  34. cancelCallback:(fbt_void_nserror)cancelCallbackBlock {
  35. self = [super init];
  36. if (self) {
  37. self.repo = repo;
  38. self.handle = fHandle;
  39. self.callbacks = callbackBlocks;
  40. self.cancelCallback = cancelCallbackBlock;
  41. }
  42. return self;
  43. }
  44. - (BOOL)responseTo:(FIRDataEventType)eventType {
  45. return self.callbacks != nil &&
  46. [self.callbacks
  47. objectForKey:[NSNumber numberWithInteger:eventType]] != nil;
  48. }
  49. - (FDataEvent *)createEventFrom:(FChange *)change query:(FQuerySpec *)query {
  50. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc]
  51. initWithRepo:self.repo
  52. path:[query.path childFromString:change.childKey]];
  53. FIRDataSnapshot *snapshot =
  54. [[FIRDataSnapshot alloc] initWithRef:ref
  55. indexedNode:change.indexedNode];
  56. FDataEvent *eventData =
  57. [[FDataEvent alloc] initWithEventType:change.type
  58. eventRegistration:self
  59. dataSnapshot:snapshot
  60. prevName:change.prevKey];
  61. return eventData;
  62. }
  63. - (void)fireEvent:(id<FEvent>)event queue:(dispatch_queue_t)queue {
  64. if ([event isCancelEvent]) {
  65. FCancelEvent *cancelEvent = event;
  66. FFLog(@"I-RDB061001", @"Raising cancel value event on %@", event.path);
  67. NSAssert(
  68. self.cancelCallback != nil,
  69. @"Raising a cancel event on a listener with no cancel callback");
  70. dispatch_async(queue, ^{
  71. self.cancelCallback(cancelEvent.error);
  72. });
  73. } else if (self.callbacks != nil) {
  74. FDataEvent *dataEvent = event;
  75. FFLog(@"I-RDB061002", @"Raising event callback (%ld) on %@",
  76. (long)dataEvent.eventType, dataEvent.path);
  77. fbt_void_datasnapshot_nsstring callback = [self.callbacks
  78. objectForKey:[NSNumber numberWithInteger:dataEvent.eventType]];
  79. if (callback != nil) {
  80. dispatch_async(queue, ^{
  81. callback(dataEvent.snapshot, dataEvent.prevName);
  82. });
  83. }
  84. }
  85. }
  86. - (FCancelEvent *)createCancelEventFromError:(NSError *)error
  87. path:(FPath *)path {
  88. if (self.cancelCallback != nil) {
  89. return [[FCancelEvent alloc] initWithEventRegistration:self
  90. error:error
  91. path:path];
  92. } else {
  93. return nil;
  94. }
  95. }
  96. - (BOOL)matches:(id<FEventRegistration>)other {
  97. return self.handle == NSNotFound || other.handle == NSNotFound ||
  98. self.handle == other.handle;
  99. }
  100. @end