FValueEventRegistration.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "FValueEventRegistration.h"
  18. #import "FIRDatabaseQuery_Private.h"
  19. #import "FQueryParams.h"
  20. #import "FQuerySpec.h"
  21. #import "FIRDataSnapshot_Private.h"
  22. #import "FCancelEvent.h"
  23. #import "FDataEvent.h"
  24. @interface FValueEventRegistration ()
  25. @property (nonatomic, strong) FRepo* repo;
  26. @property (nonatomic, copy, readwrite) fbt_void_datasnapshot callback;
  27. @property (nonatomic, copy, readwrite) fbt_void_nserror cancelCallback;
  28. @property (nonatomic, readwrite) FIRDatabaseHandle handle;
  29. @end
  30. @implementation FValueEventRegistration
  31. - (id) initWithRepo:(FRepo *)repo
  32. handle:(FIRDatabaseHandle)fHandle
  33. callback:(fbt_void_datasnapshot)callbackBlock
  34. cancelCallback:(fbt_void_nserror)cancelCallbackBlock {
  35. self = [super init];
  36. if (self) {
  37. self.repo = repo;
  38. self.handle = fHandle;
  39. self.callback = callbackBlock;
  40. self.cancelCallback = cancelCallbackBlock;
  41. }
  42. return self;
  43. }
  44. - (BOOL) responseTo:(FIRDataEventType)eventType {
  45. return eventType == FIRDataEventTypeValue;
  46. }
  47. - (FDataEvent *) createEventFrom:(FChange *)change query:(FQuerySpec *)query {
  48. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithRepo:self.repo path:query.path];
  49. FIRDataSnapshot *snapshot = [[FIRDataSnapshot alloc] initWithRef:ref indexedNode:change.indexedNode];
  50. FDataEvent *eventData = [[FDataEvent alloc] initWithEventType:FIRDataEventTypeValue eventRegistration:self
  51. dataSnapshot:snapshot];
  52. return eventData;
  53. }
  54. - (void) fireEvent:(id <FEvent>)event queue:(dispatch_queue_t)queue {
  55. if ([event isCancelEvent]) {
  56. FCancelEvent *cancelEvent = event;
  57. FFLog(@"I-RDB065001", @"Raising cancel value event on %@", event.path);
  58. NSAssert(self.cancelCallback != nil, @"Raising a cancel event on a listener with no cancel callback");
  59. dispatch_async(queue, ^{
  60. self.cancelCallback(cancelEvent.error);
  61. });
  62. } else if (self.callback != nil) {
  63. FDataEvent *dataEvent = event;
  64. FFLog(@"I-RDB065002", @"Raising value event on %@", dataEvent.snapshot.key);
  65. dispatch_async(queue, ^{
  66. self.callback(dataEvent.snapshot);
  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