FValueEventRegistration.m 3.2 KB

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