FChildEventRegistration.m 3.5 KB

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