FValueEventRegistration.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "FValueEventRegistration.h"
  17. #import "FCancelEvent.h"
  18. #import "FDataEvent.h"
  19. #import "FIRDataSnapshot_Private.h"
  20. #import "FIRDatabaseQuery_Private.h"
  21. #import "FQueryParams.h"
  22. #import "FQuerySpec.h"
  23. #import <FirebaseCore/FIRLogger.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 =
  49. [[FIRDatabaseReference alloc] initWithRepo:self.repo path:query.path];
  50. FIRDataSnapshot *snapshot =
  51. [[FIRDataSnapshot alloc] initWithRef:ref
  52. indexedNode:change.indexedNode];
  53. FDataEvent *eventData =
  54. [[FDataEvent alloc] initWithEventType:FIRDataEventTypeValue
  55. eventRegistration:self
  56. dataSnapshot:snapshot];
  57. return eventData;
  58. }
  59. - (void)fireEvent:(id<FEvent>)event queue:(dispatch_queue_t)queue {
  60. if ([event isCancelEvent]) {
  61. FCancelEvent *cancelEvent = event;
  62. FFLog(@"I-RDB065001", @"Raising cancel value event on %@", event.path);
  63. NSAssert(
  64. self.cancelCallback != nil,
  65. @"Raising a cancel event on a listener with no cancel callback");
  66. dispatch_async(queue, ^{
  67. self.cancelCallback(cancelEvent.error);
  68. });
  69. } else if (self.callback != nil) {
  70. FDataEvent *dataEvent = event;
  71. FFLog(@"I-RDB065002", @"Raising value event on %@",
  72. dataEvent.snapshot.key);
  73. dispatch_async(queue, ^{
  74. self.callback(dataEvent.snapshot);
  75. });
  76. }
  77. }
  78. - (FCancelEvent *)createCancelEventFromError:(NSError *)error
  79. path:(FPath *)path {
  80. if (self.cancelCallback != nil) {
  81. return [[FCancelEvent alloc] initWithEventRegistration:self
  82. error:error
  83. path:path];
  84. } else {
  85. return nil;
  86. }
  87. }
  88. - (BOOL)matches:(id<FEventRegistration>)other {
  89. return self.handle == NSNotFound || other.handle == NSNotFound ||
  90. self.handle == other.handle;
  91. }
  92. @end