FDataEvent.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "FDataEvent.h"
  17. #import "FEventRegistration.h"
  18. #import "FIndex.h"
  19. #import "FIRDatabaseQuery_Private.h"
  20. @interface FDataEvent ()
  21. @property (nonatomic, strong, readwrite) id<FEventRegistration> eventRegistration;
  22. @property (nonatomic, strong, readwrite) FIRDataSnapshot *snapshot;
  23. @property (nonatomic, strong, readwrite) NSString *prevName;
  24. @property (nonatomic, readwrite) FIRDataEventType eventType;
  25. @end
  26. @implementation FDataEvent
  27. @synthesize eventRegistration;
  28. @synthesize snapshot;
  29. @synthesize prevName;
  30. @synthesize eventType;
  31. - (id)initWithEventType:(FIRDataEventType)type eventRegistration:(id <FEventRegistration>)registration dataSnapshot:(FIRDataSnapshot *)dataSnapshot {
  32. return [self initWithEventType:type eventRegistration:registration dataSnapshot:dataSnapshot prevName:nil];
  33. }
  34. - (id)initWithEventType:(FIRDataEventType)type eventRegistration:(id <FEventRegistration>)registration dataSnapshot:(FIRDataSnapshot *)dataSnapshot prevName:(NSString *)previousName {
  35. self = [super init];
  36. if (self) {
  37. self.eventRegistration = registration;
  38. self.snapshot = dataSnapshot;
  39. self.prevName = previousName;
  40. self.eventType = type;
  41. }
  42. return self;
  43. }
  44. - (FPath *) path {
  45. // Used for logging, so delay calculation
  46. FIRDatabaseReference *ref = self.snapshot.ref;
  47. if (self.eventType == FIRDataEventTypeValue) {
  48. return ref.path;
  49. } else {
  50. return ref.parent.path;
  51. }
  52. }
  53. - (void) fireEventOnQueue:(dispatch_queue_t)queue {
  54. [self.eventRegistration fireEvent:self queue:queue];
  55. }
  56. - (BOOL) isCancelEvent {
  57. return NO;
  58. }
  59. - (NSString *) description {
  60. return [NSString stringWithFormat:@"event %d, data: %@", (int) eventType, [snapshot value]];
  61. }
  62. @end