FChildEventRegistration.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <FirebaseCore/FIRLogger.h>
  17. #import "FChildEventRegistration.h"
  18. #import "FIRDatabaseQuery_Private.h"
  19. #import "FQueryParams.h"
  20. #import "FQuerySpec.h"
  21. #import "FIRDataSnapshot_Private.h"
  22. #import "FDataEvent.h"
  23. #import "FCancelEvent.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 handle:(FIRDatabaseHandle)fHandle callbacks:(NSDictionary *)callbackBlocks cancelCallback:(fbt_void_nserror)cancelCallbackBlock {
  32. self = [super init];
  33. if (self) {
  34. self.repo = repo;
  35. self.handle = fHandle;
  36. self.callbacks = callbackBlocks;
  37. self.cancelCallback = cancelCallbackBlock;
  38. }
  39. return self;
  40. }
  41. - (BOOL) responseTo:(FIRDataEventType)eventType {
  42. return self.callbacks != nil && [self.callbacks objectForKey:[NSNumber numberWithInteger:eventType]] != nil;
  43. }
  44. - (FDataEvent *) createEventFrom:(FChange *)change query:(FQuerySpec *)query {
  45. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithRepo:self.repo path:[query.path childFromString:change.childKey]];
  46. FIRDataSnapshot *snapshot = [[FIRDataSnapshot alloc] initWithRef:ref indexedNode:change.indexedNode];
  47. FDataEvent *eventData = [[FDataEvent alloc] initWithEventType:change.type eventRegistration:self
  48. dataSnapshot:snapshot prevName:change.prevKey];
  49. return eventData;
  50. }
  51. - (void) fireEvent:(id <FEvent>)event queue:(dispatch_queue_t)queue {
  52. if ([event isCancelEvent]) {
  53. FCancelEvent *cancelEvent = event;
  54. FFLog(@"I-RDB061001", @"Raising cancel value event on %@", event.path);
  55. NSAssert(self.cancelCallback != nil, @"Raising a cancel event on a listener with no cancel callback");
  56. dispatch_async(queue, ^{
  57. self.cancelCallback(cancelEvent.error);
  58. });
  59. } else if (self.callbacks != nil) {
  60. FDataEvent *dataEvent = event;
  61. FFLog(@"I-RDB061002", @"Raising event callback (%ld) on %@", (long)dataEvent.eventType, dataEvent.path);
  62. fbt_void_datasnapshot_nsstring callback = [self.callbacks objectForKey:[NSNumber numberWithInteger:dataEvent.eventType]];
  63. if (callback != nil) {
  64. dispatch_async(queue, ^{
  65. callback(dataEvent.snapshot, dataEvent.prevName);
  66. });
  67. }
  68. }
  69. }
  70. - (FCancelEvent *) createCancelEventFromError:(NSError *)error path:(FPath *)path {
  71. if (self.cancelCallback != nil) {
  72. return [[FCancelEvent alloc] initWithEventRegistration:self error:error path:path];
  73. } else {
  74. return nil;
  75. }
  76. }
  77. - (BOOL) matches:(id<FEventRegistration>)other {
  78. return self.handle == NSNotFound || other.handle == NSNotFound || self.handle == other.handle;
  79. }
  80. @end